update stok audit audit

This commit is contained in:
Nur Izzati
2026-03-11 13:30:26 +08:00
parent b22436854c
commit 2ba1d47d33
3 changed files with 237 additions and 3 deletions
+172
View File
@@ -0,0 +1,172 @@
<?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);
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,
];
}
}
+44
View File
@@ -23,6 +23,7 @@ use Maatwebsite\Excel\Facades\Excel;
use App\Exports\PendSerExport;
use App\Exports\SAExport;
use App\Exports\SAcombineExport;
use App\Exports\SAExportAudit;
class AkaunController extends Controller
{
@@ -768,6 +769,49 @@ class AkaunController extends Controller
$cawangan_detail['namacaw']// ✅ Use the correct branch name from API
), 'laporan_stokaudit_' . $request->kodcaw . '_' . $tebus_date . '.xlsx');
}
elseif ($request->jenis == 'excelaudit') {
if ($request->radaktif === 'aktif') {
$stokaudit = Http::get($api_url . '/api/stokaudit/aktif/' . $request->kodcaw, [
'tarikh' => $tebus_date
])->json();
} else {
$stokaudit = Http::get($api_url . '/api/stokaudit/takaktif/' . $request->kodcaw, [
'tarikh' => $tebus_date
])->json();
}
$stokauditRecords = $stokaudit['rekod'] ?? [];
if (empty($stokauditRecords)) {
return back()->with('error', 'No records found to export.');
}
// pilih column untuk audit sahaja
$auditRecords = collect($stokauditRecords)->values()->map(function ($item, $index) {
return [
'Bil' => $index + 1,
'No Rujukan' => $item['norujukan'] ?? '',
'No KP' => $item['nokp'] ?? '',
'Marhun' => $item['marhun'] ?? '',
'Berat (g)' => $item['berat'] ?? '',
'Nilai Marhun' => $item['nilaimarhun'] ?? '',
'Pinjaman' => $item['pinjaman'] ?? '',
'Baki Pinjaman' => $item['bakipjm'] ?? '',
'Tempoh' => $item['tempoh'] ?? '',
];
});
return Excel::download(
new SAExportAudit(
$auditRecords,
'Laporan Stok Audit',
$tebus_date,
$cawangan_detail['namacaw']
),
'laporan_stokaudit_audit_' . $request->kodcaw . '_' . $tebus_date . '.xlsx'
);
}
else {
if ($request->radaktif === 'aktif') {
$stokaudit = Http::get($api_url . '/api/stokaudit/aktif/' . $request->kodcaw, ['tarikh' => $tebus_date])->json();