2020-01-20 11:10:33 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-01-20 11:10:33 +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-01-20 11:10:33 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-01-20 11:10:33 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\ValidationRules;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class ValidRefundableInvoices.
|
2020-01-20 11:10:33 +01:00
|
|
|
*/
|
|
|
|
class ValidRefundableInvoices implements Rule
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private $error_msg;
|
|
|
|
|
2020-02-05 22:54:20 +01:00
|
|
|
private $input;
|
|
|
|
|
|
|
|
public function __construct($input)
|
2020-01-20 11:10:33 +01:00
|
|
|
{
|
2020-02-05 22:54:20 +01:00
|
|
|
$this->input = $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function passes($attribute, $value)
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! array_key_exists('id', $this->input)) {
|
2021-01-24 12:48:09 +01:00
|
|
|
$this->error_msg = ctrans('texts.payment_id_required');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
return false;
|
2020-04-11 13:19:05 +02:00
|
|
|
}
|
|
|
|
|
2023-08-01 15:01:48 +02:00
|
|
|
/**@var \App\Models\Payment $payment **/
|
2020-02-05 22:54:20 +01:00
|
|
|
$payment = Payment::whereId($this->input['id'])->first();
|
2020-02-05 11:28:56 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $payment) {
|
2021-01-24 12:48:09 +01:00
|
|
|
$this->error_msg = ctrans('texts.unable_to_retrieve_payment');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return false;
|
2020-02-05 11:28:56 +01:00
|
|
|
}
|
2020-01-20 11:10:33 +01:00
|
|
|
|
|
|
|
/*If no invoices has been sent, then we apply the payment to the client account*/
|
|
|
|
$invoices = [];
|
|
|
|
|
|
|
|
if (is_array($value)) {
|
2023-08-06 09:03:12 +02:00
|
|
|
$invoices = Invoice::query()->whereIn('id', array_column($this->input['invoices'], 'invoice_id'))->company()->get();
|
2020-03-21 06:37:30 +01:00
|
|
|
} else {
|
2020-01-20 11:10:33 +01:00
|
|
|
return true;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-20 11:10:33 +01:00
|
|
|
|
|
|
|
foreach ($invoices as $invoice) {
|
|
|
|
if (! $invoice->isRefundable()) {
|
2021-01-24 12:48:09 +01:00
|
|
|
$this->error_msg = ctrans('texts.invoice_cannot_be_refunded', ['invoice' => $invoice->hashed_id]);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:54:20 +01:00
|
|
|
foreach ($this->input['invoices'] as $val) {
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($val['invoice_id'] == $invoice->id) {
|
2020-01-30 05:50:45 +01:00
|
|
|
$pivot_record = $payment->paymentables->where('paymentable_id', $invoice->id)->first();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($val['amount'] > ($pivot_record->amount - $pivot_record->refunded)) {
|
2021-01-24 12:48:09 +01:00
|
|
|
$this->error_msg = ctrans('texts.attempted_refund_failed', ['amount' => $val['amount'], 'refundable_amount' => ($pivot_record->amount - $pivot_record->refunded)]);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-29 05:25:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-20 11:10:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function message()
|
|
|
|
{
|
|
|
|
return $this->error_msg;
|
|
|
|
}
|
|
|
|
}
|