Files
admin-erahn/app/Exports/LaporanKeseluruhan/GadaiTebusAllExportExcel.php
T
2025-07-09 13:45:19 +08:00

124 lines
3.4 KiB
PHP

<?php
namespace App\Exports\LaporanKeseluruhan;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
class GadaiTebusAllExportExcel extends DefaultValueBinder implements
FromArray,
WithStrictNullComparison,
WithHeadings,
ShouldAutoSize,
WithCustomValueBinder,
WithColumnFormatting
{
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function headings(): array
{
return [
['Laporan Gadai Tebus Keseluruhan'], // Title row
['Bil',
'Cawangan',
'Bilangan SAG',
'Jumlah Pembiayaan (RM)',
]
];
}
public function array(): array
{
if (empty($this->data)) {
return [];
}
$formattedData = [];
$total_gadai_tebus_samahari = 0;
$jumlah_pinjaman = 0;
$counter = 1;
foreach ($this->data as $data) {
$formattedData[] = [
$counter++, // Numbering (Integer)
$data['details_caw'] ?? 'N/A',
(int) ($data['total_gadai_tebus_samahari'] ?? 0), // Integer
(float) ($data['jumlah_pinjaman'] ?? 0), // Decimal
];
// Update totals
$total_gadai_tebus_samahari += (int) $data['total_gadai_tebus_samahari'];
$jumlah_pinjaman += (float) $data['jumlah_pinjaman'];
}
// Append total row (without numbering)
$formattedData[] = [
'', // No numbering for total row
'Jumlah Keseluruhan',
$total_gadai_tebus_samahari,
$jumlah_pinjaman,
];
return $formattedData;
}
public function columnWidths(): array
{
return [
'A' => 5, // Bil
'B' => 25, // Cawangan
'C' => 20, // Bilangan SAG
'D' => 20, // Jumlah
];
}
public function columnFormats(): array
{
return [
'C' => NumberFormat::FORMAT_NUMBER,
'D' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1,
];
}
public function bindValue(Cell $cell, $value)
{
if (is_numeric($value) && !is_string($value)) {
$cell->setValueExplicit((float) $value, DataType::TYPE_NUMERIC);
return true;
}
return parent::bindValue($cell, $value);
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$sheet = $event->sheet;
// Merge the title row across all columns (A1 to G1)
$sheet->mergeCells('A1:G1');
// Style the title (bold, center)
$sheet->getStyle('A1')->applyFromArray([
'font' => ['bold' => true, 'size' => 14],
'alignment' => ['horizontal' => 'center', 'vertical' => 'center']
]);
}
];
}
}