Compare commits

..

2 Commits

View File

@ -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)']; 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) // 1) Prepare contract field changes (non-null)
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v)); $changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));
// 2) Prepare meta changes if provided via mapping // 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;
}
}
// 3) Prepare meta changes if provided via mapping
$metaUpdated = false; $metaUpdated = false;
$metaAppliedKeys = []; $metaAppliedKeys = [];
if (! empty($contractData['meta'] ?? null) && is_array($contractData['meta'])) { 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 // Nothing to change
return ['action' => 'skipped', 'message' => 'No contract fields or meta changes', 'contract' => $existing]; 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)) { if (! empty($changes)) {
$existing->fill($changes); $existing->fill($changes);
} }
// Restore soft-deleted contract if it was trashed
if ($isTrashed) {
$existing->restore();
}
$existing->save(); $existing->save();
// Build applied fields info, include meta keys if any // Build applied fields info, include meta keys if any
@ -2365,7 +2392,9 @@ private function upsertContractChain(Import $import, array $mapped, $mappings, b
} }
} }
return ['action' => 'updated', 'contract' => $existing, 'applied_fields' => $applied]; $actionType = $isTrashed ? 'reactivated' : 'updated';
return ['action' => $actionType, 'contract' => $existing, 'applied_fields' => $applied];
} else { } else {
if (empty($applyInsert)) { if (empty($applyInsert)) {
return ['action' => 'skipped', 'message' => 'No contract fields marked for insert']; return ['action' => 'skipped', 'message' => 'No contract fields marked for insert'];