2020-01-09 21:15:10 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-01-09 21:15:10 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-01-09 21:15:10 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-01-09 21:15:10 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\ValidationRules;
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class PaymentAmountsBalanceRule.
|
2020-01-09 21:15:10 +01:00
|
|
|
*/
|
|
|
|
class PaymentAmountsBalanceRule implements Rule
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function passes($attribute, $value)
|
2020-03-21 06:37:30 +01:00
|
|
|
{
|
|
|
|
return $this->calculateAmounts();
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function message()
|
|
|
|
{
|
2021-01-24 12:48:09 +01:00
|
|
|
return ctrans('texts.amounts_do_not_balance');
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
private function calculateAmounts(): bool
|
2020-01-09 21:15:10 +01:00
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
/*
|
2020-01-30 21:53:14 +01:00
|
|
|
* Sometimes the request may not contain the amount or it may be zero,
|
|
|
|
* and this is a valid use case, only compare the amounts if they
|
|
|
|
* have been presented!
|
|
|
|
*/
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! request()->has('amount')) {
|
2020-01-30 21:53:14 +01:00
|
|
|
return true;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-30 21:53:14 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (request()->has('amount') && request()->input('amount') == 0) {
|
2020-01-30 22:15:13 +01:00
|
|
|
return true;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-11 23:01:28 +01:00
|
|
|
|
2020-01-09 21:15:10 +01:00
|
|
|
$payment_amounts = 0;
|
|
|
|
$invoice_amounts = 0;
|
|
|
|
|
|
|
|
$payment_amounts += request()->input('amount');
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (request()->input('credits') && is_array(request()->input('credits'))) {
|
|
|
|
foreach (request()->input('credits') as $credit) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists('amount', $credit)) {
|
2022-01-12 04:40:05 +01:00
|
|
|
$payment_amounts += $credit['amount'];
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (request()->input('invoices') && is_array(request()->input('invoices'))) {
|
|
|
|
foreach (request()->input('invoices') as $invoice) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists('amount', $invoice)) {
|
2022-01-12 04:40:05 +01:00
|
|
|
$invoice_amounts += $invoice['amount'];
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
} else {
|
|
|
|
return true;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-10-21 06:12:54 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return round($payment_amounts, 2) >= round($invoice_amounts, 2);
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
|
|
|
}
|