Files
admin-erahn/app/Exports/LaporanKeseluruhan/AgingTebusAllExportExcel.php
T
2026-06-02 11:58:36 +08:00

223 lines
7.2 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;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\WithEvents;
class AgingTebusAllExportExcel extends DefaultValueBinder implements
FromArray,
WithStrictNullComparison,
WithHeadings,
ShouldAutoSize,
WithCustomValueBinder,
WithColumnFormatting,
WithEvents,
WithCustomStartCell
{
protected $aging;
protected $startDate;
protected $endDate;
public function __construct($aging,$startDate,$endDate)
{
$this->aging = $aging;
$this->startDate = $startDate;
$this->endDate = $endDate;
}
public function array(): array
{
$rows = [];
$totals = [];
foreach ($this->aging as $branch) {
$row = [$branch['cawangan']];
$i = 0;
foreach ($branch['summary_by_tempoh'] as $summary) {
$row[] = $summary['count'];
$row[] = $summary['sum_bayaran_keuntungan_rebet'];
// Totals per column
$totals[$i]['count'] = ($totals[$i]['count'] ?? 0) + $summary['count'];
$totals[$i]['sum_bayaran_keuntungan_rebet'] = ($totals[$i]['sum_bayaran_keuntungan_rebet'] ?? 0) + $summary['sum_bayaran_keuntungan_rebet'];
$i++;
}
// Total per row (cawangan)
$jumlahBil = collect($branch['summary_by_tempoh'])->sum('count');
$jumlahRM = collect($branch['summary_by_tempoh'])->sum('sum_bayaran_keuntungan_rebet');
$row[] = $jumlahBil;
$row[] = $jumlahRM;
// Add to column totals
$totals['jumlah']['count'] = ($totals['jumlah']['count'] ?? 0) + $jumlahBil;
$totals['jumlah']['sum_bayaran_keuntungan_rebet'] = ($totals['jumlah']['sum_bayaran_keuntungan_rebet'] ?? 0) + $jumlahRM;
$rows[] = $row;
}
// Add final total row
$totalRow = ['Jumlah'];
foreach ($totals as $key => $val) {
$totalRow[] = $val['count'];
$totalRow[] = $val['sum_bayaran_keuntungan_rebet'];
}
$rows[] = $totalRow;
return $rows;
}
/**
* Multi-row headings (2 rows)
*/
public function headings(): array
{
$row1 = ['Cawangan'];
$row2 = [''];
if (!empty($this->aging[0]['summary_by_tempoh'])) {
foreach ($this->aging[0]['summary_by_tempoh'] as $summary) {
$row1[] = $summary['label'];
$row1[] = ''; // empty to allow merged cell
$row2[] = 'Bil';
$row2[] = 'RM';
}
}
$row1[] = 'Jumlah';
$row1[] = ''; // for merged "Jumlah" cell
$row2[] = 'Bil';
$row2[] = 'RM';
return [$row1, $row2];
}
public function columnFormats(): array
{
$formats = [];
$colIndex = 2; // Start at column B (Cawangan is A)
$tempohCount = count($this->aging[0]['summary_by_tempoh'] ?? []);
for ($i = 0; $i < $tempohCount; $i++) {
// Bil column
$bilCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colIndex++);
$formats[$bilCol] = NumberFormat::FORMAT_NUMBER; // ✅ No decimal
// RM column
$rmCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colIndex++);
$formats[$rmCol] = NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1; // RM with comma and decimals
}
// Jumlah Bil
$jumlahBilCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colIndex++);
$formats[$jumlahBilCol] = NumberFormat::FORMAT_NUMBER; // ✅ No decimal
// Jumlah RM
$jumlahRMCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colIndex++);
$formats[$jumlahRMCol] = NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1;
return $formats;
}
public function bindValue(Cell $cell, $value)
{
if (is_numeric($value)) {
$cell->setValueExplicit($value, DataType::TYPE_NUMERIC);
return true;
}
return parent::bindValue($cell, $value);
}
/**
* Merge heading cells using AfterSheet event
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$sheet = $event->sheet->getDelegate();
// Tajuk laporan
$sheet->setCellValue('A1', 'LAPORAN KEUNTUNGAN PENEBUSAN BY TEMPOH');
// Tarikh laporan
$sheet->setCellValue(
'A2',
'Tarikh : ' .
\Carbon\Carbon::parse($this->startDate)->format('d/m/Y') .
' hingga ' .
\Carbon\Carbon::parse($this->endDate)->format('d/m/Y')
);
// Merge tajuk
$lastColumn = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(
(count($this->aging[0]['summary_by_tempoh'] ?? []) * 2) + 3
);
$sheet->mergeCells("A1:{$lastColumn}1");
// Style tajuk
$sheet->getStyle("A1:{$lastColumn}1")->applyFromArray([
'font' => [
'bold' => true,
'size' => 14,
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
]);
// Merge header tempoh
$colIndex = 2; // Start from column B
$tempohCount = count($this->aging[0]['summary_by_tempoh'] ?? []);
$headingRow = 3;
for ($i = 0; $i < $tempohCount; $i++) {
$startCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colIndex);
$endCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colIndex + 1);
$sheet->mergeCells("{$startCol}{$headingRow}:{$endCol}{$headingRow}");
$colIndex += 2;
}
// Merge Jumlah
$startCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colIndex);
$endCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colIndex + 1);
$sheet->mergeCells("{$startCol}{$headingRow}:{$endCol}{$headingRow}");
},
];
}
// Mulakan sel untuk data bermula dari A4 (Baris 3 adalah header)
public function startCell(): string
{
return 'A3'; // Data bermula dari baris 4
}
}