fixed import where it did not reactivate contract that was archived
This commit is contained in:
parent
cb7851f91c
commit
bdde610178
|
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Traits\Uuid;
|
use App\Traits\Uuid;
|
||||||
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
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
|
public function type(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(\App\Models\ContractType::class, 'type_id');
|
return $this->belongsTo(\App\Models\ContractType::class, 'type_id');
|
||||||
|
|
|
||||||
|
|
@ -258,7 +258,7 @@ public function process(Import $import, ?Authenticatable $user = null): array
|
||||||
$ref = preg_replace('/\s+/', '', trim($ref));
|
$ref = preg_replace('/\s+/', '', trim($ref));
|
||||||
}
|
}
|
||||||
if ($ref) {
|
if ($ref) {
|
||||||
$q = Contract::query()
|
$q = Contract::query()->withTrashed()
|
||||||
->when($import->client_id, function ($q2, $clientId) {
|
->when($import->client_id, function ($q2, $clientId) {
|
||||||
$q2->join('client_cases', 'contracts.client_case_id', '=', 'client_cases.id')
|
$q2->join('client_cases', 'contracts.client_case_id', '=', 'client_cases.id')
|
||||||
->where('client_cases.client_id', $clientId);
|
->where('client_cases.client_id', $clientId);
|
||||||
|
|
@ -266,10 +266,10 @@ public function process(Import $import, ?Authenticatable $user = null): array
|
||||||
->where('contracts.reference', $ref)
|
->where('contracts.reference', $ref)
|
||||||
->select('contracts.*');
|
->select('contracts.*');
|
||||||
$found = $q->first();
|
$found = $q->first();
|
||||||
if ($found) {
|
if ($found instanceof \App\Models\Contract) {
|
||||||
$contractResult = ['action' => 'resolved', 'contract' => $found];
|
$contractResult = ['action' => 'resolved', 'contract' => $found];
|
||||||
// Reactivation branch for resolved existing contract
|
// 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);
|
$reactivationApplied = $this->attemptContractReactivation($found, $user);
|
||||||
if ($reactivationApplied['reactivated']) {
|
if ($reactivationApplied['reactivated']) {
|
||||||
$reactivatedThisRow = true;
|
$reactivatedThisRow = true;
|
||||||
|
|
@ -300,7 +300,7 @@ public function process(Import $import, ?Authenticatable $user = null): array
|
||||||
} else {
|
} else {
|
||||||
$contractResult = $this->upsertContractChain($import, $mapped, $mappings);
|
$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 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'];
|
$found = $contractResult['contract'];
|
||||||
if ($found->active == 0 || $found->deleted_at) {
|
if ($found->active == 0 || $found->deleted_at) {
|
||||||
$reactivationApplied = $this->attemptContractReactivation($found, $user);
|
$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
|
// Try to find existing contract EARLY by (client_id, reference) across all cases to prevent duplicates
|
||||||
$existing = null;
|
$existing = null;
|
||||||
if ($clientId) {
|
if ($clientId) {
|
||||||
$existing = Contract::query()
|
$existing = Contract::query()->withTrashed()
|
||||||
->join('client_cases', 'contracts.client_case_id', '=', 'client_cases.id')
|
->join('client_cases', 'contracts.client_case_id', '=', 'client_cases.id')
|
||||||
->where('client_cases.client_id', $clientId)
|
->where('client_cases.client_id', $clientId)
|
||||||
->where('contracts.reference', $reference)
|
->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 not found by client+reference and a specific client_case_id is provided, try that too
|
||||||
if (! $existing && $clientCaseId) {
|
if (! $existing && $clientCaseId) {
|
||||||
$existing = Contract::query()
|
$existing = Contract::query()->withTrashed()
|
||||||
->where('client_case_id', $clientCaseId)
|
->where('client_case_id', $clientCaseId)
|
||||||
->where('reference', $reference)
|
->where('reference', $reference)
|
||||||
->first();
|
->first();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user