Files
CKB/app/Http/Controllers/Reports/ReportTechniciansController.php

180 lines
5.9 KiB
PHP

<?php
namespace App\Http\Controllers\Reports;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Menu;
use App\Services\TechnicianReportService;
use App\Exports\TechnicianReportExport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;
class ReportTechniciansController extends Controller
{
protected $technicianReportService;
public function __construct(TechnicianReportService $technicianReportService)
{
$this->technicianReportService = $technicianReportService;
}
public function index(Request $request)
{
$menu = Menu::where('link','reports.technician.index')->first();
abort_if(!Gate::allows('view', $menu), 403);
return view('reports.technician');
}
/**
* Get dealers for filter dropdown
*/
public function getDealers()
{
try {
$dealers = $this->technicianReportService->getDealers();
// Default ke "Semua Dealer" (tidak ada dealer yang terselect)
return response()->json([
'status' => 'success',
'data' => $dealers,
'default_dealer' => null
]);
} catch (\Exception $e) {
Log::error('Error getting dealers: ' . $e->getMessage());
return response()->json([
'status' => 'error',
'message' => 'Gagal mengambil data dealer'
], 500);
}
}
/**
* Get technician report data for DataTable
*/
public function getData(Request $request)
{
try {
$dealerId = $request->input('dealer_id');
$startDate = $request->input('start_date');
$endDate = $request->input('end_date');
Log::info('Requesting technician report data:', [
'dealer_id' => $dealerId,
'start_date' => $startDate,
'end_date' => $endDate
]);
$reportData = $this->technicianReportService->getTechnicianReportData(
$dealerId,
$startDate,
$endDate
);
Log::info('Technician report data response:', [
'data_count' => count($reportData['data']),
'mechanics_count' => $reportData['mechanics']->count(),
'works_count' => $reportData['works']->count(),
'mechanics' => $reportData['mechanics']->map(function($mechanic) {
return [
'id' => $mechanic->id,
'name' => $mechanic->name,
'role_id' => $mechanic->role_id
];
})
]);
return response()->json([
'status' => 'success',
'data' => $reportData['data'],
'mechanics' => $reportData['mechanics'],
'works' => $reportData['works']
]);
} catch (\Exception $e) {
Log::error('Error getting technician report data: ' . $e->getMessage(), [
'dealer_id' => $request->input('dealer_id'),
'start_date' => $request->input('start_date'),
'end_date' => $request->input('end_date'),
'trace' => $e->getTraceAsString()
]);
return response()->json([
'status' => 'error',
'message' => 'Gagal mengambil data laporan teknisi: ' . $e->getMessage()
], 500);
}
}
/**
* Get technician report data for Yajra DataTable
*/
public function getDataTable(Request $request)
{
try {
$dealerId = $request->input('dealer_id');
$startDate = $request->input('start_date');
$endDate = $request->input('end_date');
Log::info('Requesting technician report data for DataTable:', [
'dealer_id' => $dealerId,
'start_date' => $startDate,
'end_date' => $endDate
]);
$reportData = $this->technicianReportService->getTechnicianReportDataForDataTable(
$dealerId,
$startDate,
$endDate
);
return $reportData;
} catch (\Exception $e) {
Log::error('Error getting technician report data for DataTable: ' . $e->getMessage(), [
'dealer_id' => $request->input('dealer_id'),
'start_date' => $request->input('start_date'),
'end_date' => $request->input('end_date'),
'trace' => $e->getTraceAsString()
]);
return response()->json([
'error' => 'Gagal mengambil data laporan teknisi: ' . $e->getMessage()
], 500);
}
}
/**
* Export technician report to Excel
*/
public function export(Request $request)
{
try {
$dealerId = $request->input('dealer_id');
$startDate = $request->input('start_date');
$endDate = $request->input('end_date');
Log::info('Exporting technician report', [
'dealer_id' => $dealerId,
'start_date' => $startDate,
'end_date' => $endDate
]);
return Excel::download(new TechnicianReportExport($dealerId, $startDate, $endDate), 'laporan_teknisi_' . date('Y-m-d') . '.xlsx');
} catch (\Exception $e) {
Log::error('Error exporting technician report: ' . $e->getMessage(), [
'dealer_id' => $request->input('dealer_id'),
'start_date' => $request->input('start_date'),
'end_date' => $request->input('end_date')
]);
return response()->json([
'status' => 'error',
'message' => 'Gagal export laporan: ' . $e->getMessage()
], 500);
}
}
}