Merge pull request 'Updated invoice import functionality to allow unique record' (#422) from ranjith-dev into master
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 18s

Reviewed-on: #422
This commit was merged in pull request #422.
This commit is contained in:
2026-02-27 09:32:09 +00:00

View File

@@ -322,65 +322,76 @@ class InvoiceValidationImporter extends Importer
$curPanelBoxSerialNumber = null; $curPanelBoxSerialNumber = null;
} }
$record = InvoiceValidation::where('serial_number', $serialNumber) $record = InvoiceValidation::where('serial_number', $serialNumber)->where('plant_id', $plantId)->first();
->where('plant_id', $plantId)
->whereHas('stickerMasterRelation.item', function ($query) use ($plantId, $iCode) {
$query->where('plant_id', $plantId)->where('code', $iCode);
})
->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; if ($record) {
$hadPumpQr = $record->pump_scanned_status ?? null;
$hadPumpSetQr = $record->scanned_status_set ?? null;
$hadCapacitorQr = $record->capacitor_scanned_status ?? null;
if ($hadMotorQr) { $hadMotorQr = $record->motor_scanned_status ?? null;
$curMotorQr = $hadMotorQr; $hadPumpQr = $record->pump_scanned_status ?? null;
} $hadPumpSetQr = $record->scanned_status_set ?? null;
if ($hadPumpQr) { $hadCapacitorQr = $record->capacitor_scanned_status ?? null;
$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;
}
if ($record->scanned_status == 'Scanned') { if ($hadMotorQr && $hasMotorQr) {
return null; $curMotorQr = $hadMotorQr;
} elseif ($record->invoice_number != $invoiceNumber) { }
throw new RowImportFailedException('Invoice number mismatch with existing record!'); if ($hadPumpQr && $hasPumpQr) {
} $curPumpQr = $hadPumpQr;
}
// if ($hadPumpQr == $hasPumpQr && $hadPumpSetQr == $hasPumpSetQr) if ($hadPumpSetQr && $hasPumpSetQr) {
if ($hasMotorQr || $hasPumpQr || $hasPumpSetQr || $hasCapacitorQr) { $curPumpSetQr = $hadPumpSetQr;
$scanCnt = $curMotorQr ? $scanCnt + 1 : $scanCnt; }
$scanCnt = $curPumpQr ? $scanCnt + 1 : $scanCnt; if ($hadCapacitorQr && $hasCapacitorQr) {
$scanCnt = $curPumpSetQr ? $scanCnt + 1 : $scanCnt; $curCapacitorQr = $hadCapacitorQr;
$scanCnt = $curCapacitorQr ? $scanCnt + 1 : $scanCnt; $curPanelBoxCode = $record->panel_box_code ?? null;
$curPanelBoxSupplier = $record->panel_box_supplier ?? null;
$record->motor_scanned_status = $curMotorQr; $curPanelBoxSerialNumber = $record->panel_box_serial_number ?? null;
$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; $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)); throw new RowImportFailedException(implode(', ', $warnMsg));
} }
if ($hasMotorQr || $hasPumpQr || $hasPumpSetQr || $hasCapacitorQr) { if ($stickId) {
$scanCnt = $curMotorQr ? $scanCnt + 1 : $scanCnt; if ($hasMotorQr || $hasPumpQr || $hasPumpSetQr || $hasCapacitorQr) {
$scanCnt = $curPumpQr ? $scanCnt + 1 : $scanCnt; $scanCnt = $curMotorQr ? $scanCnt + 1 : $scanCnt;
$scanCnt = $curPumpSetQr ? $scanCnt + 1 : $scanCnt; $scanCnt = $curPumpQr ? $scanCnt + 1 : $scanCnt;
$scanCnt = $curCapacitorQr ? $scanCnt + 1 : $scanCnt; $scanCnt = $curPumpSetQr ? $scanCnt + 1 : $scanCnt;
$scanCnt = $curCapacitorQr ? $scanCnt + 1 : $scanCnt;
if ($packCnt == $scanCnt) { if ($packCnt == $scanCnt) {
$curScanStatus = 'Scanned'; $curScanStatus = 'Scanned';
} else { } else {
$curScanStatus = null; $curScanStatus = null;
}
} }
} // $curScanStatus
// $curScanStatus
InvoiceValidation::updateOrCreate([ InvoiceValidation::updateOrCreate([
'plant_id' => $plantId, 'plant_id' => $plantId,
'sticker_master_id' => $stickId, 'sticker_master_id' => $stickId,
'serial_number' => $serialNumber, 'serial_number' => $serialNumber,
], ],
[ [
'invoice_number' => $invoiceNumber, 'invoice_number' => $invoiceNumber,
'motor_scanned_status' => $curMotorQr, 'motor_scanned_status' => $curMotorQr,
'pump_scanned_status' => $curPumpQr, 'pump_scanned_status' => $curPumpQr,
'scanned_status_set' => $curPumpSetQr, 'scanned_status_set' => $curPumpSetQr,
'capacitor_scanned_status' => $curCapacitorQr, 'capacitor_scanned_status' => $curCapacitorQr,
'panel_box_code' => $curPanelBoxCode, 'panel_box_code' => $curPanelBoxCode,
'panel_box_supplier' => $curPanelBoxSupplier, 'panel_box_supplier' => $curPanelBoxSupplier,
'panel_box_serial_number' => $curPanelBoxSerialNumber, 'panel_box_serial_number' => $curPanelBoxSerialNumber,
'scanned_status' => $curScanStatus, 'scanned_status' => $curScanStatus,
'load_rate' => $loadRate, 'load_rate' => $loadRate,
'upload_status' => $uploadStatus, 'upload_status' => $uploadStatus,
'batch_number' => null, 'batch_number' => null,
'quantity' => null, 'quantity' => null,
'operator_id' => $operatorId, 'operator_id' => $operatorId,
'created_by' => $createdBy, 'created_by' => $createdBy,
'updated_by' => $updatedBy, 'updated_by' => $updatedBy,
'created_at' => $cDateTime->format('Y-m-d H:i:s'), 'created_at' => $cDateTime->format('Y-m-d H:i:s'),
'updated_at' => $uDateTime->format('Y-m-d H:i:s'), 'updated_at' => $uDateTime->format('Y-m-d H:i:s'),
]); ]);
}
return null; return null;
// return new InvoiceValidation; // return new InvoiceValidation;