Skip to content

Commit afbf06c

Browse files
committed
fix: impostazione banche eliminate
1 parent 69fa5a1 commit afbf06c

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

modules/fatture/src/Fattura.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,9 +1054,21 @@ private static function getBancaAzienda(Anagrafica $azienda, int $id_pagamento,
10541054
// Per le fatture di acquisto, priorità alla banca del fornitore
10551055
$anagrafica_principale = ($direzione == 'entrata') ? $azienda : $anagrafica_controparte;
10561056

1057+
// Pulizia preventiva dei riferimenti a banche inesistenti nell'anagrafica
1058+
self::cleanInvalidBankReferences($azienda);
1059+
self::cleanInvalidBankReferences($anagrafica_controparte);
1060+
10571061
// 1. Banca predefinita dell'anagrafica principale per il tipo di operazione
10581062
$id_banca = $anagrafica_principale->{"idbanca_{$conto}"};
10591063

1064+
// Verifica che la banca esista effettivamente
1065+
if ($id_banca) {
1066+
$banca_esistente = Banca::find($id_banca);
1067+
if (!$banca_esistente || $banca_esistente->deleted_at) {
1068+
$id_banca = null;
1069+
}
1070+
}
1071+
10601072
// 2. Banca dell'azienda con conto corrispondente al tipo di pagamento (predefinita)
10611073
if (empty($id_banca)) {
10621074
$id_banca = self::getBancaByPagamento($database, $azienda->id, $id_pagamento, $conto, true);
@@ -1101,4 +1113,35 @@ private static function getBancaByPagamento($database, int $id_anagrafica, int $
11011113

11021114
return $result['id'] ?? null;
11031115
}
1116+
1117+
/**
1118+
* Pulisce i riferimenti a banche inesistenti o eliminate dall'anagrafica.
1119+
*/
1120+
private static function cleanInvalidBankReferences(Anagrafica $anagrafica): void
1121+
{
1122+
$changed = false;
1123+
1124+
// Verifica idbanca_vendite
1125+
if ($anagrafica->idbanca_vendite) {
1126+
$banca = Banca::find($anagrafica->idbanca_vendite);
1127+
if (!$banca || $banca->deleted_at) {
1128+
$anagrafica->idbanca_vendite = null;
1129+
$changed = true;
1130+
}
1131+
}
1132+
1133+
// Verifica idbanca_acquisti
1134+
if ($anagrafica->idbanca_acquisti) {
1135+
$banca = Banca::find($anagrafica->idbanca_acquisti);
1136+
if (!$banca || $banca->deleted_at) {
1137+
$anagrafica->idbanca_acquisti = null;
1138+
$changed = true;
1139+
}
1140+
}
1141+
1142+
// Salva le modifiche se necessario
1143+
if ($changed) {
1144+
$anagrafica->save();
1145+
}
1146+
}
11041147
}

0 commit comments

Comments
 (0)