2017-09-06 11:59:47 +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 />.
*/
2017-09-06 11:59:47 +02:00
include_once __DIR__ . '/../../core.php' ;
2023-08-04 14:54:28 +02:00
use Carbon\Carbon ;
2024-03-22 15:52:24 +01:00
use Models\Module ;
2021-02-25 10:23:26 +01:00
use Modules\Aggiornamenti\Controlli\DatiFattureElettroniche ;
2021-12-10 12:45:44 +01:00
use Modules\Anagrafiche\Anagrafica ;
2020-11-27 15:10:27 +01:00
use Modules\Fatture\Export\CSV ;
2019-03-07 17:16:42 +01:00
use Modules\Fatture\Fattura ;
2023-08-04 14:54:28 +02:00
use Modules\Fatture\Stato ;
2019-06-13 15:57:55 +02:00
use Plugins\ExportFE\FatturaElettronica ;
2019-08-29 16:01:49 +02:00
use Plugins\ExportFE\Interaction ;
2023-08-04 14:54:28 +02:00
use Plugins\ReceiptFE\Ricevuta ;
2019-06-29 11:01:26 +02:00
use Util\Zip ;
2018-09-19 16:51:37 +02:00
2021-12-10 12:45:44 +01:00
$anagrafica_azienda = Anagrafica :: find ( setting ( 'Azienda predefinita' ));
2024-06-11 16:58:27 +02:00
$stato_emessa = Stato :: where ( 'name' , 'Emessa' ) -> first () -> id ;
2023-06-20 15:36:28 +02:00
$is_fiscale = $dbo -> selectOne ( 'zz_segments' , 'is_fiscale' , [ 'id' => $_SESSION [ 'module_' . $id_module ]])[ 'is_fiscale' ];
2021-12-10 12:45:44 +01:00
2017-09-06 11:59:47 +02:00
switch ( post ( 'op' )) {
case 'export-bulk' :
2020-09-23 13:36:37 +02:00
$dir = base_dir () . '/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
2024-04-18 17:44:05 +02:00
$fatture = $dbo -> fetchArray ( 'SELECT `co_documenti`.`id`, `numero_esterno`, `data`, `ragione_sociale`, `co_tipidocumento_lang`.`title` 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` LEFT JOIN `co_tipidocumento_lang` ON (`co_tipidocumento`.`id`=`co_tipidocumento_lang`.`id_record` AND `co_tipidocumento_lang`.`id_lang`=' . prepare ( Models\Locale :: getDefault () -> 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
2022-02-25 16:41:05 +01:00
Prints :: render ( $print [ 'id' ], $r [ 'id' ], $dir . 'tmp/' , false , false );
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
2021-02-23 11:34:37 +01:00
case 'exportFE-bulk' :
$dir = base_dir () . '/files/export_fatture/' ;
directory ( $dir . 'tmp/' );
$dir = slashes ( $dir );
$zip = slashes ( $dir . 'fattureFE_' . time () . '.zip' );
// Rimozione dei contenuti precedenti
$files = glob ( $dir . '/*.zip' );
foreach ( $files as $file ) {
delete ( $file );
}
2024-03-05 16:01:45 +01:00
$module = Module :: find ( $id_module );
2021-02-23 11:34:37 +01:00
2024-04-18 17:44:05 +02:00
if ( $module -> getTranslation ( 'title' ) == 'Fatture di vendita' ) {
2021-02-23 11:34:37 +01:00
$print_name = 'Fattura elettronica di vendita' ;
} else {
$print_name = 'Fattura elettronica di acquisto' ;
}
2022-03-11 19:16:10 +01:00
$id_print = Prints :: getPrints ()[ $print_name ];
2021-02-23 11:34:37 +01:00
if ( ! empty ( $id_records )) {
foreach ( $id_records as $id_record ) {
2023-08-04 14:54:28 +02:00
Prints :: render ( $id_print , $id_record , $dir . 'tmp/' , false , true );
2021-02-23 11:34:37 +01:00
}
// Creazione zip
if ( extension_loaded ( 'zip' )) {
Zip :: create ( $dir . 'tmp/' , $zip );
// Invio al browser dello zip
download ( $zip );
// Rimozione dei contenuti
delete ( $dir . 'tmp/' );
}
}
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 );
2020-11-20 17:53:09 +01:00
2019-07-23 16:27:36 +02:00
try {
2020-11-20 17:53:09 +01:00
$fattura_elettronica = new FatturaElettronica ( $id );
2019-07-23 16:24:04 +02:00
2020-11-20 17:53:09 +01:00
if ( ! empty ( $fattura_elettronica ) && ! $fattura_elettronica -> isGenerated ()) {
2024-04-08 15:44:33 +02:00
$file = $fattura_elettronica -> save ();
2019-06-13 15:57:55 +02:00
$added [] = $fattura -> numero_esterno ;
}
2024-05-23 11:36:25 +02:00
} catch ( UnexpectedValueException ) {
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 );
2020-11-20 17:53:09 +01:00
try {
$fattura_elettronica = new FatturaElettronica ( $fattura -> id );
if ( ! empty ( $fattura_elettronica ) && $fattura_elettronica -> 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 ();
$added [] = $fattura -> numero_esterno ;
}
2024-05-23 11:36:25 +02:00
} catch ( UnexpectedValueException ) {
2020-11-20 17:53:09 +01:00
$failed [] = $fattura -> numero_esterno ;
2019-08-29 16:01:49 +02:00
}
}
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' :
2020-09-23 13:36:37 +02:00
$dir = base_dir () . '/files/export_fatture/' ;
2019-03-07 17:16:42 +01:00
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
2024-04-18 17:44:05 +02:00
$fatture = $dbo -> fetchArray ( 'SELECT `co_documenti`.`id`, `numero_esterno`, `data`, `ragione_sociale`, `co_tipidocumento_lang`.`title`, `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` LEFT JOIN `co_tipidocumento_lang` ON (`co_tipidocumento_lang`.`id_record`=`co_tipidocumento`.`id` AND `co_tipidocumento_lang`.`id_lang`=' . prepare ( Models\Locale :: getDefault () -> 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' ) {
2020-11-20 17:53:09 +01:00
$fe = new FatturaElettronica ( $fattura -> id );
2019-03-28 13:00:49 +01:00
$include = $fe -> isGenerated ();
} else {
$include = $fattura -> isFE ();
}
2024-05-23 11:36:25 +02:00
} catch ( UnexpectedValueException ) {
2019-03-07 17:16:42 +01:00
$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' ) {
2021-09-07 15:51:03 +02:00
$dst = $fe -> getFilename ();
$src = $dbo -> selectOne ( 'zz_files' , 'filename' , [ 'original' => $dst ])[ 'filename' ];
2019-03-28 13:00:49 +01:00
} 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 );
2021-09-07 15:51:03 +02:00
$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 ;
2024-01-15 15:30:45 +01: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
2022-03-17 17:40:53 +01:00
case 'export-ricevute-bulk' :
$dir = base_dir () . '/files/export_fatture/' ;
directory ( $dir . 'tmp/' );
$dir = slashes ( $dir );
$zip = slashes ( $dir . 'ricevute_' . time () . '.zip' );
// Rimozione dei contenuti precedenti
$files = glob ( $dir . '/*.zip' );
foreach ( $files as $file ) {
delete ( $file );
}
// Selezione delle fatture da esportare
2024-04-18 17:44:05 +02:00
$fatture = $dbo -> fetchArray ( 'SELECT `co_documenti`.`id`, `numero_esterno`, `data`, `ragione_sociale`, `co_tipidocumento_lang`.`title`, `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` LEFT JOIN `co_tipidocumento_lang` ON (`co_tipidocumento`.`id` = `co_tipidocumento_lang`.`id_record` AND `co_tipidocumento_lang`.`id_lang` = ' . prepare ( Models\Locale :: getDefault () -> id ) . ') INNER JOIN `co_statidocumento` ON `co_documenti`.`idstatodocumento`=`co_statidocumento`.`id` WHERE `co_documenti`.`id` IN(' . implode ( ',' , $id_records ) . ')' );
2022-03-17 17:40:53 +01:00
$failed = [];
$added = 0 ;
if ( ! empty ( $fatture )) {
foreach ( $fatture as $r ) {
$fattura = Fattura :: find ( $r [ 'id' ]);
$zz_file = $dbo -> table ( 'zz_files' ) -> where ( 'id_module' , '=' , $id_module ) -> where ( 'id_record' , '=' , $fattura -> id ) -> where ( 'name' , 'like' , 'Ricevuta%' ) -> first ();
$src = basename ( $fattura -> uploads () -> where ( 'id' , $zz_file -> id ) -> first () -> filepath );
$dst = basename ( $fattura -> uploads () -> where ( 'id' , $zz_file -> id ) -> first () -> original_name );
$file = slashes ( $module -> upload_directory . '/' . $src );
$dest = slashes ( $dir . 'tmp/' . $dst );
$result = copy ( $file , $dest );
if ( $result ) {
++ $added ;
2024-01-15 15:30:45 +01:00
// operationLog('export-xml-bulk', ['id_record' => $r['id']]);
2022-03-17 17:40:53 +01:00
} else {
$failed [] = $fattura -> numero_esterno ;
}
}
// Creazione zip
if ( extension_loaded ( 'zip' ) and ! empty ( $added )) {
Zip :: create ( $dir . 'tmp/' , $zip );
// Invio al browser il file zip
download ( $zip );
// Rimozione dei contenuti
delete ( $dir . 'tmp/' );
}
if ( ! empty ( $failed )) {
flash () -> warning ( tr ( 'Le ricevute _LIST_ non sono state incluse poichè non ancora generate o non presenti sul server' , [
'_LIST_' => implode ( ', ' , $failed ),
]));
}
}
break ;
2020-01-17 15:23:32 +01:00
case 'copy-bulk' :
2020-07-30 18:08:02 +02:00
$list = [];
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
2024-04-08 15:44:33 +02:00
$id_segment = ( post ( 'id_segment' ) ? : $fattura -> id_segment );
2024-02-26 15:53: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
2024-01-15 15:30:45 +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
}
2024-01-15 15:30:45 +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
}
2024-01-15 15:30:45 +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
}
2024-01-15 15:30:45 +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
}
2023-08-04 14:54:28 +02:00
2020-01-28 14:06:33 +01:00
$new = $fattura -> replicate ();
2023-08-04 14:54:28 +02:00
2020-08-04 19:07:04 +02:00
$new -> data = $data ;
2020-01-28 14:06:33 +01:00
$new -> id_segment = $id_segment ;
$new -> numero = Fattura :: getNextNumero ( $data , $dir , $id_segment );
2024-03-22 15:30:25 +01:00
$new -> id_autofattura = null ;
2020-08-04 19:07:04 +02:00
2020-01-28 14:06:33 +01:00
$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 ) {
$new_riga = $riga -> replicate ();
2020-09-22 20:28:37 +02:00
$new_riga -> setDocument ( $new );
2020-01-17 15:23:32 +01:00
2020-02-27 16:19:07 +01:00
if ( ! post ( 'riferimenti' )) {
$new_riga -> idpreventivo = 0 ;
$new_riga -> idcontratto = 0 ;
$new_riga -> idintervento = 0 ;
$new_riga -> idddt = 0 ;
$new_riga -> idordine = 0 ;
}
2020-01-28 14:06:33 +01:00
$new_riga -> save ();
2020-01-17 15:23:32 +01:00
2020-02-27 16:19:07 +01:00
if ( $new_riga -> isArticolo ()) {
$new_riga -> movimenta ( $new_riga -> qta );
2020-01-17 15:23:32 +01:00
}
}
2023-02-01 20:05:39 +01:00
2023-08-04 14:54:28 +02:00
if ( ! empty ( $fattura -> numero_esterno )) {
2023-02-01 20:05:39 +01:00
array_push ( $list , $fattura -> numero_esterno );
}
2020-01-17 15:23:32 +01:00
}
2020-07-24 10:02:58 +02:00
flash () -> info ( tr ( 'Fatture _LIST_ duplicate correttamente!' , [
'_LIST_' => implode ( ',' , $list ),
]));
2020-01-17 15:23:32 +01:00
break ;
2020-11-27 15:10:27 +01:00
2020-11-21 03:16:59 +01:00
case 'check-bulk' :
2024-01-15 15:30:45 +01:00
$controllo = new DatiFattureElettroniche ();
$fatture = [];
foreach ( $id_records as $id ) {
$fattura_vendita = Fattura :: vendita ()
-> whereNotIn ( 'codice_stato_fe' , [ 'ERR' , 'NS' , 'EC02' , 'ERVAL' ])
-> where ( 'data' , '>=' , $_SESSION [ 'period_start' ])
-> where ( 'data' , '<=' , $_SESSION [ 'period_end' ])
-> where ( 'id' , '=' , $id )
-> orderBy ( 'data' )
-> first ();
if ( ! empty ( $fattura_vendita )) {
$fatture [ $id ] = $fattura_vendita ;
$controllo -> checkFattura ( $fattura_vendita );
2020-11-21 03:16:59 +01:00
}
2024-01-15 15:30:45 +01:00
}
2020-11-21 03:16:59 +01:00
2024-01-15 15:30:45 +01:00
$results = $controllo -> getResults ();
$num = count ( $results );
2021-02-25 10:23:26 +01:00
2024-01-15 15:30:45 +01:00
// Messaggi di risposta
if ( empty ( $fatture )) {
flash () -> warning ( tr ( 'Nessuna fattura utile per il controllo!' ));
} elseif ( empty ( $results )) {
flash () -> info ( tr ( 'Nessuna anomalia!' ));
} else {
flash () -> info ( tr ( 'Fatture _LIST_ controllate.' , [
'_LIST_' => implode ( ',' , array_column ( $results , 'numero' )),
]));
2020-11-21 03:16:59 +01:00
2024-01-15 15:30:45 +01:00
$riepilogo_anomalie = tr ( 'Attenzione: Trovate _NUM_ anomalie! Le seguenti fatture non trovano corrispondenza tra XML e dati nel documento' , [ '_NUM_' => $num ]) . ':</br></br>' ;
2020-11-27 15:10:27 +01:00
2024-01-15 15:30:45 +01:00
foreach ( $results as $anomalia ) {
$fattura = $fatture [ $anomalia [ 'id' ]];
2020-11-21 03:16:59 +01:00
2024-01-15 15:30:45 +01:00
$riepilogo_anomalie .= ' < ul >
2021-02-25 10:23:26 +01:00
< li > '.reference($fattura, $fattura->getReference()).' </ li >
< li > '.$anomalia[' descrizione '].' </ li >
</ ul >< br > ' ;
2020-11-21 03:16:59 +01:00
}
2024-01-15 15:30:45 +01:00
flash () -> warning ( $riepilogo_anomalie );
}
2020-11-21 03:16:59 +01:00
break ;
2020-11-27 15:10:27 +01:00
case 'export-csv' :
2020-12-04 16:37:07 +01:00
$file = temp_file ();
2020-11-27 15:10:27 +01:00
$exporter = new CSV ( $file );
2020-11-24 17:25:07 +01:00
2020-11-27 15:10:27 +01:00
// Esportazione dei record selezionati
$fatture = Fattura :: whereIn ( 'id' , $id_records ) -> get ();
$exporter -> setRecords ( $fatture );
2020-11-24 17:25:07 +01:00
2020-11-27 15:10:27 +01:00
$count = $exporter -> exportRecords ();
2020-11-24 17:25:07 +01:00
2020-11-27 15:10:27 +01:00
download ( $file , 'fatture.csv' );
2020-11-24 17:25:07 +01:00
break ;
2020-03-03 10:33:32 +01:00
case 'delete-bulk' :
foreach ( $id_records as $id ) {
$documento = Fattura :: find ( $id );
try {
$documento -> delete ();
2024-05-23 11:36:25 +02:00
} catch ( InvalidArgumentException ) {
2020-03-03 10:33:32 +01:00
}
}
flash () -> info ( tr ( 'Fatture eliminate!' ));
break ;
2021-12-10 12:45:44 +01:00
case 'change-bank' :
$list = [];
foreach ( $id_records as $id ) {
$documento = Fattura :: find ( $id );
$documento -> id_banca_azienda = post ( 'id_banca' );
$documento -> save ();
array_push ( $list , $fattura -> numero_esterno );
}
flash () -> info ( tr ( 'Banca aggiornata per le Fatture _LIST_ !' , [
'_LIST_' => implode ( ',' , $list ),
]));
2022-02-16 15:57:11 +01:00
break ;
case 'change-stato' :
$list = [];
2024-06-11 16:58:27 +02:00
$new_stato = Stato :: where ( 'name' , 'Emessa' ) -> first () -> id ;
2022-12-07 16:25:35 +01:00
$fatture = Fattura :: vendita ()
-> whereIn ( 'id' , $id_records )
-> orderBy ( 'data' )
-> get ();
2022-02-16 15:57:11 +01:00
2022-12-07 16:25:35 +01:00
foreach ( $fatture as $fattura ) {
2023-02-14 16:13:11 +01:00
$data = $fattura -> data ;
2022-12-07 16:25:35 +01:00
$fattura = Fattura :: find ( $fattura [ 'id' ]);
2022-02-16 15:57:11 +01:00
$stato_precedente = Stato :: find ( $fattura -> idstatodocumento );
2023-02-14 16:13:11 +01:00
$data_fattura_precedente = $dbo -> fetchOne ( '
SELECT
2024-02-13 15:49:40 +01:00
MAX ( `data` ) AS datamax
2023-02-14 16:13:11 +01:00
FROM
2024-02-13 15:49:40 +01:00
`co_documenti`
INNER JOIN `co_statidocumento` ON `co_statidocumento` . `id` = `co_documenti` . `idstatodocumento`
2024-03-22 15:52:24 +01:00
LEFT JOIN `co_statidocumento_lang` ON ( `co_statidocumento_lang` . `id_record` = `co_documenti` . `idstatodocumento` AND `co_statidocumento_lang` . `id_lang` = '.prepare(Models\Locale::getDefault()->id).' )
2024-02-13 15:49:40 +01:00
INNER JOIN `co_tipidocumento` ON `co_documenti` . `idtipodocumento` = `co_tipidocumento` . `id`
INNER JOIN `zz_segments` ON `zz_segments` . `id` = `co_documenti` . `id_segment`
2023-02-14 16:13:11 +01:00
WHERE
2024-04-18 17:44:05 +02:00
`co_statidocumento_lang` . `title` = " Emessa " AND `co_tipidocumento` . `dir` = " entrata " AND `co_documenti` . `id_segment` = ' . $fattura -> id_segment );
2023-02-14 16:13:11 +01:00
2024-01-15 15:30:45 +01:00
if (( setting ( 'Data emissione fattura automatica' ) == 1 ) && ( $dir == 'entrata' ) && Carbon :: parse ( $data ) -> lessThan ( Carbon :: parse ( $data_fattura_precedente [ 'datamax' ])) && ( ! empty ( $data_fattura_precedente [ 'datamax' ]))) {
2023-02-14 16:13:11 +01:00
$fattura -> data = $data_fattura_precedente [ 'datamax' ];
$fattura -> data_competenza = $data_fattura_precedente [ 'datamax' ];
}
if ( $dir == 'entrata' ) {
$fattura -> data_registrazione = post ( 'data' );
} else {
$fattura -> data_registrazione = post ( 'data_registrazione' );
}
2024-04-18 17:44:05 +02:00
if ( $stato_precedente -> getTranslation ( 'title' ) == 'Bozza' && $fattura -> isFiscale ()) {
2022-02-16 15:57:11 +01:00
$fattura -> stato () -> associate ( $new_stato );
$results = $fattura -> save ();
$message = '' ;
foreach ( $results as $numero => $result ) {
foreach ( $result as $title => $links ) {
foreach ( $links as $link => $errors ) {
if ( empty ( $title )) {
flash () -> warning ( tr ( 'La fattura elettronica num. _NUM_ potrebbe avere delle irregolarità!' , [
'_NUM_' => $numero ,
]) . ' ' . tr ( 'Controllare i seguenti campi: _LIST_' , [
2024-03-22 15:52:24 +01:00
'_LIST_' => implode ( ', ' , $errors ),
]) . '.' );
2022-02-16 15:57:11 +01:00
} else {
$message .= '
< p >< b > '.$title.' '.$link.' </ b ></ p >
< ul > ' ;
foreach ( $errors as $error ) {
if ( ! empty ( $error )) {
$message .= '
< li > '.$error.' </ li > ' ;
}
}
$message .= '
</ ul > ' ;
}
}
}
}
if ( $message ) {
// Messaggi informativi sulle problematiche
$message = tr ( 'La fattura elettronica numero _NUM_ non è stata generata a causa di alcune informazioni mancanti' , [
'_NUM_' => $numero ,
]) . ':' . $message ;
flash () -> warning ( $message );
}
array_push ( $list , $fattura -> numero_esterno );
}
}
if ( ! empty ( $list )) {
flash () -> info ( tr ( 'Le fatture _LIST_ sono state emesse!' , [
'_LIST_' => implode ( ',' , $list ),
]));
} else {
flash () -> warning ( tr ( 'Nessuna fattura emessa!' ));
}
2021-12-10 12:45:44 +01:00
break ;
2022-04-29 17:39:51 +02:00
case 'verify-notifiche' :
foreach ( $id_records as $id ) {
$documento = Fattura :: find ( $id );
2023-08-04 14:54:28 +02:00
if ( $documento -> codice_stato_fe == 'GEN' || $documento -> codice_stato_fe == 'WAIT' ) {
2022-04-29 17:39:51 +02:00
$result = Interaction :: getInvoiceRecepits ( $id );
$last_recepit = $result [ 'results' ][ 0 ];
if ( ! empty ( $last_recepit )) {
// Importazione ultima ricevuta individuata
$fattura = Ricevuta :: process ( $last_recepit );
}
}
}
break ;
2023-06-20 15:36:28 +02:00
case 'cambia-sezionale' :
$count = 0 ;
$n_doc = 0 ;
foreach ( $id_records as $id ) {
$documento = Fattura :: find ( $id );
2023-08-04 14:54:28 +02:00
++ $count ;
2023-06-20 15:36:28 +02:00
2024-04-18 17:44:05 +02:00
if ( $documento -> stato -> getTranslation ( 'title' ) == 'Bozza' ) {
2023-06-20 15:36:28 +02:00
$documento -> id_segment = post ( 'id_segment' );
$documento -> save ();
2023-08-04 14:54:28 +02:00
++ $n_doc ;
2023-06-20 15:36:28 +02:00
}
}
if ( $n_doc > 0 ) {
flash () -> info ( tr ( '_NUM_ fatture spostate' , [
'_NUM_' => $n_doc ,
]));
}
if (( $count - $n_doc ) > 0 ) {
flash () -> warning ( tr ( '_NUM_ fatture non sono state spostate perchè non sono in stato "Bozza".' , [
'_NUM_' => $count - $n_doc ,
]));
}
2023-08-04 14:54:28 +02:00
2023-06-20 15:36:28 +02:00
break ;
2017-09-06 11:59:47 +02:00
}
2023-09-15 17:40:25 +02:00
$operations [ 'change-bank' ] = [
'text' => '<span><i class="fa fa-refresh"></i> ' . tr ( 'Aggiorna banca' ) . '</span>' ,
2020-01-17 15:23:32 +01:00
'data' => [
2023-09-15 17:40:25 +02:00
'title' => tr ( 'Aggiornare la banca?' ),
'msg' => tr ( 'Per ciascuna fattura selezionata, verrà aggiornata la banca' ) . '
< br >< br > {[ " type " : " select " , " label " : " '.tr('Banca').' " , " name " : " id_banca " , " required " : 1 , " values " : " query=SELECT id, CONCAT (nome, \ ' - \ ' , iban) AS descrizione FROM co_banche WHERE id_anagrafica='.prepare( $anagrafica_azienda->idanagrafica ).' " ]} ' ,
2020-01-17 15:23:32 +01:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
],
];
2023-09-15 17:40:25 +02:00
$operations [ 'cambia-sezionale' ] = [
'text' => '<span><i class="fa fa-tags"></i> ' . tr ( 'Cambia sezionale' ),
2021-07-27 14:44:45 +02:00
'data' => [
2024-03-22 15:52:24 +01:00
'title' => tr ( 'Cambia sezionale' ),
2023-09-15 17:40:25 +02:00
'msg' => tr ( 'Scegli il sezionale _TIPOLOGIA_ in cui spostare le fatture in stato "Bozza" selezionate' , [
'_TIPOLOGIA_' => $is_fiscale ? tr ( 'fiscale' ) : tr ( 'non fiscale' ),
]) . ':<br><br>{[ "type": "select", "label": "' . tr ( 'Sezionale' ) . '", "name": "id_segment", "required": 1, "ajax-source": "segmenti", "select-options": ' . json_encode ([ 'id_module' => $id_module , 'is_sezionale' => 1 , 'is_fiscale' => $is_fiscale , 'escludi_id' => $_SESSION [ 'module_' . $id_module ][ 'id_segment' ]]) . ', "select-options-escape": true ]}' ,
2021-07-27 14:44:45 +02:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
2023-09-15 17:40:25 +02:00
'blank' => false ,
2021-07-27 14:44:45 +02:00
],
];
2023-09-15 17:48:26 +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' ,
],
];
}
2024-04-18 17:44:05 +02:00
if ( $module -> getTranslation ( 'title' ) == 'Fatture di vendita' ) {
2023-09-15 17:40:25 +02:00
$operations [ 'check-bulk' ] = [
'text' => '<span><i class="fa fa-list-alt"></i> ' . tr ( 'Controlla fatture elettroniche' ) . '</span>' ,
2019-06-13 15:57:55 +02:00
'data' => [
'title' => '' ,
2023-09-15 17:40:25 +02:00
'msg' => tr ( 'Controllare corrispondenza tra XML e fattura di vendita?<br><small>(le fatture dovranno essere state generate)</small>' ),
2019-06-13 15:57:55 +02:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
'blank' => true ,
],
];
2023-09-15 17:40:25 +02:00
}
2019-06-13 14:46:04 +02:00
2023-09-15 17:40:25 +02:00
$operations [ 'copy-bulk' ] = [
'text' => '<span><i class="fa fa-copy"></i> ' . tr ( 'Duplica selezionati' ) . '</span>' ,
'data' => [
'msg' => tr ( 'Vuoi davvero duplicare le righe selezionate?' ) . '<br><br>{[ "type": "select", "label": "' . tr ( 'Fattura in avanti di' ) . '", "name": "skip_time", "required": 1, "values": "list=\"Giorno\":\"' . tr ( 'Un giorno' ) . '\", \"Settimana\":\"' . tr ( 'Una settimana' ) . '\", \"Mese\":\"' . tr ( 'Un mese' ) . '\", \"Anno\":\"' . tr ( 'Un anno' ) . '\" ", "value": "Giorno" ]}<br>{[ "type": "select", "label": "' . tr ( 'Sezionale' ) . '", "name": "id_segment", "required": 1, "ajax-source": "segmenti", "select-options": ' . json_encode ([ 'id_module' => $id_module , 'is_sezionale' => 1 ]) . ', "value": "' . $_SESSION [ 'module_' . $id_module ][ 'id_segment' ] . '", "select-options-escape": true ]}<br>{[ "type": "checkbox", "label": "' . tr ( 'Aggiungere i riferimenti ai documenti esterni?' ) . '", "placeholder": "' . tr ( 'Aggiungere i riferimenti ai documenti esterni?' ) . '", "name": "riferimenti" ]}' ,
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
],
];
if ( App :: debug ()) {
$operations [ 'delete-bulk' ] = [
2024-05-16 18:02:50 +02:00
'text' => '<span><i class="fa fa-trash"></i> ' . tr ( 'Elimina selezionati' ) . '</span> <span class="badge badge-danger">beta</span>' ,
2023-09-15 17:40:25 +02:00
];
}
if ( $dir == 'entrata' ) {
$operations [ 'change-stato' ] = [
'text' => '<span><i class="fa fa-refresh"></i> ' . tr ( 'Emetti fatture' ) . '</span>' ,
2017-09-06 11:59:47 +02:00
'data' => [
2023-09-15 17:40:25 +02:00
'title' => tr ( 'Emissione fatture' ),
'msg' => tr ( 'Vuoi emettere le fatture selezionate? Verranno emesse solo le fatture in Bozza' ),
2017-09-06 11:59:47 +02:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
],
2019-03-07 17:16:42 +01:00
];
2023-09-15 17:40:25 +02:00
}
2020-11-21 03:16:59 +01:00
2023-09-15 17:40:25 +02:00
$operations [ 'export-csv' ] = [
'text' => '<span><i class="fa fa-download"></i> ' . tr ( 'Esporta selezionati' ) . '</span>' ,
'data' => [
'msg' => tr ( 'Vuoi esportare un CSV con le fatture selezionate?' ),
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-success' ,
'blank' => true ,
],
];
2024-04-18 17:44:05 +02:00
if ( $module -> getTranslation ( 'title' ) == 'Fatture di vendita' ) {
2023-09-15 17:40:25 +02:00
$operations [ 'export-bulk' ] = [
'text' => '<span class="' . (( ! extension_loaded ( 'zip' )) ? 'text-muted disabled' : '' ) . '"><i class="fa fa-file-archive-o"></i> ' . tr ( 'Esporta stampe' ) . '</span>' ,
2020-11-21 03:16:59 +01:00
'data' => [
'title' => '' ,
2023-09-15 17:40:25 +02:00
'msg' => tr ( 'Vuoi davvero esportare i PDF delle fatture selezionate in un archivio ZIP?' ),
2020-11-21 03:16:59 +01:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
'blank' => true ,
],
];
2019-03-07 17:16:42 +01:00
}
2023-09-15 17:40:25 +02:00
$operations [ 'exportFE-bulk' ] = [
'text' => '<span class="' . (( ! extension_loaded ( 'zip' )) ? 'text-muted disabled' : '' ) . '"><i class="fa fa-file-archive-o"></i> ' . tr ( 'Esporta stampe FE' ) . '</span>' ,
2019-03-28 13:00:49 +01:00
'data' => [
2019-06-13 14:46:04 +02:00
'title' => '' ,
2023-09-15 17:40:25 +02:00
'msg' => tr ( 'Vuoi davvero esportare i PDF delle 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 ,
],
];
2022-03-17 17:40:53 +01:00
$operations [ 'export-ricevute-bulk' ] = [
'text' => '<span class="' . (( ! extension_loaded ( 'zip' )) ? 'text-muted disabled' : '' ) . '"><i class="fa fa-file-archive-o"></i> ' . tr ( 'Esporta ricevute' ) . '</span>' ,
'data' => [
'title' => '' ,
'msg' => tr ( 'Vuoi davvero esportare le ricevute selezionate in un archivio ZIP?' ),
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
'blank' => true ,
],
];
2023-09-15 17:40:25 +02:00
$operations [ 'export-xml-bulk' ] = [
'text' => '<span class="' . (( ! extension_loaded ( 'zip' )) ? 'text-muted disabled' : '' ) . '"><i class="fa fa-file-archive-o"></i> ' . tr ( 'Esporta XML' ) . '</span>' ,
2021-12-10 12:45:44 +01:00
'data' => [
2023-09-15 17:40:25 +02:00
'title' => '' ,
'msg' => tr ( 'Vuoi davvero esportare le fatture elettroniche selezionate in un archivio ZIP?' ),
2021-12-10 12:45:44 +01:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
2023-09-15 17:40:25 +02:00
'blank' => true ,
2021-12-10 12:45:44 +01:00
],
];
2024-04-18 17:44:05 +02:00
if ( $module -> getTranslation ( 'title' ) == 'Fatture di vendita' ) {
2023-09-15 17:40:25 +02:00
$operations [ 'genera-xml' ] = [
'text' => '<span><i class="fa fa-file-code-o"></i> ' . tr ( 'Genera fatture elettroniche' ) . '</span>' ,
2022-02-16 15:57:11 +01:00
'data' => [
2023-09-15 17:40:25 +02:00
'title' => '' ,
'msg' => tr ( 'Generare le fatture elettroniche per i documenti selezionati?<br><small>(le fatture dovranno trovarsi nello stato <i class="fa fa-clock-o text-info" title="Emessa"></i> <small>Emessa</small> e non essere mai state generate)</small>' ),
2022-02-16 15:57:11 +01:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
2023-09-15 17:40:25 +02:00
'blank' => true ,
2022-02-16 15:57:11 +01:00
],
];
}
2023-09-15 17:40:25 +02:00
$operations [ 'registrazione-contabile' ] = [
'text' => '<span><i class="fa fa-calculator"></i> ' . tr ( 'Registrazione contabile' ) . '</span>' ,
2023-06-20 15:36:28 +02:00
'data' => [
2023-09-15 17:40:25 +02:00
'title' => tr ( 'Registrazione contabile' ),
'type' => 'modal' ,
'origine' => 'fatture' ,
2024-06-11 16:58:27 +02:00
'url' => base_path () . '/add.php?id_module=' . Module :: where ( 'name' , 'Prima nota' ) -> first () -> id ,
2023-06-20 15:36:28 +02:00
],
];
2019-08-29 16:01:49 +02:00
if ( Interaction :: isEnabled ()) {
2022-04-29 17:39:51 +02:00
$operations [ 'verify-notifiche' ] = [
2024-02-23 18:18:50 +01:00
'text' => '<i class="fa fa-question-circle"></i> ' . tr ( 'Verifica ricevute' ) . '</span>' ,
2022-04-29 17:39:51 +02:00
'data' => [
'title' => '' ,
'msg' => tr ( 'Vuoi verificare ed importare automaticamente le ricevute di queste fatture?' ),
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
'blank' => true ,
],
];
2019-08-29 16:01:49 +02:00
}
2019-05-13 17:01:43 +02:00
return $operations ;