SMS service
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Activity;
|
||||
use App\Models\Client;
|
||||
use App\Models\ClientCase;
|
||||
use App\Models\Contract;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -21,9 +22,13 @@ public function unread(Request $request)
|
||||
$perPage = max(1, min(100, (int) $request->integer('perPage', 15)));
|
||||
$search = trim((string) $request->input('search', ''));
|
||||
$clientUuid = trim((string) $request->input('client', ''));
|
||||
$clientCaseId = null;
|
||||
$clientId = null;
|
||||
$clientCaseIdsForFilter = collect();
|
||||
if ($clientUuid !== '') {
|
||||
$clientCaseId = ClientCase::query()->where('uuid', $clientUuid)->value('id');
|
||||
$clientId = Client::query()->where('uuid', $clientUuid)->value('id');
|
||||
if ($clientId) {
|
||||
$clientCaseIdsForFilter = ClientCase::query()->where('client_id', $clientId)->pluck('id');
|
||||
}
|
||||
}
|
||||
|
||||
$query = Activity::query()
|
||||
@@ -36,13 +41,13 @@ 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)
|
||||
->when($clientCaseIdsForFilter->isNotEmpty(), function ($q) use ($clientCaseIdsForFilter) {
|
||||
// Filter by clients: activities directly on any of the client's cases OR via contracts under those cases
|
||||
$q->where(function ($qq) use ($clientCaseIdsForFilter) {
|
||||
$qq->whereIn('activities.client_case_id', $clientCaseIdsForFilter)
|
||||
->orWhereIn('activities.contract_id', Contract::query()
|
||||
->select('id')
|
||||
->where('client_case_id', $clientCaseId)
|
||||
->whereIn('client_case_id', $clientCaseIdsForFilter)
|
||||
);
|
||||
});
|
||||
})
|
||||
@@ -65,7 +70,7 @@ public function unread(Request $request)
|
||||
$qq->select(['client_cases.id', 'client_cases.uuid', 'client_cases.client_id'])
|
||||
->with([
|
||||
'client' => function ($qqq) {
|
||||
$qqq->select(['clients.id', 'clients.person_id'])
|
||||
$qqq->select(['clients.id', 'clients.uuid', 'clients.person_id'])
|
||||
->with([
|
||||
'person' => function ($qqqq) {
|
||||
$qqqq->select(['person.id', 'person.full_name']);
|
||||
@@ -86,7 +91,7 @@ public function unread(Request $request)
|
||||
$qq->select(['person.id', 'person.full_name']);
|
||||
},
|
||||
'client' => function ($qq) {
|
||||
$qq->select(['clients.id', 'clients.person_id'])
|
||||
$qq->select(['clients.id', 'clients.uuid', 'clients.person_id'])
|
||||
->with([
|
||||
'person' => function ($qqq) {
|
||||
$qqq->select(['person.id', 'person.full_name']);
|
||||
@@ -102,7 +107,7 @@ 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)
|
||||
// Build a distinct clients list for the filter (client UUID + client.person.full_name)
|
||||
// Collect client_case_ids from both direct activities and via contracts
|
||||
$baseForClients = Activity::query()
|
||||
->select(['contract_id', 'client_case_id'])
|
||||
@@ -114,10 +119,10 @@ public function unread(Request $request)
|
||||
->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));
|
||||
->when($clientCaseIdsForFilter->isNotEmpty(), function ($q) use ($clientCaseIdsForFilter) {
|
||||
$q->where(function ($qq) use ($clientCaseIdsForFilter) {
|
||||
$qq->whereIn('activities.client_case_id', $clientCaseIdsForFilter)
|
||||
->orWhereIn('activities.contract_id', Contract::query()->select('id')->whereIn('client_case_id', $clientCaseIdsForFilter));
|
||||
});
|
||||
})
|
||||
->get();
|
||||
@@ -133,16 +138,23 @@ public function unread(Request $request)
|
||||
->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();
|
||||
// Map caseIds -> clientIds, then load clients and present as value(label)
|
||||
$clientIds = $caseIds->isNotEmpty()
|
||||
? ClientCase::query()->whereIn('id', $caseIds)->pluck('client_id')->filter()->unique()->values()
|
||||
: collect();
|
||||
|
||||
$clients = $clientIds->isNotEmpty()
|
||||
? Client::query()
|
||||
->whereIn('id', $clientIds)
|
||||
->with(['person:id,full_name'])
|
||||
->get(['id', 'uuid', 'person_id'])
|
||||
->map(fn ($c) => [
|
||||
'value' => $c->uuid,
|
||||
'label' => optional($c->person)->full_name ?: '(neznana stranka)',
|
||||
])
|
||||
->sortBy('label', SORT_NATURAL | SORT_FLAG_CASE)
|
||||
->values()
|
||||
: collect();
|
||||
|
||||
return Inertia::render('Notifications/Unread', [
|
||||
'activities' => $activities,
|
||||
|
||||
Reference in New Issue
Block a user