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\Requests\Payment;
|
|
|
|
|
|
|
|
use App\Http\Requests\Request;
|
2020-01-29 03:03:47 +01:00
|
|
|
use App\Http\ValidationRules\Payment\ValidRefundableRequest;
|
2020-01-20 11:10:33 +01:00
|
|
|
use App\Http\ValidationRules\ValidRefundableInvoices;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
|
|
|
|
class RefundPaymentRequest extends Request
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
use MakesHash;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function authorize(): bool
|
2020-01-20 11:10:33 +01:00
|
|
|
{
|
2023-08-01 14:36:04 +02:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
return $user->isAdmin();
|
2020-01-20 11:10:33 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2022-06-24 03:55:41 +02:00
|
|
|
public function prepareForValidation()
|
2020-01-20 11:10:33 +01:00
|
|
|
{
|
|
|
|
$input = $this->all();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
|
|
|
if (! isset($input['gateway_refund'])) {
|
2020-03-21 06:37:30 +01:00
|
|
|
$input['gateway_refund'] = false;
|
|
|
|
}
|
2020-01-20 11:10:33 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! isset($input['send_email'])) {
|
2020-01-29 03:03:47 +01:00
|
|
|
$input['send_email'] = false;
|
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 (isset($input['id'])) {
|
|
|
|
$input['id'] = $this->decodePrimaryKey($input['id']);
|
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (isset($input['invoices'])) {
|
|
|
|
foreach ($input['invoices'] as $key => $invoice) {
|
2020-01-29 03:03:47 +01:00
|
|
|
$input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($invoice['invoice_id']);
|
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 (isset($input['credits'])) {
|
2020-02-01 21:45:23 +01:00
|
|
|
unset($input['credits']);
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->replace($input);
|
2020-01-20 11:10:33 +01:00
|
|
|
}
|
|
|
|
|
2023-08-01 15:28:30 +02:00
|
|
|
public function rules(): array
|
2020-01-20 11:10:33 +01:00
|
|
|
{
|
2020-02-05 22:54:20 +01:00
|
|
|
$input = $this->all();
|
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
$rules = [
|
2023-08-01 15:28:30 +02:00
|
|
|
'id' => 'bail|required', //@phpstan-ignore-line
|
2020-02-05 22:54:20 +01:00
|
|
|
'id' => new ValidRefundableRequest($input),
|
2020-01-30 05:50:45 +01:00
|
|
|
'amount' => 'numeric',
|
2020-01-20 11:10:33 +01:00
|
|
|
'date' => 'required',
|
|
|
|
'invoices.*.invoice_id' => 'required',
|
2020-01-30 05:50:45 +01:00
|
|
|
'invoices.*.amount' => 'required',
|
2020-02-05 22:54:20 +01:00
|
|
|
'invoices' => new ValidRefundableInvoices($input),
|
2020-01-20 11:10:33 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2023-08-01 15:01:48 +02:00
|
|
|
public function payment(): ?\App\Models\Payment
|
2020-01-29 03:03:47 +01:00
|
|
|
{
|
2020-02-05 22:54:20 +01:00
|
|
|
$input = $this->all();
|
|
|
|
|
|
|
|
return Payment::whereId($input['id'])->first();
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
2020-01-20 11:10:33 +01:00
|
|
|
}
|