update case index page segment index and show page

This commit is contained in:
Simon Pocrnjič
2025-12-14 20:57:39 +01:00
parent a6ec92ec6b
commit 80948d2944
14 changed files with 1141 additions and 626 deletions
+68 -12
View File
@@ -4,15 +4,18 @@
use App\Http\Requests\StoreContractRequest;
use App\Http\Requests\UpdateContractRequest;
use App\Models\Client;
use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\Document;
use App\Models\Segment;
use App\Services\Documents\DocumentStreamService;
use App\Services\ReferenceDataCache;
use App\Services\Sms\SmsService;
use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
@@ -30,6 +33,16 @@ public function __construct(
public function index(ClientCase $clientCase, Request $request)
{
$search = $request->input('search');
$from = $this->normalizeDate($request->input('from'));
$to = $this->normalizeDate($request->input('to'));
$clientFilter = collect(explode(',', (string) $request->input('clients')))
->filter()
->map(fn ($value) => (int) $value)
->filter(fn ($value) => $value > 0)
->unique()
->values();
$perPage = $this->resolvePerPage($request);
$query = $clientCase::query()
->select('client_cases.*')
@@ -39,7 +52,6 @@ public function index(ClientCase $clientCase, Request $request)
->groupBy('client_cases.id');
})
->where('client_cases.active', 1)
// Use LEFT JOINs for aggregated data to avoid subqueries
->leftJoin('contracts', function ($join) {
$join->on('contracts.client_case_id', '=', 'client_cases.id')
->whereNull('contracts.deleted_at');
@@ -49,11 +61,18 @@ public function index(ClientCase $clientCase, Request $request)
->where('contract_segment.active', true);
})
->leftJoin('accounts', 'accounts.contract_id', '=', 'contracts.id')
->when($clientFilter->isNotEmpty(), function ($que) use ($clientFilter) {
$que->whereIn('client_cases.client_id', $clientFilter->all());
})
->when($from, function ($que) use ($from) {
$que->whereDate('client_cases.created_at', '>=', $from);
})
->when($to, function ($que) use ($to) {
$que->whereDate('client_cases.created_at', '<=', $to);
})
->groupBy('client_cases.id')
->addSelect([
// Count of active contracts (a contract is considered active if it has an active pivot in contract_segment)
\DB::raw('COUNT(DISTINCT CASE WHEN contract_segment.id IS NOT NULL THEN contracts.id END) as active_contracts_count'),
// Sum of balances for accounts of active contracts
\DB::raw('COALESCE(SUM(CASE WHEN contract_segment.id IS NOT NULL THEN accounts.balance_amount END), 0) as active_contracts_balance_sum'),
])
->with(['person.client', 'client.person'])
@@ -61,12 +80,49 @@ public function index(ClientCase $clientCase, Request $request)
return Inertia::render('Cases/Index', [
'client_cases' => $query
->paginate($request->integer('perPage', 15), ['*'], 'clientCasesPage')
->paginate($perPage, ['*'], 'clientCasesPage')
->withQueryString(),
'filters' => $request->only(['search']),
'filters' => [
'search' => $search,
'from' => $from,
'to' => $to,
'clients' => $clientFilter->map(fn ($value) => (string) $value)->all(),
'perPage' => $perPage,
],
'clients' => Client::query()
->select(['clients.id', 'person.full_name as name'])
->join('person', 'person.id', '=', 'clients.person_id')
->orderBy('person.full_name')
->get()
->map(fn ($client) => [
'id' => (int) $client->id,
'name' => (string) ($client->name ?? ''),
]),
]);
}
private function resolvePerPage(Request $request): int
{
$allowed = [10, 15, 25, 50, 100];
$perPage = (int) $request->integer('perPage', 15);
return in_array($perPage, $allowed, true) ? $perPage : 15;
}
private function normalizeDate(?string $value): ?string
{
if (! $value) {
return null;
}
try {
return Carbon::parse($value)->toDateString();
} catch (\Throwable) {
return null;
}
}
/**
* Show the form for creating a new resource.
*/
@@ -717,7 +773,7 @@ public function show(ClientCase $clientCase)
$documents = $this->caseDataService->getDocuments($case, $contractIds, $contractsPerPage);
// Get archive metadata using service
$archiveMeta = $this->caseDataService->getArchiveMeta();
return Inertia::render('Cases/Show', [
@@ -1031,7 +1087,7 @@ public function emergencyCreatePerson(ClientCase $clientCase, Request $request)
if ($existing && ! $existing->trashed()) {
return back()->with('flash', [
'type' => 'info',
'message' => 'Person already exists emergency creation not needed.',
'message' => 'Person already exists ÔÇô emergency creation not needed.',
]);
}
@@ -1136,10 +1192,10 @@ public function sendSmsToPhone(ClientCase $clientCase, Request $request, int $ph
if (! empty($validated['sender_id'])) {
$sender = \App\Models\SmsSender::query()->find($validated['sender_id']);
if (! $sender) {
return back()->with('error', 'Izbran pošiljatelj ne obstaja.');
return back()->with('error', 'Izbran pošiljatelj ne obstaja.');
}
if ($profile && (int) $sender->profile_id !== (int) $profile->id) {
return back()->with('error', 'Izbran pošiljatelj ne pripada izbranemu profilu.');
return back()->with('error', 'Izbran pošiljatelj ne pripada izbranemu profilu.');
}
}
if (! $profile) {
@@ -1182,7 +1238,7 @@ public function sendSmsToPhone(ClientCase $clientCase, Request $request, int $ph
}
// Create an activity before sending
$activityNote = sprintf('Št: %s | Telo: %s', (string) $phone->nu, (string) $validated['message']);
$activityNote = sprintf('Št: %s | Telo: %s', (string) $phone->nu, (string) $validated['message']);
$activityData = [
'note' => $activityNote,
'user_id' => optional($request->user())->id,
@@ -1220,7 +1276,7 @@ public function sendSmsToPhone(ClientCase $clientCase, Request $request, int $ph
activityId: $activity?->id,
);
return back()->with('success', 'SMS je bil dodan v čakalno vrsto.');
return back()->with('success', 'SMS je bil dodan v ─Źakalno vrsto.');
} catch (\Throwable $e) {
\Log::warning('SMS enqueue failed', [
'error' => $e->getMessage(),
@@ -1228,7 +1284,7 @@ public function sendSmsToPhone(ClientCase $clientCase, Request $request, int $ph
'phone_id' => $phone_id,
]);
return back()->with('error', 'SMS ni bil dodan v čakalno vrsto.');
return back()->with('error', 'SMS ni bil dodan v ─Źakalno vrsto.');
}
}