*/ private function entryComplements(Request $request): array { $data = $request->validate(['complement_origin' => 'nullable|string|max:100', 'complement_code' => 'required_with:complement_origin|string|max:40', 'complement_target' => 'nullable|string|max:100', 'complement_territory' => 'nullable|uuid']); if (empty($data['complement_origin'])) { return []; } return app(InvestmentComplements::class)->resolve([['origin' => $data['complement_origin'], 'code' => $data['complement_code'], 'target' => $data['complement_target'] ?? null, 'territory_id' => $data['complement_territory'] ?? null]]); } /** * @return array */ private function entryChoices(Request $r, InvestorContext $context): array { $data = $r->validate(['source' => 'nullable|string|max:100', 'from_dossier' => 'nullable|uuid', 'opportunities' => 'sometimes|array|max:100', 'opportunities.*' => 'integer|distinct|min:1', 'province_id' => 'nullable|uuid', 'district_id' => 'nullable|uuid', 'admin_post_id' => 'nullable|uuid', 'locality_id' => 'nullable|uuid']); $source = ! empty($data['source']) ? $context->reference($data['source']) : null; $ids = $data['opportunities'] ?? (($source['kind'] ?? '') === 'opportunity' ? [$source['id']] : []); abort_if(! empty($data['from_dossier']) && ! $ids, 422, 'Escolha a oportunidade associada à ficha da carteira.'); $path = app(InvestmentGeography::class)->selection($data); $input = array_map(fn ($id) => ['source' => 'opportunity:'.$id, ...($path ? ['territory' => 'territory:'.$path[array_key_last($path)]->id] : [])], $ids); if (! empty($data['from_dossier'])) { $input[0]['portfolio_origins'] = [$data['from_dossier']]; } foreach ($this->entryComplements($r) as $complement) { $keys = array_filter([$complement['origin']['key'], $complement['target']['key'] ?? null], fn ($key) => str_starts_with($key, 'opportunity:')); foreach ($keys as $key) { if (in_array($key, array_column($input, 'source'), true)) { continue; } $input[] = ['source' => $key, ...($complement['territory_id'] ? ['territory' => 'territory:'.$complement['territory_id']] : []), ...(str_starts_with($complement['origin']['key'], 'portfolio:') ? ['portfolio_origins' => [substr($complement['origin']['key'], 10)]] : [])]; } } return app(InvestmentOpportunityChoices::class)->resolve($input, [], true); } public function current(Request $r, InvestorContext $context): JsonResponse { return response()->json($context->current($r->user())); } public function focus(Request $r, InvestorContext $context): JsonResponse { return response()->json($context->focus($r->user(), $r->all())); } public function show(Request $r, string $id, InvestorContext $context): JsonResponse { $data = $r->validate(['opportunity' => 'nullable|string|max:100']); return response()->json($context->view($r->user(), $id, $data['opportunity'] ?? null)); } public function options(Request $r, InvestorContext $context): JsonResponse { $d = $r->validate(['q' => 'nullable|string|max:120']); return response()->json($context->options($d['q'] ?? '')); } public function corridorOptions(Request $r): JsonResponse { $data = $r->validate(['corridor' => ['nullable', Rule::in(array_keys(config('investment_corridors')))], 'territory' => 'nullable|uuid']); $service = app(InvestmentCorridorContext::class); return response()->json(['corridors' => collect(Config::array('investment_corridors'))->map(fn ($entry, $key) => ['key' => $key, 'name' => $entry['name'], 'provinces' => $service->provinces($key)])->values(), 'parks' => $service->parks($data['corridor'] ?? null, $data['territory'] ?? null)]); } public function saveDraft(Request $r, string $id, InvestorContext $context): JsonResponse { return response()->json($context->saveDraft($r->user(), $id, $r->all())); } public function edit(Request $r, string $id, InvestorContext $context): Response { $data = $context->view($r->user(), $id); abort_unless($data['can_write'], 403); $entries = $this->entryChoices($r, $context); $choices = $entries ?: null; $source = $r->query('source') ? $context->reference($r->validate(['source' => 'required|string|max:100'])['source']) : null; if ($choices === null && ($source['kind'] ?? '') === 'opportunity') { $choices = app(InvestmentOpportunityChoices::class)->resolve([['source' => $source['key']]]); } $explicitChoices = $choices; $selectionKey = $r->validate(['selection_key' => 'nullable|uuid'])['selection_key'] ?? null; $entryKey = fn ($c) => $c['opportunity']['key'].($selectionKey ? '@'.$selectionKey : ''); $entrySources = array_map($entryKey, $explicitChoices ?? []); if ($choices !== null) { $choices = app(InvestmentOpportunityChoices::class)->mergeEntries($data['context']['opportunity_choices'] ?? [], $choices); } $draft = $context->draft($r->user(), $id); if ($draft && isset($draft['opportunity_choices'])) { $recovered = app(InvestmentOpportunityChoices::class)->resolve($draft['opportunity_choices'], $data['context']['opportunity_choices'] ?? [], true); $new = array_filter($explicitChoices ?? [], fn ($c) => ! in_array($entryKey($c), $draft['entry_sources'] ?? [], true)); $choices = app(InvestmentOpportunityChoices::class)->mergeEntries($recovered, array_values($new)); } $differences = []; foreach ($explicitChoices ?? [] as $entry) { $kept = collect($choices ?? [])->firstWhere('opportunity.key', $entry['opportunity']['key']); if ($kept && ($kept['territory']['key'] ?? null) !== ($entry['territory']['key'] ?? null)) { $differences[] = ['title' => $entry['opportunity']['title'], 'saved' => $kept['territory']['title'] ?? 'Por definir', 'requested' => $entry['territory']['title'] ?? 'Por definir']; } } $complements = isset($draft['complements']) ? app(InvestmentComplements::class)->resolve($draft['complements'], $data['context']['complements'] ?? []) : ($data['context']['complements'] ?? []); $entryTerritory = null; if (($source['kind'] ?? null) === 'portfolio') { $path = app(InvestmentGeography::class)->selection($r->validate(['province_id' => 'nullable|uuid', 'district_id' => 'nullable|uuid', 'admin_post_id' => 'nullable|uuid', 'locality_id' => 'nullable|uuid'])); if ($path) { $entryTerritory = $context->reference('territory:'.$path[array_key_last($path)]->id); } } foreach ($this->entryComplements($r) as $entry) { $tag = 'complement:'.hash('sha256', $entry['key'].'@'.($selectionKey ?? '')); $entrySources[] = $tag; if (! in_array($tag, $draft['entry_sources'] ?? [], true)) { $complements = collect([...$complements, $entry])->unique('key')->values()->all(); } } return Inertia::render('investor-path/context', ['entryTerritory' => $entryTerritory, 'entryComplements' => $complements, 'entryLocationDifferences' => $differences, 'entrySources' => $entrySources, 'draft' => $draft, 'requestedChoices' => $choices, 'initial' => $data, 'options' => $context->options(), 'source' => $source, 'territorySelection' => isset($data['context']['selection']['territory']) ? app(InvestmentGeography::class)->path($data['context']['selection']['territory']['id']) : ($entryTerritory ? app(InvestmentGeography::class)->path($entryTerritory['id']) : [])]); } public function preview(Request $r, string $id, InvestorContext $context): JsonResponse { return response()->json($context->preview($r->user(), $id, $r->all())); } public function commit(Request $r, string $id, InvestorContext $context): JsonResponse { return response()->json($context->commit($r->user(), $id, $r->all())); } public function start(Request $r, InvestorPath $path, InvestorContext $context): Response { $d = $r->validate(['source' => 'nullable|string|max:100', 'step' => ['nullable', 'regex:/^S(0[1-9]|1[0-9])$/']]); $ref = isset($d['source']) ? $context->reference($d['source']) : null; $submissionId = $r->validate(['submission' => 'nullable|uuid'])['submission'] ?? null; $entrySubmission = null; if ($submissionId) { $submission = InvestmentSubmission::where('id', $submissionId)->where('user_id', $r->user()->id)->where('status', 'draft')->firstOrFail(); abort_unless(($ref['kind'] ?? null) === 'portfolio' && $submission->dossier_id === $ref['id'], 422, 'Conserve a ficha de origem da manifestação.'); $entrySubmission = ['id' => $submission->id, 'version' => $submission->version]; } $choices = $this->entryChoices($r, $context); return Inertia::render('investor-path/context-start', ['entrySubmission' => $entrySubmission, 'entryComplements' => $this->entryComplements($r), 'entryQuery' => $r->only(['complement_origin', 'complement_code', 'complement_target', 'complement_territory', 'from_dossier', 'province_id', 'district_id', 'admin_post_id', 'locality_id']), 'opportunityChoices' => $choices, 'contexts' => $path->contexts($r->user()), 'source' => $ref, 'step' => $d['step'] ?? null]); } public function installation(Request $r, InvestorContextInstallation $install): JsonResponse|Response { $plan = $install->preview($r->user()); return $r->expectsJson() ? response()->json($plan) : Inertia::render('investor-path/context-installation', ['plan' => $plan]); } public function install(Request $r, InvestorContextInstallation $install): JsonResponse { return response()->json($install->apply($r->user(), $r->all())); } }