CasperSecurity
<?php
namespace App\Http\Livewire\Transportmanagement;
use App\Models\ManageVehicle;
use App\Models\VehicleMaintenance;
use Carbon\Carbon;
use Livewire\Component;
use Livewire\WithFileUploads;
use Livewire\WithPagination;
class VehicleMaintenanceLivewire extends Component
{
use WithPagination;
use WithFileUploads;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $vehicle_id, $vehicle_maintenance_date, $vehicle_maintenance_next_date;
public $vehicle_maintenance_cost = 0;
public $modelid;
public $showmodal = false;
public $status = true;
public $successmsg = "Vehicle Maintenance has been created successfully.";
public $updatemsg = "Vehicle Maintenance has been updated successfully.";
public $deletemsg = "Vehicle Maintenance has been deleted successfully";
public $errormsg = "Oops !!! Something went wrong.";
protected $listeners = ['delete'];
protected $rules = [
'vehicle_id' => 'required|numeric',
'vehicle_maintenance_date' => 'required|date', // Consider adding date validation
'vehicle_maintenance_cost' => 'required|numeric', // Consider validating it as a number
'vehicle_maintenance_next_date' => 'required|date|after:vehicle_maintenance_date', // Add after validation
];
protected $updatedrules = [
'vehicle_id' => 'required|numeric',
'vehicle_maintenance_date' => 'required|date', // Consistent validation
'vehicle_maintenance_cost' => 'required|numeric',
'vehicle_maintenance_next_date' => 'required|date|after:vehicle_maintenance_date', // Add after validation
];
protected $messages = [
'vehicle_id.required' => 'The vehicle registration number cannot be empty.',
'vehicle_maintenance_date.required' => 'The vehicle maintenance date cannot be empty.',
'vehicle_maintenance_cost.required' => 'The vehicle maintenance cost cannot be empty.', // Corrected message
'vehicle_maintenance_cost.numeric' => 'The vehicle maintenance cost must be a number.', // Added numeric validation message
'vehicle_maintenance_next_date.required' => 'The vehicle maintenance next date cannot be empty.',
'vehicle_maintenance_next_date.after' => 'The next maintenance date must be after the vehicle maintenance date.',
];
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_maintenance_date = null;
$this->vehicle_maintenance_cost = null;
$this->vehicle_maintenance_next_date = null;
$this->status = null;
}
public function loadData($id)
{
$response = VehicleMaintenance::find($id);
if ($response) {
$this->vehicle_id = $response['vehicle_id'];
$this->vehicle_maintenance_date = Carbon::parse($response['vehicle_maintenance_date'])->format('Y-m-d'); // Format as needed
$this->vehicle_maintenance_cost = $response['vehicle_maintenance_cost'];
$this->vehicle_maintenance_next_date = Carbon::parse($response['vehicle_maintenance_next_date'])->format('Y-m-d'); // Format as needed
$this->status = $response['status'];
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
// Ensure vehicle maintenance date is in the correct format before checking for duplicates
$formattedDate = Carbon::parse($this->vehicle_maintenance_date)->format('Y-m-d');
$response = VehicleMaintenance::where('vehicle_maintenance_date', $formattedDate)->latest()->first();
if ($response) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
} else {
$response = VehicleMaintenance::create([
'vehicle_id' => $this->vehicle_id,
'vehicle_maintenance_date' => $formattedDate,
'vehicle_maintenance_cost' => $this->vehicle_maintenance_cost,
'vehicle_maintenance_next_date' => Carbon::parse($this->vehicle_maintenance_next_date)->format('Y-m-d'), // Format as needed
]);
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 = VehicleMaintenance::find($this->modelid);
if ($response) {
$response->update([
'vehicle_id' => $this->vehicle_id,
'vehicle_maintenance_date' => Carbon::parse($this->vehicle_maintenance_date)->format('Y-m-d'), // Format as needed
'vehicle_maintenance_cost' => $this->vehicle_maintenance_cost,
'vehicle_maintenance_next_date' => Carbon::parse($this->vehicle_maintenance_next_date)->format('Y-m-d'), // Format as needed
'status' => $this->status,
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function delete()
{
$response = VehicleMaintenance::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()
{
$query = VehicleMaintenance::select('vehicle_maintenances.*', 'manage_vehicles.vehicle_registration_no')
->join('manage_vehicles', 'vehicle_maintenances.vehicle_id', '=', 'manage_vehicles.id');
if ($this->search) {
$query->where('manage_vehicles.vehicle_registration_no', 'like', '%' . $this->search . '%');
}
$validOrderColumns = ['id', 'vehicle_id', 'created_at', 'updated_at', 'vehicle_registration_no']; // Add actual columns you expect
if (in_array($this->orderBy, $validOrderColumns)) {
$query->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc');
} else {
$query->orderBy('id', 'asc');
}
return $query->paginate($this->perPage);
}
public function render()
{
$datatable = $this->read();
$vehicles = ManageVehicle::where('status', 1)->get();
return view('livewire.transportmanagement.vehicle-maintenance-livewire', compact('datatable', 'vehicles'));
}
}