From 5c9c425b4bc16de7706f72ca5d2aea5a10e19eee Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 27 Feb 2026 15:01:17 +0530 Subject: [PATCH] Updated invoice import functionality to allow unique record --- .../Imports/InvoiceValidationImporter.php | 189 ++++++++++-------- 1 file changed, 101 insertions(+), 88 deletions(-) diff --git a/app/Filament/Imports/InvoiceValidationImporter.php b/app/Filament/Imports/InvoiceValidationImporter.php index a8b982c..4f20ae5 100644 --- a/app/Filament/Imports/InvoiceValidationImporter.php +++ b/app/Filament/Imports/InvoiceValidationImporter.php @@ -322,65 +322,76 @@ class InvoiceValidationImporter extends Importer $curPanelBoxSerialNumber = null; } - $record = InvoiceValidation::where('serial_number', $serialNumber) - ->where('plant_id', $plantId) - ->whereHas('stickerMasterRelation.item', function ($query) use ($plantId, $iCode) { - $query->where('plant_id', $plantId)->where('code', $iCode); - }) - ->first(); + $record = InvoiceValidation::where('serial_number', $serialNumber)->where('plant_id', $plantId)->first(); - $invalidPackage = false; + if ($record && $record->sticker_master_id != $stickId) { + $stickId = null; + $warnMsg[] = 'Item code mismatch with existing record!'; + } elseif ($record) { + $record = InvoiceValidation::where('serial_number', $serialNumber)->where('plant_id', $plantId) + ->whereHas('stickerMasterRelation.item', function ($query) use ($plantId, $iCode) { + $query->where('plant_id', $plantId)->where('code', $iCode); + }) + ->first(); - if ($record) { + $invalidPackage = false; - $hadMotorQr = $record->motor_scanned_status ?? null; - $hadPumpQr = $record->pump_scanned_status ?? null; - $hadPumpSetQr = $record->scanned_status_set ?? null; - $hadCapacitorQr = $record->capacitor_scanned_status ?? null; + if ($record) { - if ($hadMotorQr) { - $curMotorQr = $hadMotorQr; - } - if ($hadPumpQr) { - $curPumpQr = $hadPumpQr; - } - if ($hadPumpSetQr) { - $curPumpSetQr = $hadPumpSetQr; - } - if ($hadCapacitorQr) { - $curCapacitorQr = $hadCapacitorQr; - $curPanelBoxCode = $record->panel_box_code ?? null; - $curPanelBoxSupplier = $record->panel_box_supplier ?? null; - $curPanelBoxSerialNumber = $record->panel_box_serial_number ?? null; - } + $hadMotorQr = $record->motor_scanned_status ?? null; + $hadPumpQr = $record->pump_scanned_status ?? null; + $hadPumpSetQr = $record->scanned_status_set ?? null; + $hadCapacitorQr = $record->capacitor_scanned_status ?? null; - if ($record->scanned_status == 'Scanned') { - return null; - } elseif ($record->invoice_number != $invoiceNumber) { - throw new RowImportFailedException('Invoice number mismatch with existing record!'); - } - - // if ($hadPumpQr == $hasPumpQr && $hadPumpSetQr == $hasPumpSetQr) - if ($hasMotorQr || $hasPumpQr || $hasPumpSetQr || $hasCapacitorQr) { - $scanCnt = $curMotorQr ? $scanCnt + 1 : $scanCnt; - $scanCnt = $curPumpQr ? $scanCnt + 1 : $scanCnt; - $scanCnt = $curPumpSetQr ? $scanCnt + 1 : $scanCnt; - $scanCnt = $curCapacitorQr ? $scanCnt + 1 : $scanCnt; - - $record->motor_scanned_status = $curMotorQr; - $record->pump_scanned_status = $curPumpQr; - $record->scanned_status_set = $curPumpSetQr; - $record->capacitor_scanned_status = $curCapacitorQr; - $record->panel_box_code = $curPanelBoxCode; - $record->panel_box_supplier = $curPanelBoxSupplier; - $record->panel_box_serial_number = $curPanelBoxSerialNumber; - if ($packCnt == $scanCnt) { - $record->scanned_status = 'Scanned'; + if ($hadMotorQr && $hasMotorQr) { + $curMotorQr = $hadMotorQr; + } + if ($hadPumpQr && $hasPumpQr) { + $curPumpQr = $hadPumpQr; + } + if ($hadPumpSetQr && $hasPumpSetQr) { + $curPumpSetQr = $hadPumpSetQr; + } + if ($hadCapacitorQr && $hasCapacitorQr) { + $curCapacitorQr = $hadCapacitorQr; + $curPanelBoxCode = $record->panel_box_code ?? null; + $curPanelBoxSupplier = $record->panel_box_supplier ?? null; + $curPanelBoxSerialNumber = $record->panel_box_serial_number ?? null; } - $record->updated_by = $updatedBy; - $record->save(); - return null; + $warnMsg[] = 'Record Item ID : '.$record->sticker_master_id.' Master Item ID : '.$stickId; + if ($record->invoice_number != $invoiceNumber) { + $stickId = null; + $warnMsg[] = 'Invoice number mismatch with existing record!'; + // throw new RowImportFailedException('Invoice number mismatch with existing record!'); + } elseif ($record->scanned_status == 'Scanned') { + $stickId = null; + + return null; + } else { + // if ($hadPumpQr == $hasPumpQr && $hadPumpSetQr == $hasPumpSetQr) + if ($hasMotorQr || $hasPumpQr || $hasPumpSetQr || $hasCapacitorQr) { + $scanCnt = $curMotorQr ? $scanCnt + 1 : $scanCnt; + $scanCnt = $curPumpQr ? $scanCnt + 1 : $scanCnt; + $scanCnt = $curPumpSetQr ? $scanCnt + 1 : $scanCnt; + $scanCnt = $curCapacitorQr ? $scanCnt + 1 : $scanCnt; + + $record->motor_scanned_status = $curMotorQr; + $record->pump_scanned_status = $curPumpQr; + $record->scanned_status_set = $curPumpSetQr; + $record->capacitor_scanned_status = $curCapacitorQr; + $record->panel_box_code = $curPanelBoxCode; + $record->panel_box_supplier = $curPanelBoxSupplier; + $record->panel_box_serial_number = $curPanelBoxSerialNumber; + if ($packCnt == $scanCnt) { + $record->scanned_status = 'Scanned'; + } + $record->updated_by = $updatedBy; + $record->save(); + + return null; + } + } } } } @@ -426,45 +437,47 @@ class InvoiceValidationImporter extends Importer throw new RowImportFailedException(implode(', ', $warnMsg)); } - if ($hasMotorQr || $hasPumpQr || $hasPumpSetQr || $hasCapacitorQr) { - $scanCnt = $curMotorQr ? $scanCnt + 1 : $scanCnt; - $scanCnt = $curPumpQr ? $scanCnt + 1 : $scanCnt; - $scanCnt = $curPumpSetQr ? $scanCnt + 1 : $scanCnt; - $scanCnt = $curCapacitorQr ? $scanCnt + 1 : $scanCnt; + if ($stickId) { + if ($hasMotorQr || $hasPumpQr || $hasPumpSetQr || $hasCapacitorQr) { + $scanCnt = $curMotorQr ? $scanCnt + 1 : $scanCnt; + $scanCnt = $curPumpQr ? $scanCnt + 1 : $scanCnt; + $scanCnt = $curPumpSetQr ? $scanCnt + 1 : $scanCnt; + $scanCnt = $curCapacitorQr ? $scanCnt + 1 : $scanCnt; - if ($packCnt == $scanCnt) { - $curScanStatus = 'Scanned'; - } else { - $curScanStatus = null; + if ($packCnt == $scanCnt) { + $curScanStatus = 'Scanned'; + } else { + $curScanStatus = null; + } } - } - // $curScanStatus + // $curScanStatus - InvoiceValidation::updateOrCreate([ - 'plant_id' => $plantId, - 'sticker_master_id' => $stickId, - 'serial_number' => $serialNumber, - ], - [ - 'invoice_number' => $invoiceNumber, - 'motor_scanned_status' => $curMotorQr, - 'pump_scanned_status' => $curPumpQr, - 'scanned_status_set' => $curPumpSetQr, - 'capacitor_scanned_status' => $curCapacitorQr, - 'panel_box_code' => $curPanelBoxCode, - 'panel_box_supplier' => $curPanelBoxSupplier, - 'panel_box_serial_number' => $curPanelBoxSerialNumber, - 'scanned_status' => $curScanStatus, - 'load_rate' => $loadRate, - 'upload_status' => $uploadStatus, - 'batch_number' => null, - 'quantity' => null, - 'operator_id' => $operatorId, - 'created_by' => $createdBy, - 'updated_by' => $updatedBy, - 'created_at' => $cDateTime->format('Y-m-d H:i:s'), - 'updated_at' => $uDateTime->format('Y-m-d H:i:s'), - ]); + InvoiceValidation::updateOrCreate([ + 'plant_id' => $plantId, + 'sticker_master_id' => $stickId, + 'serial_number' => $serialNumber, + ], + [ + 'invoice_number' => $invoiceNumber, + 'motor_scanned_status' => $curMotorQr, + 'pump_scanned_status' => $curPumpQr, + 'scanned_status_set' => $curPumpSetQr, + 'capacitor_scanned_status' => $curCapacitorQr, + 'panel_box_code' => $curPanelBoxCode, + 'panel_box_supplier' => $curPanelBoxSupplier, + 'panel_box_serial_number' => $curPanelBoxSerialNumber, + 'scanned_status' => $curScanStatus, + 'load_rate' => $loadRate, + 'upload_status' => $uploadStatus, + 'batch_number' => null, + 'quantity' => null, + 'operator_id' => $operatorId, + 'created_by' => $createdBy, + 'updated_by' => $updatedBy, + 'created_at' => $cDateTime->format('Y-m-d H:i:s'), + 'updated_at' => $uDateTime->format('Y-m-d H:i:s'), + ]); + } return null; // return new InvoiceValidation; -- 2.49.1