1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Http/ValidationRules/ValidRefundableInvoices.php
David Bomba 45cc67075d
Include contacts in sample migration files. (#3260)
* add types to transformers

* minor fixes for test data creator

* Working on refunds

* Update migration files to include client contacts

* Working on refunds

* Working on refunds

* Working on refunds

* Refund Tests

* Working on refund tests
2020-01-29 13:03:47 +11:00

85 lines
2.1 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\ValidationRules;
use App\Models\Invoice;
use App\Models\Payment;
use App\Utils\Traits\MakesHash;
use Illuminate\Contracts\Validation\Rule;
/**
* Class ValidRefundableInvoices
* @package App\Http\ValidationRules
*/
class ValidRefundableInvoices implements Rule
{
use MakesHash;
/**
* @param string $attribute
* @param mixed $value
* @return bool
*/
private $error_msg;
public function passes($attribute, $value)
{
$payment = Payment::whereId($this->decodePrimaryKey(request()->input('id')))->first();
if($request->has('refunded') && ($request->input('refunded') > ($payment->amount - $payment->refunded))){
$this->error_msg = "Attempting to refunded more than payment amount, enter a value equal to or lower than the payment amount of ". $payment->amount;
return false;
}
/*If no invoices has been sent, then we apply the payment to the client account*/
$invoices = [];
if (is_array($value)) {
$invoices = Invoice::whereIn('id', array_column($value, 'invoice_id'))->company()->get();
}
else
return true;
foreach ($invoices as $invoice) {
if (! $invoice->isRefundable()) {
$this->error_msg = "One or more of these invoices have been paid";
return false;
}
foreach ($value as $val) {
if ($val['invoice_id'] == $invoice->id) {
if($val['refunded'] > ($invoice->amount - $invoice->balance))
$this->error_msg = "Attempting to refund more than is possible for an invoice";
return false;
}
}
}
return true;
}
/**
* @return string
*/
public function message()
{
return $this->error_msg;
}
}