@@ -584,17 +584,19 @@ public function save(array $options = [])
584584 $ this ->attributes ['ritenutaacconto ' ] = $ this ->ritenuta_acconto ;
585585
586586
587- $ result = parent ::save ($ options );
587+ $ save_result = parent ::save ($ options );
588588
589589 $ this ->id_riga_bollo = $ this ->gestoreBollo ->manageRigaMarcaDaBollo ();
590590 $ this ->id_riga_spese_incasso = $ this ->manageRigaSpeseIncasso ();
591591
592592 // Generazione numero fattura se non presente (Bozza -> Emessa)
593+ $ numero_generato = false ;
593594 if ((($ id_stato_precedente == $ id_stato_bozza && $ id_stato_attuale == $ id_stato_emessa ) or (!$ is_fiscale )) && empty ($ this ->numero_esterno )) {
594595 $ this ->numero_esterno = self ::getNextNumeroSecondario ($ this ->data , $ this ->direzione , $ this ->id_segment );
596+ $ numero_generato = true ;
595597 }
596598
597- if ($ this ->isDirty (['id_riga_bollo ' , 'id_riga_spese_incasso ' ])) {
599+ if ($ this ->isDirty (['id_riga_bollo ' , 'id_riga_spese_incasso ' ]) || $ numero_generato ) {
598600 parent ::save ($ options );
599601 }
600602
@@ -649,6 +651,13 @@ public function save(array $options = [])
649651 $ stato_fe = StatoFE::find ($ this ->codice_stato_fe );
650652 $ abilita_genera = empty ($ this ->codice_stato_fe ) || intval ($ stato_fe ['is_generabile ' ]);
651653
654+ $ this ->refresh ();
655+
656+ if (empty ($ this ->numero_esterno )) {
657+ flash ()->error (tr ('Impossibile generare la fattura elettronica: numero fattura mancante ' ));
658+ return $ save_result ;
659+ }
660+
652661 // Generazione automatica della Fattura Elettronica
653662 $ checks = FatturaElettronica::controllaFattura ($ this );
654663 $ fattura_elettronica = new FatturaElettronica ($ this ->id );
@@ -657,30 +666,40 @@ public function save(array $options = [])
657666
658667 if (!$ fattura_elettronica ->isValid ()) {
659668 $ errors = $ fattura_elettronica ->getErrors ();
660-
661- $ result [$ this ->numero_esterno ][0 ][0 ] = $ errors ;
669+ // Gestione errori tramite flash message invece di array di ritorno
670+ if (is_array ($ errors ) && !empty ($ errors )) {
671+ flash ()->error (tr ('Errori nella generazione della fattura elettronica: _ERRORS_ ' , [
672+ '_ERRORS_ ' => implode (', ' , $ errors )
673+ ]));
674+ } else {
675+ flash ()->error (tr ('Errori nella generazione della fattura elettronica ' ));
676+ }
662677 }
663678 } elseif (!empty ($ checks )) {
664679 // Rimozione eventuale fattura generata erronamente
665680 // Fix per la modifica di dati interni su fattura già generata
666681 if ($ abilita_genera ) {
667682 $ fattura_elettronica ->delete ();
668683 }
684+ $ error_messages = [];
669685 foreach ($ checks as $ check ) {
670- $ errors = [];
671- foreach ($ check ['errors ' ] as $ error ) {
672- if (!empty ($ error )) {
673- $ errors = $ error ;
686+ if (!empty ($ check ['errors ' ])) {
687+ foreach ($ check ['errors ' ] as $ error ) {
688+ if (!empty ($ error )) {
689+ $ error_messages [] = strip_tags ($ error );
690+ }
674691 }
675692 }
676- if (!empty ($ errors )) {
677- $ result [$ this ->numero_esterno ][$ check ['name ' ]][$ check ['link ' ]] = $ check ['errors ' ];
678- }
693+ }
694+ if (!empty ($ error_messages )) {
695+ flash ()->warning (tr ('Controlli fattura elettronica falliti: _ERRORS_ ' , [
696+ '_ERRORS_ ' => implode (', ' , $ error_messages )
697+ ]));
679698 }
680699 }
681700 }
682701
683- return $ result ;
702+ return $ save_result ;
684703 }
685704
686705 public function delete ()
0 commit comments