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
* Copyright ( C ) DevCode s . n . c .
*
* 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
2020-09-08 10:56:18 +02:00
use Models\Upload ;
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
2020-09-08 10:56:18 +02:00
/* 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. */
2020-09-08 11:38:25 +02:00
$stato_fe = $database -> fetchOne ( 'SELECT * FROM fe_stati_documento WHERE codice = ' . prepare ( $fattura -> codice_stato_fe ));
2020-09-08 15:11:04 +02:00
$abilita_genera = ( empty ( $fattura -> codice_stato_fe ) && $fattura -> stato -> descrizione != 'Bozza' ) || intval ( $stato_fe [ 'is_generabile' ]);
2020-09-08 10:56:18 +02:00
$ricevuta_principale = $fattura -> getRicevutaPrincipale ();
2019-07-16 12:27:36 +02:00
2018-09-21 15:50:42 +02:00
if ( ! empty ( $fattura_pa )) {
2020-09-08 11:38:25 +02:00
$generata = $fattura_pa -> isGenerated ();
2018-09-18 15:57:55 +02:00
} else {
2020-09-08 11:38:25 +02:00
$generata = false ;
2018-07-09 15:01:11 +02:00
}
2019-08-29 14:28:05 +02:00
$checks = FatturaElettronica :: controllaFattura ( $fattura );
if ( ! empty ( $checks )) {
2018-07-09 15:01:11 +02:00
echo '
< div class = " alert alert-warning " >
2019-08-29 14:28:05 +02:00
< p >< i class = " fa fa-warning " ></ i > '.tr(' Prima di procedere alla generazione della fattura elettronica completa le seguenti informazioni ').' :</ p > ' ;
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 > ' ;
}
}
2018-11-12 13:14:41 +01:00
2019-08-29 14:28:05 +02:00
echo '
</ ul > ' ;
2018-07-09 15:01:11 +02:00
}
echo '
</ div > ' ;
}
2018-10-13 10:15:54 +02:00
echo '
< p > '.tr("Per effettuare la generazione dell' XML della fattura elettronica clicca sul pulsante _BTN_ " , [
2018-10-25 16:57:39 +02:00
'_BTN_' => '<b>Genera</b>' ,
2018-11-09 13:15:19 +01:00
]) . '. ' . tr ( 'Successivamente sarà possibile procedere alla visualizzazione e al download della fattura generata attraverso i pulsanti dedicati' ) . ' .</ p >
2019-07-16 10:01:16 +02:00
< p > '.tr("Tutti gli allegati inseriti all' interno della categoria \ " Allegati Fattura Elettronica \" saranno inclusi nell'XML " ) . ' .</ p >
2018-11-09 13:15:19 +01:00
< br > ' ;
2018-07-09 15:11:19 +02:00
2018-07-09 12:57:55 +02:00
echo '
2018-10-13 10:15:54 +02:00
< div class = " text-center " >
2018-11-08 13:11:50 +01:00
< form action = " " method = " post " role = " form " style = " display:inline-block " id = " form-xml " >
2018-10-13 10:15:54 +02:00
< 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-07-09 12:57:55 +02:00
2020-09-08 15:11:04 +02:00
< button type = " button " class = " btn btn-primary btn-lg '.(! $abilita_genera ? 'disabled' : '').' " onclick = " generaFE(this) " >
2018-10-13 10:15:54 +02:00
< i class = " fa fa-file " ></ i > '.tr(' Genera ').'
</ button >
</ form > ' ;
2019-03-08 15:18:52 +01:00
2020-09-08 11:38:25 +02:00
$file = $generata ? Upload :: where ( 'filename' , $fattura_pa -> getFilename ())
2020-09-08 10:56:18 +02:00
-> where ( 'id_record' , $id_record )
-> first () : null ;
2019-01-17 16:11:10 +01:00
2018-10-13 10:15:54 +02:00
echo '
2018-12-07 18:16:46 +01:00
< i class = " fa fa-arrow-right fa-fw text-muted " ></ i >
2020-09-08 11:38:25 +02:00
< a href = " '.ROOTDIR.'/view.php?file_id='.( $file ? $file->id : null).' " class = " btn btn-info btn-lg '.( $generata ? '' : 'disabled').' " target = " _blank " '.($generata ? ' ' : ' disabled ').' >
2018-10-13 10:15:54 +02:00
< i class = " fa fa-eye " ></ i > '.tr(' Visualizza ').'
2018-12-07 18:16:46 +01:00
</ a > ' ;
2019-03-08 15:18:52 +01:00
// Scelgo quando posso inviarla
2020-09-08 11:38:25 +02:00
$inviabile = Interaction :: isEnabled () && $generata && intval ( $stato_fe [ 'is_inviabile' ]);
2019-03-08 15:18:52 +01:00
2019-02-26 12:13:51 +01:00
echo '
< i class = " fa fa-arrow-right fa-fw text-muted " ></ i >
2018-12-07 18:23:46 +01:00
2020-09-08 11:38:25 +02:00
< a href = " '. $structure->fileurl ('download.php').'?id_record='. $id_record .' " class = " btn btn-primary btn-lg '.( $generata ? '' : 'disabled').' " target = " _blank " '.($generata ? ' ' : ' disabled ').' >
2019-02-26 12:13:51 +01:00
< i class = " fa fa-download " ></ i > '.tr(' Scarica ').'
</ a > ' ;
2019-03-08 15:18:52 +01:00
2019-01-17 16:11:10 +01:00
echo '
2018-12-07 18:16:46 +01:00
< i class = " fa fa-arrow-right fa-fw text-muted " ></ i >
2020-09-08 15:11:04 +02:00
< button type = " button " onclick = " inviaFE(this) " class = " btn btn-success btn-lg '.( $inviabile ? '' : 'disabled').' " >
2018-12-07 18:16:46 +01:00
< i class = " fa fa-paper-plane " ></ i > '.tr(' Invia ').'
2020-07-01 15:57:16 +02:00
</ button > ' ;
2020-09-08 11:38:25 +02:00
$verify = Interaction :: isEnabled () && $generata ;
2020-07-01 15:57:16 +02:00
echo '
< i class = " fa fa-arrow-right fa-fw text-muted " ></ i >
2020-09-08 15:11:04 +02:00
< button type = " button " onclick = " verificaNotificheFE(this) " class = " btn btn-warning btn-lg '.( $verify ? '' : 'disabled').' " >
2020-07-01 15:57:16 +02:00
< i class = " fa fa-question-circle " ></ i > '.tr(' Verifica notifiche ').'
2020-07-08 10:40:04 +02:00
</ button >
</ div > ' ;
2020-07-01 15:57:16 +02:00
echo '<br><br>' ;
2018-12-07 18:23:46 +01:00
2020-09-08 10:38:27 +02:00
// Messaggio informativo sulla ricevuta principale impostata
2020-09-08 11:38:25 +02:00
if ( ! empty ( $fattura -> codice_stato_fe )) {
echo '
< div class = " alert alert-'. $stato_fe['tipo'] .' " >
2020-07-08 10:40:04 +02:00
< div class = " pull-right " >
2020-09-08 11:38:25 +02:00
< i class = " fa fa-clock-o tip " title = " '.tr('Data e ora').' " ></ i > '.timestampFormat($record[' data_stato_fe ' ]);
2020-07-08 10:40:04 +02:00
2020-09-08 11:38:25 +02:00
if ( ! empty ( $ricevuta_principale )) {
echo '
2020-09-08 10:38:27 +02:00
< a href = " '.ROOTDIR.'/view.php?file_id='. $ricevuta_principale->id .' " target = " _blank " class = " btn btn-info btn-xs " >
2020-07-08 10:40:04 +02:00
< i class = " fa fa-external-link " ></ i > '.tr(' Visualizza ricevuta ').'
</ a > ' ;
2020-09-08 11:38:25 +02:00
}
2019-01-25 15:54:56 +01:00
2020-09-08 11:38:25 +02:00
echo '
2020-07-08 10:40:04 +02:00
</ div >
2020-09-08 11:38:25 +02:00
< big >
< i class = " '. $stato_fe['icon'] .' " style = " color:#fff; " ></ i >
< b > '.$stato_fe[' codice '].' </ b > - '.$stato_fe[' descrizione '].'
</ big > ' ;
if ( ! empty ( $record [ 'descrizione_ricevuta_fe' ])) {
echo '
< br >< b > '.tr(' Note ', [], [' upper ' => true]).' :</ b > '.$record[' descrizione_ricevuta_fe ' ];
}
if ( $fattura -> codice_stato_fe == 'GEN' ) {
echo '
< br >< i class = " fa fa-info-circle " ></ i > '.tr("La fattura è stata generata ed è pronta per l' invio " ).'.';
}
echo '
2018-07-09 12:57:55 +02:00
</ div > ' ;
2018-11-08 13:11:50 +01:00
2020-09-08 11:38:25 +02:00
// Lettura della ricevuta
if ( ! empty ( $ricevuta_principale )) {
$contenuto_ricevuta = XML :: readFile ( $ricevuta_principale -> filepath );
$lista_errori = $contenuto_ricevuta [ 'ListaErrori' ];
2020-07-08 10:40:04 +02:00
2020-09-08 11:38:25 +02:00
if ( ! empty ( $lista_errori )) {
echo '
2020-07-08 10:40:04 +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 > ' ;
2020-09-08 11:38:25 +02:00
$lista_errori = $lista_errori [ 0 ] ? $lista_errori : [ $lista_errori ];
foreach ( $lista_errori as $errore ) {
$errore = $errore [ 'Errore' ];
echo '
2020-07-08 10:40:04 +02:00
< tr >
< td > '.$errore[' Codice '].' </ td >
< td > '.$errore[' Descrizione '].' </ td >
</ tr > ' ;
2020-09-08 11:38:25 +02:00
}
2020-07-08 10:40:04 +02:00
2020-09-08 11:38:25 +02:00
echo '
2020-07-08 10:40:04 +02:00
</ tbody >
</ table > ' ;
}
}
}
2019-07-23 16:12:46 +02:00
echo '
2018-11-08 13:11:50 +01:00
< script >
2020-09-08 15:11:04 +02:00
function inviaFE ( button ) {
if ( confirm ( " '.tr('Inviare la fattura al SDI?').' " )) {
let restore = buttonLoading ( button );
$ . ajax ({
url : globals . rootdir + " /actions.php " ,
type : " post " ,
data : {
op : " send " ,
id_module : " '. $id_module .' " ,
id_plugin : " '. $id_plugin .' " ,
id_record : " '. $id_record .' " ,
},
success : function ( data ) {
buttonRestore ( button , restore );
data = JSON . parse ( data );
if ( data . code === " 200 " ) {
swal ( " '.tr('Fattura inviata!').' " , data . message , " success " );
$ ( button ) . attr ( " disabled " , true ) . addClass ( " disabled " );
} else {
swal ( " '.tr('Invio fallito').' " , data . code + " - " + data . message , " error " );
}
},
error : function () {
swal ( " '.tr('Errore').' " , " '.tr('Errore durante il salvataggio').' " , " 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
}
2020-09-08 15:11:04 +02:00
});
}
2020-07-01 15:57:16 +02:00
}
2020-09-08 15:11:04 +02:00
function verificaNotificheFE ( button ) {
2020-09-08 10:38:27 +02:00
openModal ( " '.tr('Gestione ricevute').' " , " '. $structure->fileurl ('notifiche.php').' " );
/*
2020-09-08 15:11:04 +02:00
let restore = buttonLoading ( button );
2020-07-01 15:57:16 +02:00
$ . ajax ({
url : globals . rootdir + " /actions.php " ,
type : " post " ,
data : {
op : " verify " ,
id_module : " '. $id_module .' " ,
id_plugin : " '. $id_plugin .' " ,
id_record : " '. $id_record .' " ,
},
success : function ( data ) {
2020-09-08 15:11:04 +02:00
buttonRestore ( button , restore );
2020-07-02 10:26:17 +02:00
data = JSON . parse ( data );
2020-07-01 15:57:16 +02:00
2020-07-02 10:26: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-07-01 15:57:16 +02:00
} else {
swal ( " '.tr('Verifica fallita').' " , data . code + " - " + data . message , " error " );
}
},
error : function ( data ) {
swal ( " '.tr('Errore').' " , " '.tr('Errore durante la verifica').' " , " error " );
2020-09-08 15:11:04 +02:00
buttonRestore ( button , restore );
2020-07-01 15:57:16 +02:00
}
2020-09-08 10:38:27 +02:00
}); */
2020-07-01 15:57:16 +02:00
}
2020-09-08 15:11:04 +02:00
function generaFE ( button ) {
salvaForm ( button , " #edit-form " ) . then ( function ( valid ) {
2020-08-04 16:29:01 +02:00
if ( valid ) { ' ;
2019-07-23 16:12:46 +02:00
2020-09-08 11:38:25 +02:00
if ( $generata ) {
2019-07-23 16:12:46 +02:00
echo '
2020-09-08 15:11:04 +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>*/
2020-08-04 16:29:01 +02:00
swal ({
title : " '.tr('Sei sicuro di rigenerare la fattura?').' " ,
2020-09-08 15:11:04 +02:00
html : " <p>'.tr('Attenzione: sarà generato un nuovo progressivo invio').'.</p> " ,
2020-08-04 16:29:01 +02:00
type : " warning " ,
showCancelButton : true ,
confirmButtonColor : " #30d64b " ,
cancelButtonColor : " #d33 " ,
2020-09-08 15:11:04 +02:00
confirmButtonText : " '.tr('Genera').' "
2020-08-04 16:29:01 +02:00
}) . then (( result ) => {
if ( result ) {
$ ( " #form-xml " ) . submit ();
}
}); ' ;
2019-07-23 16:12:46 +02:00
} else {
echo '
2020-07-01 15:57:16 +02:00
2020-08-04 16:29:01 +02:00
$ ( " #form-xml " ) . submit (); ' ;
2019-07-23 16:12:46 +02:00
}
echo '
2020-08-04 16:29:01 +02:00
} else {
swal ({
type : " error " ,
title : " '.tr('Errore').' " ,
text : " '.tr('Alcuni campi obbligatori non sono stati compilati correttamente').'. " ,
});
}
});
2020-09-08 15:11:04 +02:00
};
2018-11-08 13:11:50 +01:00
</ script > ' ;