83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Exports;
|
||
|
|
|
||
|
|
use App\Models\Cat;
|
||
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
||
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||
|
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
||
|
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
||
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||
|
|
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
|
||
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||
|
|
|
||
|
|
class CatWeightTemplateExport implements FromCollection, WithHeadings, WithMapping, WithStyles, ShouldAutoSize, WithColumnFormatting
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Ambil data kucing yang aktif
|
||
|
|
*/
|
||
|
|
public function collection()
|
||
|
|
{
|
||
|
|
// Tetap mengambil semua kucing yang tidak dihapus
|
||
|
|
return Cat::whereNull('deleted_at')->get(['id', 'nama_kucing']);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Header Tabel
|
||
|
|
*/
|
||
|
|
public function headings(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
"Kode Kucing",
|
||
|
|
"Nama Kucing",
|
||
|
|
"Berat Badan (Kg)",
|
||
|
|
"Tanggal & Waktu" // Ubah header agar staf tahu ini mendukung jam
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Mapping baris data
|
||
|
|
*/
|
||
|
|
public function map($cat): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
$cat->id,
|
||
|
|
$cat->nama_kucing,
|
||
|
|
'', // Kosongkan untuk diisi staf
|
||
|
|
|
||
|
|
// Konversi tanggal PHP murni ke Serial Number Date milik Excel
|
||
|
|
// Ini mencegah Excel mengubah tanggal menjadi format teks yang berantakan
|
||
|
|
Date::dateTimeToExcel(now())
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Format Kolom secara spesifik
|
||
|
|
*/
|
||
|
|
public function columnFormats(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
// Memaksa kolom D (Tanggal & Waktu) menggunakan format kalender bawaan Excel
|
||
|
|
// Formatnya akan menjadi YYYY-MM-DD HH:MM:SS saat staf membuka filenya
|
||
|
|
'D' => NumberFormat::FORMAT_DATE_DATETIME,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Styling Header agar lebih cantik
|
||
|
|
*/
|
||
|
|
public function styles(Worksheet $sheet)
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
1 => [
|
||
|
|
'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
|
||
|
|
'fill' => [
|
||
|
|
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
|
||
|
|
'startColor' => ['rgb' => '4F81BD']
|
||
|
|
],
|
||
|
|
],
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|