diff --git a/app/Http/Controllers/LelongController.php b/app/Http/Controllers/LelongController.php index 909391c..ce7709e 100644 --- a/app/Http/Controllers/LelongController.php +++ b/app/Http/Controllers/LelongController.php @@ -1069,44 +1069,79 @@ class LelongController extends Controller public function exportLelong(Request $request) { - $currentDate = Carbon::now(); - $currentDate = $currentDate->toDateString(); + $currentDate = Carbon::now()->toDateString(); - $cawangan = Cawangan::on('mysql7')->where('kodcaw', $request->kodcaw)->first(); + // --- Step 0: Dapatkan Cawangan --- + $cawangan = Cawangan::on('mysql7') + ->where('kodcaw', $request->kodcaw) + ->first(); - $batchlel = BatchLel::on($cawangan['mysql']) - ->select('batchno') - ->orderBy('batchno', 'desc') - ->first(); + if (!$cawangan) { + Log::error("Export Lelong gagal: cawangan tidak ditemui", ['kodcaw' => $request->kodcaw]); + return response()->json(['error' => 'Cawangan tidak ditemui'], 404); + } - $newBatchNo = $batchlel->batchno + 1; - - $newBatchLel = BatchLel::on($cawangan['mysql'])->create([ - 'kodcaw' => $request->kodcaw, - 'batchno' => $newBatchNo, - 'tarikh' => $request->tarikh, - 'tarikh2' => $request->tarikh, - 't_daftar' => $currentDate, - 'masadari' => $request->masadari, - 'masahingga' => $request->masahingga + Log::info("Mula Export Lelong", [ + 'kodcaw' => $cawangan->kodcaw, + 'nama_cawangan' => $cawangan->details_caw, ]); - $lelong = Lelong::on($cawangan['mysql']) - ->select('sirilel') - ->orderBy('batchno', 'desc') - ->first(); + // --- Step 1: Batch (Cegah duplicate) --- + $batchlel = BatchLel::on($cawangan['mysql']) + ->where('kodcaw', $request->kodcaw) + ->whereDate('tarikh', $request->tarikh) + ->where('masadari', $request->masadari) + ->where('masahingga', $request->masahingga) + ->first(); + + if (!$batchlel) { + $lastBatchNo = BatchLel::on($cawangan['mysql'])->max('batchno') ?? 0; + $newBatchNo = $lastBatchNo + 1; + + $batchlel = BatchLel::on($cawangan['mysql'])->create([ + 'kodcaw' => $request->kodcaw, + 'batchno' => $newBatchNo, + 'tarikh' => $request->tarikh, + 'tarikh2' => $request->tarikh, + 't_daftar' => $currentDate, + 'masadari' => $request->masadari, + 'masahingga' => $request->masahingga + ]); + + Log::info("Batch baru dicipta", ['batchno' => $newBatchNo]); + } else { + $newBatchNo = $batchlel->batchno; + Log::info("Batch sedia ada digunakan", ['batchno' => $newBatchNo]); + } + + // --- Step 2: Siri Lelong Terakhir --- + $lastSiriLelong = Lelong::on($cawangan['mysql'])->max('sirilel') ?? 0; + $lelong_arr = []; + + // --- Step 3: Cegah duplicate Lelong --- + $existingNorujukan = Lelong::on($cawangan['mysql']) + ->whereIn('norujukan', collect($request->lelongbid)->pluck('norujukan')) + ->pluck('norujukan') + ->toArray(); foreach ($request->lelongbid as $index) { + if (in_array($index['norujukan'], $existingNorujukan)) { + Log::warning("Skip Lelong duplicate", ['norujukan' => $index['norujukan']]); + continue; + } - $no_pembeli = Pembeli::on($cawangan['mysql'])->where('nokp', '=', $index['user_details']['nokp'])->select('nopembeli')->first(); - if ($no_pembeli) { - $no_pembeli_data = $no_pembeli->nopembeli; - } else { - $last_no_pembeli = Pembeli::on($cawangan['mysql'])->select('nopembeli')->orderBy('nopembeli', 'desc')->first(); - $no_pembeli_data = $last_no_pembeli->nopembeli + 1; + // --- Step 4: Semak / cipta pembeli --- + $no_pembeli = Pembeli::on($cawangan['mysql']) + ->where('nokp', $index['user_details']['nokp']) + ->value('nopembeli'); - $newPembeli = Pembeli::on($cawangan['mysql'])->create([ - 'nopembeli' => $no_pembeli_data, + if (!$no_pembeli) { + $last_no_pembeli = Pembeli::on($cawangan['mysql']) + ->max('nopembeli') ?? 0; + $no_pembeli = $last_no_pembeli + 1; + + Pembeli::on($cawangan['mysql'])->create([ + 'nopembeli' => $no_pembeli, 'nokp' => $index['user_details']['nokp'], 'nama' => $index['user_details']['name'], 'alamat1' => $index['user_details']['alamat'], @@ -1114,105 +1149,147 @@ class LelongController extends Controller ]); } - $update_gadai = Gadai::on($cawangan['mysql'])->where('norujukan','=',$index['norujukan'])->update(array( - 'sttebus' => "L", - 't_update' => $request->tarikh - )); + // --- Step 5: Update gadai --- + Gadai::on($cawangan['mysql']) + ->where('norujukan', $index['norujukan']) + ->update([ + 'sttebus' => "L", + 't_update' => $request->tarikh + ]); - $lelong_to_be_inserted = new Lelong(); - $lelong_to_be_inserted->batchno = $newBatchNo; - $lelong_to_be_inserted->sirilel = $lelong->sirilel + 1; - $lelong_to_be_inserted->kodcaw = $index['kodcaw']; - $lelong_to_be_inserted->norujukan = $index['norujukan']; - $lelong_to_be_inserted->t_lelong = $request->tarikh; - $lelong_to_be_inserted->hargaasas = $index['hargaasas']; - $lelong_to_be_inserted->hargajual = $index['hargajual']; - $lelong_to_be_inserted->glcode = "1000/010"; - $lelong_to_be_inserted->t_jual = $index['t_jual']; - $lelong_to_be_inserted->nopembeli = $no_pembeli_data; - $lelong_to_be_inserted->jbg = $index['jbg']; - $lelong_to_be_inserted->kadarupah = $index['kadarupah']; - $lelong_to_be_inserted->upahsimpan = $index['upahsimpan']; - $lelong_to_be_inserted->ansuran = $index['ansuran']; - $lelong_to_be_inserted->nilaimarhun = $index['nilaimarhun']; - $lelong_to_be_inserted->nilaimarhuns = $index['nmarhuns']; - $lelong_to_be_inserted->pinjaman = $index['pinjaman']; - $lelong_to_be_inserted->bakiupah = $index['bakiupah']; - $lelong_to_be_inserted->cajlelong = $index['cajlelong']; - $lelong_to_be_inserted->baki = $index['baki']; - $lelong_to_be_inserted->teller = $index['teller']; - $lelong_to_be_inserted->prkod = "01"; - $lelong_to_be_inserted->kodgl = "1000/013"; - $lelong_to_be_inserted->berat = $index['berat']; - $lelong_to_be_inserted->marhun = $index['marhun']; - $lelong_to_be_inserted->fasa = $index['fasa']; - $lelong_to_be_inserted->ujrah = $index['ujrah']; - $lelong_to_be_inserted->onepercent = $index['onepercent']; - $lelong_to_be_inserted->tagujrah = $index['tagujrah']; - - $lelong_arr[] = $lelong_to_be_inserted->attributesToArray(); + // --- Step 6: Simpan data Lelong --- + $lelong_arr[] = [ + 'batchno' => $newBatchNo, + 'sirilel' => ++$lastSiriLelong, + 'kodcaw' => $index['kodcaw'], + 'norujukan' => $index['norujukan'], + 't_lelong' => $request->tarikh, + 'hargaasas' => $index['hargaasas'], + 'hargajual' => $index['hargajual'], + 'glcode' => "1000/010", + 't_jual' => $index['t_jual'], + 'nopembeli' => $no_pembeli, + 'jbg' => $index['jbg'], + 'kadarupah' => $index['kadarupah'], + 'upahsimpan' => $index['upahsimpan'], + 'ansuran' => $index['ansuran'], + 'nilaimarhun' => $index['nilaimarhun'], + 'nilaimarhuns' => $index['nmarhuns'], + 'pinjaman' => $index['pinjaman'], + 'bakiupah' => $index['bakiupah'], + 'cajlelong' => $index['cajlelong'], + 'baki' => $index['baki'], + 'teller' => $index['teller'], + 'prkod' => "01", + 'kodgl' => "1000/013", + 'berat' => $index['berat'], + 'marhun' => $index['marhun'], + 'fasa' => $index['fasa'], + 'ujrah' => $index['ujrah'], + 'onepercent' => $index['onepercent'], + 'tagujrah' => $index['tagujrah'], + ]; } - Lelong::on($cawangan['mysql'])->insert($lelong_arr); - - foreach ($request->lelongbidmarhun as $data) { - $lelmarhun_to_be_inserted = new LelMarhun(); - $lelmarhun_to_be_inserted->kodcaw = $request->kodcaw; - $lelmarhun_to_be_inserted->norujukan = $data['norujukan']; - $lelmarhun_to_be_inserted->bil = $data['bil']; - $lelmarhun_to_be_inserted->marhun = $data['marhun']; - $lelmarhun_to_be_inserted->nota = $data['nota']; - $lelmarhun_to_be_inserted->karat = $data['karat']; - $lelmarhun_to_be_inserted->berat = $data['berat']; - $lelmarhun_to_be_inserted->harga = $data['harga']; - $lelmarhun_to_be_inserted->n_marhun = $data['n_marhun']; - $lelmarhun_to_be_inserted->hargaasas = $data['hargaasas']; - $lelmarhun_to_be_inserted->batch_no = $newBatchNo; - // $lelmarhun_to_be_inserted->beratasal = $; - - $lelmarhun_arr[] = $lelmarhun_to_be_inserted->attributesToArray(); - + if ($lelong_arr) { + Lelong::on($cawangan['mysql'])->insert($lelong_arr); + Log::info("Berjaya simpan lelong baru", ['total' => count($lelong_arr)]); } - LelMarhun::on($cawangan['mysql'])->insert($lelmarhun_arr); + // --- Step 7: LELMARHUN (cegah duplicate) --- + if ($request->lelongbidmarhun) { + $existingMarhun = LelMarhun::on($cawangan['mysql']) + ->whereIn('norujukan', collect($request->lelongbidmarhun)->pluck('norujukan')) + ->pluck('norujukan') + ->toArray(); - if ($request->tunai != null) { - $tunai = Tunai::on($cawangan['mysql'])->create([ - 'tarikh' => $request->tarikh, - 'kodcaw' => $request->kodcaw, - 'kodlaluan' => "LELONG", - 'masuk' => $request->tunai, - 'baki' => $request->tunai, - ]); + $lelmarhun_arr = collect($request->lelongbidmarhun) + ->reject(fn($data) => in_array($data['norujukan'], $existingMarhun)) + ->map(function ($data) use ($request, $newBatchNo) { + return [ + 'kodcaw' => $request->kodcaw, + 'norujukan' => $data['norujukan'], + 'bil' => $data['bil'], + 'marhun' => $data['marhun'], + 'nota' => $data['nota'], + 'karat' => $data['karat'], + 'berat' => $data['berat'], + 'harga' => $data['harga'], + 'n_marhun' => $data['n_marhun'], + 'hargaasas' => $data['hargaasas'], + 'batch_no' => $newBatchNo + ]; + }) + ->toArray(); + + if ($lelmarhun_arr) { + LelMarhun::on($cawangan['mysql'])->insert($lelmarhun_arr); + Log::info("LelMarhun baru disimpan", ['total' => count($lelmarhun_arr)]); + } } - if ($request->gldetail != null) { - $gldetail = GlDetail::on($cawangan['mysql'])->create([ - 'kodcaw' => $request->kodcaw, - 'tarikh' => $request->tarikh, - 'dtacct' => "1000/010", - 'ktacct' => "1000/020", - 'descpt' => "SERAHAN LELONG ONLINE, " . $request->tarikh, - 'amaun' => $request->gldetail, - 'teller' => "LELONG ONLINE", - 'serahan' => $request->gldetail, - ]); + // --- Step 8: GL dan Tunai (check duplicate descpt) --- + if ($request->tunai > 0) { + $existsTunai = Tunai::on($cawangan['mysql']) + ->where('kodcaw', $request->kodcaw) + ->where('tarikh', $request->tarikh) + ->where('kodlaluan', 'LELONG') + ->exists(); + + if (!$existsTunai) { + Tunai::on($cawangan['mysql'])->create([ + 'tarikh' => $request->tarikh, + 'kodcaw' => $request->kodcaw, + 'kodlaluan' => "LELONG", + 'masuk' => $request->tunai, + 'baki' => $request->tunai, + ]); + Log::info("Rekod Tunai lelong dimasukkan"); + } else { + Log::warning("Skip Tunai duplicate"); + } } - if ($request->gldetailcdm != null) { - GlDetail::on($cawangan['mysql'])->create([ - 'kodcaw' => $request->kodcaw, - 'tarikh' => $request->tarikh, - 'dtacct' => "1000/010", - 'ktacct' => "1000/020", - 'descpt' => "SERAHAN LELONG CDM, " . $request->tarikh, - 'amaun' => $request->gldetailcdm, - 'teller' => "LELONG ONLINE", - 'serahan' => $request->gldetailcdm, - ]); + $glDescs = [ + 'SERAHAN LELONG ONLINE' => $request->gldetail, + 'SERAHAN LELONG CDM' => $request->gldetailcdm, + ]; + + foreach ($glDescs as $desc => $amount) { + if ($amount > 0) { + $existsGl = GlDetail::on($cawangan['mysql']) + ->where('kodcaw', $request->kodcaw) + ->where('tarikh', $request->tarikh) + ->where('descpt', 'like', "%{$desc}%") + ->exists(); + + if (!$existsGl) { + GlDetail::on($cawangan['mysql'])->create([ + 'kodcaw' => $request->kodcaw, + 'tarikh' => $request->tarikh, + 'dtacct' => "1000/010", + 'ktacct' => "1000/020", + 'descpt' => "{$desc}, {$request->tarikh}", + 'amaun' => $amount, + 'teller' => "LELONG ONLINE", + 'serahan' => $amount, + ]); + Log::info("Rekod GL dimasukkan", ['desc' => $desc]); + } else { + Log::warning("Skip GL duplicate", ['desc' => $desc]); + } + } } - return response()->json("success"); + // --- Step 9: Log Akhir --- + Log::info('Berjaya Export Cawangan:', [ + 'kodcaw' => $cawangan->kodcaw, + 'nama_cawangan' => $cawangan->details_caw, + 'batch_baru' => $newBatchNo, + 'total_lelong' => count($lelong_arr), + ]); + + return response()->json(['status' => 'success']); } public function getRingkasanHarian(Request $request){