@@ -7,6 +7,7 @@ use App\Models\Requisition;
|
||||
use App\Models\RequisitionItem;
|
||||
use App\Models\SupplyRecord;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
@@ -16,6 +17,8 @@ use Illuminate\Validation\ValidationException;
|
||||
/** Documentary production/maintenance orders owned by Compras; no production scheduling or SGF writes. */
|
||||
final class OperationalOrders
|
||||
{
|
||||
private const REGISTRY_READERS = ['admin', 'procurement', 'requester', 'production_order_prepare', 'maintenance_order_prepare'];
|
||||
|
||||
public function ready(): bool
|
||||
{
|
||||
$db = DB::connection();
|
||||
@@ -43,6 +46,26 @@ final class OperationalOrders
|
||||
return app(SupplyPlanning::class)->latest('op_order_policy')->first();
|
||||
}
|
||||
|
||||
/** @return Builder<SupplyRecord> */
|
||||
private function registryOrders(User $actor): Builder
|
||||
{
|
||||
return app(SupplyPlanning::class)->latest('op_order')->where('tenant_id', $actor->tenant_id)
|
||||
->when(! $actor->hasFunctions(['admin', 'procurement']), fn ($q) => $q->where('data->department_id', $actor->department_id));
|
||||
}
|
||||
|
||||
/** Current native registry scope for read-only cross-module search.
|
||||
* @return Builder<SupplyRecord>|null
|
||||
*/
|
||||
public function searchQuery(User $actor): ?Builder
|
||||
{
|
||||
if (! $this->ready() || auth()->id() !== $actor->id || $actor->tenant_id === null
|
||||
|| app(OrganizationAccess::class)->maintenanceOnly($actor) || ! $actor->hasFunctions(self::REGISTRY_READERS)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->registryOrders($actor);
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
private function types(User $actor, ?SupplyRecord $policy): array
|
||||
{
|
||||
@@ -57,15 +80,15 @@ final class OperationalOrders
|
||||
}
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
public function workspace(User $actor, ?Requisition $request = null): array
|
||||
public function workspace(User $actor, ?Requisition $request = null, ?int $selectedOrderId = null): array
|
||||
{
|
||||
return DB::transaction(function () use ($actor, $request) {
|
||||
return DB::transaction(function () use ($actor, $request, $selectedOrderId) {
|
||||
$actor = $this->actor($actor);
|
||||
$policy = $this->policy();
|
||||
if ($request !== null) {
|
||||
$request = Requisition::query()->visibleTo($actor)->findOrFail($request->id);
|
||||
} else {
|
||||
abort_unless($actor->hasFunctions(['admin', 'procurement', 'requester', 'production_order_prepare', 'maintenance_order_prepare']), 403);
|
||||
abort_unless($actor->hasFunctions(self::REGISTRY_READERS), 403);
|
||||
}
|
||||
$canLink = $request !== null && $request->status === 'draft' && $request->requester_id === $actor->id
|
||||
&& app(OrganizationAccess::class)->canRequest($actor) && ! app(ConflictGuard::class)->participationBlocked($actor, $request);
|
||||
@@ -75,15 +98,19 @@ final class OperationalOrders
|
||||
$linkedIds[] = $link->data['order_id'];
|
||||
}
|
||||
$allDepartments = $actor->hasFunctions(['admin', 'procurement']);
|
||||
$orders = app(SupplyPlanning::class)->latest('op_order')
|
||||
->when($request !== null && ! $canLink, fn ($q) => $q->whereIn('id', $linkedIds))
|
||||
->when($request !== null, fn ($q) => $q->where('data->department_id', $request->department_id))
|
||||
->when($request === null && ! $allDepartments, fn ($q) => $q->where('data->department_id', $actor->department_id));
|
||||
$orders = $request === null ? $this->registryOrders($actor)
|
||||
: app(SupplyPlanning::class)->latest('op_order')->where('data->department_id', $request->department_id)
|
||||
->when(! $canLink, fn ($q) => $q->whereIn('id', $linkedIds));
|
||||
if ($selectedOrderId !== null) {
|
||||
(clone $orders)->findOrFail($selectedOrderId);
|
||||
$orders->whereKey($selectedOrderId);
|
||||
}
|
||||
|
||||
return ['actorId' => $actor->id, 'tenantId' => $actor->tenant_id, 'policy' => $policy,
|
||||
'canConfigure' => $actor->hasFunctions(['admin']), 'allowedTypes' => $this->types($actor, $policy),
|
||||
'departments' => Department::query()->when(! $allDepartments, fn ($q) => $q->whereKey($actor->department_id))->orderBy('name')->get(['id', 'name']),
|
||||
'orders' => $orders->latest('id')->paginate(15)->withQueryString(),
|
||||
'orders' => $orders->latest('id')->paginate(15, ['*'], 'page', $selectedOrderId === null ? null : 1)->withQueryString(),
|
||||
'selectedOrderId' => $selectedOrderId,
|
||||
'requisition' => $request?->only(['id', 'number', 'status']),
|
||||
'items' => $request?->items()->get(['id', 'description']) ?? [],
|
||||
'links' => $links, 'canLink' => $canLink,
|
||||
|
||||
Reference in New Issue
Block a user