2017-09-06 11:59:47 +02:00
< ? php
include_once __DIR__ . '/../../core.php' ;
2019-03-07 17:16:42 +01:00
use Modules\Fatture\Fattura ;
2019-06-13 15:57:55 +02:00
use Plugins\ExportFE\FatturaElettronica ;
2019-08-29 16:01:49 +02:00
use Plugins\ExportFE\Interaction ;
2019-06-29 11:01:26 +02:00
use Util\Zip ;
2018-09-19 16:51:37 +02:00
2017-09-06 11:59:47 +02:00
switch ( post ( 'op' )) {
case 'export-bulk' :
$dir = DOCROOT . '/files/export_fatture/' ;
2017-09-13 15:38:47 +02:00
directory ( $dir . 'tmp/' );
2017-09-06 11:59:47 +02:00
2019-03-07 17:16:42 +01:00
$dir = slashes ( $dir );
$zip = slashes ( $dir . 'fatture_' . time () . '.zip' );
2017-09-07 16:51:14 +02:00
// Rimozione dei contenuti precedenti
$files = glob ( $dir . '/*.zip' );
foreach ( $files as $file ) {
2017-09-11 17:49:03 +02:00
delete ( $file );
2017-09-07 16:51:14 +02:00
}
2017-09-06 11:59:47 +02:00
// Selezione delle fatture da stampare
2018-07-18 15:20:10 +02:00
$fatture = $dbo -> fetchArray ( 'SELECT co_documenti.id, numero_esterno, data, ragione_sociale, co_tipidocumento.descrizione FROM co_documenti INNER JOIN an_anagrafiche ON co_documenti.idanagrafica=an_anagrafiche.idanagrafica INNER JOIN co_tipidocumento ON co_documenti.idtipodocumento=co_tipidocumento.id WHERE co_documenti.id IN(' . implode ( ',' , $id_records ) . ')' );
2017-09-06 11:59:47 +02:00
2018-07-18 15:20:10 +02:00
if ( ! empty ( $fatture )) {
foreach ( $fatture as $r ) {
2018-09-26 10:49:38 +02:00
$print = Prints :: getModulePredefinedPrint ( $id_module );
2018-06-26 14:30:26 +02:00
2019-07-10 11:53:35 +02:00
Prints :: render ( $print [ 'id' ], $r [ 'id' ], $dir . 'tmp/' );
2017-09-13 18:47:42 +02:00
}
2017-09-06 11:59:47 +02:00
2017-09-13 18:47:42 +02:00
// Creazione zip
if ( extension_loaded ( 'zip' )) {
2019-03-07 17:16:42 +01:00
Zip :: create ( $dir . 'tmp/' , $zip );
2017-09-06 11:59:47 +02:00
2017-09-13 18:47:42 +02:00
// Invio al browser dello zip
2019-03-07 17:16:42 +01:00
download ( $zip );
2017-09-06 11:59:47 +02:00
2017-09-13 18:47:42 +02:00
// Rimozione dei contenuti
delete ( $dir . 'tmp/' );
}
2017-09-06 12:50:44 +02:00
}
2017-09-06 11:59:47 +02:00
break ;
2018-03-19 19:07:32 +01:00
2019-03-07 17:16:42 +01:00
case 'delete-bulk' :
2019-05-16 04:52:16 +02:00
foreach ( $id_records as $id ) {
$dbo -> query ( 'DELETE FROM co_documenti WHERE id = ' . prepare ( $id ) . Modules :: getAdditionalsQuery ( $id_module ));
$dbo -> query ( 'DELETE FROM co_righe_documenti WHERE iddocumento=' . prepare ( $id ) . Modules :: getAdditionalsQuery ( $id_module ));
$dbo -> query ( 'DELETE FROM co_scadenziario WHERE iddocumento=' . prepare ( $id ) . Modules :: getAdditionalsQuery ( $id_module ));
$dbo -> query ( 'DELETE FROM mg_movimenti WHERE iddocumento=' . prepare ( $id ) . Modules :: getAdditionalsQuery ( $id_module ));
}
flash () -> info ( tr ( 'Fatture eliminate!' ));
2019-03-07 17:16:42 +01:00
break ;
2019-06-13 15:57:55 +02:00
case 'genera-xml' :
$failed = [];
2019-06-29 11:01:26 +02:00
$added = [];
2019-06-13 15:57:55 +02:00
2019-06-29 11:01:26 +02:00
foreach ( $id_records as $id ) {
2019-06-13 15:57:55 +02:00
$fattura = Fattura :: find ( $id );
2019-07-23 16:27:36 +02:00
try {
2019-06-13 15:57:55 +02:00
$fattura_pa = new FatturaElettronica ( $id );
2019-07-23 16:24:04 +02:00
if ( ! empty ( $fattura_pa ) && ! $fattura_pa -> isGenerated ()) {
2019-06-13 15:57:55 +02:00
$file = $fattura_pa -> save ( $upload_dir );
$added [] = $fattura -> numero_esterno ;
}
2019-07-23 16:27:36 +02:00
} catch ( UnexpectedValueException $e ) {
2019-06-13 15:57:55 +02:00
$failed [] = $fattura -> numero_esterno ;
}
}
if ( ! empty ( $failed )) {
flash () -> warning ( tr ( 'Le fatture elettroniche _LIST_ non sono state generate.' , [
'_LIST_' => implode ( ', ' , $failed ),
]));
}
if ( ! empty ( $added )) {
flash () -> info ( tr ( 'Le fatture elettroniche _LIST_ sono state generate.' , [
'_LIST_' => implode ( ', ' , $added ),
]));
}
break ;
2019-06-13 16:24:50 +02:00
2019-08-29 16:01:49 +02:00
case 'hook-send' :
foreach ( $id_records as $id ) {
$fattura = Fattura :: find ( $id );
$fe = new \Plugins\ExportFE\FatturaElettronica ( $fattura -> id );
if ( $fe -> isGenerated () && $fattura -> codice_stato_fe == 'GEN' ) {
$fattura -> codice_stato_fe = 'QUEUE' ;
$fattura -> data_stato_fe = date ( 'Y-m-d H:i:s' );
$fattura -> hook_send = true ;
$fattura -> save ();
}
}
flash () -> info ( tr ( 'Le fatture elettroniche sono state aggiunte alla coda di invio' ));
break ;
2019-03-07 17:16:42 +01:00
case 'export-xml-bulk' :
$dir = DOCROOT . '/files/export_fatture/' ;
directory ( $dir . 'tmp/' );
$dir = slashes ( $dir );
$zip = slashes ( $dir . 'fatture_' . time () . '.zip' );
// Rimozione dei contenuti precedenti
$files = glob ( $dir . '/*.zip' );
foreach ( $files as $file ) {
delete ( $file );
}
2019-06-13 14:46:04 +02:00
// Selezione delle fatture da esportare
2019-05-10 17:30:00 +02:00
$fatture = $dbo -> fetchArray ( 'SELECT co_documenti.id, numero_esterno, data, ragione_sociale, co_tipidocumento.descrizione, co_tipidocumento.dir FROM co_documenti INNER JOIN an_anagrafiche ON co_documenti.idanagrafica=an_anagrafiche.idanagrafica INNER JOIN co_tipidocumento ON co_documenti.idtipodocumento=co_tipidocumento.id INNER JOIN co_statidocumento ON co_documenti.idstatodocumento=co_statidocumento.id WHERE co_documenti.id IN(' . implode ( ',' , $id_records ) . ')' );
2019-03-07 17:16:42 +01:00
$failed = [];
2019-06-13 14:46:04 +02:00
$added = 0 ;
2019-03-07 17:16:42 +01:00
if ( ! empty ( $fatture )) {
foreach ( $fatture as $r ) {
$fattura = Fattura :: find ( $r [ 'id' ]);
$include = true ;
try {
2019-03-28 13:00:49 +01:00
if ( $r [ 'dir' ] == 'entrata' ) {
$fe = new \Plugins\ExportFE\FatturaElettronica ( $fattura -> id );
$include = $fe -> isGenerated ();
} else {
$include = $fattura -> isFE ();
}
2019-03-07 17:16:42 +01:00
} catch ( UnexpectedValueException $e ) {
$include = false ;
2018-06-26 14:30:26 +02:00
}
2019-03-07 17:16:42 +01:00
if ( ! $include ) {
$failed [] = $fattura -> numero_esterno ;
} else {
2019-03-28 13:00:49 +01:00
if ( $r [ 'dir' ] == 'entrata' ) {
$src = $fe -> getFilename ();
$dst = $src ;
} else {
2019-04-04 17:30:58 +02:00
$src = basename ( $fattura -> uploads () -> where ( 'name' , 'Fattura Elettronica' ) -> first () -> filepath );
2019-07-19 18:11:07 +02:00
$dst = basename ( $fattura -> uploads () -> where ( 'name' , 'Fattura Elettronica' ) -> first () -> original_name );
2019-03-28 13:00:49 +01:00
}
2019-03-07 17:16:42 +01:00
2019-03-28 13:00:49 +01:00
$file = slashes ( $module -> upload_directory . '/' . $src );
$dest = slashes ( $dir . '/tmp/' . $dst );
2019-03-07 17:16:42 +01:00
$result = copy ( $file , $dest );
2019-06-13 14:46:04 +02:00
2019-03-07 17:16:42 +01:00
if ( $result ) {
2019-06-29 11:01:26 +02:00
++ $added ;
2019-08-29 10:25:14 +02:00
//operationLog('export-xml-bulk', ['id_record' => $r['id']]);
2019-03-07 17:16:42 +01:00
} else {
$failed [] = $fattura -> numero_esterno ;
}
}
}
// Creazione zip
2019-06-29 11:01:26 +02:00
if ( extension_loaded ( 'zip' ) and ! empty ( $added )) {
2019-03-07 17:16:42 +01:00
Zip :: create ( $dir . 'tmp/' , $zip );
// Invio al browser il file zip
download ( $zip );
// Rimozione dei contenuti
delete ( $dir . 'tmp/' );
2018-06-26 14:30:26 +02:00
}
2019-03-07 17:16:42 +01:00
if ( ! empty ( $failed )) {
2019-06-13 14:46:04 +02:00
flash () -> warning ( tr ( 'Le fatture elettroniche _LIST_ non sono state incluse poichè non ancora generate o non presenti sul server' , [
2019-03-07 17:16:42 +01:00
'_LIST_' => implode ( ', ' , $failed ),
]));
}
}
2018-03-22 15:40:20 +01:00
break ;
2017-09-06 11:59:47 +02:00
}
2019-05-13 17:01:43 +02:00
if ( App :: debug ()) {
2019-05-16 04:52:16 +02:00
$operations = [
2019-07-25 12:54:28 +02:00
'delete-bulk' => '<span><i class="fa fa-trash"></i> ' . tr ( 'Elimina selezionati' ) . '</span>' ,
2019-05-16 04:52:16 +02:00
];
2019-05-13 17:01:43 +02:00
}
2018-03-19 19:07:32 +01:00
2019-07-25 15:06:07 +02:00
$operations [ 'registrazione-contabile' ] = [
2019-07-25 12:54:28 +02:00
'text' => '<span><i class="fa fa-calculator"></i> ' . tr ( 'Registrazione contabile' ) . '</span>' ,
'data' => [
'title' => tr ( 'Registrazione contabile' ),
'type' => 'modal' ,
'origine' => 'fatture' ,
'url' => $rootdir . '/add.php?id_module=' . Modules :: get ( 'Prima nota' )[ 'id' ],
],
];
2019-06-13 15:57:55 +02:00
if ( $module -> name == 'Fatture di vendita' ) {
$operations [ 'genera-xml' ] = [
2019-07-25 12:54:28 +02:00
'text' => '<span><i class="fa fa-file-code-o"></i> ' . tr ( 'Genera fatture elettroniche' ) . '</span>' ,
2019-06-13 15:57:55 +02:00
'data' => [
'title' => '' ,
'msg' => tr ( 'Generare le fatture elettroniche per i documenti selezionati?<br><small>(le fatture dovranno essere nello stato <i class="fa fa-clock-o text-info" title="Emessa"></i> <small>Emessa</small> e non essere mai state generate)</small>' ),
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
'blank' => true ,
],
];
2019-06-13 14:46:04 +02:00
2019-05-13 17:01:43 +02:00
$operations [ 'export-bulk' ] = [
2019-07-25 12:54:28 +02:00
'text' => '<span class="' . (( ! extension_loaded ( 'zip' )) ? 'text-muted disabled' : '' ) . '"><i class="fa fa-file-archive-o"></i> ' . tr ( 'Esporta stampe' ) . '</span>' ,
2017-09-06 11:59:47 +02:00
'data' => [
2019-06-13 14:46:04 +02:00
'title' => '' ,
'msg' => tr ( 'Vuoi davvero esportare i PDF delle fatture selezionate in un archivio ZIP?' ),
2017-09-06 11:59:47 +02:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
2017-09-07 16:51:14 +02:00
'blank' => true ,
2017-09-06 11:59:47 +02:00
],
2019-03-07 17:16:42 +01:00
];
}
2019-05-13 17:01:43 +02:00
$operations [ 'export-xml-bulk' ] = [
2019-07-25 12:54:28 +02:00
'text' => '<span class="' . (( ! extension_loaded ( 'zip' )) ? 'text-muted disabled' : '' ) . '"><i class="fa fa-file-archive-o"></i> ' . tr ( 'Esporta XML' ) . '</span>' ,
2019-03-28 13:00:49 +01:00
'data' => [
2019-06-13 14:46:04 +02:00
'title' => '' ,
'msg' => tr ( 'Vuoi davvero esportare le fatture elettroniche selezionate in un archivio ZIP?' ),
2019-03-28 13:00:49 +01:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
'blank' => true ,
],
];
2019-08-29 16:01:49 +02:00
if ( Interaction :: isEnabled ()) {
$operations [ 'hook-send' ] = [
'text' => '<span><i class="fa fa-paper-plane"></i> ' . tr ( 'Coda di invio FE' ) . '</span>' ,
'data' => [
'title' => '' ,
'msg' => tr ( 'Vuoi davvero aggiungere queste fatture alla coda di invio per le fatture elettroniche?' ),
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
],
];
}
2019-05-13 17:01:43 +02:00
return $operations ;