CasperSecurity
<?php
namespace App\Http\Livewire\Transportmanagement;
use App\Models\FuelUsage;
use App\Models\ManageVehicle;
use Livewire\Component;
use Livewire\WithFileUploads;
use Livewire\WithPagination;
class FuelUsageLivewire extends Component
{
use WithPagination;
use WithFileUploads;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $vehicle_id,$fuel_type,$fuel_filling_date;
public $fuel_filling_qty = 0;
public $fuel_filling_cost = 0;
public $modelid;
public $showmodal = false;
public $status = true;
public $successmsg="Fuel Filling has created successfully.";
public $updatemsg="Fuel Filling has updated successfully.";
public $deletemsg="Fuel Filling has deleted successfully";
public $errormsg="Oops !!! Something went wrong.";
protected $listeners = ['delete'];
protected $rules = [
'vehicle_id'=>'required',
'fuel_type'=>'required',
'fuel_filling_date'=>'required',
'fuel_filling_qty'=>'nullable',
'fuel_filling_cost'=>'required',
];
protected $updatedrules = [
'vehicle_id'=>'required',
'fuel_type'=>'required',
'fuel_filling_date'=>'required',
'fuel_filling_qty'=>'nullable',
'fuel_filling_cost'=>'required',
'status' => 'nullable',
];
protected $messages = [
'fuel_type.required'=>'This field is required',
'fuel_filling_date.required'=>'This field is required',
'fuel_filling_qty.required'=>'This field is required',
'fuel_filling_cost.required'=>'This field is required',
];
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->fuel_type=null;
$this->fuel_filling_date=null;
$this->fuel_filling_qty=null;
$this->fuel_filling_cost=null;
$this->status=null;
}
public function loadData($id)
{
$response = FuelUsage::find($id);
if ($response) {
$this->vehicle_id = $response['vehicle_id'];
$this->fuel_type = $response['fuel_type'];
$this->fuel_filling_date = $response['fuel_filling_date'];
$this->fuel_filling_qty = $response['fuel_filling_qty'];
$this->fuel_filling_cost = $response['fuel_filling_cost'];
$this->status = $response['status'];
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$response = FuelUsage::where('vehicle_id', $this->vehicle_id)->latest()->first();
if ($response) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
} else {
$response = FuelUsage::create([
'vehicle_id' => $this->vehicle_id,
'fuel_type' => $this->fuel_type,
'fuel_filling_date' => $this->fuel_filling_date,
'fuel_filling_qty' => $this->fuel_filling_qty,
'fuel_filling_cost' => $this->fuel_filling_cost,
]);
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 = FuelUsage::find($this->modelid);
if ($response) {
$response->update([
'vehicle_id' => $this->vehicle_id,
'fuel_type' => $this->fuel_type,
'fuel_filling_date' => $this->fuel_filling_date,
'fuel_filling_qty' => $this->fuel_filling_qty,
'fuel_filling_cost' => $this->fuel_filling_cost,
'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 = FuelUsage::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()
{
$fuel_usages = FuelUsage::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $fuel_usages;
}
public function render()
{
$datatable=[];
$datatable=$this->read();
if (sizeof($datatable) > 0) {
foreach ($datatable as $key => $data) {
$vehicle = ManageVehicle::where('id',$data->vehicle_id)->first();
//dd($holiday);
//$datatable[$key]['holiday_type'] = $holiday ? $holiday->holiday_type:'N/A';
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.fuel-usage-livewire',compact('datatable','vehicles'));
}
}