From 8031501d25f96646b3ce2182b34a9e67835312ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Pocrnji=C4=8D?= Date: Fri, 2 Jan 2026 14:49:05 +0100 Subject: [PATCH] Changes to import where on reactivation if start_date not set import mapping it should fill field with current date, same for end_date but it sets it to null --- app/Services/ImportProcessor.php | 35 +++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/app/Services/ImportProcessor.php b/app/Services/ImportProcessor.php index f322245..6a7ce39 100644 --- a/app/Services/ImportProcessor.php +++ b/app/Services/ImportProcessor.php @@ -2301,10 +2301,31 @@ private function upsertContractChain(Import $import, array $mapped, $mappings, b return ['action' => 'skipped_history', 'contract' => $existing, 'message' => 'Existing contract left unchanged (history import)']; } + + // Check if contract is soft-deleted and needs reactivation + $isTrashed = $existing->trashed(); + // 1) Prepare contract field changes (non-null) $changes = array_filter($applyUpdate, fn ($v) => ! is_null($v)); + + // 2) Handle reactivation defaults when contract is soft-deleted + if ($isTrashed || $existing->active == 0) { + // Check if start_date is in the mappings + $hasStartDateMapping = $this->mappingIncludes($mappings, 'contract.start_date'); + if (!$hasStartDateMapping) { + // Default to current date when not in mappings + $changes['start_date'] = now()->toDateString(); + } + + // Check if end_date is in the mappings + $hasEndDateMapping = $this->mappingIncludes($mappings, 'contract.end_date'); + if (!$hasEndDateMapping) { + // Default to null when not in mappings + $changes['end_date'] = null; + } + } - // 2) Prepare meta changes if provided via mapping + // 3) Prepare meta changes if provided via mapping $metaUpdated = false; $metaAppliedKeys = []; if (! empty($contractData['meta'] ?? null) && is_array($contractData['meta'])) { @@ -2347,7 +2368,7 @@ private function upsertContractChain(Import $import, array $mapped, $mappings, b } } - if (empty($changes) && ! $metaUpdated) { + if (empty($changes) && ! $metaUpdated && ! $isTrashed) { // Nothing to change return ['action' => 'skipped', 'message' => 'No contract fields or meta changes', 'contract' => $existing]; } @@ -2355,6 +2376,12 @@ private function upsertContractChain(Import $import, array $mapped, $mappings, b if (! empty($changes)) { $existing->fill($changes); } + + // Restore soft-deleted contract if it was trashed + if ($isTrashed) { + $existing->restore(); + } + $existing->save(); // Build applied fields info, include meta keys if any @@ -2364,8 +2391,10 @@ private function upsertContractChain(Import $import, array $mapped, $mappings, b $applied['meta:'.$k] = 'updated'; } } + + $actionType = $isTrashed ? 'reactivated' : 'updated'; - return ['action' => 'updated', 'contract' => $existing, 'applied_fields' => $applied]; + return ['action' => $actionType, 'contract' => $existing, 'applied_fields' => $applied]; } else { if (empty($applyInsert)) { return ['action' => 'skipped', 'message' => 'No contract fields marked for insert'];