129 lines
4.0 KiB
PHP
Executable File
129 lines
4.0 KiB
PHP
Executable File
<?php
|
|
namespace App\Services\FattureElettroniche;
|
|
|
|
use App\Models\Amministratore;
|
|
use App\Models\Fornitore;
|
|
|
|
class FeSubjectModuleService
|
|
{
|
|
/**
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function forAmministratore(?Amministratore $amministratore): array
|
|
{
|
|
if (! $amministratore instanceof Amministratore) {
|
|
return $this->disabled('amministratore_non_disponibile');
|
|
}
|
|
|
|
$sources = ['manual_upload', 'cassetto_fiscale'];
|
|
|
|
return [
|
|
'enabled' => true,
|
|
'subject_type' => 'amministratore',
|
|
'archive_scope' => 'stabile',
|
|
'reason' => null,
|
|
'sources' => $sources,
|
|
'source_labels' => $this->sourceLabels($sources),
|
|
'provider_contract_enabled' => (bool) ($amministratore->fe_cassetto_enabled ?? false),
|
|
'serials_enabled' => false,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function forFornitore(?Fornitore $fornitore): array
|
|
{
|
|
if (! $fornitore instanceof Fornitore) {
|
|
return $this->disabled('fornitore_non_disponibile');
|
|
}
|
|
|
|
$features = $fornitore->fe_features_safe;
|
|
$sources = $this->normalizeSources(data_get($features, 'invoice_import.sources', [
|
|
'manual_upload',
|
|
'pec',
|
|
'cassetto_fiscale',
|
|
'gestionale_esterno',
|
|
]));
|
|
|
|
return [
|
|
'enabled' => (bool) data_get($features, 'invoice_import.enabled', true),
|
|
'subject_type' => 'fornitore',
|
|
'archive_scope' => 'fornitore',
|
|
'reason' => null,
|
|
'sources' => $sources,
|
|
'source_labels' => $this->sourceLabels($sources),
|
|
'provider_contract_enabled' => false,
|
|
'serials_enabled' => (bool) data_get($fornitore->operational_config_safe, 'catalog_import.track_serials_from_fe', true),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function disabled(string $reason): array
|
|
{
|
|
return [
|
|
'enabled' => false,
|
|
'subject_type' => null,
|
|
'archive_scope' => null,
|
|
'reason' => $reason,
|
|
'sources' => [],
|
|
'source_labels' => [],
|
|
'provider_contract_enabled' => false,
|
|
'serials_enabled' => false,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<int,string> $sources
|
|
* @return array<int,string>
|
|
*/
|
|
public function sourceLabels(array $sources): array
|
|
{
|
|
$labels = [
|
|
'manual_upload' => 'Upload manuale',
|
|
'pec' => 'PEC',
|
|
'cassetto_fiscale' => 'Cassetto Fiscale',
|
|
'gestionale_esterno' => 'Altro gestionale',
|
|
];
|
|
|
|
return array_values(array_map(
|
|
static fn(string $source): string => $labels[$source] ?? $source,
|
|
$sources
|
|
));
|
|
}
|
|
|
|
/**
|
|
* @param array<int,string>|mixed $sources
|
|
* @return array<int,string>
|
|
*/
|
|
private function normalizeSources(mixed $sources): array
|
|
{
|
|
$allowed = [
|
|
'manual_upload' => true,
|
|
'pec' => true,
|
|
'cassetto_fiscale' => true,
|
|
'gestionale_esterno' => true,
|
|
];
|
|
|
|
if (! is_array($sources)) {
|
|
return ['manual_upload'];
|
|
}
|
|
|
|
$normalized = [];
|
|
foreach ($sources as $source) {
|
|
$source = trim((string) $source);
|
|
if ($source !== '' && isset($allowed[$source])) {
|
|
$normalized[] = $source;
|
|
}
|
|
}
|
|
|
|
if ($normalized === []) {
|
|
return ['manual_upload'];
|
|
}
|
|
|
|
return array_values(array_unique($normalized));
|
|
}
|
|
}
|