diff --git a/app/Http/Controllers/AdminPageController.php b/app/Http/Controllers/AdminPageController.php index bb36bc1..64049a1 100644 --- a/app/Http/Controllers/AdminPageController.php +++ b/app/Http/Controllers/AdminPageController.php @@ -30,6 +30,7 @@ use App\Status; use App\LookupStatus; use App\ARFiles; use App\WTDDraft; +use App\WTDSimulasi; use App\SerahBaki; use App\WTD; use App\StokAudit1; @@ -62,7 +63,6 @@ use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use App\Jobs\AccountCheckJob; -use App\Penyelarasan; use Illuminate\Support\Str; use Illuminate\Support\Facades\Cache; @@ -2365,7 +2365,6 @@ class AdminPageController extends Controller $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$request->kodcaw)->first(); //$latestbatch = SerahBaki::on($cawangan['mysql'])->where('norujukan','baru')->get()->toArray(); - //new added $serahBaki = SerahBaki::on($cawangan['mysql']) ->where('norujukan', 'baru') @@ -2374,28 +2373,28 @@ class AdminPageController extends Controller $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(); - }); + ->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; @@ -2427,29 +2426,27 @@ class AdminPageController extends Controller $finalDataArray = $finalData->toArray(); //end new added - + $batch_old = WTDDraft::on('mysql7')->select('kodcaw')->where('batchno_serah',0)->where('kodcaw',$request->kodcaw) ->groupBy('kodcaw')->get(); $batch_old_noruj = WTDDraft::on('mysql7')->where('batchno_serah',0)->where('kodcaw',$request->kodcaw)->pluck('norujukan'); $lelong_data = Lelong::on($cawangan['mysql'])->whereIn('norujukan',$batch_old_noruj)->get(); - + foreach($batch_old as $index=>$bo) { //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($finalDataArray,8); - //dd($data); - return response()->json($data,200); } - public function rekodWTDByBatchNo($batchno,Request $request){ $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$request->kodcaw)->first(); @@ -2727,7 +2724,7 @@ class AdminPageController extends Controller 'prkod' => "01", 'teller' => "DM", 'bakipjm' => ($lel['bakipjm']) ? $lel['bakipjm'] : 0, - 'bakiupah' => ($lel['bakiupah']) ? $lel['bakiupah'] : 0, + 'bakiupah' => $lel['bakiupah'], 'cajlain' => '0.00', 'cajlelong' => $lel['cajlelong'], 'kodgl' => '', @@ -2749,7 +2746,7 @@ class AdminPageController extends Controller 't_jual' => $lel['t_jual'], 'hargajual' => $lel['hargajual'], 'pinjaman' => $lel['pinjaman'], - 'bakiupah' => ($lel['bakiupah']) ? $lel['bakiupah'] : 0, + 'bakiupah' => $lel['bakiupah'], 'cajlelong' => $lel['cajlelong'], 'gst_cl' => $lel['gst_cl'], 'baki' => $lel['baki'], @@ -2779,222 +2776,6 @@ 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(); @@ -3445,8 +3226,6 @@ class AdminPageController extends Controller return response()->json($customers); } - - public function AkruKeuntunganSebenar(Request $request){ @@ -4045,13 +3824,13 @@ class AdminPageController extends Controller $get_imbangduga = imbangdugabackup::on($cawangan['mysql'])->where('tarikh', $lastDayOfYear)->first(); $get_imbangduga = optional($get_imbangduga)->toArray() ?? []; - if(empty($get_imbangduga)){ - $requestData = new Request([ - 'tarikh' => $request->tarikh, - 'kodcaw' => $kodcaw, - ]); - $exception_case = $this->AkruKeuntunganSebenar($requestData)->getData(); - } + // if(empty($get_imbangduga)){ + // $requestData = new Request([ + // 'tarikh' => $request->tarikh, + // 'kodcaw' => $kodcaw, + // ]); + // $exception_case = $this->AkruKeuntunganSebenar($requestData); + // } $backdate_keun_sebenar += isset($get_imbangduga['keun_sebenar']) ? $get_imbangduga['keun_sebenar'] : 0; $backdate_keun_sebenar += (float) ($exception_case ?? 0); @@ -4070,14 +3849,7 @@ class AdminPageController extends Controller $total = ($utgrugilama + $lelong + $upahsimpantebus + $upahsimpanlelong + $upahsimpanansur + $gstcajlelong + $keuntungansebenar + $upahsimpankeuntungansebelumtawarruq + $available_akru + $total_keuntungan_sebenar + $akru_ujrah + $akru_1percent) + $parameter; if($kodcaw == '011229') - $total -= 45390.37; - - if($kodcaw == 'KB2') - $total -= 64.8; - - $total += Penyelarasan::on('mysql7') - ->where('kodcaw', $kodcaw) - ->sum(DB::raw('rugilelong + bakilelong + `lain-lain`')); + $total -= 45390.37; return response()->json($total,200); } @@ -4250,4 +4022,222 @@ class AdminPageController extends Controller 'result' => $status === 'completed' ? $result : null, ]); } + + //function wtd baru + 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!']); + + } }