diff --git a/app/Exports/LaporanKeseluruhan/AnsuranAllExportExcel.php b/app/Exports/LaporanKeseluruhan/AnsuranAllExportExcel.php new file mode 100644 index 0000000..579e0a2 --- /dev/null +++ b/app/Exports/LaporanKeseluruhan/AnsuranAllExportExcel.php @@ -0,0 +1,144 @@ +data = $data; + } + + public function headings(): array + { + return [ + ['Laporan Ansuran Keseluruhan'], // Title row + ['Bil', + 'Cawangan', + 'Bilangan Ansuran', + 'Nilai Marhun', + 'Bayaran Pembiayaan (RM)', + 'Bayaran Kos Keuntungan (RM)', + 'Jumlah (RM)', + ] + ]; + } + + public function array(): array + { + if (empty($this->data)) { + return []; + } + + $formattedData = []; + $bilangan_ansuran = 0; + $total_nilai_marhun = 0; + $bayaran_pembiayaan = 0; + $bayaran_kos_keuntungan = 0; + $jumlah_bayaran = 0; + $counter = 1; + + foreach ($this->data as $data) { + $formattedData[] = [ + $counter++, // Numbering (Integer) + $data['cawangan_detail'] ?? 'N/A', // Text + (int) ($data['bilangan_ansuran'] ?? 0), // Integer + (float) ($data['total_nilai_marhun'] ?? 0), // Decimal + (float) ($data['bayaran_pembiayaan'] ?? 0), // Decimal + (float) ($data['bayaran_kos_keuntungan'] ?? 0), // Decimal + (float) ($data['jumlah_bayaran'] ?? 0), // Decimal + ]; + + // Update totals + $bilangan_ansuran += (int) $data['bilangan_ansuran']; + $total_nilai_marhun += (float) $data['total_nilai_marhun']; + $bayaran_pembiayaan += (float) $data['bayaran_pembiayaan']; + $bayaran_kos_keuntungan += (float) $data['bayaran_kos_keuntungan']; + $jumlah_bayaran += (float) $data['jumlah_bayaran']; + } + + // Append total row (without numbering) + $formattedData[] = [ + '', // No numbering for total row + 'Jumlah Keseluruhan', + $bilangan_ansuran, + $total_nilai_marhun, + $bayaran_pembiayaan, + $bayaran_kos_keuntungan, + $jumlah_bayaran, + ]; + + return $formattedData; + } + + public function columnWidths(): array + { + return [ + 'A' => 5, // Bil + 'B' => 25, // Cawangan + 'C' => 20, // Bilangan Ansuran + 'D' => 18, // Nilai Marhun + 'E' => 20, // Bayaran Pembiayaan + 'F' => 20, // Bayaran Kos Keuntungan + 'G' => 20, // Jumlah + ]; + } + + public function columnFormats(): array + { + return [ + 'C' => NumberFormat::FORMAT_NUMBER, + 'D' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1, + 'E' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1, + 'F' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1, + 'G' => 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'] + ]); + } + ]; + } +} diff --git a/app/Http/Controllers/Reports/AnsuranController.php b/app/Http/Controllers/Reports/AnsuranController.php index d4a5f52..190ef24 100644 --- a/app/Http/Controllers/Reports/AnsuranController.php +++ b/app/Http/Controllers/Reports/AnsuranController.php @@ -9,12 +9,16 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Http; use Maatwebsite\Excel\Facades\Excel; +use App\Exports\LaporanKeseluruhan\AnsuranAllExportExcel; class AnsuranController extends Controller { public function index(Request $request) { $current = new Carbon(); + $request->hostname = config('api.url'); + $kod_cawangan = $request->kod_cawangan; + $action = $request->input('action', 'search'); $getReportDate = function ($date_string) use ($current) { if ($date_string == null) { @@ -37,6 +41,55 @@ class AnsuranController extends Controller $startDate = $getReportDate($request->startDate); $endDate = $getReportDate($request->endDate); + if ($action === 'excel') { + $file_title = sprintf("LaporanAnsuran %s (%s - %s).xlsx", $kod_cawangan, $request->startDate, $request->endDate); + return Excel::download(new AnsuranAllExportExcel($data), $file_title); + } + + return view('reports.ansuran.index', compact('summary_ansuran_by_cawangan', 'summary_ansuran', 'startDate', 'endDate')); + } + + public function show(Request $request) + { + $current = new Carbon(); + $action = $request->input('action', 'search'); + + $getReportDate = function ($date_string) use ($current) { + if ($date_string == null) { + return $current->toDateString(); + } + + return $date_string; + }; + + $hostname = config('api.url'); + + $response = Http::get($hostname . '/api/reports/ansuran', [ + 'startDate' => $getReportDate($request->startDate), + 'endDate' => $getReportDate($request->endDate), + ]); + + $ansuran = $response->json() ?? []; + if (empty($ansuran)) { + return back()->with('error', 'Failed to fetch data from API.'); + } + + $summary_ansuran_by_cawangan = $response['summary_ansuran_by_cawangan']; + $summary_ansuran = $response['summary_ansuran']; + + $startDate = $getReportDate($request->startDate); + $endDate = $getReportDate($request->endDate); + // dd($ansuran); + + if ($action === 'excel') { + + $exportData = $ansuran['summary_ansuran_by_cawangan']; + // Ensure $kod_cawangan is defined (Modify as per your data structure) + $kod_cawangan = $request->input('kod_cawangan', 'DEFAULT'); + + $file_title = sprintf("LaporanAnsuran %s (%s - %s).xlsx", $kod_cawangan, $startDate, $endDate); + return Excel::download(new AnsuranAllExportExcel($exportData), $file_title); + } return view('reports.ansuran.index', compact('summary_ansuran_by_cawangan', 'summary_ansuran', 'startDate', 'endDate')); } diff --git a/resources/views/reports/ansuran/index.blade.php b/resources/views/reports/ansuran/index.blade.php index 58ff99c..87e1d29 100644 --- a/resources/views/reports/ansuran/index.blade.php +++ b/resources/views/reports/ansuran/index.blade.php @@ -13,11 +13,11 @@
-
+
Laporan Ansuran Pembiayaan
-
+
@@ -34,9 +34,15 @@ - + +
@@ -72,6 +78,10 @@ class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider"> Bil Ansuran + + Nilai Marhun + Bayaran Pembiayaan @@ -127,6 +137,15 @@
+ +
+
+

+ {{ number_format($ansuran['total_nilai_marhun'], 2) }} +

+
+
+
@@ -189,6 +208,15 @@
+ +
+
+

+ {{ number_format($summary_ansuran['total_nilai_marhun'], 2) }} +

+
+
+
@@ -229,3 +257,11 @@
@endsection + + + diff --git a/routes/reports.php b/routes/reports.php index 669f380..76a5efe 100644 --- a/routes/reports.php +++ b/routes/reports.php @@ -23,4 +23,5 @@ Route::middleware(['roleChecker:admin,akaun,operasi,kpo, null'])->group(function Route::get('laporan-lelong', [LelonganController::class, 'index'])->name('reports.lelongan.index'); Route::get('lelongan/{kod_cawangan}', [LelonganController::class, 'downloadLelonganCawanganExcel'])->name('reports.lelongan.cawangan.excel'); Route::get('laporan-lelong/view', [LelonganController::class, 'show'])->name('reports.lelongan.show'); + Route::get('laporan-ansuran/view', [AnsuranController::class, 'show'])->name('reports.ansuran.show'); });