penambahan fitur ekspor di hse, qma, dan pg
This commit is contained in:
@@ -5,8 +5,12 @@ namespace App\Http\Controllers\Hse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Employee;
|
||||
use App\Models\User;
|
||||
use App\Services\SimperPdfExporter;
|
||||
use App\Services\SimpleXlsxExporter;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\View\View;
|
||||
|
||||
@@ -62,9 +66,9 @@ class EmployeeController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Tampilkan daftar master karyawan HSE & status SIMPER.
|
||||
* Helper query builder untuk memfilter master data karyawan & status SIMPER.
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
private function getFilteredEmployees(Request $request): Builder
|
||||
{
|
||||
$query = Employee::query();
|
||||
|
||||
@@ -114,11 +118,171 @@ class EmployeeController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
$employees = $query->orderBy('name')->paginate(20)->withQueryString();
|
||||
return $query->orderBy('name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tampilkan daftar master karyawan HSE & status SIMPER.
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$employees = $this->getFilteredEmployees($request)->paginate(20)->withQueryString();
|
||||
|
||||
return view('hse.employees_index', compact('employees'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor data master SIMPER karyawan ke format Excel (.xlsx).
|
||||
*/
|
||||
public function exportExcel(Request $request): Response
|
||||
{
|
||||
$employees = $this->getFilteredEmployees($request)->get();
|
||||
|
||||
$today = now()->translatedFormat('d F Y, H:i');
|
||||
$driverFilter = match ($request->input('driver_status')) {
|
||||
'driver' => 'Driver',
|
||||
'non_driver' => 'Bukan Driver',
|
||||
default => 'Semua Status Driver'
|
||||
};
|
||||
$simperFilter = match ($request->input('simper_status')) {
|
||||
'valid' => 'Masih Berlaku (Aktif)',
|
||||
'expiring_soon' => 'Segera Habis (≤ 30 Hari)',
|
||||
'expired' => 'Kedaluwarsa (Expired)',
|
||||
default => 'Semua Masa Berlaku'
|
||||
};
|
||||
|
||||
$metaLines = [
|
||||
'Filter: ' . $driverFilter . ' | ' . $simperFilter . ($request->filled('search') ? ' | Pencarian: "' . $request->input('search') . '"' : ''),
|
||||
'Total Data: ' . $employees->count() . ' Karyawan | Tanggal Dicetak: ' . $today . ' WIB',
|
||||
];
|
||||
|
||||
$headers = [
|
||||
'No' => 6,
|
||||
'NIK' => 16,
|
||||
'Nama Lengkap' => 30,
|
||||
'Jabatan' => 24,
|
||||
'Unit Kerja' => 20,
|
||||
'Divisi' => 20,
|
||||
'Status Driver' => 16,
|
||||
'Masa Berlaku SIMPER' => 22,
|
||||
'Status SIMPER' => 24,
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
$now = now();
|
||||
$soon = now()->addDays(30);
|
||||
|
||||
foreach ($employees as $idx => $emp) {
|
||||
$isDriver = $emp->is_operator;
|
||||
$expDate = $emp->simper_expired_at ? $emp->simper_expired_at->format('d/m/Y') : '-';
|
||||
|
||||
if (! $isDriver) {
|
||||
$statusText = 'Bukan Driver';
|
||||
} elseif (! $emp->simper_expired_at) {
|
||||
$statusText = 'Belum Ada SIMPER';
|
||||
} elseif ($emp->simper_expired_at->isPast() && ! $emp->simper_expired_at->isToday()) {
|
||||
$statusText = 'Kedaluwarsa (Expired)';
|
||||
} elseif ($emp->simper_expired_at->lte($soon)) {
|
||||
$statusText = 'Segera Habis (≤ 30 Hari)';
|
||||
} else {
|
||||
$statusText = 'Masih Berlaku (Aktif)';
|
||||
}
|
||||
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$emp->nik,
|
||||
$emp->name,
|
||||
$emp->position ?: '-',
|
||||
$emp->unit_kerja ?: '-',
|
||||
$emp->division ?: '-',
|
||||
$isDriver ? 'Driver' : 'Bukan Driver',
|
||||
$expDate,
|
||||
$statusText,
|
||||
];
|
||||
}
|
||||
|
||||
$alignments = [
|
||||
'center', // No
|
||||
'center', // NIK
|
||||
'left', // Nama
|
||||
'left', // Jabatan
|
||||
'left', // Unit Kerja
|
||||
'left', // Divisi
|
||||
'center', // Status Driver
|
||||
'center', // Masa Berlaku
|
||||
'center', // Status SIMPER
|
||||
];
|
||||
|
||||
$fileName = 'Laporan_Data_Simper_' . now()->format('Ymd_Hi') . '.xlsx';
|
||||
$binary = SimpleXlsxExporter::create(
|
||||
'LAPORAN DATA SIMPER & STATUS DRIVER KARYAWAN - PT KERINCI MERANGIN HIDRO',
|
||||
$metaLines,
|
||||
$headers,
|
||||
$rows,
|
||||
$alignments
|
||||
);
|
||||
|
||||
return response($binary, 200, [
|
||||
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($binary),
|
||||
'Cache-Control' => 'max-age=0, no-cache, no-store, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tampilkan laporan resmi SIMPER ke format PDF (Pratinjau PDF Asli dengan Logo PT KMH).
|
||||
*/
|
||||
public function exportPdf(Request $request): Response
|
||||
{
|
||||
$employees = $this->getFilteredEmployees($request)->get();
|
||||
|
||||
$driverFilter = match ($request->input('driver_status')) {
|
||||
'driver' => 'Driver',
|
||||
'non_driver' => 'Bukan Driver',
|
||||
default => 'Semua Status Driver'
|
||||
};
|
||||
$simperFilter = match ($request->input('simper_status')) {
|
||||
'valid' => 'Masih Berlaku (Aktif)',
|
||||
'expiring_soon' => 'Segera Habis (≤ 30 Hari)',
|
||||
'expired' => 'Kedaluwarsa (Expired)',
|
||||
default => 'Semua Masa Berlaku'
|
||||
};
|
||||
|
||||
$totalActive = $employees->filter(fn ($e) => $e->is_operator && $e->hasValidSimper())->count();
|
||||
$totalExpired = $employees->filter(fn ($e) => $e->is_operator && ! $e->hasValidSimper())->count();
|
||||
$totalNonDriver = $employees->filter(fn ($e) => ! $e->is_operator)->count();
|
||||
|
||||
$metaLeft = [
|
||||
'Kategori' => $driverFilter,
|
||||
'Tanggal Cetak' => now()->translatedFormat('d F Y, H:i') . ' WIB',
|
||||
];
|
||||
|
||||
$statusSummary = "{$totalActive} Aktif, {$totalExpired} Expired";
|
||||
if ($totalNonDriver > 0) {
|
||||
$statusSummary .= ", {$totalNonDriver} Non-Driver";
|
||||
}
|
||||
|
||||
$metaRight = [
|
||||
'Total Karyawan' => $employees->count() . ' Orang',
|
||||
'Status SIMPER' => $statusSummary,
|
||||
];
|
||||
|
||||
$subtitle = 'Data Karyawan & Driver - ' . now()->translatedFormat('d F Y');
|
||||
|
||||
$pdfBinary = SimperPdfExporter::generate($employees, $metaLeft, $metaRight, $subtitle);
|
||||
$fileName = 'Laporan_Daftar_Simper_' . now()->format('Ymd_Hi') . '.pdf';
|
||||
|
||||
return response($pdfBinary, 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($pdfBinary),
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simpan data karyawan baru beserta izin SIMPER (Form modal tambah karyawan).
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user