Add validation to service cancellation, and displaying cancellation costs if any

This commit is contained in:
2024-08-17 10:33:56 +10:00
parent 7a41dd803f
commit 6ac1b11864
9 changed files with 142 additions and 31 deletions

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
/**
* Editing Suppliers
*/
class ServiceCancel extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return Gate::allows('view',$this->route('o'));
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
//dd(request()->post());
return [
'stop_at'=> [
'required',
'date',
'after:today',
'exclude_unless:extra_charges,null',
function($attribute,$value,$fail) {
if ($this->route('o')->cancel_date->greaterThan($value))
$fail(sprintf('Service cannot be cancelled before: %s',$this->route('o')->cancel_date->format('Y-m-d')));
}
],
'extra_charges_amount' => [
'nullable',
'exclude_unless:extra_charges,null',
function($attribute,$value,$fail) {
if ($this->route('o')->cancel_date->greaterThan(request('stop_at')) && (request('extra_charges') !== 1))
$fail('Extra charges must be accepted if cancelling before contract end');
},
],
'extra_charges' => 'sometimes|required|accepted',
'notes' => 'nullable|min:5',
];
}
}