diff --git a/app/Services/PembiayaanOnline/PembiayaanOnlineService.php b/app/Services/PembiayaanOnline/PembiayaanOnlineService.php index 31fde74..b57e7e4 100644 --- a/app/Services/PembiayaanOnline/PembiayaanOnlineService.php +++ b/app/Services/PembiayaanOnline/PembiayaanOnlineService.php @@ -593,20 +593,14 @@ class PembiayaanOnlineService } } - $batchNo = $this->nextBatchNo($mysql, $kodcaw, $tarikh, $slot); - $jumlah = round((float) $lines->sum('pinjaman'), 2); - - $batch = BatchPembiayaanOnline::on($mysql)->create([ - 'batch_no' => $batchNo, - 'kodcaw' => $kodcaw, - 'tarikh' => $tarikh, - 'slot' => $slot, - 'bilangan' => $lines->count(), - 'jumlah_keseluruhan' => $jumlah, - 'status' => 'SUBMITTED', - 'pc_submitted_by' => $pcBy, - 'submitted_at' => $now, - ]); + list($batch, $appended) = $this->findOrCreateSlotBatch( + $mysql, + $kodcaw, + $tarikh, + $slot, + $pcBy, + $now + ); foreach ($lines as $line) { PembiayaanOnline::on($mysql)->where('id', $line->id)->update([ @@ -619,6 +613,14 @@ class PembiayaanOnlineService ]); } + if ($appended) { + BatchPembiayaanOnline::on($mysql)->where('id', $batch->id)->update([ + 'submitted_at' => $now, + ]); + } + + $batch = $this->refreshBatchStatus($mysql, $batch->id); + $freshLines = PembiayaanOnline::on($mysql) ->where('batch_id', $batch->id) ->orderBy('id', 'asc') @@ -626,11 +628,12 @@ class PembiayaanOnlineService return [ 'ok' => true, - 'status' => 201, + 'status' => $appended ? 200 : 201, 'payload' => [ 'batch' => $batch, 'lines' => $freshLines, 'slot_info' => $slotInfo, + 'appended' => $appended, ], ]; }); @@ -1535,24 +1538,45 @@ class PembiayaanOnlineService ]; } - protected function nextBatchNo($mysql, $kodcaw, $tarikh, $slot) + /** + * One batch per branch / day / slot (B1-001 or B2-001). Later submits append. + * + * @return array{0:\App\BatchPembiayaanOnline,1:bool} [batch, appended] + */ + protected function findOrCreateSlotBatch($mysql, $kodcaw, $tarikh, $slot, $pcBy, $now) { - $slotTag = $slot === 'BATCH1' ? 'B1' : 'B2'; - $ymd = Carbon::parse($tarikh)->format('Ymd'); - $prefix = strtoupper($kodcaw) . '-PO-' . $ymd . '-' . $slotTag . '-'; - - $last = BatchPembiayaanOnline::on($mysql) + $batch = BatchPembiayaanOnline::on($mysql) ->where('kodcaw', $kodcaw) ->where('tarikh', $tarikh) ->where('slot', $slot) - ->orderBy('id', 'desc') + ->orderBy('id', 'asc') + ->lockForUpdate() ->first(); - $seq = 1; - if ($last && preg_match('/-(\d+)$/', $last->batch_no, $m)) { - $seq = ((int) $m[1]) + 1; + if ($batch) { + return [$batch, true]; } - return $prefix . str_pad((string) $seq, 3, '0', STR_PAD_LEFT); + $batch = BatchPembiayaanOnline::on($mysql)->create([ + 'batch_no' => $this->slotBatchNo($kodcaw, $tarikh, $slot), + 'kodcaw' => $kodcaw, + 'tarikh' => $tarikh, + 'slot' => $slot, + 'bilangan' => 0, + 'jumlah_keseluruhan' => 0, + 'status' => 'SUBMITTED', + 'pc_submitted_by' => $pcBy, + 'submitted_at' => $now, + ]); + + return [$batch, false]; + } + + protected function slotBatchNo($kodcaw, $tarikh, $slot) + { + $slotTag = $slot === 'BATCH1' ? 'B1' : 'B2'; + $ymd = Carbon::parse($tarikh)->format('Ymd'); + + return strtoupper($kodcaw) . '-PO-' . $ymd . '-' . $slotTag . '-001'; } }