180 lines
5.3 KiB
PHP
180 lines
5.3 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('Berat (g)'),
|
|
'Nilai Marhun' => $data->sum('Nilai Marhun'),
|
|
'Pinjaman' => $data->sum('Pinjaman'),
|
|
'Baki Pinjaman' => $data->sum('Baki Pinjaman'),
|
|
];
|
|
|
|
$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);
|
|
|
|
// Border untuk semua data
|
|
$lastRow = $sheet->getHighestRow();
|
|
$lastCol = 'K';
|
|
$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 for some columns
|
|
$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 berdasarkan content
|
|
foreach (range('A', $lastCol) as $col) {
|
|
$sheet->getColumnDimension($col)->setAutoSize(true);
|
|
}
|
|
|
|
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,
|
|
];
|
|
}
|
|
} |