DONE: optimize loading page on employee, append the online payment under same batch

This commit is contained in:
ISMAIL MASSERAN
2026-09-06 10:21:20 +08:00
parent 0213f40b11
commit 8aedce7a53
@@ -593,20 +593,14 @@ class PembiayaanOnlineService
} }
} }
$batchNo = $this->nextBatchNo($mysql, $kodcaw, $tarikh, $slot); list($batch, $appended) = $this->findOrCreateSlotBatch(
$jumlah = round((float) $lines->sum('pinjaman'), 2); $mysql,
$kodcaw,
$batch = BatchPembiayaanOnline::on($mysql)->create([ $tarikh,
'batch_no' => $batchNo, $slot,
'kodcaw' => $kodcaw, $pcBy,
'tarikh' => $tarikh, $now
'slot' => $slot, );
'bilangan' => $lines->count(),
'jumlah_keseluruhan' => $jumlah,
'status' => 'SUBMITTED',
'pc_submitted_by' => $pcBy,
'submitted_at' => $now,
]);
foreach ($lines as $line) { foreach ($lines as $line) {
PembiayaanOnline::on($mysql)->where('id', $line->id)->update([ 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) $freshLines = PembiayaanOnline::on($mysql)
->where('batch_id', $batch->id) ->where('batch_id', $batch->id)
->orderBy('id', 'asc') ->orderBy('id', 'asc')
@@ -626,11 +628,12 @@ class PembiayaanOnlineService
return [ return [
'ok' => true, 'ok' => true,
'status' => 201, 'status' => $appended ? 200 : 201,
'payload' => [ 'payload' => [
'batch' => $batch, 'batch' => $batch,
'lines' => $freshLines, 'lines' => $freshLines,
'slot_info' => $slotInfo, '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'; $batch = BatchPembiayaanOnline::on($mysql)
$ymd = Carbon::parse($tarikh)->format('Ymd');
$prefix = strtoupper($kodcaw) . '-PO-' . $ymd . '-' . $slotTag . '-';
$last = BatchPembiayaanOnline::on($mysql)
->where('kodcaw', $kodcaw) ->where('kodcaw', $kodcaw)
->where('tarikh', $tarikh) ->where('tarikh', $tarikh)
->where('slot', $slot) ->where('slot', $slot)
->orderBy('id', 'desc') ->orderBy('id', 'asc')
->lockForUpdate()
->first(); ->first();
$seq = 1; if ($batch) {
if ($last && preg_match('/-(\d+)$/', $last->batch_no, $m)) { return [$batch, true];
$seq = ((int) $m[1]) + 1;
} }
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';
} }
} }