76 lines
3.2 KiB
PHP
76 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Activity;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
|
|
class NotificationController extends Controller
|
|
{
|
|
public function unread(Request $request)
|
|
{
|
|
$user = $request->user();
|
|
if (! $user) {
|
|
abort(403);
|
|
}
|
|
|
|
$today = now()->toDateString();
|
|
$perPage = max(1, min(100, (int) $request->integer('perPage', 15)));
|
|
$search = trim((string) $request->input('search', ''));
|
|
|
|
$query = Activity::query()
|
|
->select(['id', 'due_date', 'amount', 'contract_id', 'client_case_id', 'created_at'])
|
|
->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');
|
|
})
|
|
// allow simple search by contract reference or person name
|
|
->when($search !== '', function ($q) use ($search) {
|
|
$s = mb_strtolower($search);
|
|
$q->leftJoin('contracts', 'contracts.id', '=', 'activities.contract_id')
|
|
->leftJoin('client_cases', 'client_cases.id', '=', 'activities.client_case_id')
|
|
->leftJoin('person', 'person.id', '=', 'client_cases.person_id')
|
|
->where(function ($qq) use ($s) {
|
|
$qq->whereRaw('LOWER(COALESCE(contracts.reference, \'\')) LIKE ?', ['%'.$s.'%'])
|
|
->orWhereRaw('LOWER(COALESCE(person.full_name, \'\')) LIKE ?', ['%'.$s.'%']);
|
|
});
|
|
})
|
|
->with([
|
|
'contract' => function ($q) {
|
|
$q->select(['contracts.id', 'contracts.uuid', 'contracts.reference', 'contracts.client_case_id'])
|
|
->with([
|
|
'clientCase' => function ($qq) {
|
|
$qq->select(['client_cases.id', 'client_cases.uuid']);
|
|
},
|
|
'account' => function ($qq) {
|
|
$qq->select(['accounts.id', 'accounts.contract_id', 'accounts.balance_amount', 'accounts.initial_amount']);
|
|
},
|
|
]);
|
|
},
|
|
'clientCase' => function ($q) {
|
|
$q->select(['client_cases.id', 'client_cases.uuid', 'client_cases.person_id'])
|
|
->with([
|
|
'person' => function ($qq) {
|
|
$qq->select(['person.id', 'person.full_name']);
|
|
},
|
|
]);
|
|
},
|
|
])
|
|
// force ordering by due_date DESC only
|
|
->orderByDesc('activities.due_date');
|
|
|
|
// Use a custom page parameter name to match the frontend DataTableServer
|
|
$activities = $query->paginate($perPage, ['*'], 'unread-page')->withQueryString();
|
|
|
|
return Inertia::render('Notifications/Unread', [
|
|
'activities' => $activities,
|
|
'today' => $today,
|
|
]);
|
|
}
|
|
}
|