administrator($request->user()); $ready = Schema::hasTable('organization_access'); $state = $ready ? DB::table('organization_access')->where('tenant_id', $actor->tenant_id)->first() : null; Inertia::encryptHistory(); $response = Inertia::render('company/parameters', ['ready' => $ready, 'active' => (bool) ($state->active ?? false), 'version' => $state->version ?? 1, 'companyRules' => $this->companyRules((int) $actor->tenant_id), 'permissions' => OrganizationAccess::PERMISSIONS, 'profiles' => $ready ? DB::table('organization_profiles')->where('tenant_id', $actor->tenant_id)->orderBy('name')->get()->map(fn ($p) => ['id' => $p->id, 'name' => $p->name, 'permissions' => json_decode($p->permissions, true, 32, JSON_THROW_ON_ERROR)]) : [], 'memberships' => $ready ? DB::table('organization_memberships')->where('tenant_id', $actor->tenant_id)->get(['user_id', 'profile_id', 'maintenance', 'expires_at', 'reason']) : [], 'users' => DB::table('users')->where('tenant_id', $actor->tenant_id)->orderBy('name')->get(['id', 'name', 'email', 'department_id']), 'departments' => DB::table('departments')->where('tenant_id', $actor->tenant_id)->orderBy('name')->get(['id', 'name']), 'costCenters' => DB::table('cost_centers')->where('tenant_id', $actor->tenant_id)->orderBy('code')->get(['id', 'code', 'name']), 'notice' => $request->session()->get('organization_notice')])->toResponse($request); $response->headers->set('Cache-Control', 'private, no-store, max-age=0'); return $response; } public function command(Request $request, string $command, OrganizationConfiguration $service): Response { $actor = $service->administrator($request->user()); abort_unless(Schema::hasTable('organization_access'), 503, 'A instalação da parametrização ainda não está concluída.'); $rules = ['version' => 'required|integer|min:1', 'tenant_id' => 'prohibited', 'purchasing_role' => 'prohibited']; if ($command === 'profile') { $rules += ['id' => 'nullable|integer|min:1', 'name' => ['required', 'string', 'max:120', Rule::unique('organization_profiles')->where('tenant_id', $actor->tenant_id)->ignore($request->integer('id'))], 'permissions' => 'present|array', 'permissions.*' => ['string', 'distinct', Rule::in(array_keys(OrganizationAccess::PERMISSIONS))]]; } elseif ($command === 'user') { $rules += ['name' => 'required|string|max:100', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|string|min:12|confirmed', 'department_id' => 'required|integer|min:1', 'profile_ids' => 'present|array', 'profile_ids.*' => 'integer|distinct|min:1', 'reason' => 'required|string|min:10|max:4000']; } elseif ($command === 'assign') { $rules += ['user_id' => 'required|integer|min:1', 'profile_ids' => 'present|array', 'profile_ids.*' => 'integer|distinct|min:1', 'maintenance' => 'required|boolean', 'expires_at' => 'nullable|date|after:now', 'reason' => 'required|string|min:10|max:4000']; } elseif ($command === 'rules') { $rules += ['first_limit' => ['required', 'string', 'regex:/^\d{1,12}(\.\d{1,2})?$/D'], 'second_limit' => ['required', 'string', 'regex:/^\d{1,12}(\.\d{1,2})?$/D'], 'includes_tax' => 'required|boolean', 'reason' => 'required|string|min:10|max:4000']; } else { abort_unless($command === 'activate', 404); $rules += ['reason' => 'required|string|min:10|max:4000', 'confirm' => 'required|accepted']; } $service->execute($actor, $command, $request->validate($rules)); return to_route('organization.parameters')->with('organization_notice', 'Parametrização actualizada e registada na auditoria.'); } /** @return array */ private function companyRules(int $tenant): array { $company = Tenant::query()->findOrFail($tenant); return ['first_limit' => (string) BigDecimal::of((string) $company->first_limit_minor)->dividedBy(100, 2), 'second_limit' => (string) BigDecimal::of((string) $company->second_limit_minor)->dividedBy(100, 2), 'includes_tax' => $company->approval_includes_tax, 'provisional' => $company->rules_provisional]; } }