2018-07-09 12:57:55 +02:00
< ? php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright ( C ) DevCode s . r . l .
2020-09-07 15:04:06 +02:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
2018-07-09 12:57:55 +02:00
2018-09-18 15:57:55 +02:00
include_once __DIR__ . '/init.php' ;
2018-07-09 12:57:55 +02:00
2024-03-22 15:52:24 +01:00
use Modules\Fatture\StatoFE ;
2018-11-30 16:10:15 +01:00
use Plugins\ExportFE\FatturaElettronica ;
2018-12-07 18:16:46 +01:00
use Plugins\ExportFE\Interaction ;
2020-09-08 10:56:18 +02:00
use Util\XML ;
2018-11-30 16:10:15 +01:00
2021-04-08 17:17:17 +02:00
if ( $fattura !== null ) {
/* Per le PA EC02 e EC01 sono dei stati successivi a NE il quale a sua volta è successivo a RC. EC01 e EC02 sono definiti all'interno della ricevuta di NE che di fatto indica il rifiuto o l'accettazione. */
2024-04-12 13:22:14 +02:00
$stato_fe = StatoFE :: find ( $fattura -> codice_stato_fe );
2024-04-18 17:44:05 +02:00
$abilita_genera = $fattura -> stato -> getTranslation ( 'title' ) != 'Bozza' && ( empty ( $fattura -> codice_stato_fe ) || intval ( $stato_fe -> is_generabile ));
2021-04-08 17:17:17 +02:00
$ricevuta_principale = $fattura -> getRicevutaPrincipale ();
if ( ! empty ( $fattura_pa )) {
$generata = $fattura_pa -> isGenerated ();
} else {
$generata = false ;
}
2019-08-29 14:28:05 +02:00
2021-04-08 17:17:17 +02:00
$checks = FatturaElettronica :: controllaFattura ( $fattura );
if ( ! empty ( $checks )) {
2019-08-29 14:28:05 +02:00
echo '
2021-04-08 17:17:17 +02:00
< div class = " alert alert-warning " >
< p >< i class = " fa fa-warning " ></ i > '.tr(' Prima di procedere alla generazione della fattura elettronica completa le seguenti informazioni ').' :</ p > ' ;
2019-08-29 14:28:05 +02:00
2021-04-08 17:17:17 +02:00
foreach ( $checks as $check ) {
echo '
< p >< b > '.$check[' name '].' '.$check[' link '].' </ b ></ p >
< ul > ' ;
foreach ( $check [ 'errors' ] as $error ) {
if ( ! empty ( $error )) {
echo '
< li > '.$error.' </ li > ' ;
}
2019-08-29 14:28:05 +02:00
}
2021-04-08 17:17:17 +02:00
echo '
</ ul > ' ;
2019-08-29 14:28:05 +02:00
}
2018-11-12 13:14:41 +01:00
2019-08-29 14:28:05 +02:00
echo '
2021-04-08 17:17:17 +02:00
</ div > ' ;
2018-07-09 15:01:11 +02:00
}
echo '
2021-04-08 17:17:17 +02:00
< p > '.tr("Per effettuare la generazione dell' XML della fattura elettronica clicca sul pulsante _BTN_ " , [
'_BTN_' => '<b>Genera</b>' ,
]) . '. ' . tr ( 'Successivamente sarà possibile procedere alla visualizzazione e al download della fattura generata attraverso i pulsanti dedicati' ) . ' .</ p >
2019-03-08 15:18:52 +01:00
2021-04-08 17:17:17 +02:00
< p > '.tr("Tutti gli allegati inseriti all' interno della categoria \ " Allegati Fattura Elettronica \" saranno inclusi nell'XML " ) . ' .</ p >
< br > ' ;
2019-01-17 16:11:10 +01:00
2021-04-08 17:17:17 +02:00
echo '
< div class = " text-center " >
< form action = " " method = " post " role = " form " style = " display:inline-block " id = " form-xml " >
< input type = " hidden " name = " id_plugin " value = " '. $id_plugin .' " >
< input type = " hidden " name = " id_record " value = " '. $id_record .' " >
< input type = " hidden " name = " backto " value = " record-edit " >
< input type = " hidden " name = " op " value = " generate " >
2018-10-13 10:15:54 +02:00
2021-04-08 17:17:17 +02:00
< button type = " button " class = " btn btn-primary btn-lg '.(! $abilita_genera ? 'disabled' : '').' " onclick = " generaFE(this) " >
< i class = " fa fa-file " ></ i > '.tr(' Genera ').'
</ button >
</ form > ' ;
2018-12-07 18:16:46 +01:00
2021-04-12 15:51:27 +02:00
$file = $generata ? $fattura -> getFatturaElettronica () : null ;
2018-12-07 18:16:46 +01:00
2021-04-08 17:17:17 +02:00
echo '
2019-03-08 15:18:52 +01:00
2021-04-08 17:17:17 +02:00
< i class = " fa fa-arrow-right fa-fw text-muted " ></ i >
2018-12-07 18:23:46 +01:00
2021-04-08 17:17:17 +02:00
< a href = " '.base_path().'/view.php?file_id='.( $file ? $file->id : null).' " class = " btn btn-info btn-lg '.( $generata ? '' : 'disabled').' " target = " _blank " '.($generata ? ' ' : ' disabled ').' >
< i class = " fa fa-eye " ></ i > '.tr(' Visualizza ').'
</ a > ' ;
2019-03-08 15:18:52 +01:00
2021-04-12 15:51:27 +02:00
// Scelgo quando posso inviarla
$inviabile = Interaction :: isEnabled () && $generata && intval ( $stato_fe [ 'is_inviabile' ]);
2018-12-07 18:16:46 +01:00
2021-04-08 17:17:17 +02:00
echo '
< i class = " fa fa-arrow-right fa-fw text-muted " ></ i >
2018-12-07 18:16:46 +01:00
2021-04-08 17:17:17 +02:00
< a href = " '. $structure->fileurl ('download.php').'?id_record='. $id_record .' " class = " btn btn-primary btn-lg '.( $generata ? '' : 'disabled').' " target = " _blank " '.($generata ? ' ' : ' disabled ').' >
< i class = " fa fa-download " ></ i > '.tr(' Scarica ').'
</ a > ' ;
2020-07-01 15:57:16 +02:00
2021-04-08 17:17:17 +02:00
echo '
2020-07-01 15:57:16 +02:00
2021-04-08 17:17:17 +02:00
< i class = " fa fa-arrow-right fa-fw text-muted " ></ i >
2020-07-01 15:57:16 +02:00
2021-04-08 17:17:17 +02:00
< button type = " button " onclick = " inviaFE(this) " class = " btn btn-success btn-lg '.( $inviabile ? '' : 'disabled').' " >
< i class = " fa fa-paper-plane " ></ i > '.tr(' Invia ').'
</ button > ' ;
2018-12-07 18:23:46 +01:00
2021-04-08 17:17:17 +02:00
$verify = Interaction :: isEnabled () && $generata ;
2020-09-08 11:38:25 +02:00
echo '
2021-04-08 17:17:17 +02:00
< i class = " fa fa-arrow-right fa-fw text-muted " ></ i >
2019-02-01 18:19:13 +01:00
2021-04-08 17:17:17 +02:00
< button type = " button " onclick = " verificaNotificheFE(this) " class = " btn btn-warning btn-lg '.( $verify ? '' : 'disabled').' " >
2024-02-23 18:18:50 +01:00
< i class = " fa fa-question-circle " ></ i > '.tr(' Verifica ricevute ').'
2021-04-08 17:17:17 +02:00
</ button >
</ div > ' ;
2020-07-08 10:40:04 +02:00
2021-04-08 17:17:17 +02:00
echo '<br><br>' ;
2020-09-08 11:38:25 +02:00
2021-04-08 17:17:17 +02:00
// Messaggio informativo sulla ricevuta principale impostata
if ( ! empty ( $ricevuta_principale )) {
2020-10-16 15:50:37 +02:00
echo '
2021-04-08 17:17:17 +02:00
< div class = " alert alert-'. $stato_fe['tipo'] .' " >
2024-05-16 18:02:50 +02:00
< div class = " float-right d-none d-sm-inline " >
2021-04-08 17:17:17 +02:00
< i class = " fa fa-clock-o tip " title = " '.tr('Data e ora').' " ></ i > '.timestampFormat($record[' data_stato_fe ' ]);
if ( ! empty ( $ultima_ricevuta )) {
echo '
< a href = " '.ROOTDIR.'/view.php?file_id='. $ultima_ricevuta->id .' " target = " _blank " class = " btn btn-info btn-xs " >
< i class = " fa fa-external-link " ></ i > '.tr(' Visualizza ricevuta ').'
</ a > ' ;
}
2020-09-08 11:38:25 +02:00
2020-10-16 15:50:37 +02:00
echo '
2021-04-08 17:17:17 +02:00
</ div >
2020-09-08 11:38:25 +02:00
2021-04-08 17:17:17 +02:00
< big >
< i class = " '. $stato_fe['icon'] .' " style = " color:#fff; " ></ i >
< b > '.$stato_fe[' codice '].' </ b > - '.$stato_fe[' descrizione '].'
</ big > ' ;
2018-11-08 13:11:50 +01:00
2021-04-08 17:17:17 +02:00
if ( ! empty ( $record [ 'descrizione_ricevuta_fe' ])) {
echo '
< br >< b > '.tr(' Note ', [], [' upper ' => true]).' :</ b > '.$record[' descrizione_ricevuta_fe ' ];
}
2020-07-08 10:40:04 +02:00
2021-04-08 17:17:17 +02:00
if ( $fattura -> codice_stato_fe == 'GEN' ) {
2020-10-16 15:50:37 +02:00
echo '
2021-04-08 17:17:17 +02:00
< br >< i class = " fa fa-info-circle " ></ i > '.tr("La fattura è stata generata ed è pronta per l' invio " ).'.';
}
echo '
</ div > ' ;
// Lettura della ricevuta
2024-09-05 12:51:52 +02:00
if ( ! empty ( $ricevuta_principale ) && ! empty ( $ricevuta_principale -> filename ) && file_exists ( $ricevuta_principale -> filename )) {
2024-09-03 15:54:42 +02:00
$contenuto_ricevuta = XML :: readFile ( $ricevuta_principale -> filename );
2021-04-08 17:17:17 +02:00
$lista_errori = $contenuto_ricevuta [ 'ListaErrori' ];
if ( ! empty ( $lista_errori )) {
2020-10-16 15:50:37 +02:00
echo '
2021-04-08 17:17:17 +02:00
< h4 > '.tr(' Elenco degli errori ').' </ h4 >
< table class = " table table-striped " >
< thead >
< tr >
< th > '.tr(' Codice ').' </ th >
< th > '.tr(' Descrizione ').' </ th >
</ tr >
</ thead >
< tbody > ' ;
$lista_errori = $lista_errori [ 0 ] ? $lista_errori : [ $lista_errori ];
foreach ( $lista_errori as $errore ) {
$errore = $errore [ 'Errore' ];
echo '
< tr >
< td > '.$errore[' Codice '].' </ td >
2024-05-23 11:36:25 +02:00
< td > '.htmlentities((string) $errore[' Descrizione ']).' </ td >
2021-04-08 17:17:17 +02:00
</ tr > ' ;
}
2020-07-08 10:40:04 +02:00
2021-04-08 17:17:17 +02:00
echo '
</ tbody >
</ table > ' ;
}
2020-07-08 10:40:04 +02:00
}
}
2021-04-08 17:17:17 +02:00
echo '
< script >
function inviaFE ( button ) {
if ( confirm ( " '.tr('Inviare la fattura al SDI?').' " )) {
let restore = buttonLoading ( button );
$ . ajax ({
url : globals . rootdir + " /actions.php " ,
type : " post " ,
dataType : " json " ,
data : {
op : " send " ,
id_module : " '. $id_module .' " ,
id_plugin : " '. $id_plugin .' " ,
id_record : " '. $id_record .' " ,
},
success : function ( data ) {
buttonRestore ( button , restore );
if ( data . code === 200 ) {
swal ( " '.tr('Fattura inviata!').' " , data . message , " success " );
$ ( button ) . attr ( " disabled " , true ) . addClass ( " disabled " );
2024-05-28 15:28:23 +02:00
// Ricarica la pagina dopo 3 secondi
setTimeout ( function () { location . reload (); }, 3000 );
2021-04-08 17:17:17 +02:00
} else if ( data . code === 301 ) {
swal ( " '.tr('Invio già effettuato').' " , data . code + " - " + data . message , " error " );
$ ( button ) . attr ( " disabled " , true ) . addClass ( " disabled " );
} else if ( data . code === 500 ) {
swal ( " '.tr( " Errore durante l 'invio").' " , " '.tr("Si è verificato un problema durante l' invio della fattura ! Riprova tra qualche minuto oppure contatta l 'assistenza se il problema persiste.").' " , " error " );
} else {
swal ( " '.tr('Invio fallito').' " , data . code + " - " + data . message , " error " );
}
},
error : function () {
swal ( " '.tr('Errore').' " , " '.tr('Errore durante il salvataggio').' " , " error " );
buttonRestore ( button , restore );
}
});
}
}
function verificaNotificheFE ( button ) {
openModal ( " '.tr('Gestione ricevute').' " , " '. $structure->fileurl ('notifiche.php').'?id_module='. $id_module .'&id_plugin='. $id_plugin .'&id_record='. $id_record .' " );
/*
2020-09-08 15:11:04 +02:00
let restore = buttonLoading ( button );
$ . ajax ({
url : globals . rootdir + " /actions.php " ,
type : " post " ,
data : {
2021-04-08 17:17:17 +02:00
op : " verify " ,
2020-09-08 15:11:04 +02:00
id_module : " '. $id_module .' " ,
id_plugin : " '. $id_plugin .' " ,
id_record : " '. $id_record .' " ,
},
success : function ( data ) {
buttonRestore ( button , restore );
2021-04-08 17:17:17 +02:00
data = JSON . parse ( data );
2020-09-08 15:11:04 +02:00
2021-04-08 17:17:17 +02:00
if ( data . file ) {
swal ( " '.tr('Verifica completata con successo!').' " , " '.tr('Lo stato della Fattura Elettronica è stato aggiornato in base all \ 'ultima notifica disponibile nel sistema!').' " , " success " ) . then ( function () {
location . reload (); // Ricaricamento pagina
});
2020-09-08 15:11:04 +02:00
} else {
2021-04-08 17:17:17 +02:00
swal ( " '.tr('Verifica fallita').' " , data . code + " - " + data . message , " error " );
2020-09-08 15:11:04 +02:00
}
},
2021-04-08 17:17:17 +02:00
error : function ( data ) {
swal ( " '.tr('Errore').' " , " '.tr('Errore durante la verifica').' " , " error " );
2020-07-01 15:57:16 +02:00
2020-09-08 15:11:04 +02:00
buttonRestore ( button , restore );
2020-07-01 15:57:16 +02:00
}
2021-04-08 17:17:17 +02:00
}); */
2020-09-08 15:11:04 +02:00
}
2020-07-01 15:57:16 +02:00
2021-04-08 17:17:17 +02:00
function generaFE ( button ) {
salvaForm ( " #edit-form " , {}, button )
. then ( function ( valid ) { ' ;
2020-07-01 15:57:16 +02:00
2021-04-12 15:51:27 +02:00
if ( $generata ) {
echo '
2021-04-08 17:17:17 +02:00
/*<p class=\"text-danger\">'.tr('Se stai attendendo una ricevuta dal sistema SdI, rigenerando la fattura elettronica non sarà possibile corrispondere la ricevuta una volta emessa').'.</p>*/
swal ({
title : " '.tr('Sei sicuro di rigenerare la fattura?').' " ,
html : " <p>'.tr('Attenzione: sarà generato un nuovo progressivo invio').'.</p> " ,
type : " warning " ,
showCancelButton : true ,
confirmButtonColor : " #30d64b " ,
cancelButtonColor : " #d33 " ,
confirmButtonText : " '.tr('Genera').' "
}) . then (( result ) => {
if ( result ) {
$ ( " #form-xml " ) . submit ();
}
}); ' ;
2021-04-12 15:51:27 +02:00
} else {
echo '
2019-07-23 16:12:46 +02:00
2021-04-08 17:17:17 +02:00
$ ( " #form-xml " ) . submit (); ' ;
2021-04-12 15:51:27 +02:00
}
echo '
2021-04-08 17:17:17 +02:00
}) . catch ( function () {
swal ({
type : " error " ,
title : " '.tr('Errore').' " ,
text : " '.tr('Alcuni campi obbligatori non sono stati compilati correttamente').'. " ,
});
2020-08-04 16:29:01 +02:00
});
2021-04-08 17:17:17 +02:00
};
</ script > ' ;
2021-04-12 15:51:27 +02:00
}