2020-02-28 02:58:49 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-02-28 02:58:49 +01:00
|
|
|
*
|
|
|
|
* @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-02-28 02:58:49 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\ValidationRules\Payment;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class ValidInvoicesRules.
|
2020-02-28 02:58:49 +01:00
|
|
|
*/
|
|
|
|
class ValidInvoicesRules 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)
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
return $this->checkInvoicesAreHomogenous();
|
2020-02-28 02:58:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function checkInvoicesAreHomogenous()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! array_key_exists('client_id', $this->input)) {
|
2021-01-24 12:48:09 +01:00
|
|
|
|
|
|
|
$this->error_msg = ctrans('texts.client_id_required');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-28 02:58:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-28 09:18:34 +01:00
|
|
|
$unique_array = [];
|
2020-09-19 07:02:34 +02:00
|
|
|
|
|
|
|
//todo optimize this into a single query
|
2020-03-21 06:37:30 +01:00
|
|
|
foreach ($this->input['invoices'] as $invoice) {
|
2021-01-21 00:53:02 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$unique_array[] = $invoice['invoice_id'];
|
2020-02-28 09:18:34 +01:00
|
|
|
|
|
|
|
$inv = Invoice::whereId($invoice['invoice_id'])->first();
|
2020-02-28 02:58:49 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $inv) {
|
2021-01-21 00:53:02 +01:00
|
|
|
|
2021-01-24 12:48:09 +01:00
|
|
|
$this->error_msg = ctrans('texts.invoice_not_found');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-28 02:58:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($inv->client_id != $this->input['client_id']) {
|
2021-01-21 00:53:02 +01:00
|
|
|
|
2021-01-24 12:48:09 +01:00
|
|
|
$this->error_msg = ctrans('texts.invoices_dont_match_client');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return false;
|
2020-02-28 02:58:49 +01:00
|
|
|
}
|
2021-01-21 00:53:02 +01:00
|
|
|
|
2021-01-21 00:58:54 +01:00
|
|
|
if($inv->status_id == Invoice::STATUS_DRAFT && $invoice['amount'] <= $inv->amount){
|
2021-01-21 00:53:02 +01:00
|
|
|
//catch here nothing to do - we need this to prevent the last elseif triggering
|
|
|
|
}
|
|
|
|
else if($inv->status_id == Invoice::STATUS_DRAFT && $invoice['amount'] > $inv->amount){
|
|
|
|
|
|
|
|
$this->error_msg = 'Amount cannot be greater than invoice balance';
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if($invoice['amount'] > $inv->balance) {
|
|
|
|
|
2021-01-24 12:48:09 +01:00
|
|
|
$this->error_msg = ctrans('texts.amount_greater_than_balance');
|
2021-01-21 00:53:02 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-28 02:58:49 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! (array_unique($unique_array) == $unique_array)) {
|
2021-01-24 12:48:09 +01:00
|
|
|
$this->error_msg = ctrans('texts.duplicate_invoices_submitted');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-28 09:18:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-28 02:58:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function message()
|
|
|
|
{
|
|
|
|
return $this->error_msg;
|
|
|
|
}
|
|
|
|
}
|