86 lines
3.0 KiB
PHP
86 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace Modules\Maintenance\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\Maintenance\Http\Controllers\Concerns\AuthorizesMaintenance;
|
|
use Modules\Maintenance\Services\MaintenanceReportService;
|
|
use Modules\Maintenance\Utils\MaintenanceUtil;
|
|
|
|
class ReportController extends Controller
|
|
{
|
|
use AuthorizesMaintenance;
|
|
|
|
public function __construct(
|
|
protected MaintenanceReportService $reportService,
|
|
protected MaintenanceUtil $maintenanceUtil
|
|
) {}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.reports.view');
|
|
|
|
$business = $this->maintenanceUtil->getBusiness();
|
|
if (! $business) {
|
|
abort(404);
|
|
}
|
|
|
|
$business_id = $business->id;
|
|
$equipment_id = $request->integer('equipment_id') ?: null;
|
|
$from = $this->maintenanceUtil->parseInputDate($request->get('from'));
|
|
$to = $this->maintenanceUtil->parseInputDate($request->get('to'));
|
|
$year = (int) $request->get('year', now()->year);
|
|
$month = (int) $request->get('month', now()->month);
|
|
|
|
$equipment_costs = $this->reportService->equipmentCostReport($business, $equipment_id);
|
|
$downtime = $this->reportService->downtimeReport($business, $from, $to);
|
|
$technicians = $this->reportService->technicianPerformance($business);
|
|
$consumption = $this->reportService->sparePartsConsumption($business, $from, $to);
|
|
$monthly = $this->reportService->monthlyStatistics($business, $year, $month);
|
|
$failures = $this->reportService->failureAnalysis($business);
|
|
|
|
return view('maintenance::reports.index', compact(
|
|
'equipment_costs',
|
|
'downtime',
|
|
'technicians',
|
|
'consumption',
|
|
'monthly',
|
|
'failures',
|
|
'equipment_id',
|
|
'from',
|
|
'to',
|
|
'year',
|
|
'month'
|
|
))->with([
|
|
'equipment_dropdown' => $this->maintenanceUtil->getEquipmentDropdown($business_id),
|
|
]);
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.reports.view');
|
|
|
|
$business = $this->maintenanceUtil->getBusiness();
|
|
if (! $business) {
|
|
abort(404);
|
|
}
|
|
|
|
$from = $this->maintenanceUtil->parseInputDate($request->get('from'));
|
|
$to = $this->maintenanceUtil->parseInputDate($request->get('to'));
|
|
|
|
$rows = $this->reportService->exportActivityCsv($business, $from, $to);
|
|
|
|
$filename = 'maintenance-report-'.now()->format('Y-m-d').'.csv';
|
|
|
|
return response()->streamDownload(function () use ($rows) {
|
|
$out = fopen('php://output', 'w');
|
|
fputcsv($out, ['type', 'date', 'description', 'cost']);
|
|
foreach ($rows as $row) {
|
|
fputcsv($out, $row);
|
|
}
|
|
fclose($out);
|
|
}, $filename, ['Content-Type' => 'text/csv']);
|
|
}
|
|
}
|