diff --git a/app/Http/Controllers/Reports/AnalisaController.php b/app/Http/Controllers/Reports/AnalisaController.php new file mode 100644 index 0000000..ebf5b75 --- /dev/null +++ b/app/Http/Controllers/Reports/AnalisaController.php @@ -0,0 +1,70 @@ +getCawanganReport(); + } + + public function getSummaryLaporanPelangganBaru(Request $request) + { + $startDate = $request->startDate; + $endDate = $request->endDate; + + // Get active branch DBs from helper + $active_cawangan_db_connection = Helper::getActiveCawanganDbConnection(); + + // Fetch branch details with mysql info + $cawangan_available = Cawangan::select('kodcaw', 'details_caw', 'mysql') + ->whereIn('mysql', $active_cawangan_db_connection) + ->get(); + + $active_cawangan_db_connection = array_column($cawangan_available->toArray(), 'mysql'); + + $active_cawangan_detail = $cawangan_available->map(function ($c) { + return [ + 'kodcaw' => $c->kodcaw, + 'details_caw' => $c->details_caw, + ]; + })->values()->toArray(); + + $summary_gadaian_data = array_map(function ($db_connection) use ($startDate, $endDate) { + $query = Gadai::on($db_connection) + ->select( + DB::raw('count(DISTINCT nokp) as total_pelanggan_baru'), + DB::raw('sum(berat) as sum_berat'), + DB::raw('sum(nilaimarhun) as sum_nilai_marhun'), + DB::raw('sum(pinjaman) as sum_pinjaman') + ) + ->whereIn('nokp', function ($sub) use ($startDate, $endDate) { + $sub->select('nokp') + ->from('gadai as g2') + ->groupBy('nokp') + ->havingRaw('MIN(t_gadai) BETWEEN ? AND ?', [$startDate, $endDate]); + }); + + return $query->get()->toArray(); + }, $active_cawangan_db_connection); + + $response = array_map(function ($cawangan_detail, $summary_gadaian_data) { + return array_merge($cawangan_detail, $summary_gadaian_data[0]); + }, $active_cawangan_detail, $summary_gadaian_data); + + return response()->json($response); + } + + +} diff --git a/routes/report.php b/routes/report.php index 06d4920..cf1206e 100644 --- a/routes/report.php +++ b/routes/report.php @@ -39,5 +39,7 @@ $router->group(['prefix' => 'api/reports'], function () use ($router) { $router->get('outstanding', ['uses' => 'OutstandingController@getSummaryLaporanOutstanding']); $router->get('petibesi', ['uses' => 'OutstandingController@getSummaryLaporanPetibesi']); + + $router->get('pelangganbaru', ['uses' => 'AnalisaController@getSummaryLaporanPelangganBaru']); }); });