This commit is contained in:
Simon Pocrnjič
2025-10-20 19:39:26 +02:00
parent 90bbf1942c
commit 872b76b012
9 changed files with 268 additions and 43 deletions
@@ -415,6 +415,15 @@ public function missingContracts(Import $import)
->where('client_cases.client_id', $import->client_id)
->where('contracts.active', 1)
->whereNull('contracts.deleted_at')
// Exclude contracts that have any ACTIVE segment marked as excluded
->whereNotExists(function ($sq) {
$sq->select(\DB::raw(1))
->from('contract_segment')
->join('segments', 'segments.id', '=', 'contract_segment.segment_id')
->whereColumn('contract_segment.contract_id', 'contracts.id')
->where('contract_segment.active', true)
->where('segments.exclude', true);
})
->when(count($present) > 0, function ($q) use ($present) {
$q->whereNotIn('contracts.reference', $present);
})