Files
admin-erahn/app/Exports/SAExportAudit.php
T
2026-03-11 14:06:18 +08:00

189 lines
5.7 KiB
PHP

<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
class SAExportAudit implements FromCollection, WithHeadings, WithTitle, WithMapping, WithStyles, WithCustomStartCell, WithColumnFormatting
{
protected $stokaudit;
protected $jenisLaporan;
protected $tebus_date;
protected $namacaw;
public function __construct($stokaudit, $jenisLaporan = 'LAPORAN STOK AUDIT', $tebus_date, $namacaw)
{
$this->stokaudit = $stokaudit;
$this->jenisLaporan = strtoupper($jenisLaporan);
$this->tebus_date = $tebus_date;
$this->namacaw = $namacaw;
}
public function collection()
{
$data = collect($this->stokaudit);
$totals = [
'No Rujukan' => 'TOTAL',
'No KP' => '',
'Marhun' => '',
'Tempoh' => '',
'Berat (g)' => $data->sum(fn($row) => floatval($row['Berat (g)'] ?? 0)),
'Nilai Marhun' => $data->sum(fn($row) => floatval($row['Nilai Marhun'] ?? 0)),
'Pinjaman' => $data->sum(fn($row) => floatval($row['Pinjaman'] ?? 0)),
'Baki Pinjaman' => $data->sum(fn($row) => floatval($row['Baki Pinjaman'] ?? 0)),
];
$data->push($totals);
return $data;
}
public function headings(): array
{
return [
['LAPORAN STOK AUDIT'],
["CAWANGAN:", $this->namacaw],
["TARIKH:", $this->tebus_date],
[],
[
'BIL',
'NORUJUKAN',
'NO KP',
'MARHUN',
'BERAT (G)',
'BERAT AUDIT',
'NILAI MARHUN (RM)',
'PINJAMAN (RM)',
'BAKI PINJAMAN (RM)',
'TEMPOH',
'CATATAN'
]
];
}
public function startCell(): string
{
return 'A1';
}
public function map($row): array
{
static $counter = 1;
if (($row['No Rujukan'] ?? '') === 'TOTAL') {
return [
'',
'JUMLAH',
'',
'',
$row['Berat (g)'] ?? 0,
'',
$row['Nilai Marhun'] ?? 0,
$row['Pinjaman'] ?? 0,
$row['Baki Pinjaman'] ?? 0,
'',
'',
];
}
return [
$row['Bil'] ?? $counter++,
$row['No Rujukan'] ?? '',
isset($row['No KP']) ? "'" . $row['No KP'] : '',
$row['Marhun'] ?? '',
$row['Berat (g)'] ?? 0,
'',
$row['Nilai Marhun'] ?? 0,
$row['Pinjaman'] ?? 0,
$row['Baki Pinjaman'] ?? 0,
$row['Tempoh'] ?? '',
'',
];
}
public function title(): string
{
return $this->jenisLaporan;
}
public function styles(Worksheet $sheet)
{
$sheet->mergeCells('A1:K1');
$sheet->getStyle('A1')->getFont()->setBold(true)->setSize(16);
$sheet->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$sheet->getStyle('A5:K5')->getFont()->setBold(true);
$lastRow = $sheet->getHighestRow();
$lastCol = 'K';
// Border untuk semua data
$sheet->getStyle("A5:{$lastCol}{$lastRow}")
->getBorders()
->getAllBorders()
->setBorderStyle(Border::BORDER_THIN);
// Bold TOTAL row
$sheet->getStyle("A{$lastRow}:{$lastCol}{$lastRow}")->getFont()->setBold(true);
// Align center untuk column tertentu
$sheet->getStyle("A5:A{$lastRow}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$sheet->getStyle("B5:B{$lastRow}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$sheet->getStyle("C5:C{$lastRow}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$sheet->getStyle("D5:D{$lastRow}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
// Wrap text untuk semua column
$sheet->getStyle("A5:{$lastCol}{$lastRow}")->getAlignment()->setWrapText(true);
// Auto-fit semua column kecuali Marhun
foreach (range('A', $lastCol) as $col) {
if ($col !== 'D') { // skip Marhun
$sheet->getColumnDimension($col)->setAutoSize(true);
}
}
// Untuk column Marhun (D), kita boleh set width minimum supaya tak terlalu besar
$sheet->getColumnDimension('D')->setWidth(18); // boleh adjust ikut perlu
return [];
}
public function columnWidths(): array
{
return [
'A' => 5,
'B' => 20,
'C' => 18,
'D' => 18,
'E' => 12,
'F' => 15,
'G' => 18,
'H' => 18,
'I' => 18,
'J' => 12,
'K' => 15,
];
}
public function columnFormats(): array
{
return [
'E' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1,
'F' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1,
'G' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1,
'H' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1,
'I' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1,
];
}
}