Changes to import and notifications

This commit is contained in:
Simon Pocrnjič
2025-10-13 21:14:10 +02:00
parent 0bbed64542
commit 79b3e20b02
28 changed files with 2173 additions and 438 deletions
+51 -3
View File
@@ -499,7 +499,7 @@ public function process(Import $import, ?Authenticatable $user = null): array
$contractKeyMode = $tplMeta['contract_key_mode'] ?? null;
if (! $accountIdForPayment && $paymentsImport && $contractKeyMode === 'reference') {
$contractRef = $mapped['contract']['reference'] ?? null;
if ($contractRef) {
if (! $contractId) {
$contract = \App\Models\Contract::query()
->when($import->client_id, function ($q, $clientId) {
$q->join('client_cases', 'contracts.client_case_id', '=', 'client_cases.id')
@@ -508,9 +508,25 @@ public function process(Import $import, ?Authenticatable $user = null): array
->where('contracts.reference', $contractRef)
->select('contracts.id')
->first();
if ($contract) {
$accountIdForPayment = \App\Models\Account::where('contract_id', $contract->id)->value('id');
} elseif ($hasContractRoot) {
// If mapping for contract.reference is keyref, do NOT create contract lookup only
$refMode = $this->mappingMode($mappings, 'contract.reference');
if ($refMode === 'keyref') {
ImportEvent::create([
'import_id' => $import->id,
'user_id' => null,
'event' => 'row_skipped',
'level' => 'warning',
'message' => 'Contract reference '.$contractRef.' does not exist (keyref); row skipped.',
]);
return [
'action' => 'skipped',
'message' => 'contract.reference keyref lookup failed: not found',
];
}
/* Lines 1242-1269 omitted */
$contractId = $createdContract->id;
}
}
@@ -1583,6 +1599,20 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
}
}
// 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));
@@ -2040,6 +2070,24 @@ private function normalizeTargetField(string $target, array $rootAliasMap, array
return $bracket !== null ? ($root.'['.$bracket.']') : $root;
}
/**
* Get apply mode for a specific mapping target field, normalized via normalizeMappings beforehand.
* Returns lowercased mode string like 'insert', 'update', 'both', 'keyref', or null if not present.
*/
private function mappingMode($mappings, string $targetField): ?string
{
foreach ($mappings as $map) {
$target = (string) ($map->target_field ?? '');
if ($target === $targetField) {
$mode = $map->apply_mode ?? null;
return is_string($mode) ? strtolower($mode) : null;
}
}
return null;
}
protected function loadImportEntityConfig(): array
{
$entities = ImportEntity::all();