40 lines
1.7 KiB
PHP
40 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Opportunity;
|
|
|
|
/** Read-only presentation of existing references. Does not reconcile, confirm or save data. */
|
|
class OpportunityTerritorialPresentation
|
|
{
|
|
/**
|
|
* @param array<array-key, mixed> $locations
|
|
* @return array<array-key, mixed>
|
|
*/
|
|
public function describe(Opportunity $record, array $locations): array
|
|
{
|
|
$path = array_values(array_filter(app(InvestmentGeography::class)->path($record->primary_territory_id, true), fn ($node) => in_array($node->location_type, ['province', 'district', 'admin_post', 'locality'], true)));
|
|
$mainIds = array_column($path, 'id', 'location_type');
|
|
$differences = [];
|
|
foreach ($locations as $location) {
|
|
if ($location->deleted_at || empty($location->payload['is_primary']) || ! $path) {
|
|
continue;
|
|
}
|
|
$p = $location->payload;
|
|
$different = false;
|
|
foreach ($mainIds as $level => $id) {
|
|
if (! empty($p[$level.'_id']) && $p[$level.'_id'] !== $id) {
|
|
$different = true;
|
|
}
|
|
}
|
|
$foreign = ! empty($p['country_code']) && $p['country_code'] !== 'MZ';
|
|
if ($foreign || $different) {
|
|
$differences[] = ['resource_id' => data_get($location, 'id'), 'title' => $p['title'] ?? 'Localização sem título',
|
|
'message' => 'A referência principal da ficha e esta localização documentada apontam para territórios diferentes. Confirme se a distinção é intencional; nenhum valor foi substituído.'];
|
|
}
|
|
}
|
|
|
|
return ['path' => $path, 'differences' => $differences];
|
|
}
|
|
}
|