From 1f27d90698c2acdb7a2d3b47671cee5a0f225a10 Mon Sep 17 00:00:00 2001 From: aiman Date: Sun, 14 Sep 2025 16:03:49 +0800 Subject: [PATCH] Tukar step pampasan v3 ke v2: --- app/Http/Controllers/BarangkesController.php | 88 +++++++++++++++----- config/app_parameter.php | 4 +- 2 files changed, 69 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/BarangkesController.php b/app/Http/Controllers/BarangkesController.php index dd4f10c..8e37309 100644 --- a/app/Http/Controllers/BarangkesController.php +++ b/app/Http/Controllers/BarangkesController.php @@ -10,34 +10,78 @@ use Illuminate\Http\Request; use Carbon\Carbon; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Http; class BarangkesController extends Controller { public function LaporanBarangKes(Request $request) { + $status = $request->status; - $cawangan = Cawangan::on('mysql7')->where('kodcaw', $request->kodcaw)->first(); + $cawangan = Cawangan::on('mysql7')->where('kodcaw', $request->kodcaw)->first(); - if (!$cawangan) { - return response()->json(['error' => 'Invalid kodcaw'], 403); + if (!$cawangan) { + return response()->json(['error' => 'Invalid kodcaw'], 403); + } + + $query = DB::connection($cawangan['mysql'])->table('palsu') + ->select([ + 'tarikh', + 'norujukan', + 'nota', + 'pembiayaan', + 'nilaimarhun', + 'jbg', + 'untung', + 'untungonepercent', + 'untungcajlain' + ]); + + // Only apply whereBetween if tarikh_mula and tarikh_akhir are provided + if ($request->filled('tarikh_mula') && $request->filled('tarikh_akhir')) { + $query->whereBetween('tarikh', [$request->tarikh_mula, $request->tarikh_akhir]); + } + + $result = $query->get(); + + $pampasan = Http::get(config('app_parameter.api_v3') . "pampasan", ['kodcaw' => $request->kodcaw]) + ->json()['data']; + + $palsu_array = $result->toArray(); + + // dd($pampasan, $palsu_array); + + $indexedArray2 = collect($pampasan)->keyBy('norujukan'); + + $merged = collect($palsu_array)->map(function ($item) use ($indexedArray2) { + $id = $item->norujukan; + $itemArray = (array) $item; // Convert object to array + $additional = $indexedArray2->get($id, []); + return array_merge($itemArray, $additional); + })->values()->toArray(); + + // dd($merged, $status); + + $filteredResults = []; + + if ($status == 'dituntut') { + foreach ($merged as $pampasan) { + if (isset($pampasan['status']) && $pampasan['status'] == 1) { + $filteredResults[] = $pampasan; + } + } + } + if ($status == 'belum-tuntut') { + foreach ($merged as $pampasan) { + if (!isset($pampasan['status']) ) { + $filteredResults[] = $pampasan; + } + } + } + if ($status == 'all') { + $filteredResults = $merged; + } + + return response()->json($filteredResults); } - - $query = DB::connection($cawangan['mysql'])->table('palsu') - ->select([ - 'tarikh', - 'norujukan', - 'nota', - 'pembiayaan', - 'nilaimarhun', - 'jbg', - 'untung', - 'untungonepercent', - 'untungcajlain' - ]) - ->whereBetween('tarikh', [$request->tarikh_mula, $request->tarikh_akhir]); - - $result = $query->get(); - - return response()->json($result); } -} \ No newline at end of file diff --git a/config/app_parameter.php b/config/app_parameter.php index a6cf4d8..c46f175 100644 --- a/config/app_parameter.php +++ b/config/app_parameter.php @@ -2,5 +2,7 @@ return [ 'url' => env('APP_URL', url('/')), - 'api_arrahnbid' => env('API_ARRAHNBID', 'http://lelong.test/api/') + 'api_arrahnbid' => env('API_ARRAHNBID', 'http://lelong.test/api/'), + 'api_v3' => env('API_V3', 'http://apiv3.localhost/api/') ]; +