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
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-02-28 02:58:49 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-02-28 02:58:49 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
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 = [];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
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) {
|
|
|
|
$unique_array[] = $invoice['invoice_id'];
|
2020-02-28 09:18:34 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! array_key_exists('amount', $invoice)) {
|
|
|
|
$this->error_msg = ctrans('texts.amount').' required';
|
|
|
|
|
2022-01-12 04:40:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-27 01:51:54 +02:00
|
|
|
$inv = Invoice::withTrashed()->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-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-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
|
|
|
|
2022-06-21 11:57:17 +02: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
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif ($inv->status_id == Invoice::STATUS_DRAFT && floatval($invoice['amount']) > floatval($inv->amount)) {
|
2021-01-21 00:53:02 +01:00
|
|
|
$this->error_msg = 'Amount cannot be greater than invoice balance';
|
|
|
|
|
|
|
|
return false;
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif (floatval($invoice['amount']) > floatval($inv->balance)) {
|
2021-04-13 23:40:07 +02:00
|
|
|
$this->error_msg = ctrans('texts.amount_greater_than_balance_v5');
|
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;
|
|
|
|
}
|
|
|
|
}
|