2020-01-29 03:03:47 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-01-29 03:03:47 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-01-29 03:03:47 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-01-29 03:03:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\ValidationRules\Payment;
|
|
|
|
|
|
|
|
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 ValidRefundableRequest.
|
2020-01-29 03:03:47 +01:00
|
|
|
*/
|
|
|
|
class ValidRefundableRequest 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)
|
|
|
|
{
|
|
|
|
$this->input = $input;
|
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
|
|
|
|
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-06 09:35:19 +02:00
|
|
|
$payment = Payment::query()->where('id', $this->input['id'])->withTrashed()->first();
|
2020-01-29 03:03:47 +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-01-29 03:03:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:54:20 +01:00
|
|
|
$request_invoices = request()->has('invoices') ? $this->input['invoices'] : [];
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($payment->invoices()->exists()) {
|
2023-09-01 04:56:06 +02:00
|
|
|
$this->checkInvoice($payment->invoices, $request_invoices);
|
|
|
|
// foreach ($payment->invoices as $paymentable_invoice) {
|
|
|
|
// $this->checkInvoice($paymentable_invoice, $request_invoices);
|
|
|
|
// }
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
foreach ($request_invoices as $request_invoice) {
|
2020-01-29 03:03:47 +01:00
|
|
|
$this->checkInvoiceIsPaymentable($request_invoice, $payment);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (strlen($this->error_msg) > 0) {
|
2020-01-29 03:45:20 +01:00
|
|
|
return false;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-29 03:45:20 +01:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkInvoiceIsPaymentable($invoice, $payment)
|
|
|
|
{
|
2023-08-01 15:28:30 +02:00
|
|
|
/**@var \App\Models\Invoice $invoice **/
|
2023-08-06 09:35:19 +02:00
|
|
|
$invoice = Invoice::query()->where('id', $invoice['invoice_id'])->where('company_id', $payment->company_id)->withTrashed()->first();
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $invoice) {
|
|
|
|
$this->error_msg = 'Invoice not found for refund';
|
|
|
|
|
2022-01-12 04:40:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($payment->invoices()->exists()) {
|
2020-01-29 03:03:47 +01:00
|
|
|
$paymentable_invoice = $payment->invoices->where('id', $invoice->id)->first();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $paymentable_invoice) {
|
2021-01-24 12:48:09 +01:00
|
|
|
$this->error_msg = ctrans('texts.invoice_not_related_to_payment', ['invoice' => $invoice->hashed_id]);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
} else {
|
2021-01-24 12:48:09 +01:00
|
|
|
$this->error_msg = ctrans('texts.invoice_not_related_to_payment', ['invoice' => $invoice->hashed_id]);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 04:56:06 +02:00
|
|
|
private function checkInvoice($paymentables, $request_invoices)
|
2020-01-29 03:03:47 +01:00
|
|
|
{
|
|
|
|
$record_found = false;
|
|
|
|
|
2023-09-01 04:56:06 +02:00
|
|
|
foreach($paymentables as $paymentable) {
|
|
|
|
foreach ($request_invoices as $request_invoice) {
|
|
|
|
|
|
|
|
if ($request_invoice['invoice_id'] == $paymentable->pivot->paymentable_id) {
|
|
|
|
$record_found = true;
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2023-09-01 04:56:06 +02:00
|
|
|
$refundable_amount = ($paymentable->pivot->amount - $paymentable->pivot->refunded);
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2023-09-01 04:56:06 +02:00
|
|
|
if ($request_invoice['amount'] > $refundable_amount) {
|
|
|
|
$invoice = $paymentable;
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2023-09-01 04:56:06 +02:00
|
|
|
$this->error_msg = ctrans('texts.max_refundable_invoice', ['invoice' => $invoice->hashed_id, 'amount' => $refundable_amount]);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2023-09-01 04:56:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $record_found) {
|
2021-01-24 12:48:09 +01:00
|
|
|
$this->error_msg = ctrans('texts.refund_without_invoices');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function message()
|
|
|
|
{
|
|
|
|
return $this->error_msg;
|
|
|
|
}
|
|
|
|
}
|