invoiceninja/app/Http/Requests/CreatePaymentAPIRequest.php
2019-01-30 22:00:26 +11:00

59 lines
1.3 KiB
PHP
Executable File

<?php
namespace App\Http\Requests;
use App\Models\Invoice;
use App\Models\Payment;
class CreatePaymentAPIRequest extends PaymentRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return $this->user()->can('create', Payment::class);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
if (! $this->invoice_id || ! $this->amount) {
return [
'invoice_id' => 'required|numeric|min:1',
'amount' => 'required|numeric',
];
}
$this->invoice = $invoice = Invoice::scope($this->invoice_public_id ?: $this->invoice_id)
->withArchived()
->invoices()
->first();
if (! $this->invoice) {
abort(404, 'Invoice was not found');
}
$this->merge([
'invoice_id' => $invoice->id,
'client_id' => $invoice->client->id,
]);
$rules = [
'amount' => 'required|numeric',
];
if ($this->payment_type_id == PAYMENT_TYPE_CREDIT) {
$rules['payment_type_id'] = 'has_credit:' . $invoice->client->public_id . ',' . $this->amount;
}
return $rules;
}
}