CasperSecurity
<?php
namespace App\Http\Livewire\Report;
use App\Models\Organisation;
use App\Models\VehicleMaintenance;
use Carbon\Carbon;
use Livewire\Component;
class MaintainenanceReportLivewire extends Component
{
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = 'asc';
public $filter_by;
public $from_date,$to_date,$data;
public function closemodalclick()
{
$this->resetValidation();
$this->datatable = [];
}
public function mount()
{
$this->from_date = Carbon::now()->format('Y-m-d');
$this->to_date = Carbon::now()->format('Y-m-d');
}
public function read()
{
$vehicle_maintenances = VehicleMaintenance::select('vehicle_maintenances.*', 'manage_vehicles.vehicle_registration_no')
->join('manage_vehicles', 'vehicle_maintenances.vehicle_id', '=', 'manage_vehicles.id')
->whereDate('vehicle_maintenances.vehicle_maintenance_date','>=',$this->from_date)
->where('vehicle_maintenances.status', 1);
if ($this->search != null) {
if ($this->filter_by == 'vehicle_registration_no') {
$vehicle_maintenances->where(function($query) {
$query->where('manage_vehicles.vehicle_registration_no', 'like', '%' . $this->search . '%');
});
} elseif ($this->filter_by == 'vehicle_maintenance_next_date') {
$vehicle_maintenances->where(function($query) {
$query->where('vehicle_maintenances.vehicle_maintenance_next_date', 'like', '%' . $this->search . '%');
});
}
}
return $vehicle_maintenances->paginate($this->perPage);
}
public function getTotalCost()
{
return $this->read()->sum('vehicle_maintenance_cost');
}
public function render()
{
$org = Organisation::latest()->first();
$datatable = $this->read();
$totalCost = $this->getTotalCost();
return view('livewire.report.maintainenance-report-livewire',compact('datatable','org','totalCost'));
}
}