import fix for update so it does not insert person and client case

This commit is contained in:
Simon Pocrnjič
2025-10-16 20:11:45 +02:00
parent ed62311ba4
commit e782bcca7c
11 changed files with 393 additions and 54 deletions
+19 -18
View File
@@ -1490,6 +1490,9 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
return ['action' => 'invalid', 'message' => 'Missing contract.reference'];
}
// Determine mapping mode for contract.reference (e.g., keyref)
$refMode = $this->mappingMode($mappings, 'contract.reference');
// Determine client_case_id: prefer provided, else derive via person/client
$clientCaseId = $contractData['client_case_id'] ?? null;
$clientId = $import->client_id; // may be null
@@ -1513,6 +1516,19 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
->first();
}
// If contract.reference is keyref and contract not found, do not create any entities
if (! $existing && $refMode === 'keyref') {
ImportEvent::create([
'import_id' => $import->id,
'user_id' => null,
'event' => 'row_skipped',
'level' => 'warning',
'message' => 'Contract reference '.$reference.' does not exist (keyref); row skipped.',
]);
return ['action' => 'skipped', 'message' => 'contract.reference keyref lookup failed: not found'];
}
// If we still need to insert, we must resolve clientCaseId, but avoid creating new person/case unless necessary
if (! $existing && ! $clientCaseId) {
$clientRef = $mapped['client_case']['client_ref'] ?? null;
@@ -1573,7 +1589,7 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
continue;
}
$parts = explode('.', $map->target_field);
if ($parts[0] !== 'contract') {
if (($parts[0] ?? null) !== 'contract') {
continue;
}
$field = $parts[1] ?? null;
@@ -1588,31 +1604,16 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
// keyref: used as lookup and applied on insert, but not on update
if ($mode === 'keyref') {
$applyInsert[$field] = $value;
continue;
}
if (in_array($mode, ['insert', 'both'])) {
if (in_array($mode, ['insert', 'both'], true)) {
$applyInsert[$field] = $value;
}
if (in_array($mode, ['update', 'both'])) {
if (in_array($mode, ['update', 'both'], true)) {
$applyUpdate[$field] = $value;
}
}
// If contract not found and contract.reference is keyref, skip without creating entities
$refMode = $this->mappingMode($mappings, 'contract.reference');
if (! $existing && $refMode === 'keyref') {
ImportEvent::create([
'import_id' => $import->id,
'user_id' => null,
'event' => 'row_skipped',
'level' => 'warning',
'message' => 'Contract reference '.$reference.' does not exist (keyref); row skipped.',
]);
return ['action' => 'skipped', 'message' => 'contract.reference keyref lookup failed: not found'];
}
if ($existing) {
// 1) Prepare contract field changes (non-null)
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));