diff --git a/app/Http/Controllers/AdminPageController.php b/app/Http/Controllers/AdminPageController.php index 4f59be5..ac61471 100644 --- a/app/Http/Controllers/AdminPageController.php +++ b/app/Http/Controllers/AdminPageController.php @@ -2364,7 +2364,70 @@ class AdminPageController extends Controller $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$request->kodcaw)->first(); - $latestbatch = SerahBaki::on($cawangan['mysql'])->where('norujukan','baru')->get()->toArray(); + //$latestbatch = SerahBaki::on($cawangan['mysql'])->where('norujukan','baru')->get()->toArray(); + + //new added + $serahBaki = SerahBaki::on($cawangan['mysql']) + ->where('norujukan', 'baru') + ->get(); + + $allBatchNo = $serahBaki->pluck('batchno')->unique()->toArray(); + + $wtddraftData = WTDDraft::on('mysql7') + ->selectRaw(" + batchno_serah, + YEAR(t_jual) as tahun, + GROUP_CONCAT( + DISTINCT MONTH(t_jual) + ORDER BY MONTH(t_jual) + SEPARATOR ',' + ) as bulan + ") + ->where('kodcaw', $request->kodcaw) + ->whereIn('batchno_serah', $allBatchNo) + ->groupBy('batchno_serah', DB::raw('YEAR(t_jual)')) + ->get() + ->groupBy('batchno_serah') + ->map(function ($rows) { + return $rows->map(function ($row) { + return [ + 'tahun' => $row->tahun, + 'bulan' => $row->bulan + ]; + })->values(); + }); + + $finalData = $serahBaki->map(function ($item) use ($wtddraftData) { + $batchno = $item->batchno; + + $tahun = null; + $bulan = []; + + if (!empty($wtddraftData[$batchno])) { + // ambil tahun pertama (kebiasaannya satu tahun sahaja) + $tahun = $wtddraftData[$batchno][0]['tahun']; + + // convert string "2,3,4,5" → array + $bulan = explode(',', $wtddraftData[$batchno][0]['bulan']); + } + + return [ + 'batchno' => $batchno, + 'norujukan' => $item->norujukan, + 'tarikh' => $item->tarikh, + 't_jual' => $item->t_jual, + 'baki' => $item->baki, + 't_htr_ag' => $item->t_htr_ag, + 'aktif' => $item->aktif, + 'tahun' => $tahun, + 'bulan' => implode(',',$bulan), + // field lain kekal + ]; + }); + + $finalDataArray = $finalData->toArray(); + //end new added + $batch_old = WTDDraft::on('mysql7')->select('kodcaw')->where('batchno_serah',0)->where('kodcaw',$request->kodcaw) ->groupBy('kodcaw')->get(); @@ -2373,15 +2436,20 @@ class AdminPageController extends Controller foreach($batch_old as $index=>$bo) { - array_push($latestbatch,['recno' => 0,'norujukan' => 'lama','batchno' => 0,'baki' => $lelong_data->sum('baki'),'aktif' => 1]); + //array_push($latestbatch,['recno' => 0,'norujukan' => 'lama','batchno' => 0,'baki' => $lelong_data->sum('baki'),'aktif' => 1]); + array_push($finalDataArray,['recno' => 0,'norujukan' => 'lama','batchno' => 0,'baki' => $lelong_data->sum('baki'),'aktif' => 1]); } - $data = $this->paginate($latestbatch,8); + //$data = $this->paginate($latestbatch,8); + $data = $this->paginate($finalDataArray,8); + + //dd($data); return response()->json($data,200); } + public function rekodWTDByBatchNo($batchno,Request $request){ $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$request->kodcaw)->first(); @@ -2698,8 +2766,9 @@ class AdminPageController extends Controller 't_update' => $current->toDateString())); } - $serahbakiupdate = SerahBaki::on($cawangan['mysql'])->where('batchno',$batchno)->update(array('aktif' => 1,'t_jual' => $current->toDateString(),'nopelanggan' => $request->nocek));; - + //$serahbakiupdate = SerahBaki::on($cawangan['mysql'])->where('batchno',$batchno)->update(array('aktif' => 1,'t_jual' => $current->toDateString(),'nopelanggan' => $request->nocek));; + $serahbakiupdate = SerahBaki::on($cawangan['mysql'])->where('batchno',$batchno)->update(array('aktif' => 1,'t_jual' => $current->toDateString(),'nopelanggan' => $request->nocek,'t_htr_ag' => $request->tarikhAG)); + return response()->json('success',200); }catch (\Throwable $e){ @@ -2710,6 +2779,222 @@ class AdminPageController extends Controller } + public function janaSimulasiWTD(Request $request) + { + $cawangan = Cawangan::on('mysql7')->where('kodcaw', '=', $request->kodcaw)->first(); + + //listing sag belum tuntut cwgn + $sag_cwgn = collect( + Lelong::on($cawangan['mysql']) + ->selectRaw('norujukan') + ->whereYear('t_jual', $request->tahun) + ->whereNull('trkhtuntut') + ->where('baki', '>', 0) + ->pluck('norujukan') // lebih bersih + ); + + //listing sag blocked + $sag_online = collect( + WTDDraft::on('mysql7') + ->selectRaw('norujukan') + ->where('kodcaw', '=', $request->kodcaw) + ->pluck('norujukan') + ); + //dd($sag_online); + //filter sag yg tk wujud dlm wtd_draft + $filtersag = $sag_cwgn->diff($sag_online); + + //query baru + $tarikh_belum_tuntut = Lelong::on($cawangan['mysql']) + ->selectRaw(' + MONTH(t_jual) as bulan, + SUM(pinjaman) as pembiayaan, + ABS(SUM(baki)) as baki, + COUNT(DISTINCT norujukan) as jumlah_norujukan, + kodcaw + ') + ->whereIn('norujukan', $filtersag) + ->groupBy('bulan', 'kodcaw') + ->orderBy('bulan') + ->get(); + + $tarikh_belum_tuntut = $tarikh_belum_tuntut->map(function ($item) { + $item->bulan = \Carbon\Carbon::create()->month($item->bulan)->format('F'); + return $item; + }); + + return response()->json($tarikh_belum_tuntut); + } + + public function RekodSimulasiWTD(Request $request) + { + $cawangan = Cawangan::on('mysql7')->where('kodcaw', '=', $request->kodcaw)->first(); + + $rekod_simulasi = Lelong::on($cawangan['mysql']) + ->join('gadai','lelong.norujukan','=','gadai.norujukan') + ->selectRaw('ABS(lelong.baki) as baki, lelong.t_lelong, lelong.pinjaman, lelong.norujukan, gadai.nokp') + ->whereMonth('lelong.t_jual', $request->month) + ->whereYear('lelong.t_jual', $request->year) + ->whereNull('lelong.trkhtuntut') + ->where('lelong.baki', '>', 0) + ->get(); + + return response()->json($rekod_simulasi); + } + + public function prosesSimulasiWTD(Request $request) + { + $proses = $request->input; + + // dd($proses, $cawangan); + + $baki = 0; + $jumlahPembiayaan = 0; + $simulasi = WTDSimulasi::max('simulasi')+1; + + foreach ($proses as $item) { + WTDSimulasi::create([ + 'bulan' => $item['bulan'], + 'tahun' => $item['tahun'], + 'kodcaw' => $item['kodcaw'], + 'jumlah_sag' => $item['jumlah_sag'], + 'pembiayaan'=> $item['pembiayaan'], + 'baki'=> $item['baki'], + 'simulasi' => $simulasi, + ]); + $baki += $item['baki']; + $jumlahPembiayaan = $item['pembiayaan']; + } + + return response()->json(['status' => 'success','simulasi' => $simulasi,'baki' => $baki,'pinjaman' => $jumlahPembiayaan]); + } + + public function pengesahanSimulasiWTD(Request $request) + { + // Decode the JSON records from the request + $records = json_decode($request->records, true); + + // Retrieve and clean 'kodcaw' from the request + $kodcaw = $request->kodcaw; + $kodcaw_clean = str_replace('"', '', $kodcaw); + + // Get the database connection for the branch (cawangan) + $cawangan = Cawangan::where('kodcaw', $kodcaw_clean)->first(); + + // Check if 'Cawangan' exists + if (!$cawangan) { + return response()->json(['status' => 'error', 'message' => 'Invalid branch code (kodcaw).'], 404); + } + + $cawangan_connection = $cawangan->mysql; + + // Get the latest batch number and increment it + $latestBatch = SerahBaki::on($cawangan_connection)->max('batchno') + 1; + + // Initialize total variables + $totalPembiayaan = 0; + $totalBaki = 0; + + + try { + // Iterate through the records and save them to the WTDDraft table + foreach ($records as $record) { + // Check if 'bulan' and 'tahun' exist in the record + if (!isset($record['bulan']) || !isset($record['tahun'])) { + return response()->json(['status' => 'error', 'message' => 'Missing bulan or tahun in records.'], 400); + } + $month = DateTime::createFromFormat('F', $record["bulan"])->format('n'); + + + // Query norujukan records from the database + $query_norujukan = Lelong::on($cawangan_connection) + ->join('gadai', 'lelong.norujukan', '=', 'gadai.norujukan') + ->selectRaw('ABS(lelong.baki) as baki, lelong.t_lelong, lelong.pinjaman, lelong.norujukan, gadai.nokp, lelong.t_jual') + ->whereMonth('lelong.t_jual', $month) + ->whereYear('lelong.t_jual', $record["tahun"]) + ->whereNull('lelong.trkhtuntut') + ->where('lelong.baki', '>', 0) + ->get(); + + // Save each record to the WTDDraft table + foreach ($query_norujukan as $query) { + $wtddraft = new WTDDraft(); + $wtddraft->nokp = $query['nokp']; + $wtddraft->kodcaw = $kodcaw_clean; + $wtddraft->norujukan = $query['norujukan']; + $wtddraft->t_jual = $query['t_jual']; + $wtddraft->t_gadai = $query['t_gadai'] ?? null; // Optional: ensure 't_gadai' exists + $wtddraft->t_lelong = $query['t_lelong']; + $wtddraft->batchno_serah = $latestBatch; + $wtddraft->save(); + } + + // Accumulate totals + $totalPembiayaan += $record['pembiayaan'] ?? 0; + $totalBaki += $record['baki'] ?? 0; + } + + // Create a new record in the SerahBaki table + SerahBaki::on($cawangan_connection)->create([ + 'norujukan' => 'baru', + 'batchno' => $latestBatch, + 'hargajual' => 0.00, + 'pinjaman' => $totalPembiayaan, + 'upah' => 0.00, + 'cajlel' => 0.00, + 'gst_cl' => 0.00, + 'gst_hj' => 0.00, + 'baki' => $totalBaki, + 'tarikh' => Carbon::now()->toDateString(), + 't_jual' => Carbon::now()->toDateString(), + 'nopelanggan' => '', + 'aktif' => 0, + ]); + + // Return a success response with the new batch number + return response()->json(['status' => 'success', 'batchno' => $latestBatch], 200); + + } catch (\Throwable $e) { + // Log the error and return a failure response + Log::error($e->getMessage()); + Log::error($e->getLine()); + return response()->json(['status' => 'error', 'message' => $e->getMessage()], 500); + } + } + + + + public function simulasiToDraft(Request $request) + { + $bulan = $request->bulan; + + if (!$bulan || count($bulan) === 0) { + return response()->json(['error' => 'Sila pilih sekurang-kurangnya satu bulan.'], 400); + } + + $simulasi = WTDSimulasi::whereIn('bulan', $bulan)->get(); + + if ($simulasi->isEmpty()) { + return response()->json(['error' => 'Tiada data untuk bulan yang dipilih.'], 404); + } + + // foreach ($simulasi as $data) { + // WTDDraft::create([ + // 'nokp' => $data->nokp ?? null, + // 'kodcaw' => $data->kodcaw ?? null, + // 'norujukan' => $data->norujukan ?? null, + // 't_jual' => $data->t_jual ?? null, + // 't_gadai' => $data->t_gadai ?? null, + // 't_lelong' => $data->t_lelong ?? null, + // 'batchno_serah' => $data->simulasi, // Simpan sebagai rujukan + // ]); + // } + + dd($bulan, $simulasi); + + return response()->json(['message' => 'Data berjaya dipindahkan ke WTD Draft!']); + + } public function PenjanaanPC(Request $request){ $timeloni = Carbon::now(); diff --git a/app/WTDSimulasi.php b/app/WTDSimulasi.php new file mode 100644 index 0000000..38a963f --- /dev/null +++ b/app/WTDSimulasi.php @@ -0,0 +1,32 @@ +getPdo(); + return true; + } catch (\Exception $e) { + return false; + } + }); + } + + + public function up() + { + $db_connection = $this->getActiveDB(); + + $key = array_search('mysql7', $db_connection); + + if ($key !== false) { + unset($db_connection[$key]); + } + + foreach ($db_connection as $dbase) { + Schema::connection($dbase)->table('serahbaki', function (Blueprint $table) use ($dbase) { + + if (!Schema::connection($dbase)->hasColumn('serahbaki', 't_htr_ag')) { + $table->date('t_htr_ag')->nullable()->after('aktif'); + } + }); + } + } + + + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + $db_connection = $this->getActiveDB(); + + $key = array_search('mysql7', $db_connection); + + if ($key !== false) { + unset($db_connection[$key]); + } + + foreach ($db_connection as $dbase) { + if (Schema::connection($dbase)->hasColumn('serahbaki', 't_htr_ag')) { + Schema::connection($dbase)->table('serahbaki', function (Blueprint $table) { + $table->dropColumn('t_htr_ag'); + }); + } + } + } +} + diff --git a/database/migrations/2025_12_18_163018_create_wtd_simulasi_table.php b/database/migrations/2025_12_18_163018_create_wtd_simulasi_table.php new file mode 100644 index 0000000..214b2d7 --- /dev/null +++ b/database/migrations/2025_12_18_163018_create_wtd_simulasi_table.php @@ -0,0 +1,40 @@ +id(); + + $table->string('bulan'); + $table->string('tahun'); + $table->string('kodcaw'); + $table->integer('jumlah_sag'); + $table->decimal('pembiayaan', 15, 2); + $table->decimal('baki', 15, 2); + $table->integer('simulasi'); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('wtd_simulasi'); + } +} \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index bf0dfe3..dcafcdc 100644 --- a/routes/web.php +++ b/routes/web.php @@ -891,6 +891,17 @@ $router->group(['prefix'=>'admin'], function () use ($router){ $router->post('wtd/delete',['uses'=>'AdminPageController@RemoveDraftWTD']); $router->get('wtd/dalamproses',['uses'=>'AdminPageController@rekodWTDDalamProses']); + //Aiman Ngube WTD + $router->get('wtd/janaSimulasi',['uses'=>'AdminPageController@JanaSimulasiWTD']); + $router->get('wtd/rekodSimulasi',['uses'=>'AdminPageController@RekodSimulasiWTD']); + $router->post('wtd/prosesSimulasi',['uses'=>'AdminPageController@ProsesSimulasiWTD']); + $router->post('wtd/pengesahanSimulasi',['uses'=>'AdminPageController@PengesahanSimulasiWTD']); + + $router->post('wtd/simulasiToDraft',['uses'=>'AdminPageController@SimulasiToDraft']); + + $router->post('wtd/simulasiBaru',['uses'=>'AdminPageController@SimulasiBaru']); + + $router->get('audit/keuntungansebenar',['uses'=>'AdminPageController@KeuntunganSebenarExport']); $router->get('laporan/aging',['uses'=>'AdminPageController@LaporanAging']); $router->get('dashboard/aging',['uses'=>'AdminPageController@DashboardAging']);