CasperSecurity
<?php
namespace App\Http\Livewire\Transportmanagement;
use App\Models\ManageVehicle;
use App\Models\VehicleRunning;
use Livewire\Component;
use Livewire\WithFileUploads;
use Livewire\WithPagination;
class VehicleRunningLivewire extends Component
{
use WithPagination;
use WithFileUploads;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $vehicle_id,$vehicle_running_date,$vehicle_start_km,$vehicle_end_km;
public $modelid;
public $showmodal = false;
public $status = true;
public $successmsg="Vehicle Running has been created successfully.";
public $updatemsg="Vehicle Running has been updated successfully.";
public $deletemsg="Vehicle Running has been deleted successfully";
public $errormsg="Oops !!! Something went wrong.";
protected $listeners = ['delete'];
protected $rules = [
'vehicle_id' => 'required',
'vehicle_running_date' => 'required',
'vehicle_start_km' => 'required|numeric',
'vehicle_end_km' => 'required|numeric|gt:vehicle_start_km', // Use gt to ensure end km is greater than start km
];
protected $updatedrules = [
'vehicle_id' => 'required',
'vehicle_running_date' => 'required',
'vehicle_start_km' => 'required|numeric',
'vehicle_end_km' => 'required|numeric|gt:vehicle_start_km', // Use gt to ensure end km is greater than start km
'status' => 'nullable',
];
protected $messages = [
'vehicle_running_date.required' => 'The Vehicle Running date cannot be empty.',
'vehicle_start_km.required' => 'The Vehicle Running Start Km cannot be empty.',
'vehicle_start_km.numeric' => 'The Vehicle Running Start Km must be a number.', // Added numeric validation message
'vehicle_end_km.required' => 'The Vehicle Running End Km cannot be empty.',
'vehicle_end_km.numeric' => 'The Vehicle Running End Km must be a number.', // Added numeric validation message
'vehicle_end_km.gt' => 'The Vehicle Running End Km must be greater than the Start Km.', // Custom message for greater than
'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_id=null;
$this->vehicle_running_date=null;
$this->vehicle_start_km=null;
$this->vehicle_end_km=null;
$this->status=null;
}
public function loadData($id)
{
$response = VehicleRunning::find($id);
if ($response) {
$this->vehicle_id = $response['vehicle_id'];
$this->vehicle_running_date = $response['vehicle_running_date'];
$this->vehicle_start_km = $response['vehicle_start_km'];
$this->vehicle_end_km = $response['vehicle_end_km'];
$this->status = $response['status'];
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$response = VehicleRunning::where('vehicle_running_date', $this->vehicle_running_date)->latest()->first();
if ($response) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
} else {
$response = VehicleRunning::create([
'vehicle_id' => $this->vehicle_id,
'vehicle_running_date' => $this->vehicle_running_date,
'vehicle_start_km' => $this->vehicle_start_km,
'vehicle_end_km' => $this->vehicle_end_km,
]);
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 updated()
{
if ($this->vehicle_start_km >= $this->vehicle_end_km)
{
if ($this->vehicle_start_km === $this->vehicle_end_km)
{
$this->vehicle_end_km = $this->vehicle_start_km + 30;
}
}
}*/
public function update()
{
$this->validate($this->updatedrules, $this->messages);
$response = VehicleRunning::find($this->modelid);
if ($response) {
$response->update([
'vehicle_id' => $this->vehicle_id,
'vehicle_running_date' => $this->vehicle_running_date,
'vehicle_start_km' => $this->vehicle_start_km,
'vehicle_end_km' => $this->vehicle_end_km,
'status' => $this->status,
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
} else {
// $this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function delete()
{
$response = VehicleRunning::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()
{
$vehicle_runnings = VehicleRunning::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $vehicle_runnings;
}
public function render()
{
$datatable=[];
$datatable=$this->read();
if (sizeof($datatable) > 0) {
foreach ($datatable as $key => $data) {
$vehicle = ManageVehicle::where('id',$data->vehicle_id)->first();
if($vehicle){
$datatable[$key]['vehicle_registration_no'] = $vehicle->vehicle_registration_no;
}
else{
$datatable[$key]['vehicle_registration_no'] ="NA";
}
}
}
$vehicles = ManageVehicle::where('status', 1)->get();
return view('livewire.transportmanagement.vehicle-running-livewire',compact('datatable','vehicles'));
}
}