diff --git a/app/Models/Contract.php b/app/Models/Contract.php index 3c46a87..0f97745 100644 --- a/app/Models/Contract.php +++ b/app/Models/Contract.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Traits\Uuid; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -43,6 +44,38 @@ protected function casts(): array ]; } + /** + * Normalize start_date inputs to Y-m-d (or null) on assignment. + */ + protected function startDate(): Attribute + { + return Attribute::make( + set: function ($value) { + if (is_null($value)) { + return null; + } + $str = is_string($value) ? $value : (string) $value; + return \App\Services\DateNormalizer::toDate($str); + } + ); + } + + /** + * Normalize end_date inputs to Y-m-d (or null) on assignment. + */ + protected function endDate(): Attribute + { + return Attribute::make( + set: function ($value) { + if (is_null($value)) { + return null; + } + $str = is_string($value) ? $value : (string) $value; + return \App\Services\DateNormalizer::toDate($str); + } + ); + } + public function type(): BelongsTo { return $this->belongsTo(\App\Models\ContractType::class, 'type_id'); diff --git a/app/Services/ImportProcessor.php b/app/Services/ImportProcessor.php index 436552b..d7b65a0 100644 --- a/app/Services/ImportProcessor.php +++ b/app/Services/ImportProcessor.php @@ -258,7 +258,7 @@ public function process(Import $import, ?Authenticatable $user = null): array $ref = preg_replace('/\s+/', '', trim($ref)); } if ($ref) { - $q = Contract::query() + $q = Contract::query()->withTrashed() ->when($import->client_id, function ($q2, $clientId) { $q2->join('client_cases', 'contracts.client_case_id', '=', 'client_cases.id') ->where('client_cases.client_id', $clientId); @@ -266,10 +266,10 @@ public function process(Import $import, ?Authenticatable $user = null): array ->where('contracts.reference', $ref) ->select('contracts.*'); $found = $q->first(); - if ($found) { + if ($found instanceof \App\Models\Contract) { $contractResult = ['action' => 'resolved', 'contract' => $found]; // Reactivation branch for resolved existing contract - if ($reactivateMode && ($found->active == 0 || $found->deleted_at)) { + if ($found->active == 0 || $found->deleted_at) { $reactivationApplied = $this->attemptContractReactivation($found, $user); if ($reactivationApplied['reactivated']) { $reactivatedThisRow = true; @@ -300,7 +300,7 @@ public function process(Import $import, ?Authenticatable $user = null): array } else { $contractResult = $this->upsertContractChain($import, $mapped, $mappings); // If contract was resolved/updated/inserted and reactivation requested but not needed (already active), we just continue normal flow. - if ($reactivateMode && $contractResult && isset($contractResult['contract']) && $contractResult['contract'] instanceof Contract) { + if ($contractResult && isset($contractResult['contract']) && $contractResult['contract'] instanceof Contract) { $found = $contractResult['contract']; if ($found->active == 0 || $found->deleted_at) { $reactivationApplied = $this->attemptContractReactivation($found, $user); @@ -1501,7 +1501,7 @@ private function upsertContractChain(Import $import, array $mapped, $mappings): // Try to find existing contract EARLY by (client_id, reference) across all cases to prevent duplicates $existing = null; if ($clientId) { - $existing = Contract::query() + $existing = Contract::query()->withTrashed() ->join('client_cases', 'contracts.client_case_id', '=', 'client_cases.id') ->where('client_cases.client_id', $clientId) ->where('contracts.reference', $reference) @@ -1511,7 +1511,7 @@ private function upsertContractChain(Import $import, array $mapped, $mappings): // If not found by client+reference and a specific client_case_id is provided, try that too if (! $existing && $clientCaseId) { - $existing = Contract::query() + $existing = Contract::query()->withTrashed() ->where('client_case_id', $clientCaseId) ->where('reference', $reference) ->first();