refactor(webstatement): optimalkan pemrosesan batch data bill detail

- Mengubah mekanisme pemrosesan batch `billDetailBatch` menjadi pemrosesan per chunk untuk manajemen memori yang lebih baik.
- Menambahkan langkah penghapusan data eksisting berdasarkan `stmt_entry_ids` sebelum melakukan insert untuk menghindari konflik data.
- Mengganti metode `upsert` dengan kombinasi `delete` dan `insert` untuk tiap chunk data.
- Menyisipkan komentar untuk memberikan penjelasan yang lebih rinci terkait proses chunking.
- Memastikan batch `billDetailBatch` di-reset setelah selesai diproses.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
Daeng Deni Mardaeni
2025-05-29 21:58:08 +07:00
parent e14ae2ef9c
commit 8ca526e4f2

View File

@@ -182,14 +182,19 @@
{
try {
if (!empty($this->billDetailBatch)) {
// Bulk insert/update bill details
TempBillDetail::upsert(
$this->billDetailBatch,
['_id'], // Unique key
array_diff((new TempBillDetail())->getFillable(), ['_id']) // Update columns
);
// Process in smaller chunks for better memory management
foreach ($this->billDetailBatch as $entry) {
// Extract all stmt_entry_ids from the current chunk
$entryIds = array_column($entry, '_id');
// Reset batch after processing
// Delete existing records with these IDs to avoid conflicts
TempBillDetail::whereIn('_id', $entryIds)->delete();
// Insert all records in the chunk at once
TempBillDetail::insert($entry);
}
// Reset entry batch after processing
$this->billDetailBatch = [];
}
} catch (Exception $e) {