*/ public function share(Request $request): array { return [ ...parent::share($request), 'auth' => [ 'user' => $request->user()?->load(['contractor', 'employeeProfile', 'customerProfile']), 'roles' => $request->user()?->getRoleNames() ?? [], 'permissions' => $request->user()?->getAllPermissions()->pluck('name') ?? [], ], 'flash' => [ 'success' => $request->session()->get('success'), 'error' => $request->session()->get('error'), ], 'sidebarBadges' => fn () => $request->user() ? (function () use ($request) { $user = $request->user(); // Drafts belong to their creator. Submitted requests are // relevant only when the current user is an assigned approver. $ownDraftRequisitions = MaterialRequisition::where('status', 'draft') ->where('requested_by', $user->id) ->count(); $assignedRequisitionApprovals = ApprovalChain::where('status', 'pending') ->where('type', 'material_requisition') ->whereHas('steps', function ($query) use ($user) { $query->where('approver_id', $user->id) ->where('status', 'pending'); }) ->count(); $ownDraftPurchaseOrders = PurchaseOrder::where('status', 'draft') ->where('requested_by', $user->id) ->count(); $assignedApprovals = ApprovalChain::where('status', 'pending') ->whereHas('steps', function ($query) use ($user) { $query->where('approver_id', $user->id) ->where('status', 'pending'); }) ->count(); return [ 'pending_requisitions' => $ownDraftRequisitions + $assignedRequisitionApprovals, 'pending_purchase_orders' => $ownDraftPurchaseOrders, 'pending_approvals' => $assignedApprovals, ]; })() : null, 'projectOptions' => fn () => $request->user() ? Project::where('current_wizard_step', '>=', 7) ->whereHas('bidPackages') ->with('parentProject:id,ulid,name,code') ->get(['id', 'ulid', 'name', 'code', 'project_type', 'parent_project_id', 'status']) : [], 'unconfirmed_payments' => fn () => $request->user() ? (function () use ($request) { $visibleProjectIds = Project::query()->pluck('projects.id')->all(); $pendingInvoices = \Modules\FinancialManagement\Models\FinancialInvoice::with('project:id,name,code') ->whereIn('project_id', $visibleProjectIds) ->where('status', 'payment_sent') ->get(['id', 'ulid', 'invoice_number', 'project_id', 'total_amount', 'status']) ->map(fn ($inv) => [ 'id' => 'inv_' . $inv->id, 'ulid' => $inv->ulid, 'number' => $inv->invoice_number, 'project_name' => $inv->project?->name ?? 'Project', 'amount' => (float) $inv->total_amount, 'type' => 'invoice', 'confirm_url' => route('finance.confirm-payment', $inv->ulid), 'view_url' => route('finance.show', $inv->ulid), ]); $pendingRetentions = \Modules\FinancialManagement\Models\RetentionEntry::with('project:id,name,code') ->whereIn('project_id', $visibleProjectIds) ->where('type', 'credit') ->whereIn('status', ['payment_sent', 'submitted']) ->get(['id', 'ulid', 'project_id', 'amount', 'status', 'description']) ->map(fn ($ret) => [ 'id' => 'ret_' . $ret->id, 'ulid' => $ret->ulid, 'number' => 'Retention Release', 'project_name' => $ret->project?->name ?? 'Project', 'amount' => (float) $ret->amount, 'type' => 'retention', 'status' => $ret->status, 'confirm_url' => route('retention.confirm', $ret->ulid), 'view_url' => route('retention.index'), ]); $merged = $pendingInvoices->concat($pendingRetentions); return [ 'count' => $merged->count(), 'total_amount' => (float) $merged->sum('amount'), 'items' => $merged->values()->all(), ]; })() : ['count' => 0, 'total_amount' => 0, 'items' => []], ]; } }