2020-11-02 02:11:49 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-11-02 02:11:49 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|