updated search and fix error in template where it removed client from template when saving in edit
This commit is contained in:
+59
-7
@@ -61,8 +61,10 @@
|
||||
})->name('dashboard');
|
||||
|
||||
Route::get('testing', function () {
|
||||
return Inertia::render('Testing', []);
|
||||
});
|
||||
return Inertia::render('Testing/Index', [
|
||||
'example' => 'Hello World',
|
||||
]);
|
||||
})->name('testing.index');
|
||||
|
||||
// Phone page
|
||||
Route::get('phone', [PhoneViewController::class, 'index'])->name('phone.index');
|
||||
@@ -88,7 +90,7 @@
|
||||
$builder->join('client_cases', 'person.id', '=', 'client_cases.person_id')
|
||||
->leftJoin('person_addresses', 'person.id', '=', 'person_addresses.person_id')
|
||||
->leftJoin('person_phones', 'person.id', '=', 'person_phones.person_id')
|
||||
->select('person.*', 'client_cases.uuid as case_uuid')
|
||||
->select('person.*', 'client_cases.uuid as case_uuid', 'client_cases.id as case_id')
|
||||
->limit($request->input('limit'));
|
||||
})
|
||||
->get();
|
||||
@@ -100,9 +102,21 @@
|
||||
$contractCases = \App\Models\Contract::query()
|
||||
->join('client_cases', 'contracts.client_case_id', '=', 'client_cases.id')
|
||||
->join('person', 'client_cases.person_id', '=', 'person.id')
|
||||
// portable case-insensitive match across drivers
|
||||
->leftJoin('contract_segment', function ($j) {
|
||||
$j->on('contract_segment.contract_id', '=', 'contracts.id')
|
||||
->where('contract_segment.active', true);
|
||||
})
|
||||
->leftJoin('segments', 'segments.id', '=', 'contract_segment.segment_id')
|
||||
// case-insensitive reference match
|
||||
->whereRaw('LOWER(contracts.reference) LIKE ?', ['%'.mb_strtolower($query).'%'])
|
||||
->select('person.*', 'client_cases.uuid as case_uuid')
|
||||
->select(
|
||||
'person.*',
|
||||
'client_cases.uuid as case_uuid',
|
||||
'client_cases.id as case_id',
|
||||
'contracts.reference as contract_reference',
|
||||
\DB::raw("COALESCE(json_agg(DISTINCT jsonb_build_object('id', segments.id, 'name', segments.name)) FILTER (WHERE segments.id IS NOT NULL), '[]') as contract_segments")
|
||||
)
|
||||
->groupBy('person.id', 'client_cases.uuid', 'client_cases.id', 'contracts.reference')
|
||||
->limit($limit)
|
||||
->get();
|
||||
|
||||
@@ -110,8 +124,46 @@
|
||||
$clientCases = $clientCases
|
||||
->concat($contractCases)
|
||||
->unique('case_uuid')
|
||||
->values()
|
||||
->take($limit);
|
||||
->values();
|
||||
|
||||
// Collect all case ids for segment lookup (for non-contract matches)
|
||||
$caseIds = $clientCases->pluck('case_id')->filter()->unique()->values();
|
||||
if ($caseIds->isNotEmpty()) {
|
||||
$caseSegments = \DB::table('client_cases')
|
||||
->join('contracts', 'contracts.client_case_id', '=', 'client_cases.id')
|
||||
->join('contract_segment', function ($j) {
|
||||
$j->on('contract_segment.contract_id', '=', 'contracts.id')
|
||||
->where('contract_segment.active', true);
|
||||
})
|
||||
->join('segments', 'segments.id', '=', 'contract_segment.segment_id')
|
||||
->whereIn('client_cases.id', $caseIds)
|
||||
->select(
|
||||
'client_cases.id as case_id',
|
||||
\DB::raw("COALESCE(json_agg(DISTINCT jsonb_build_object('id', segments.id, 'name', segments.name)) FILTER (WHERE segments.id IS NOT NULL), '[]') as segments_json")
|
||||
)
|
||||
->groupBy('client_cases.id')
|
||||
->get()
|
||||
->keyBy('case_id');
|
||||
|
||||
$clientCases = $clientCases->map(function ($row) use ($contractCases, $caseSegments) {
|
||||
$contractHit = $contractCases->firstWhere('case_uuid', $row->case_uuid);
|
||||
if ($contractHit) {
|
||||
$row->contract_reference = $contractHit->contract_reference;
|
||||
$segmentsJson = $contractHit->contract_segments ?? '[]';
|
||||
$row->contract_segments = is_string($segmentsJson) ? json_decode($segmentsJson, true) : (array) $segmentsJson;
|
||||
} else {
|
||||
$segRow = $caseSegments->get($row->case_id);
|
||||
if ($segRow) {
|
||||
$row->case_segments = json_decode($segRow->segments_json, true) ?? [];
|
||||
} else {
|
||||
$row->case_segments = [];
|
||||
}
|
||||
}
|
||||
return $row;
|
||||
})->take($limit);
|
||||
} else {
|
||||
$clientCases = $clientCases->take($limit);
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user