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 [
|
|
|
|
'invoice_id' => 'required',
|
|
|
|
'amount' => 'required',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice = Invoice::scope($this->invoice_id)->firstOrFail();
|
|
|
|
|
|
|
|
$this->merge([
|
|
|
|
'invoice_id' => $invoice->id,
|
|
|
|
'client_id' => $invoice->client->id,
|
|
|
|
]);
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
$rules = [
|
2016-05-02 10:38:01 +02:00
|
|
|
'amount' => "required|less_than:{$invoice->balance}|positive",
|
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;
|
|
|
|
}
|
|
|
|
}
|