2020-11-02 02:11:49 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @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-11-02 02:11:49 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-11-02 02:11:49 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\ValidationRules\Credit;
|
|
|
|
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CreditsSumRule.
|
|
|
|
*/
|
|
|
|
class CreditsSumRule implements Rule
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
|
|
|
|
private $input;
|
|
|
|
|
|
|
|
public function __construct($input)
|
|
|
|
{
|
|
|
|
$this->input = $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function passes($attribute, $value)
|
|
|
|
{
|
|
|
|
return $this->checkCreditTotals();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkCreditTotals()
|
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
if (array_sum(array_column($this->input['credits'], 'amount')) > array_sum(array_column($this->input['invoices'], 'amount'))) {
|
2020-11-02 02:11:49 +01:00
|
|
|
return false;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-11-02 02:11:49 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function message()
|
|
|
|
{
|
2021-01-24 12:48:09 +01:00
|
|
|
return ctrans('texts.credits_applied_validation');
|
2020-11-02 02:11:49 +01:00
|
|
|
}
|
|
|
|
}
|