From 96d70bf2a93f7e0fdbb2a94cc3a774660c55acf6 Mon Sep 17 00:00:00 2001 From: Ain Izazi Date: Mon, 2 Feb 2026 10:44:02 +0800 Subject: [PATCH] fix report gadaian v2 not sync with v3 --- app/Http/Controllers/GadaiController.php | 99 +++++++---- .../Miscellaneous/StatistikService.php | 165 ++++++++++++++++++ 2 files changed, 232 insertions(+), 32 deletions(-) create mode 100644 app/Services/Miscellaneous/StatistikService.php diff --git a/app/Http/Controllers/GadaiController.php b/app/Http/Controllers/GadaiController.php index 71f3863..942cc85 100644 --- a/app/Http/Controllers/GadaiController.php +++ b/app/Http/Controllers/GadaiController.php @@ -29,6 +29,7 @@ use App\KomuditiTransaksi; use App\Advansur; use App\HistoryEmasSandaran; use App\InOutMar; +use App\Services\Miscellaneous\StatistikService; use Illuminate\Http\Request; @@ -587,44 +588,78 @@ class GadaiController extends Controller public function getLaporanGadaiRangeDate($kodcaw,Request $request){ - $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first(); + $mula = $request->mula; + $akhir = $request->akhir; - $gadai_data = Gadai::on($cawangan['mysql'])->select('t_gadai','norujukan','nokp','semakbarcode','masa','berat','nilaimarhun','pinjaman','upah12','percentpinj','teller','nokp','ujrah','onepercent','tagbaru') - ->where('t_gadai','>=',$request->mula) - ->where('t_gadai','<=',$request->akhir) - ->orderBy('t_gadai') - ->orderBy('teller') - ->orderBy('sirig') - ->get(); + $cawangan = Cawangan::on('mysql7')->where('kodcaw', $kodcaw)->first(); - $gadai = []; + $gadai_data = Gadai::on($cawangan['mysql']) + ->whereBetween('t_gadai', [$request->mula, $request->akhir]) + ->orderBy('t_gadai') + ->orderBy('teller') + ->orderBy('sirig') + ->get(); - if($gadai_data){ - foreach($gadai_data as $index=>$gadaiData){ - $customer_info = Customer::on('mysql7')->where('kplama','=',$gadaiData['nokp'])->orWhere('kpbaru','=',$gadaiData['nokp'])->first(); - array_push($gadai,[ - "t_gadai"=> $gadaiData['t_gadai'], - "norujukan"=> $gadaiData['norujukan'], - "nama"=> $customer_info['nama'], - "nokp"=> $gadaiData['nokp'], - "semakbarcode"=> $gadaiData['semakbarcode'], - "masa"=> $gadaiData['masa'], - "berat"=> $gadaiData['berat'], - "nilaimarhun"=> $gadaiData['nilaimarhun'], - "pinjaman"=> $gadaiData['pinjaman'], - "upah12"=> $gadaiData['upah12'], - "percentpinj"=> $gadaiData['percentpinj'], - "alamat1"=> $customer_info['alamat1'], - "teller"=> $gadaiData['teller'], - "ujrah"=> $gadaiData['ujrah'], - "onepercent"=> $gadaiData['onepercent'], - "tagbaru"=> $gadaiData['tagbaru'] + $nokps = $gadai_data->pluck('nokp')->unique(); - ]); + $customers = Customer::on('mysql7') + ->where(function($query) use ($nokps) { + $query->whereIn('kpbaru', $nokps) + ->orWhereIn('kplama', $nokps); + }) + ->get() + ->flatMap(fn($c) => [ + $c->kpbaru => $c, + $c->kplama => $c + ]); + $gadai = $gadai_data->map(function ($g) use ($customers) { + + foreach($customers as $key=>$value){ + if($g->nokp == $value->kpbaru || $g->nokp == $value->kplama){ + $customer = $value; + } } - } - return response()->json($gadai); + return [ + + "nama" => optional($customer)->nama, + "alamat1" => optional($customer)->alamat1, + "t_gadai" => $g->t_gadai, + "norujukan" => $g->norujukan, + "norujukan"=> $g->norujukan, + + "nokp"=> $g->nokp, + "semakbarcode"=> $g->semakbarcode, + "masa"=> $g->masa, + "berat"=> $g->berat, + "nilaimarhun"=> $g->nilaimarhun, + "pinjaman"=> $g->pinjaman, + "upah12"=> $g->upah12, + "percentpinj"=> $g->percentpinj, + "alamat1"=> optional($customer)->alamat1, + "teller"=> $g->teller, + "ujrah"=> $g->ujrah, + "onepercent"=> $g->onepercent, + "tagbaru"=> $g->tagbaru, + "kodkerja"=> optional($customer)->kodkerja, + "bangsa"=> optional($customer)->bangsa, + + ]; + }); + // dd($gadai); + + $statistikService = new StatistikService($kodcaw,$mula,$akhir); + $reportgadai = $statistikService->LaporanStatistik($gadai); + // dd($nokps); + + return response()->json([ + 'gadai' => $gadai, + 'kodcaw' => $kodcaw, + 'mula' => $mula, + 'akhir' => $akhir, + 'statistik' => $reportgadai, + 'jumlah' => $gadai->count(), + ]); } public function getLaporanBatalGadai($kodcaw,$tarikh){ diff --git a/app/Services/Miscellaneous/StatistikService.php b/app/Services/Miscellaneous/StatistikService.php new file mode 100644 index 0000000..3e251b4 --- /dev/null +++ b/app/Services/Miscellaneous/StatistikService.php @@ -0,0 +1,165 @@ +kodcaw = $kodcaw; + $this->mysql = Cawangan::on('mysql7')->where('kodcaw', $kodcaw)->value('mysql'); + $this->mula = $mula; + $this->akhir = $akhir; + } + + public function LaporanStatistik($gadai) : array + { + $reportPembiayaan = $this->countPawnByType($gadai); + $reportLoanAmount = $this->countPawnByLoan($gadai); + $reportMarhunValue = $this->countMarhunByValue($gadai); + $reportMarhunMargin = $this->countMarhunByMargin($gadai); + $reportCustomerJob = $this->countCustomerByJob($gadai); + $reportCustomerRace = $this->countCustomerByRace($gadai); + // dd($reportCustomerRace); + + return [ + 'reportPembiayaan' => $reportPembiayaan, + 'reportLoanAmount' => $reportLoanAmount, + 'reportMarhunValue' => $reportMarhunValue, + 'reportMarhunMargin' => $reportMarhunMargin, + 'reportCustomerJob' => $reportCustomerJob, + 'reportCustomerRace' => $reportCustomerRace, + ]; + } + + private function countPawnByType($gadai) + { + $pembiayaanbaru = Tebus::on($this->mysql) + ->join('gadai as g1', 'tebus.norujukan', '=', 'g1.norujukan') + ->join('gadai as g2', function ($join) { + $join->on('g2.nokp', '=', 'g1.nokp') + ->on('g2.t_gadai', '=', 'tebus.t_tebus') + ->whereRaw('ABS(g2.berat - g1.berat) <= 0.02') + ->whereColumn('g2.norujukan', '!=', 'g1.norujukan'); + }) + ->whereBetween('tebus.t_tebus', [$this->mula, $this->akhir]) + ->groupBy('tebus.norujukan') + ->selectRaw(" + tebus.norujukan as norujukan_tebus, + MAX(g2.norujukan) as norujukanpembiayaansemula, + MAX(g1.taglel) as taglel_lama + ") + ->get() + ->toArray(); + + $pembiayaanbaruNorujukan = array_column($pembiayaanbaru, 'norujukanpembiayaansemula'); + $filteredNorujukanLelongan = array_column( + array_filter($pembiayaanbaru, function ($item) { + return $item['taglel_lama'] == 1; + }), + 'norujukanpembiayaansemula' + ); + $filteredPSNorujukan = array_column( + array_filter($pembiayaanbaru, function ($item) { + return $item['taglel_lama'] == 0; + }), + 'norujukanpembiayaansemula' + ); + + + $pembiayaanbaru = $gadai->whereIn('norujukan', $filteredPSNorujukan)->count(); + $pembiayaansemula = $gadai->whereNotIn('norujukan', $pembiayaanbaruNorujukan)->count(); + $lelongan = $gadai->whereIn('norujukan', $filteredNorujukanLelongan)->count(); + + return [ + 'pembiayaanbaru' => $pembiayaanbaru, + 'pembiayaansemula' => $pembiayaansemula, + 'lelongan' => $lelongan + ]; + } + + private function countPawnByLoan($gadai) + { + return collect($gadai)->countBy(function ($item) { + $loan = $item['pinjaman']; + + if ($loan <= 400.99) return '< 400.99'; + if ($loan >= 401 && $loan <= 2000) return '401 - 2000'; + if ($loan >= 2001 && $loan <= 24999) return '2001 - 2499'; + if ($loan >= 2500) return '>= 2500'; + if ($loan > 101000) return '>101000'; + })->toArray(); + } + + private function countMarhunByValue($gadai) + { + return collect($gadai)->countBy(function ($item) { + $nilaimarhun = $item['nilaimarhun']; + + if ($nilaimarhun <= 400.99) return '< 1-400'; + if ($nilaimarhun >= 401 && $nilaimarhun <= 2000) return '401-2000'; + if ($nilaimarhun > 2001) return '> 2001'; + })->toArray(); + } + + private function countMarhunByMargin($gadai) + { + return collect($gadai)->countBy(function ($item) { + $margin= $item['percentpinj']; + + if ($margin >= 10 && $margin <= 40.99) return '10-40.99'; + if ($margin >= 41 && $margin <= 60.99) return '41-60.99'; + if ($margin >= 61 && $margin <= 80) return '> 61-80'; + })->toArray(); + } + + private function countCustomerByJob($gadai) + { + $jobMap = [ + 1 => 'Peniaga', + 2 => 'Pekerja Swasta', + 3 => 'Pekerja Kerajaan', + 4 => 'Bekerja Sendiri', + 5 => 'Suri Rumah', + 6 => 'Lain-lain', + ]; + + return collect($gadai)->countBy(function ($item) use ($jobMap) { + $kodkerja = floatval($item['kodkerja']); + return $jobMap[$kodkerja] ?? 'Lain-lain'; + })->toArray(); + + } + + private function countCustomerByRace($gadai){ + + $raceMap = [ + 'M' => 'Melayu', + 'C' => 'Cina', + 'I' => 'India', + 'L' => 'Lain-lain', + ]; + + return collect($gadai)->countBy(function ($item) use ($raceMap) { + return $raceMap[$item['bangsa']] ?? 'Lain-lain'; + + })->toArray(); + + } +}