CasperSecurity
<?php
namespace App\Http\Livewire\Transportmanagement;
use App\Models\ManageVehicle;
use App\Models\VehicleType;
use Livewire\Component;
use Livewire\WithFileUploads;
use Livewire\WithPagination;
class ManageVehicleLivewire extends Component
{
use WithPagination;
use WithFileUploads;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $vehicle_type, $vehicle_model, $vehicle_registration_no, $vehicle_insurance_no, $vehicle_insurance_expiry_date, $vehicle_insurance_docs, $vehicle_puc_no, $vehicle_puc_start_date, $vehicle_puc_end_date, $vehicle_fitness_docs, $vehicle_fitness_expiry_date;
protected $listeners = ['delete'];
public $modelid;
public $showmodal = false;
public $status = true;
public $successmsg=" Vehicle has been created successfully.";
public $deletemsg=" Vehicle has been deleted successfully";
public $duplicateerrormsg = "Sorry !!! registration has already exists in our database. Please Try Another Name";
public $updatemsg="Vehicle has been updated successfully";
public $errormsg="Oops !!! Something went wrong.";
protected $rules = [
'vehicle_type'=>'required',
'vehicle_model'=>'required',
'vehicle_registration_no'=>'required',
'vehicle_insurance_no'=>'required',
'vehicle_insurance_expiry_date'=>'required',
'vehicle_insurance_docs'=>'required',
'vehicle_puc_no'=>'required',
'vehicle_puc_start_date'=>'required',
'vehicle_puc_end_date'=>'required',
'vehicle_fitness_docs'=>'required',
'vehicle_fitness_expiry_date'=>'required',
];
protected $updatedrules = [
'vehicle_type'=>'required',
'vehicle_model'=>'required',
'vehicle_registration_no'=>'required',
'vehicle_insurance_no'=>'required',
'vehicle_insurance_expiry_date'=>'required',
'vehicle_insurance_docs'=>'required',
'vehicle_puc_no'=>'required',
'vehicle_puc_start_date'=>'required',
'vehicle_puc_end_date'=>'required',
'vehicle_fitness_docs'=>'required',
'vehicle_fitness_expiry_date'=>'required',
];
protected $messages = [
'vehicle_type.required'=>'The vehicle type cannot be empty.',
'vehicle_model.required'=>'The vehicle model cannot be empty.',
'vehicle_registration_no.required'=>'The vehicle Registration no cannot be empty.',
'vehicle_insurance_no.required'=>'The vehicle insurance no field cannot be empty.',
'vehicle_insurance_expiry_date.required'=>'The vehicle insurance no field cannot be empty.',
'vehicle_insurance_docs.required'=>'The vehicle insurance document cannot be empty.',
'vehicle_puc_no.required'=>'The vehicle Puc no cannot be empty.',
'vehicle_puc_start_date.required'=>'The vehicle Puc start date cannot be empty.',
'vehicle_puc_end_date.required'=>'The vehicle Puc end date cannot be empty.',
'vehicle_fitness_docs.required'=>'The vehicle fitness document field cannot be empty.',
'vehicle_fitness_expiry_date.required'=>'The vehicle fitness document field cannot be empty.',
'status.required'=>'The status cannot be empty.',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick()
{
$this->cleanVar();
$this->resetValidation();
$this->showmodal=true;
}
public function closemodalclick()
{
$this->resetValidation();
$this->showmodal = false;
}
public function updateclick($id)
{
$this->modelid = $id;
$this->loadData($this->modelid);
$this->showmodal = true;
}
public function deleteclick($id)
{
$this->modelid = $id;
$this->dispatchBrowserEvent('confirmdelete', [
'message' => 'Are you sure want to delete this data ?',
'funcname' => 'delete'
]);
}
public function cleanVar()
{
$this->modelid=null;
$this->vehicle_type=null;
$this->vehicle_model=null;
$this->vehicle_registration_no=null;
$this->vehicle_insurance_no=null;
$this->vehicle_insurance_expiry_date=null;
$this->vehicle_puc_no=null;
$this->vehicle_puc_start_date=null;
$this->vehicle_puc_end_date=null;
$this->vehicle_fitness_expiry_date=null;
$this->status=null;
$this->showmodal=true;
}
public function loadData($id)
{
$response = ManageVehicle::find($id);
if ($response) {
$this->vehicle_type = $response->vehicle_type;
$this->vehicle_model = $response->vehicle_model;
$this->vehicle_registration_no = $response->vehicle_registration_no;
$this->vehicle_insurance_no = $response->vehicle_insurance_no;
$this->vehicle_insurance_expiry_date = $response->vehicle_insurance_expiry_date;
$this->vehicle_insurance_docs = null;
$this->vehicle_puc_no = $response->vehicle_puc_no;
$this->vehicle_puc_start_date = $response->vehicle_puc_start_date;
$this->vehicle_puc_end_date = $response->vehicle_puc_end_date;
$this->vehicle_fitness_docs = null;
$this->vehicle_fitness_expiry_date = $response->vehicle_fitness_expiry_date;
$this->status = $response->status;
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$response = ManageVehicle::where('vehicle_registration_no', $this->vehicle_registration_no)->first();
if ($response) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->duplicateerrormsg
]);
} else {
$response = ManageVehicle::create([
'vehicle_type' => $this->vehicle_type,
'vehicle_model' => $this->vehicle_model,
'vehicle_registration_no' => $this->vehicle_registration_no,
'vehicle_insurance_no' => $this->vehicle_insurance_no,
'vehicle_insurance_expiry_date' => $this->vehicle_insurance_expiry_date,
'vehicle_insurance_docs' => $this->vehicle_insurance_docs->store('manage_vehicles', 'public'), // Fixing "strore" typo
'vehicle_puc_no' => $this->vehicle_puc_no,
'vehicle_puc_start_date' => $this->vehicle_puc_start_date,
'vehicle_puc_end_date' => $this->vehicle_puc_end_date,
'vehicle_fitness_docs' => $this->vehicle_fitness_docs->store('manage_vehicles', 'public'), // Fixing "strore" typo
'vehicle_fitness_expiry_date' => $this->vehicle_fitness_expiry_date,
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function update()
{
$this->validate($this->updatedrules, $this->messages);
$response = ManageVehicle::find($this->modelid);
if ($response) {
$vehicleInsuranceDocs = $this->vehicle_insurance_docs ? $this->vehicle_insurance_docs->store('manage_vehicles') : $response->vehicle_insurance_docs;
$vehicleFitnessDocs = $this->vehicle_fitness_docs ? $this->vehicle_fitness_docs->store('manage_vehicles') : $response->vehicle_fitness_docs;
$updated = $response->update([
'vehicle_type' => $this->vehicle_type,
'vehicle_model' => $this->vehicle_model,
'vehicle_registration_no' => $this->vehicle_registration_no,
'vehicle_insurance_no' => $this->vehicle_insurance_no,
'vehicle_insurance_expiry_date' => $this->vehicle_insurance_expiry_date,
'vehicle_insurance_docs' => $vehicleInsuranceDocs,
'vehicle_puc_no' => $this->vehicle_puc_no,
'vehicle_puc_start_date' => $this->vehicle_puc_start_date,
'vehicle_puc_end_date' => $this->vehicle_puc_end_date,
'vehicle_fitness_docs' => $vehicleFitnessDocs,
'vehicle_fitness_expiry_date' => $this->vehicle_fitness_expiry_date,
'status' => $this->status,
]);
if ($updated) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'Vehicle not found.',
]);
}
}
public function delete()
{
$response = ManageVehicle::find($this->modelid);
if ($response) {
if ($response->delete()) {
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->deletemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function read(){
$manage_vehicles = ManageVehicle::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $manage_vehicles ;
}
public function render()
{
$datatable=[];
$datatable = $this->read();
if(sizeof($datatable)>0) {
foreach ($datatable as $key => $data) {
$vehicle = VehicleType::where('id', $data->vehicle_type)->first();
if ($vehicle) {
$datatable[$key]['vehicle_type'] = $vehicle->vehicle_type;
} else {
$datatable[$key]['vehicle_type'] = "NA";
}
}
}
$vehicles=VehicleType::where('status',1)->get();
return view('livewire.transportmanagement.manage-vehicle-livewire',compact('datatable','vehicles'));
}
}