diff --git a/app/Jobs/ProcessBillDetailDataJob.php b/app/Jobs/ProcessBillDetailDataJob.php index 0afe94e..d04ad33 100644 --- a/app/Jobs/ProcessBillDetailDataJob.php +++ b/app/Jobs/ProcessBillDetailDataJob.php @@ -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) {