2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
2016-05-02 10:38:01 +02:00
|
|
|
|
|
|
|
use App\Models\Invoice;
|
2016-12-13 07:52:09 +01:00
|
|
|
|
2016-05-02 10:38:01 +02:00
|
|
|
class CreatePaymentAPIRequest extends PaymentRequest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
return $this->user()->can('create', ENTITY_PAYMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $this->invoice_id || ! $this->amount) {
|
2016-05-02 10:38:01 +02:00
|
|
|
return [
|
2016-09-27 16:13:42 +02:00
|
|
|
'invoice_id' => 'required|numeric|min:1',
|
2017-08-09 09:57:24 +02:00
|
|
|
'amount' => 'required|numeric',
|
2016-05-02 10:38:01 +02:00
|
|
|
];
|
|
|
|
}
|
2016-07-18 20:12:18 +02:00
|
|
|
|
2017-10-20 14:04:52 +02:00
|
|
|
$this->invoice = $invoice = Invoice::scope($this->invoice_public_id ?: $this->invoice_id)
|
2017-08-07 08:22:01 +02:00
|
|
|
->withArchived()
|
2016-07-18 20:12:18 +02:00
|
|
|
->invoices()
|
2017-08-07 13:09:49 +02:00
|
|
|
->first();
|
|
|
|
|
|
|
|
if (! $this->invoice) {
|
|
|
|
abort(404, 'Invoice was not found');
|
|
|
|
}
|
2016-05-02 10:38:01 +02:00
|
|
|
|
|
|
|
$this->merge([
|
2016-07-18 20:12:18 +02:00
|
|
|
'invoice_id' => $invoice->id,
|
2016-05-02 10:38:01 +02:00
|
|
|
'client_id' => $invoice->client->id,
|
|
|
|
]);
|
2016-07-18 20:12:18 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
$rules = [
|
2018-06-17 08:54:05 +02:00
|
|
|
'amount' => 'required|numeric',
|
2016-07-03 18:11:58 +02:00
|
|
|
];
|
2016-05-02 10:38:01 +02:00
|
|
|
|
|
|
|
if ($this->payment_type_id == PAYMENT_TYPE_CREDIT) {
|
|
|
|
$rules['payment_type_id'] = 'has_credit:' . $invoice->client->public_id . ',' . $this->amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
}
|