user('web'); abort_unless($identity instanceof User && $identity->tenant_id !== null, 403); $actor = User::query()->where('tenant_id', $identity->tenant_id)->findOrFail($identity->id); abort_if(app(OrganizationAccess::class)->maintenanceOnly($actor), 403); abort_unless(app(OrganizationAccess::class)->permissions($actor) !== [], 403); $request->setUserResolver(fn () => $actor); $data = $request->validate(['q' => 'nullable|string|max:100', 'requests_page' => 'nullable|integer|min:1|max:1000000', 'orders_page' => 'nullable|integer|min:1|max:1000000', 'suppliers_page' => 'nullable|integer|min:1|max:1000000']); $term = trim($data['q'] ?? ''); $request->validate(['invoices_page' => 'nullable|integer|min:1|max:1000000']); $request->validate(['equipment_page' => 'nullable|integer|min:1|max:1000000', 'stock_page' => 'nullable|integer|min:1|max:1000000', 'warehouses_page' => 'nullable|integer|min:1|max:1000000', 'receipts_page' => 'nullable|integer|min:1|max:1000000']); $groups = DB::transaction(function () use ($actor, $term) { if (DB::getDriverName() === 'pgsql' && DB::transactionLevel() === 1) { DB::statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY'); } if (mb_strlen($term) < 2) { return []; } // Treat wildcard characters as text; never expand a term into an unrestricted match. $pattern = '%'.str_replace(['!', '%', '_'], ['!!', '!%', '!_'], mb_strtolower($term)).'%'; $requests = Requisition::query()->visibleTo($actor)->where(function ($query) use ($pattern) { $query->whereRaw("LOWER(number) LIKE ? ESCAPE '!'", [$pattern])->orWhereRaw("LOWER(justification) LIKE ? ESCAPE '!'", [$pattern]); })->latest('id')->paginate(15, ['id', 'number', 'status'], 'requests_page')->withQueryString() ->through(fn ($row) => ['id' => $row->id, 'title' => $row->number, 'detail' => $row->status, 'href' => '/requisicoes/'.$row->id]); $groups = [['key' => 'requests', 'title' => 'Requisições', 'records' => $requests]]; if ($actor->hasFunctions([PurchasingRole::Admin, PurchasingRole::Procurement, PurchasingRole::Finance, PurchasingRole::DepartmentHead])) { $orders = PurchaseOrder::query()->visibleTo($actor)->whereRaw("LOWER(number) LIKE ? ESCAPE '!'", [$pattern])->latest('id') ->paginate(15, ['id', 'number', 'status'], 'orders_page')->withQueryString() ->through(fn ($row) => ['id' => $row->id, 'title' => $row->number, 'detail' => $row->status, 'href' => '/ordens/'.$row->id]); $groups[] = ['key' => 'orders', 'title' => 'Ordens', 'records' => $orders]; } if (Gate::forUser($actor)->allows('viewAny', Supplier::class)) { $suppliers = Supplier::query()->where(function ($query) use ($pattern) { $query->whereRaw("LOWER(name) LIKE ? ESCAPE '!'", [$pattern])->orWhereRaw("LOWER(nuit) LIKE ? ESCAPE '!'", [$pattern]); })->latest('id')->paginate(15, ['id', 'name', 'status'], 'suppliers_page')->withQueryString() ->through(fn ($row) => ['id' => $row->id, 'title' => $row->name, 'detail' => $row->status, 'href' => '/fornecedores/'.$row->id]); $groups[] = ['key' => 'suppliers', 'title' => 'Fornecedores', 'records' => $suppliers]; } if ($actor->hasFunctions([PurchasingRole::Admin, PurchasingRole::Warehouse, PurchasingRole::Procurement, PurchasingRole::Finance, PurchasingRole::DepartmentHead])) { $equipment = Equipment::query()->when($actor->departmentScoped(), fn ($query) => $query->where('department_id', $actor->department_id)) ->where(fn ($query) => $query->whereRaw("LOWER(code) LIKE ? ESCAPE '!'", [$pattern])->orWhereRaw("LOWER(description) LIKE ? ESCAPE '!'", [$pattern])) ->latest('id')->paginate(15, ['id', 'code', 'description'], 'equipment_page')->withQueryString() ->through(fn ($row) => ['id' => $row->id, 'title' => $row->code.' · '.$row->description, 'detail' => 'Equipamento', 'href' => '/equipamentos/'.$row->id]); $groups[] = ['key' => 'equipment', 'title' => 'Equipamentos', 'records' => $equipment]; } if ($actor->hasFunctions([PurchasingRole::Admin, PurchasingRole::Warehouse, PurchasingRole::Procurement, PurchasingRole::Finance])) { foreach (['stock' => [StockItem::class, 'Artigos de stock'], 'warehouses' => [Warehouse::class, 'Armazéns']] as $key => [$model, $label]) { $records = $model::query()->where(fn ($query) => $query->whereRaw("LOWER(code) LIKE ? ESCAPE '!'", [$pattern])->orWhereRaw("LOWER(name) LIKE ? ESCAPE '!'", [$pattern])) ->latest('id')->paginate(15, ['id', 'code', 'name'], $key.'_page')->withQueryString() ->through(fn ($row) => ['id' => $row->id, 'title' => $row->code.' · '.$row->name, 'detail' => $label, 'href' => $key === 'stock' ? '/stock?search='.rawurlencode($row->code) : '/stock?warehouse_id='.$row->id]); $groups[] = ['key' => $key, 'title' => $label, 'records' => $records]; } } if (Gate::forUser($actor)->allows('viewAny', GoodsReceipt::class)) { $orders = PurchaseOrder::query()->when($actor->departmentScoped(), fn ($query) => $query->whereIn('requisition_id', Requisition::query()->visibleTo($actor)->select('id'))); $receipts = GoodsReceipt::query()->whereIn('purchase_order_id', $orders->select('id'))->whereRaw("LOWER(delivery_note) LIKE ? ESCAPE '!'", [$pattern]) ->latest('id')->paginate(15, ['id', 'delivery_note'], 'receipts_page')->withQueryString() ->through(fn ($row) => ['id' => $row->id, 'title' => 'Recepção #'.$row->id.' · '.$row->delivery_note, 'detail' => 'Recepção', 'href' => '/recepcoes/'.$row->id]); $groups[] = ['key' => 'receipts', 'title' => 'Recepções', 'records' => $receipts]; } if (config('procurement_search.finance_metadata_enabled') === true && $actor->hasFunctions([PurchasingRole::Admin, PurchasingRole::Finance])) { $invoices = Invoice::query()->whereIn('purchase_order_id', PurchaseOrder::query()->visibleTo($actor)->select('id')) ->whereRaw("LOWER(number) LIKE ? ESCAPE '!'", [$pattern])->latest('id') ->paginate(15, ['id', 'number', 'status'], 'invoices_page')->withQueryString() ->through(fn ($row) => ['id' => $row->id, 'title' => $row->number, 'detail' => $row->status, 'href' => '/facturas?invoice_id='.$row->id]); $groups[] = ['key' => 'invoices', 'title' => 'Facturas de fornecedor', 'records' => $invoices]; } return $groups; }); Inertia::encryptHistory(); $response = Inertia::render('search/procurement', ['term' => $term, 'groups' => $groups, 'invoiceSearchAvailable' => config('procurement_search.finance_metadata_enabled') === true && $actor->hasFunctions([PurchasingRole::Admin, PurchasingRole::Finance]), ])->toResponse($request); $response->headers->set('Cache-Control', 'private, no-store, max-age=0'); return $response; } }