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
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. 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
|
|
|
|
2022-08-22 05:24:33 +02:00
|
|
|
/////
|
|
|
|
$inv_collection = Invoice::withTrashed()->whereIn('id', array_column($this->input['invoices'], 'invoice_id'))->get();
|
|
|
|
|
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-08-22 05:24:33 +02:00
|
|
|
/////
|
|
|
|
$inv = $inv_collection->firstWhere('id', $invoice['invoice_id']);
|
|
|
|
|
|
|
|
// $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');
|
2024-07-22 05:05:40 +02:00
|
|
|
return false;
|
|
|
|
} elseif($inv->is_deleted){
|
|
|
|
$this->error_msg = 'One or more invoices in this request have since been deleted';
|
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;
|
|
|
|
}
|
|
|
|
}
|