netgescon-day0/app/Modules/Contabilita/Models/MovimentoBanca.php

140 lines
3.8 KiB
PHP

<?php
namespace App\Modules\Contabilita\Models;
use App\Models\GestioneContabile;
use App\Models\DatiBancari;
use Illuminate\Database\Eloquent\Model;
class MovimentoBanca extends Model
{
protected static ?array $cleaningConfigCache = null;
protected $table = 'contabilita_movimenti_banca';
protected $fillable = [
'stabile_id',
'conto_id',
'gestione_id',
'iban',
'data',
'valuta',
'descrizione',
'descrizione_estesa',
'importo',
'causale',
'source_file',
'raw_line',
'row_hash',
'da_confermare',
'match_data',
'rubrica_mittente_id',
'rubrica_beneficiario_id',
'fornitore_id',
'registrazione_id',
];
protected $casts = [
'data' => 'date',
'valuta' => 'date',
'importo' => 'decimal:2',
'da_confermare' => 'boolean',
'match_data' => 'array',
];
public function getDescrizioneEstesaPulitaAttribute(): ?string
{
$raw = $this->descrizione_estesa ?? $this->descrizione;
if (! is_string($raw)) {
return null;
}
$text = trim($raw);
if ($text === '') {
return null;
}
if (is_array($this->match_data ?? null) && ! empty($this->match_data['descrizione_base'])) {
$base = trim((string) $this->match_data['descrizione_base']);
if ($base !== '') {
$text = $base . ' ' . $text;
}
}
$text = $this->applyCleaningRules($text);
$text = preg_replace('/\s{2,}/', ' ', $text) ?? $text;
$text = trim($text, " \t\n\r\0\x0B|-");
return $text !== '' ? $text : null;
}
protected function applyCleaningRules(string $text): string
{
$patterns = [];
$config = self::loadCleaningConfig();
if (isset($config['base']['remove_patterns']) && is_array($config['base']['remove_patterns'])) {
$patterns = array_merge($patterns, $config['base']['remove_patterns']);
}
$codice = $this->resolveCodiceBreve();
if ($codice && isset($config['codici'][$codice]['remove_patterns']) && is_array($config['codici'][$codice]['remove_patterns'])) {
$patterns = array_merge($patterns, $config['codici'][$codice]['remove_patterns']);
}
foreach ($patterns as $pattern) {
if (! is_string($pattern) || $pattern === '') {
continue;
}
$text = preg_replace('/' . $pattern . '/i', '', $text) ?? $text;
}
return $text;
}
protected function resolveCodiceBreve(): ?string
{
$raw = strtoupper((string) ($this->descrizione ?? ''));
if (str_contains($raw, 'ACCREDITO_BEU_CON_CONTABILE')) {
return 'ACCBEUCONCON';
}
if (str_contains($raw, 'COMBONMYB')) {
return 'COMBONMYB';
}
return null;
}
protected static function loadCleaningConfig(): array
{
if (self::$cleaningConfigCache !== null) {
return self::$cleaningConfigCache;
}
$path = base_path('resources/bank-cleaning.json');
if (! is_file($path)) {
return self::$cleaningConfigCache = [];
}
$json = file_get_contents($path);
$data = is_string($json) ? json_decode($json, true) : null;
if (! is_array($data)) {
return self::$cleaningConfigCache = [];
}
return self::$cleaningConfigCache = $data;
}
public function registrazione()
{
return $this->belongsTo(Registrazione::class, 'registrazione_id');
}
public function conto()
{
return $this->belongsTo(DatiBancari::class, 'conto_id');
}
public function gestioneContabile()
{
return $this->belongsTo(GestioneContabile::class, 'gestione_id');
}
}