2016-05-02 10:38:01 +02:00
|
|
|
<?php namespace App\Http\Requests;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
if ( ! $this->invoice_id || ! $this->amount) {
|
|
|
|
return [
|
2016-09-27 16:13:42 +02:00
|
|
|
'invoice_id' => 'required|numeric|min:1',
|
|
|
|
'amount' => 'required|numeric|min:0.01',
|
2016-05-02 10:38:01 +02:00
|
|
|
];
|
|
|
|
}
|
2016-07-18 20:12:18 +02:00
|
|
|
|
|
|
|
$invoice = Invoice::scope($this->invoice_id)
|
|
|
|
->invoices()
|
2016-12-05 09:11:33 +01:00
|
|
|
->whereIsPublic(true)
|
2016-07-18 20:12:18 +02:00
|
|
|
->firstOrFail();
|
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 = [
|
2016-08-28 16:19:33 +02:00
|
|
|
'amount' => "required|numeric|between:0.01,{$invoice->balance}",
|
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;
|
|
|
|
}
|
|
|
|
}
|