From 623db125e57e9acb8e4c6c67bb997c694dd2bc51 Mon Sep 17 00:00:00 2001 From: BTMS2 Date: Tue, 20 May 2025 12:37:19 +0800 Subject: [PATCH] tambah pelanggan aktif untuk admin portal --- .../Controllers/Reports/AnalisaController.php | 107 ++++++++++++++++++ routes/report.php | 2 + 2 files changed, 109 insertions(+) diff --git a/app/Http/Controllers/Reports/AnalisaController.php b/app/Http/Controllers/Reports/AnalisaController.php index b1ddf36..df7401b 100644 --- a/app/Http/Controllers/Reports/AnalisaController.php +++ b/app/Http/Controllers/Reports/AnalisaController.php @@ -5,6 +5,8 @@ namespace App\Http\Controllers\Reports; use App\Http\Controllers\Controller; use App\Cawangan; use App\Gadai; +use App\Customer; +use App\Tebus; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use App\Services\Reports\GadaianService as ReportGadaianService; @@ -61,5 +63,110 @@ class AnalisaController extends Controller return response()->json($response); } + // public function getSummaryLaporanPelangganAktif(Request $request) + // { + // $startDate = $request->startDate; + // $endDate = $request->endDate; + + // // Dapatkan senarai cawangan aktif + // $active_cawangan_db_connection = Helper::getActiveCawanganDbConnection(); + + // // Dapatkan maklumat cawangan + // $cawangan_available = Cawangan::select('kodcaw', 'details_caw', 'mysql') + // ->whereIn('mysql', $active_cawangan_db_connection) + // ->get(); + + // $active_cawangan_detail = $cawangan_available->map(function ($c) { + // return [ + // 'kodcaw' => $c->kodcaw, + // 'details_caw' => $c->details_caw, + // 'mysql' => $c->mysql, + // ]; + // })->toArray(); + + // $response = array_map(function ($cawangan) use ($startDate, $endDate) { + // $db_connection = $cawangan['mysql']; + + // $total_aktif = Gadai::on($db_connection) + // ->where(function ($query) use ($startDate) { + // $query->where('sttebus', '') + // ->where('t_update', '>=', $startDate); + // }) + // ->where('t_gadai', '<=', $endDate) + // ->distinct('nokp') + // ->count('nokp'); + + // return [ + // 'kodcaw' => $cawangan['kodcaw'], + // 'details_caw' => $cawangan['details_caw'], + // 'total_pelanggan_aktif' => $total_aktif, + // ]; + // }, $active_cawangan_detail); + + + // return response()->json($response); + // } + + +public function getSummaryLaporanPelangganAktif(Request $request) +{ + $startDate = $request->startDate; + $endDate = $request->endDate; + + $active_cawangan = Cawangan::select('kodcaw', 'details_caw', 'mysql') + ->whereIn('mysql', Helper::getActiveCawanganDbConnection()) + ->get(); + + $response = $active_cawangan->map(function ($cawangan) use ($startDate, $endDate) { + $db = $cawangan->mysql; + + // Ambil nokp dari Gadai + $nokp_gadai = DB::connection($db)->table('gadai') + ->whereBetween('t_gadai', [$startDate, $endDate]) + ->pluck('nokp') + ->toArray(); + + // Ambil nokp dari Tebus + $nokp_tebus = DB::connection($db)->table('tebus as t') + ->join('gadai as g', 't.norujukan', '=', 'g.norujukan') + ->whereBetween('t.t_tebus', [$startDate, $endDate]) + ->pluck('g.nokp') + ->toArray(); + + // Gabung semua nokp + $semua_nokp = collect(array_merge($nokp_gadai, $nokp_tebus)) + ->filter(fn($item) => !empty($item)) + ->unique() + ->values(); + + if ($semua_nokp->isEmpty()) { + // Jika array kosong, kita kembalikan hasil kosong + $pelanggan_aktif = collect(); + } else { + $pelanggan_aktif = Customer::on($db) + ->where(function ($query) use ($semua_nokp) { + $query->whereIn('kpbaru', $semua_nokp) + ->orWhereIn('kplama', $semua_nokp); + }) + ->get(['nama', 'kpbaru', 'kplama']) + ->toArray();; + } + + return [ + 'kodcaw' => $cawangan->kodcaw, + 'details_caw' => $cawangan->details_caw, + 'total_pelanggan_aktif' => count($semua_nokp), + ]; + }); + + return response()->json($response); +} + + + + + + + } diff --git a/routes/report.php b/routes/report.php index cf1206e..8fad3f0 100644 --- a/routes/report.php +++ b/routes/report.php @@ -41,5 +41,7 @@ $router->group(['prefix' => 'api/reports'], function () use ($router) { $router->get('petibesi', ['uses' => 'OutstandingController@getSummaryLaporanPetibesi']); $router->get('pelangganbaru', ['uses' => 'AnalisaController@getSummaryLaporanPelangganBaru']); + + $router->get('pelangganaktif', ['uses' => 'AnalisaController@getSummaryLaporanPelangganAktif']); }); });