Notifications change
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Activity;
|
||||
use App\Models\ClientCase;
|
||||
use App\Models\Contract;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
|
||||
@@ -18,6 +20,11 @@ public function unread(Request $request)
|
||||
$today = now()->toDateString();
|
||||
$perPage = max(1, min(100, (int) $request->integer('perPage', 15)));
|
||||
$search = trim((string) $request->input('search', ''));
|
||||
$clientUuid = trim((string) $request->input('client', ''));
|
||||
$clientCaseId = null;
|
||||
if ($clientUuid !== '') {
|
||||
$clientCaseId = ClientCase::query()->where('uuid', $clientUuid)->value('id');
|
||||
}
|
||||
|
||||
$query = Activity::query()
|
||||
->select(['id', 'due_date', 'amount', 'contract_id', 'client_case_id', 'created_at'])
|
||||
@@ -29,6 +36,16 @@ public function unread(Request $request)
|
||||
->where('anr.user_id', $user->id)
|
||||
->whereColumn('anr.due_date', 'activities.due_date');
|
||||
})
|
||||
->when($clientCaseId, function ($q) use ($clientCaseId) {
|
||||
// Match activities for the client case directly OR via contracts belonging to the case
|
||||
$q->where(function ($qq) use ($clientCaseId) {
|
||||
$qq->where('activities.client_case_id', $clientCaseId)
|
||||
->orWhereIn('activities.contract_id', Contract::query()
|
||||
->select('id')
|
||||
->where('client_case_id', $clientCaseId)
|
||||
);
|
||||
});
|
||||
})
|
||||
// allow simple search by contract reference or person name
|
||||
->when($search !== '', function ($q) use ($search) {
|
||||
$s = mb_strtolower($search);
|
||||
@@ -45,7 +62,17 @@ public function unread(Request $request)
|
||||
$q->select(['contracts.id', 'contracts.uuid', 'contracts.reference', 'contracts.client_case_id'])
|
||||
->with([
|
||||
'clientCase' => function ($qq) {
|
||||
$qq->select(['client_cases.id', 'client_cases.uuid']);
|
||||
$qq->select(['client_cases.id', 'client_cases.uuid', 'client_cases.client_id'])
|
||||
->with([
|
||||
'client' => function ($qqq) {
|
||||
$qqq->select(['clients.id', 'clients.person_id'])
|
||||
->with([
|
||||
'person' => function ($qqqq) {
|
||||
$qqqq->select(['person.id', 'person.full_name']);
|
||||
},
|
||||
]);
|
||||
},
|
||||
]);
|
||||
},
|
||||
'account' => function ($qq) {
|
||||
$qq->select(['accounts.id', 'accounts.contract_id', 'accounts.balance_amount', 'accounts.initial_amount']);
|
||||
@@ -53,11 +80,19 @@ public function unread(Request $request)
|
||||
]);
|
||||
},
|
||||
'clientCase' => function ($q) {
|
||||
$q->select(['client_cases.id', 'client_cases.uuid', 'client_cases.person_id'])
|
||||
$q->select(['client_cases.id', 'client_cases.uuid', 'client_cases.person_id', 'client_cases.client_id'])
|
||||
->with([
|
||||
'person' => function ($qq) {
|
||||
$qq->select(['person.id', 'person.full_name']);
|
||||
},
|
||||
'client' => function ($qq) {
|
||||
$qq->select(['clients.id', 'clients.person_id'])
|
||||
->with([
|
||||
'person' => function ($qqq) {
|
||||
$qqq->select(['person.id', 'person.full_name']);
|
||||
},
|
||||
]);
|
||||
},
|
||||
]);
|
||||
},
|
||||
])
|
||||
@@ -67,9 +102,52 @@ public function unread(Request $request)
|
||||
// Use a custom page parameter name to match the frontend DataTableServer
|
||||
$activities = $query->paginate($perPage, ['*'], 'unread-page')->withQueryString();
|
||||
|
||||
// Build a distinct clients list for the filter (client_case UUID + person.full_name)
|
||||
// Collect client_case_ids from both direct activities and via contracts
|
||||
$baseForClients = Activity::query()
|
||||
->select(['contract_id', 'client_case_id'])
|
||||
->whereNotNull('due_date')
|
||||
->whereDate('due_date', '<=', $today)
|
||||
->whereNotExists(function ($q) use ($user) {
|
||||
$q->from('activity_notification_reads as anr')
|
||||
->whereColumn('anr.activity_id', 'activities.id')
|
||||
->where('anr.user_id', $user->id)
|
||||
->whereColumn('anr.due_date', 'activities.due_date');
|
||||
})
|
||||
->when($clientCaseId, function ($q) use ($clientCaseId) {
|
||||
$q->where(function ($qq) use ($clientCaseId) {
|
||||
$qq->where('activities.client_case_id', $clientCaseId)
|
||||
->orWhereIn('activities.contract_id', Contract::query()->select('id')->where('client_case_id', $clientCaseId));
|
||||
});
|
||||
})
|
||||
->get();
|
||||
|
||||
$contractIds = $baseForClients->pluck('contract_id')->filter()->unique()->values();
|
||||
$directCaseIds = $baseForClients->pluck('client_case_id')->filter()->unique()->values();
|
||||
$mapContractToCase = $contractIds->isNotEmpty()
|
||||
? Contract::query()->whereIn('id', $contractIds)->pluck('client_case_id', 'id')
|
||||
: collect();
|
||||
$caseIds = $directCaseIds
|
||||
->merge($contractIds->map(fn ($cid) => $mapContractToCase->get($cid)))
|
||||
->filter()
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
$clients = ClientCase::query()
|
||||
->whereIn('id', $caseIds)
|
||||
->with(['person:id,full_name'])
|
||||
->get(['id', 'uuid', 'person_id'])
|
||||
->map(fn ($cc) => [
|
||||
'value' => $cc->uuid,
|
||||
'label' => optional($cc->person)->full_name ?: '(neznana stranka)',
|
||||
])
|
||||
->sortBy('label', SORT_NATURAL | SORT_FLAG_CASE)
|
||||
->values();
|
||||
|
||||
return Inertia::render('Notifications/Unread', [
|
||||
'activities' => $activities,
|
||||
'today' => $today,
|
||||
'clients' => $clients,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user