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
2018-11-30 16:10:15 +01:00
use Plugins\ExportFE\FatturaElettronica ;
2018-12-07 18:16:46 +01:00
use Plugins\ExportFE\Interaction ;
2018-11-30 16:10:15 +01:00
2020-06-04 12:27:07 +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-04-07 18:25:48 +02:00
$abilita_genera = empty ( $fattura -> codice_stato_fe ) || in_array ( $fattura -> codice_stato_fe , [ 'GEN' , 'NS' , 'EC02' , 'ERR' ]);
2019-07-16 12:27:36 +02:00
2018-09-21 15:50:42 +02:00
if ( ! empty ( $fattura_pa )) {
2018-07-09 15:01:11 +02:00
$disabled = false ;
2018-12-07 17:01:42 +01:00
$generated = $fattura_pa -> isGenerated ();
2018-09-18 15:57:55 +02:00
} else {
2018-07-09 15:01:11 +02:00
$disabled = true ;
2018-09-18 15:57:55 +02:00
$generated = 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
2019-07-16 12:27:36 +02:00
< button id = " genera " type = " submit " class = " btn btn-primary btn-lg '.( $disabled || ! $abilita_genera ? 'disabled' : '').' " '.($disabled || !$abilita_genera ? ' disabled ' : null).' >
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
$file = $generated ? Models\Upload :: where ( 'filename' , $fattura_pa -> getFilename ()) -> 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 >
2019-01-17 16:11:10 +01:00
< a href = " '.ROOTDIR.'/view.php?file_id='.( $file ? $file->id : null).' " class = " btn btn-info btn-lg '.( $generated ? '' : 'disabled').' " target = " _blank " '.($generated ? ' ' : ' 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-01-20 19:03:10 +01:00
$send = Interaction :: isEnabled () && $generated && in_array ( $record [ 'codice_stato_fe' ], [ 'GEN' , 'ERVAL' , 'ERR' ]);
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
2019-02-26 12:13:51 +01:00
< a href = " '. $structure->fileurl ('download.php').'?id_record='. $id_record .' " class = " btn btn-primary btn-lg '.( $generated ? '' : 'disabled').' " target = " _blank " '.($generated ? ' ' : ' disabled ').' >
< 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 >
2019-01-22 10:09:22 +01:00
< button onclick = " if( confirm( \ ''.tr('Inviare la fattura al SDI?').' \ ') ) { send(this); } " class = " btn btn-success btn-lg '.( $send ? '' : 'disabled').' " target = " _blank " '.($send ? ' ' : ' 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 > ' ;
$verify = Interaction :: isEnabled () && $generated ;
echo '
< i class = " fa fa-arrow-right fa-fw text-muted " ></ i >
< button class = " btn btn-warning btn-lg '.( $verify ? '' : 'disabled').' " target = " _blank " '.($verify ? ' ' : ' disabled ').' onclick = " verify(this) " >
< 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
$ricevuta_principale = $fattura -> getRicevutaPrincipale ();
2019-02-02 11:23:34 +01:00
if ( ! empty ( $record [ 'codice_stato_fe' ])) {
2019-02-01 18:19:13 +01:00
if ( $record [ 'codice_stato_fe' ] == 'GEN' ) {
echo '
2020-08-31 16:08:59 +02:00
< div class = " alert alert-info " >< i class = " fa fa-info-circle " ></ i > '.tr("La fattura è stata generata ed è pronta per l' invio " ).'.</div>';
2019-02-01 18:19:13 +01:00
} else {
$stato_fe = database () -> fetchOne ( 'SELECT codice, descrizione, icon FROM fe_stati_documento WHERE codice=' . prepare ( $record [ 'codice_stato_fe' ]));
if ( in_array ( $stato_fe [ 'codice' ], [ 'EC01' , 'RC' ])) {
$class = 'success' ;
2020-06-04 12:27:07 +02:00
} elseif ( in_array ( $stato_fe [ 'codice' ], [ 'ERVAL' , 'GEN' , 'MC' , 'WAIT' , 'NE' ])) {
2019-02-01 18:19:13 +01:00
$class = 'warning' ;
} else {
$class = 'danger' ;
}
echo '
2020-07-08 10:40:04 +02:00
< div class = " alert alert-'. $class .' " >
< div class = " pull-right " >
2020-08-31 16:08:59 +02:00
< i class = " fa fa-clock-o tip " title = " '.tr('Data e ora ricezione').' " ></ i > '.Translator::timestampToLocale($record[' data_stato_fe ' ]);
2020-07-08 10:40:04 +02:00
2020-09-08 10:38:27 +02:00
if ( ! empty ( $ricevuta_principale )) {
2020-07-08 10:40:04 +02:00
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 > ' ;
}
2019-01-25 15:54:56 +01:00
2020-07-08 10:40:04 +02:00
echo '
</ div >
< big >< i class = " '. $stato_fe['icon'] .' " style = " color:#fff; " ></ i >
< b > '.$stato_fe[' codice '].' </ b > - '.$stato_fe[' descrizione '].' </ big > '.(!empty($record[' descrizione_ricevuta_fe ']) ? ' < br >< b > '.tr(' Note ', [], [' upper ' => true]).' :</ b > '.$record[' descrizione_ricevuta_fe '] : ' ').'
2018-07-09 12:57:55 +02:00
</ div > ' ;
2018-11-08 13:11:50 +01:00
2020-07-08 10:40:04 +02:00
// Lettura della ricevuta
2020-09-08 10:38:27 +02:00
if ( ! empty ( $ricevuta_principale ) && $stato_fe [ 'codice' ] == 'NS' ) {
$contenuto_ricevuta = \Util\XML :: readFile ( $ricevuta_principale -> filepath );
2020-07-08 10:40:04 +02:00
$lista_errori = $contenuto_ricevuta [ 'ListaErrori' ];
if ( ! empty ( $lista_errori )) {
echo '
< 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 >
< td > '.$errore[' Descrizione '].' </ td >
</ tr > ' ;
}
echo '
</ tbody >
</ table > ' ;
}
}
}
}
2019-07-23 16:12:46 +02:00
echo '
2018-11-08 13:11:50 +01:00
< script >
2020-07-01 15:57:16 +02:00
function send ( btn ) {
2020-09-08 10:38:27 +02:00
let restore = buttonLoading ( btn );
2020-07-01 15:57:16 +02:00
$ . 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 ( btn , restore );
2020-07-02 10:26:17 +02:00
data = JSON . parse ( data );
2020-07-01 15:57:16 +02:00
if ( data . code == " 200 " ) {
swal ( " '.tr('Fattura inviata!').' " , data . message , " success " );
$ ( btn ) . attr ( " disabled " , true ) . addClass ( " disabled " );
} else {
swal ( " '.tr('Invio fallito').' " , data . code + " - " + data . message , " error " );
}
},
error : function ( data ) {
swal ( " '.tr('Errore').' " , " '.tr('Errore durante il salvataggio').' " , " error " );
buttonRestore ( btn , restore );
}
});
}
function verify ( btn ) {
2020-09-08 10:38:27 +02:00
openModal ( " '.tr('Gestione ricevute').' " , " '. $structure->fileurl ('notifiche.php').' " );
/*
let restore = buttonLoading ( btn );
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 ) {
buttonRestore ( btn , 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 " );
buttonRestore ( btn , restore );
}
2020-09-08 10:38:27 +02:00
}); */
2020-07-01 15:57:16 +02:00
}
2020-08-04 16:29:01 +02:00
$ ( " #genera " ) . click ( function ( event ) {
2018-11-08 13:11:50 +01:00
event . preventDefault ();
2018-12-07 18:16:46 +01:00
2020-08-04 16:29:01 +02:00
salvaForm ( this , " #edit-form " ) . then ( function ( valid ) {
if ( valid ) { ' ;
2019-07-23 16:12:46 +02:00
if ( $generated ) {
echo '
2020-08-04 16:29:01 +02:00
swal ({
title : " '.tr('Sei sicuro di rigenerare la fattura?').' " ,
html : " <p>'.tr('Attenzione: sarà generato un nuovo progressivo invio').'.</p><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> " ,
type : " warning " ,
showCancelButton : true ,
confirmButtonColor : " #30d64b " ,
cancelButtonColor : " #d33 " ,
confirmButtonText : " Genera "
}) . 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').'. " ,
});
}
});
2018-12-07 18:16:46 +01:00
});
2018-11-08 13:11:50 +01:00
</ script > ' ;