diff --git a/app/Http/Controllers/Reports/LelonganController.php b/app/Http/Controllers/Reports/LelonganController.php index 0faa26f..bdc0f4d 100644 --- a/app/Http/Controllers/Reports/LelonganController.php +++ b/app/Http/Controllers/Reports/LelonganController.php @@ -85,4 +85,76 @@ class LelonganController extends Controller return json_encode($response); } +public function getSummaryLaporanBakiLelongBelumTuntut(Request $request) + { + $onDate = $request->onDate; + $startDate = $request->startDate; + $endDate = $request->endDate; + + // Get active branches in the order we want + $active_cawangan_db_connection = Helper::getActiveCawanganDbConnection(); + + // Preserve keys while mapping database names + $active_cawangan_db_name = array_combine( + $active_cawangan_db_connection, + array_map(function ($db_connection) { + return config('database.connections.' . $db_connection . '.database'); + }, $active_cawangan_db_connection) + ); + // dd($active_cawangan_db_name); + + // Retrieve branch details and re-index by db_name + $active_cawangan_detail = Cawangan::select('kodcaw', 'details_caw', 'db_name') + ->whereIn('db_name', array_values($active_cawangan_db_name)) + ->get() + ->keyBy('db_name') + ->toArray(); + // dd($active_cawangan_detail); + + // Retrieve lelong summary data and re-index by db_connection + $summary_lelongan_data = []; + foreach ($active_cawangan_db_connection as $db_connection) { + $query = Lelong::on($db_connection) + ->join('gadai', 'gadai.norujukan', '=', 'lelong.norujukan') + ->select(DB::raw('count(lelong.norujukan) as total_lelongan, + sum(lelong.berat) as sum_berat, + sum(lelong.nilaimarhun) as sum_nilai_marhun, + sum(gadai.pinjaman) as sum_pinjaman, + sum(lelong.pinjaman) as sum_bakipjm, + sum(lelong.bakiupah) as sum_bakiupah, + sum(lelong.cajlain) as sum_cajlain, + sum(lelong.cajlelong) as sum_cajlelong, + sum(lelong.hargajual) as sum_hargajual, + sum(lelong.baki) as sum_baki')) + ->whereNull('lelong.trkhtuntut'); + + + if ($request->filled('startDate') && $request->filled('endDate')) { + $query->whereBetween('lelong.t_lelong', [$request->startDate, $request->endDate]); + } + + $summary_lelongan_data[$db_connection] = $query->first()->toArray(); + } + + // Ensure data is sorted according to getActiveCawanganDbConnection() + $response = []; + foreach ($active_cawangan_db_connection as $db_connection) { + $db_name = $active_cawangan_db_name[$db_connection] ?? null; + if ($db_name && isset($active_cawangan_detail[$db_name])) { + $response[] = array_merge( + $active_cawangan_detail[$db_name], + $summary_lelongan_data[$db_connection] ?? [] + ); + } + } + + return json_encode($response); + } + + public function getLaporanBakiLelongBelumTuntutCawangan(string $kod_cawangan, Request $request) + { + $reportLelonganService = new ReportLelonganService($kod_cawangan, $request); + return $reportLelonganService->getCawanganReportBakiLelongBelumTuntut(); + } + } diff --git a/app/Services/Reports/LelonganService.php b/app/Services/Reports/LelonganService.php index 8df6d04..f64cad8 100644 --- a/app/Services/Reports/LelonganService.php +++ b/app/Services/Reports/LelonganService.php @@ -252,4 +252,96 @@ class LelonganService }, $marhun_lelongan); return $marhun_lelongan; } + + public function getCawanganReportBakiLelongBelumTuntut() + { + $cawangan_connection = Cawangan::where('kodcaw', $this->kod_cawangan)->first()->mysql; + + $query_total_lelongan = Lelong::on($cawangan_connection) + ->join('gadai', 'gadai.norujukan', '=', 'lelong.norujukan') // Joining gadai table + ->select( + 'lelong.t_lelong', + 'lelong.norujukan', + 'gadai.nokp', + 'gadai.masa', + 'lelong.berat', + 'lelong.nilaimarhun', + 'gadai.pinjaman', + 'gadai.percentpinj', + 'lelong.teller', + 'gadai.bakipjm', // Fetch pinjaman from gadai + 'gadai.marhun' // Fetch marhun from gadai + ) + ->whereNull('lelong.trkhtuntut'); + + $query_total_lelongan->when(!empty($this->startDate) and !empty($this->endDate), function ($q) { + return $q->where('lelong.t_lelong', '>=', $this->startDate) + ->where('lelong.t_lelong', '<=', $this->endDate); + }); + + $total_lelongan = $this->getTotalLelongan($query_total_lelongan); + // dd($total_lelongan); + $total_summary_lelongan_teller = $this->getTotalSummaryLelonganGroupByTeller($query_total_lelongan); + $total_summary_lelongan = $this->getTotalSummaryLelongan($query_total_lelongan); + $customer_info = $this->getCustomerInfo($total_lelongan); + $marhun_lelongan = $this->getMarhunLelongan($total_lelongan, $cawangan_connection); + $cawangan_detail = $this->getCawanganDetail($cawangan_connection); + + $total_lelongan = $total_lelongan->zip($customer_info, $marhun_lelongan)->map(function ($data) { + + [$lelong, $customer_info, $marhun_lelongan] = $data; + + return [ + 'no_rujukan' => $lelong['norujukan'], + 'nama' => $customer_info['nama'], + 'alamat' => $customer_info['alamat1'], + 'no_kp' => $lelong['nokp'], + 'masa' => $lelong['masa'], + 'marhun' => $marhun_lelongan, + 'berat' => $lelong['berat'], + 'nilai_marhun' => $lelong['nilaimarhun'], + 'pinjaman' => $lelong['pinjaman'], + 'peratusan_margin_pinjaman' => $lelong['percentpinj'], + 'bakipjm' => $lelong['bakipjm'], + 'keuntungan' => $lelong['bakiupah'], + 'cajlelong' => $lelong['cajlelong'], + 'hargajual' => $lelong['hargajual'], + 'baki' => $lelong['baki'], + 'teller' => $lelong['teller'], + 't_lelong' => $lelong['t_lelong'], + ]; + }); + + + $total_lelongan = $total_lelongan->groupBy('teller'); + + $lelongan_tellers = $total_lelongan->keys(); + + $total_lelongan = $total_lelongan + ->zip($total_summary_lelongan_teller) + ->map(function ($data) { + + [$lelongan, $summary] = $data; + + return [ + 'lelongan' => $lelongan, + 'summary_lelongan_teller' => $summary, + ]; + }); + + $total_lelongan = $lelongan_tellers->combine($total_lelongan); + + $data = [ + 'query' => [ + 'startDate' => Helper::formatDate($this->startDate), + 'endDate' => Helper::formatDate($this->endDate), + ], + 'cawangan_detail' => $cawangan_detail, + 'lelongan' => $total_lelongan, + 'summary_lelongan' => $total_summary_lelongan, + ]; + + return json_encode($data); + + } } diff --git a/routes/report.php b/routes/report.php index 8fad3f0..783e100 100644 --- a/routes/report.php +++ b/routes/report.php @@ -43,5 +43,9 @@ $router->group(['prefix' => 'api/reports'], function () use ($router) { $router->get('pelangganbaru', ['uses' => 'AnalisaController@getSummaryLaporanPelangganBaru']); $router->get('pelangganaktif', ['uses' => 'AnalisaController@getSummaryLaporanPelangganAktif']); + + $router->get('bakilelongbelumtuntut', ['uses' => 'LelonganController@getSummaryLaporanBakiLelongBelumTuntut']); + $router->get('bakilelongbelumtuntut/{kod_cawangan}', ['uses' => 'LelonganController@getLaporanBakiLelongBelumTuntutCawangan']); + }); });