1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-19 17:42:44 +01:00
invoiceninja/app/Http/ValidationRules/ValidRefundableInvoices.php
David Bomba ba75a44eb8
Laravel 7.x Shift (#40)
* Adopt Laravel coding style

The Laravel framework adopts the PSR-2 coding style with some additions.
Laravel apps *should* adopt this coding style as well.

However, Shift allows you to customize the adopted coding style by
adding your own [PHP CS Fixer][1] `.php_cs` config to your project.

You may use [Shift's .php_cs][2] file as a base.

[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
[2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200

* Shift bindings

PHP 5.5.9+ adds the new static `class` property which provides the fully qualified class name. This is preferred over using class name strings as these references are checked by the parser.

* Shift core files

* Shift to Throwable

* Add laravel/ui dependency

* Unindent vendor mail templates

* Shift config files

* Default config files

In an effort to make upgrading the constantly changing config files
easier, Shift defaulted them so you can review the commit diff for
changes. Moving forward, you should use ENV variables or create a
separate config file to allow the core config files to remain
automatically upgradeable.

* Shift Laravel dependencies

* Shift cleanup

* Upgrade to Laravel 7

Co-authored-by: Laravel Shift <shift@laravelshift.com>
2020-09-06 19:38:10 +10:00

104 lines
2.9 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.
*/
class ValidRefundableInvoices implements Rule
{
use MakesHash;
/**
* @param string $attribute
* @param mixed $value
* @return bool
*/
private $error_msg;
private $input;
public function __construct($input)
{
$this->input = $input;
}
public function passes($attribute, $value)
{
if (! array_key_exists('id', $this->input)) {
$this->error_msg = 'Payment `id` required.';
return false;
}
$payment = Payment::whereId($this->input['id'])->first();
if (! $payment) {
$this->error_msg = "Payment couldn't be retrieved cannot be refunded ";
return false;
}
/*We are not sending the Refunded amount in the 'amount field, this is the Payment->amount, need to skip this check. */
// if (request()->has('amount') && (request()->input('amount') > ($payment->amount - $payment->refunded))) {
// $this->error_msg = "Attempting to refund 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($this->input['invoices'], 'invoice_id'))->company()->get();
} else {
return true;
}
foreach ($invoices as $invoice) {
if (! $invoice->isRefundable()) {
$this->error_msg = 'Invoice id '.$invoice->hashed_id.' cannot be refunded';
return false;
}
foreach ($this->input['invoices'] as $val) {
if ($val['invoice_id'] == $invoice->id) {
//$pivot_record = $invoice->payments->where('id', $invoice->id)->first();
$pivot_record = $payment->paymentables->where('paymentable_id', $invoice->id)->first();
if ($val['amount'] > ($pivot_record->amount - $pivot_record->refunded)) {
$this->error_msg = 'Attempting to refund '.$val['amount'].' only '.($pivot_record->amount - $pivot_record->refunded).' available for refund';
return false;
}
}
}
}
return true;
}
/**
* @return string
*/
public function message()
{
return $this->error_msg;
}
}