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 ;
2020-01-17 15:23:32 +01:00
case 'copy-bulk' :
2020-01-28 14:06:33 +01:00
foreach ( $id_records as $id ) {
$fattura = Fattura :: find ( $id );
2020-01-17 15:23:32 +01:00
2020-01-28 14:06:33 +01:00
$id_segment = ( post ( 'id_segment' ) ? post ( 'id_segment' ) : $fattura -> id_segment );
2020-02-05 14:51:39 +01:00
$dir = $dbo -> fetchOne ( 'SELECT dir FROM co_tipidocumento WHERE id=' . prepare ( $fattura -> idtipodocumento ))[ 'dir' ];
2020-01-17 15:23:32 +01:00
2020-01-28 14:06:33 +01:00
//+ 1 giorno
2020-02-05 14:51:39 +01:00
if ( post ( 'skip_time' ) == 'Giorno' ) {
$data = date ( 'Y-m-d' , strtotime ( '+1 day' , strtotime ( $fattura -> data )));
2020-01-17 15:23:32 +01:00
}
//+ 1 settimana
2020-02-05 14:51:39 +01:00
if ( post ( 'skip_time' ) == 'Settimana' ) {
$data = date ( 'Y-m-d' , strtotime ( '+1 week' , strtotime ( $fattura -> data )));
2020-01-17 15:23:32 +01:00
}
//+ 1 mese
2020-02-05 14:51:39 +01:00
if ( post ( 'skip_time' ) == 'Mese' ) {
$data = date ( 'Y-m-d' , strtotime ( '+1 month' , strtotime ( $fattura -> data )));
2020-01-17 15:23:32 +01:00
}
//+ 1 anno
2020-02-05 14:51:39 +01:00
if ( post ( 'skip_time' ) == 'Anno' ) {
$data = date ( 'Y-m-d' , strtotime ( '+1 year' , strtotime ( $fattura -> data )));
2020-01-17 15:23:32 +01:00
}
2020-01-28 14:06:33 +01:00
$new = $fattura -> replicate ();
$new -> id_segment = $id_segment ;
$new -> numero_esterno = Fattura :: getNextNumeroSecondario ( $data , $dir , $id_segment );
$new -> numero = Fattura :: getNextNumero ( $data , $dir , $id_segment );
$new -> idstatodocumento = 2 ;
$new -> data = $data ;
$new -> save ();
2020-01-17 15:23:32 +01:00
2020-01-28 14:06:33 +01:00
$righe = $fattura -> getRighe ();
foreach ( $righe as $riga ) {
2020-02-05 14:51:39 +01:00
if ( ! post ( 'riferimenti' )) {
2020-01-28 14:06:33 +01:00
$riga -> idpreventivo = 0 ;
$riga -> idcontratto = 0 ;
$riga -> idintervento = 0 ;
$riga -> idddt = 0 ;
$riga -> idordine = 0 ;
}
2020-01-17 15:23:32 +01:00
2020-01-28 14:06:33 +01:00
$new_riga = $riga -> replicate ();
$new_riga -> setParent ( $new );
2020-01-17 15:23:32 +01:00
2020-01-28 14:06:33 +01:00
$new_riga -> save ();
2020-01-17 15:23:32 +01:00
2020-02-05 14:51:39 +01:00
if ( $new_riga -> idarticolo ) {
2020-01-28 14:06:33 +01:00
$articolo = Articolo :: find ( $new_riga -> id );
$articolo -> movimentaMagazzino ( $articolo -> qta );
2020-01-17 15:23:32 +01:00
}
}
}
flash () -> info ( tr ( 'Fatture duplicate correttamente!' ));
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
2020-01-17 15:23:32 +01:00
$operations [ 'copy-bulk' ] = [
'text' => '<span><i class="fa fa-copy"></i> ' . tr ( 'Duplica selezionati' ) . '</span>' ,
'data' => [
2020-01-28 14:06:33 +01:00
'msg' => tr ( 'Vuoi davvero duplicare le righe selezionate?' ) . '<br><br>{[ "type": "select", "label": "", "name": "skip_time", "required": 1, "values": "list=\"Giorno\":\"' . tr ( 'Giorno' ) . '\", \"Settimana\":\"' . tr ( 'Settimana' ) . '\", \"Mese\":\"' . tr ( 'Mese' ) . '\", \"Anno\":\"' . tr ( 'Anno' ) . '\" ", "value": "Giorno" ]}<br>{[ "type": "select", "label": "", "name": "id_segment", "required": 1, "values": "query=SELECT id, name AS descrizione FROM zz_segments WHERE id_module=' . $id_module . ' ORDER BY name", "value": "Giorno" ]}<br>{[ "type": "checkbox", "placeholder": "' . tr ( 'Aggiungere i riferimenti ai documenti esterni?' ) . '", "name": "riferimenti" ]}' ,
2020-01-17 15:23:32 +01:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
],
];
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 ;