Files
ilycats/app/Exports/UnifiedReportExport.php
T

131 lines
4.8 KiB
PHP
Raw Normal View History

2026-02-08 15:16:35 +07:00
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\{FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithStyles, WithEvents, WithCustomStartCell};
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
class UnifiedReportExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithStyles, WithEvents, WithCustomStartCell
{
protected $data;
protected $module;
public function __construct($data, $module)
{
$this->data = $data;
$this->module = $module;
}
public function collection()
{
return $this->data;
}
/**
* Data tabel dimulai dari baris ke-7 (setelah metadata)
*/
public function startCell(): string
{
return 'A7';
}
public function headings(): array
{
return match ($this->module) {
'cats' => ['ID', 'NAMA KUCING', 'RAS', 'GENDER', 'TGL LAHIR', 'STATUS', 'PIC STAFF'],
'vaccine', 'deworming', 'flea', 'grooming' => ['ID', 'NAMA KUCING', 'TINDAKAN', 'JADWAL', 'STATUS', 'PIC STAFF'],
'medicine', 'food', 'supplies' => ['ID', 'NAMA PRODUK', 'STOK', 'SATUAN', 'TGL EXPIRED', 'PIC STAFF'],
default => ['ID', 'NAMA', 'DETAIL', 'TANGGAL', 'PIC']
};
}
public function map($item): array
{
$dateCol = match($this->module) {
'cats' => 'tanggal_lahir',
'medicine', 'food', 'supplies' => 'expired_date',
'vaccine' => 'jadwal_vaksin',
'deworming' => 'jadwal_obat_cacing',
'flea' => 'jadwal_obat_kutu',
'grooming' => 'jadwal_grooming',
default => 'created_at'
};
$dateValue = $item->{$dateCol} ? Carbon::parse($item->{$dateCol})->format('d/m/Y') : 'Not Set';
return match ($this->module) {
'cats' => [
$item->id, $item->nama_kucing, $item->ras, $item->jenis_kelamin,
$dateValue, $item->riwayat_sakit_terakhir ? 'IN TREATMENT' : 'HEALTHY',
$item->pic->name ?? 'System'
],
'vaccine', 'deworming', 'flea', 'grooming' => [
$item->id, $item->nama_kucing, strtoupper($this->module),
$dateValue, $this->getScheduleStatus($item->{$dateCol}),
$item->pic->name ?? 'System'
],
'medicine', 'food', 'supplies' => [
$item->id, $item->nama_obat ?? $item->nama_makanan ?? $item->nama_perlengkapan,
$item->qty, $item->unit, $dateValue, $item->pic->name ?? 'System'
],
default => [
$item->id, $item->nama_kucing ?? 'Data', $item->ras ?? '-', $dateValue, $item->pic->name ?? 'System'
]
};
}
private function getScheduleStatus($date) {
if (!$date) return 'NOT SET';
return Carbon::parse($date)->isPast() ? 'OVERDUE' : 'SCHEDULED';
}
/**
* Menulis Metadata di bagian atas Sheet
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$sheet = $event->sheet;
// Menulis Metadata
2026-09-23 12:45:02 +07:00
$sheet->setCellValue('A1', 'ILY CATTERY - LAPORAN TERPADU');
2026-02-08 15:16:35 +07:00
$sheet->setCellValue('A2', 'Dicetak Oleh: ' . Auth::user()->name);
$sheet->setCellValue('A3', 'Tanggal Cetak: ' . now()->translatedFormat('d F Y H:i'));
$sheet->setCellValue('A4', 'Total Data: ' . $this->data->count() . ' Records');
$sheet->setCellValue('A5', 'Status Laporan: LAPORAN ' . strtoupper($this->module));
// Styling Metadata
$sheet->getStyle('A1')->getFont()->setSize(14)->setBold(true);
$sheet->getStyle('A2:A5')->getFont()->setBold(true);
},
];
}
public function styles(Worksheet $sheet)
{
return [
// Style untuk Header Tabel (Sekarang di baris 7)
7 => [
'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => ['rgb' => '4F46E5']
],
'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER]
],
// Border untuk seluruh tabel
'A7:G' . (7 + $this->data->count()) => [
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
'color' => ['rgb' => 'E2E8F0'],
],
],
],
];
}
}