199 lines
6.5 KiB
PHP
199 lines
6.5 KiB
PHP
<?php
|
|
namespace App\Support;
|
|
|
|
use App\Models\Amministratore;
|
|
use App\Models\DatiBancari;
|
|
use App\Models\GestioneContabile;
|
|
use App\Models\Fornitore;
|
|
use App\Models\Stabile;
|
|
use App\Models\User;
|
|
|
|
class ArchivioPaths
|
|
{
|
|
private static function sanitizeFolder(string $value): string
|
|
{
|
|
$value = trim($value);
|
|
if ($value === '') {
|
|
return '';
|
|
}
|
|
|
|
$value = preg_replace('/[^A-Za-z0-9._-]+/', '-', $value) ?? '';
|
|
return trim($value, '-');
|
|
}
|
|
|
|
public static function adminBase(Amministratore | string $admin): string
|
|
{
|
|
$code = $admin instanceof Amministratore
|
|
? (string) ($admin->codice_univoco ?: $admin->codice_amministratore ?: '')
|
|
: (string) $admin;
|
|
|
|
$code = self::sanitizeFolder($code);
|
|
if ($code === '') {
|
|
$code = 'ID-0';
|
|
}
|
|
|
|
return 'amministratori/' . $code;
|
|
}
|
|
|
|
public static function stabileFolderName(Stabile $stabile, ?Amministratore $admin = null): ?string
|
|
{
|
|
$admin = $admin ?: $stabile->amministratore;
|
|
if (! $admin instanceof Amministratore) {
|
|
return null;
|
|
}
|
|
|
|
$stabileCode = self::sanitizeFolder((string) ($stabile->codice_univoco ?: $stabile->codice_stabile ?: ''));
|
|
|
|
if ($stabileCode === '') {
|
|
return null;
|
|
}
|
|
|
|
// Nota: l'archivio dello stabile è già sotto /amministratori/<ADMIN>/,
|
|
// quindi replicare <ADMIN>-<STABILE> è ridondante.
|
|
return $stabileCode;
|
|
}
|
|
|
|
public static function stabileBase(Stabile $stabile, ?Amministratore $admin = null): ?string
|
|
{
|
|
$admin = $admin ?: $stabile->amministratore;
|
|
if (! $admin instanceof Amministratore) {
|
|
return null;
|
|
}
|
|
|
|
$folder = self::stabileFolderName($stabile, $admin);
|
|
if (! is_string($folder) || $folder === '') {
|
|
return null;
|
|
}
|
|
|
|
return self::adminBase($admin) . '/stabili/' . $folder;
|
|
}
|
|
|
|
public static function gestioneFolderName(?GestioneContabile $gestione, ?int $fallbackYear = null, ?string $fallbackType = 'ordinaria'): string
|
|
{
|
|
$year = $fallbackYear ?: (int) now()->format('Y');
|
|
$type = $fallbackType ?: 'ordinaria';
|
|
|
|
if ($gestione instanceof GestioneContabile) {
|
|
$year = (int) ($gestione->anno_gestione ?: $year);
|
|
$type = (string) ($gestione->tipo_gestione ?: $type);
|
|
}
|
|
|
|
$prefix = match (strtolower(trim($type))) {
|
|
'straordinaria', 'straordinario', 's' => 'S',
|
|
'riscaldamento', 'r', 'acqua', 'a' => 'R',
|
|
default => 'O',
|
|
};
|
|
|
|
return $prefix . $year;
|
|
}
|
|
|
|
public static function fattureXmlYearPath(Stabile $stabile, int|string $year, ?Amministratore $admin = null): ?string
|
|
{
|
|
$base = self::stabileBase($stabile, $admin);
|
|
if (! is_string($base) || $base === '') {
|
|
return null;
|
|
}
|
|
|
|
return $base . '/fatture_xml/' . (int) $year;
|
|
}
|
|
|
|
public static function fatturePdfGestionePath(Stabile $stabile, ?GestioneContabile $gestione = null, ?int $fallbackYear = null, ?string $fallbackType = 'ordinaria', ?Amministratore $admin = null): ?string
|
|
{
|
|
$base = self::stabileBase($stabile, $admin);
|
|
if (! is_string($base) || $base === '') {
|
|
return null;
|
|
}
|
|
|
|
return $base . '/fatture/' . self::gestioneFolderName($gestione, $fallbackYear, $fallbackType);
|
|
}
|
|
|
|
public static function bankAccountFolderName(DatiBancari $conto): string
|
|
{
|
|
$bankLabel = strtolower(trim((string) ($conto->denominazione_banca ?? '')));
|
|
|
|
$bankCode = match (true) {
|
|
str_contains($bankLabel, 'monte') || str_contains($bankLabel, 'paschi') || str_contains($bankLabel, 'mps') => 'MPS',
|
|
str_contains($bankLabel, 'intesa') => 'INTESA',
|
|
str_contains($bankLabel, 'unicredit') => 'UNICREDIT',
|
|
str_contains($bankLabel, 'bper') => 'BPER',
|
|
str_contains($bankLabel, 'bcc') => 'BCC',
|
|
default => strtoupper(self::sanitizeFolder((string) ($conto->denominazione_banca ?: 'BANCA'))),
|
|
};
|
|
|
|
if ($bankCode === '') {
|
|
$bankCode = 'BANCA';
|
|
}
|
|
|
|
$typeCode = strtolower(trim((string) ($conto->tipo_conto ?? 'corrente'))) === 'cassa'
|
|
? 'CA'
|
|
: 'CC';
|
|
|
|
$identifier = preg_replace('/\D+/', '', (string) ($conto->numero_conto ?? '')) ?? '';
|
|
if ($identifier === '') {
|
|
$identifier = self::sanitizeFolder((string) ($conto->legacy_cod_cassa ?? ''));
|
|
}
|
|
if ($identifier === '') {
|
|
$iban = strtoupper(preg_replace('/[^A-Z0-9]+/', '', (string) ($conto->iban ?? '')) ?? '');
|
|
$identifier = $iban !== '' ? substr($iban, -8) : '';
|
|
}
|
|
if ($identifier === '') {
|
|
$identifier = 'ID' . (int) $conto->id;
|
|
}
|
|
|
|
return $bankCode . '-' . $typeCode . '-' . $identifier;
|
|
}
|
|
|
|
public static function bankYearPath(Stabile $stabile, DatiBancari $conto, int|string $year, ?Amministratore $admin = null): ?string
|
|
{
|
|
$base = self::stabileBase($stabile, $admin);
|
|
if (! is_string($base) || $base === '') {
|
|
return null;
|
|
}
|
|
|
|
return $base . '/banca_cc/' . self::bankAccountFolderName($conto) . '/' . (int) $year;
|
|
}
|
|
|
|
public static function fornitoreBase(Fornitore $fornitore, ?Amministratore $admin = null): ?string
|
|
{
|
|
$code = (string) ($fornitore->codice_univoco ?: '');
|
|
$code = self::sanitizeFolder($code);
|
|
if ($code === '') {
|
|
return null;
|
|
}
|
|
|
|
return 'fornitori/' . $code;
|
|
}
|
|
|
|
public static function fornitoreLegacyBase(Fornitore $fornitore, ?Amministratore $admin = null): ?string
|
|
{
|
|
$code = (string) ($fornitore->codice_univoco ?: '');
|
|
$code = self::sanitizeFolder($code);
|
|
if ($code === '') {
|
|
return null;
|
|
}
|
|
|
|
$admin = $admin ?: $fornitore->amministratore;
|
|
if (! $admin instanceof Amministratore) {
|
|
return null;
|
|
}
|
|
|
|
return self::adminBase($admin) . '/fornitori/' . $code;
|
|
}
|
|
|
|
public static function userBase(User $user, ?Amministratore $admin = null): ?string
|
|
{
|
|
$code = (string) ($user->codice_univoco ?: '');
|
|
$code = self::sanitizeFolder($code);
|
|
if ($code === '') {
|
|
return null;
|
|
}
|
|
|
|
$admin = $admin ?: $user->amministratoreOwner;
|
|
if (! $admin instanceof Amministratore) {
|
|
return null;
|
|
}
|
|
|
|
return self::adminBase($admin) . '/utenti/' . $code;
|
|
}
|
|
}
|