fix: preserve territorial scope in catalogue CSV and record local visual QA

This commit is contained in:
2026-11-04 10:49:22 +02:00
parent dc0bbd74d5
commit 6dde36b5ca
7 changed files with 225 additions and 7 deletions
@@ -199,15 +199,16 @@ class InvestmentDiscoveryController extends Controller
{
$records = $catalogue->query($catalogue->filters($request))->limit(1000)->get();
return response()->streamDownload(function () use ($records) {
return response()->streamDownload(function () use ($records, $catalogue) {
$stream = fopen('php://output', 'w');
if ($stream === false) {
return;
}
fwrite($stream, "\xEF\xBB\xBF");
fputcsv($stream, ['Referência', 'Título', 'Sector', 'Província', 'Montante', 'Moeda', 'Maturidade', 'URL'], ';', '"', '');
fputcsv($stream, ['Referência', 'Título', 'Sector', 'Província', 'Montante', 'Moeda', 'Maturidade', 'URL', 'Localização e âmbito'], ';', '"', '');
foreach ($records as $r) {
$fields = [$r->reference, $r->title, $r->sector?->name, $r->province, $r->estimated_investment_amount, $r->estimated_investment_currency, $r->maturity_label, url('/oportunidades/'.$r->slug)];
$location = $catalogue->locationPresentation($r);
$fields = [$r->reference, $r->title, $r->sector?->name, implode(' / ', $location['provinces']), $r->estimated_investment_amount, $r->estimated_investment_currency, $r->maturity_label, url('/oportunidades/'.$r->slug), $location['summary']];
fputcsv($stream, array_map(fn ($v) => preg_match('/^[\s]*[=+@-]/u', (string) $v) ? "'".$v : $v, $fields), ';', '"', '');
}
fclose($stream);
+18 -3
View File
@@ -260,26 +260,41 @@ class InvestmentCatalogue
/** Public result presentation only; no editorial or territorial data is saved. */
public function presentLocation(Opportunity $record): Opportunity
{
$record->setAttribute('territorial_summary', $this->locationPresentation($record)['summary']);
return $record;
}
/** @return array{summary: string, provinces: list<string>} */
public function locationPresentation(Opportunity $record): array
{
$geo = app(InvestmentGeography::class);
$paths = [];
$provinceNames = [];
$ids = array_filter([$record->primary_territory_id, ...$record->additionalTerritories()->pluck('geo_locations.id')->all()]);
foreach (array_unique($ids) as $id) {
$path = $geo->path($id);
if ($path) {
$paths[] = implode(' / ', array_column($path, 'name'));
foreach ($path as $node) {
if ($node->location_type === 'province') {
$provinceNames[] = (string) $node->name;
}
}
}
}
if ($paths) {
$label = implode('; ', array_unique($paths));
} elseif ($provinces = app(InvestmentCorridorContext::class)->catalogueProvinces($record)) {
$label = implode(' / ', array_column($provinces, 'name')).' — contexto provincial do corredor; distrito e posto administrativo não indicados';
$provinceNames = array_map(fn ($province) => (string) $province->name, $provinces);
$label = implode(' / ', $provinceNames).' — contexto provincial do corredor; distrito e posto administrativo não indicados';
} else {
$label = implode(' / ', array_filter([$record->province, $record->district])) ?: 'Localização não indicada';
$provinceNames = $record->province ? [(string) $record->province] : [];
}
$record->setAttribute('territorial_summary', $label);
return $record;
return ['summary' => $label, 'provinces' => array_values(array_unique($provinceNames))];
}
/** @param array<string,mixed> $territoryFilters