2018-02-09 15:43:51 +01: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 />.
*/
2018-02-09 15:43:51 +01:00
2019-11-08 15:09:05 +01:00
include_once __DIR__ . '/../../core.php' ;
2018-06-18 15:56:00 +02:00
2018-12-29 12:03:22 +01:00
use Modules\Anagrafiche\Anagrafica ;
2018-09-27 17:27:39 +02:00
use Modules\Fatture\Fattura ;
use Modules\Fatture\Tipo ;
2020-03-09 16:36:35 +01:00
use Modules\Interventi\Intervento ;
use Modules\Interventi\Stato ;
2018-12-29 12:03:22 +01:00
use Util\Zip ;
2018-09-19 16:51:37 +02:00
2018-09-27 17:27:39 +02:00
// Segmenti
$id_fatture = Modules :: get ( 'Fatture di vendita' )[ 'id' ];
if ( ! isset ( $_SESSION [ 'module_' . $id_fatture ][ 'id_segment' ])) {
$segments = Modules :: getSegments ( $id_fatture );
$_SESSION [ 'module_' . $id_fatture ][ 'id_segment' ] = isset ( $segments [ 0 ][ 'id' ]) ? $segments [ 0 ][ 'id' ] : null ;
}
$id_segment = $_SESSION [ 'module_' . $id_fatture ][ 'id_segment' ];
2018-02-09 15:43:51 +01:00
switch ( post ( 'op' )) {
case 'export-bulk' :
2020-09-23 13:36:37 +02:00
$dir = base_dir () . '/files/export_interventi/' ;
2018-02-09 15:43:51 +01:00
directory ( $dir . 'tmp/' );
// Rimozione dei contenuti precedenti
$files = glob ( $dir . '/*.zip' );
foreach ( $files as $file ) {
delete ( $file );
}
2018-07-18 15:20:10 +02:00
// Selezione degli interventi da stampare
$interventi = $dbo -> fetchArray ( 'SELECT in_interventi.id, in_interventi.codice, data_richiesta, ragione_sociale FROM in_interventi INNER JOIN an_anagrafiche ON in_interventi.idanagrafica=an_anagrafiche.idanagrafica WHERE in_interventi.id IN(' . implode ( ',' , $id_records ) . ')' );
2018-02-09 15:43:51 +01:00
2018-07-18 15:20:10 +02:00
if ( ! empty ( $interventi )) {
foreach ( $interventi as $r ) {
2018-09-26 10:49:38 +02:00
$print = Prints :: getModulePredefinedPrint ( $id_module );
2018-02-18 19:53:23 +01:00
2019-07-10 11:53:35 +02:00
Prints :: render ( $print [ 'id' ], $r [ 'id' ], $dir . 'tmp/' );
2018-02-09 15:43:51 +01:00
}
$dir = slashes ( $dir );
$file = slashes ( $dir . 'interventi_' . time () . '.zip' );
// Creazione zip
if ( extension_loaded ( 'zip' )) {
2018-09-19 16:51:37 +02:00
Zip :: create ( $dir . 'tmp/' , $file );
2018-02-09 15:43:51 +01:00
// Invio al browser dello zip
download ( $file );
// Rimozione dei contenuti
delete ( $dir . 'tmp/' );
}
}
2018-03-22 15:40:20 +01:00
break ;
2018-08-31 12:33:48 +02:00
case 'crea_fattura' :
$id_documento_cliente = [];
$n_interventi = 0 ;
2018-03-22 15:40:20 +01:00
2018-03-08 17:29:32 +01:00
$data = date ( 'Y-m-d' );
$dir = 'entrata' ;
2018-09-27 17:27:39 +02:00
$tipo_documento = Tipo :: where ( 'descrizione' , 'Fattura immediata di vendita' ) -> first ();
2018-03-08 17:29:32 +01:00
2018-09-03 16:01:05 +02:00
$id_iva = setting ( 'Iva predefinita' );
$id_conto = setting ( 'Conto predefinito fatture di vendita' );
2018-03-08 17:29:32 +01:00
2018-08-31 12:33:48 +02:00
$accodare = post ( 'accodare' );
2018-09-27 17:27:39 +02:00
$id_segment = post ( 'id_segment' );
2018-03-08 17:29:32 +01:00
2021-03-30 15:18:15 +02:00
$interventi = $dbo -> fetchArray ( 'SELECT *, IFNULL((SELECT MIN(orario_inizio) FROM in_interventi_tecnici WHERE in_interventi_tecnici.idintervento = in_interventi.id), in_interventi.data_richiesta) AS data, in_statiintervento.descrizione AS stato, in_interventi.codice AS codice_intervento FROM in_interventi INNER JOIN in_statiintervento ON in_interventi.idstatointervento=in_statiintervento.idstatointervento WHERE in_statiintervento.is_completato=1 AND in_interventi.id NOT IN (SELECT idintervento FROM co_righe_documenti WHERE idintervento IS NOT NULL) AND in_interventi.id_preventivo IS NULL AND in_interventi.id_contratto IS NULL AND in_interventi.id_ordine IS NULL AND in_interventi.id NOT IN (SELECT idintervento FROM co_promemoria WHERE idintervento IS NOT NULL) AND in_interventi.id IN (' . implode ( ',' , $id_records ) . ')' );
2018-09-24 10:51:48 +02:00
2018-08-31 12:33:48 +02:00
// Lettura righe selezionate
foreach ( $interventi as $intervento ) {
$id_anagrafica = $intervento [ 'idanagrafica' ];
2018-03-22 15:40:20 +01:00
2018-08-31 12:33:48 +02:00
$id_documento = $id_documento_cliente [ $id_anagrafica ];
2018-03-22 15:40:20 +01:00
2018-08-31 12:33:48 +02:00
// Se non c'è già una fattura appena creata per questo cliente, creo una fattura nuova
if ( empty ( $id_documento )) {
if ( ! empty ( $accodare )) {
$documento = $dbo -> fetchOne ( 'SELECT co_documenti.id FROM co_documenti INNER JOIN co_statidocumento ON co_documenti.idstatodocumento = co_statidocumento.id WHERE co_statidocumento.descrizione = \'Bozza\' AND idanagrafica = ' . prepare ( $id_anagrafica ));
2018-03-22 15:40:20 +01:00
2018-08-31 12:33:48 +02:00
$id_documento = $documento [ 'id' ];
$id_documento_cliente [ $id_anagrafica ] = $id_documento ;
2018-03-22 15:40:20 +01:00
}
2018-08-31 12:33:48 +02:00
if ( empty ( $id_documento )) {
2018-09-27 17:27:39 +02:00
$anagrafica = Anagrafica :: find ( $id_anagrafica );
2019-01-02 14:15:16 +01:00
$fattura = Fattura :: build ( $anagrafica , $tipo_documento , $data , $id_segment );
2018-09-27 17:27:39 +02:00
$id_documento = $fattura -> id ;
2018-08-31 12:33:48 +02:00
$id_documento_cliente [ $id_anagrafica ] = $id_documento ;
2018-03-22 15:40:20 +01:00
}
2018-08-31 12:33:48 +02:00
}
2018-03-22 15:40:20 +01:00
2021-07-28 11:50:02 +02:00
$descrizione = tr ( 'Attività numero _NUM_ del _DATE_ [_STATE_]' , [
2020-01-24 11:52:36 +01:00
'_NUM_' => $intervento [ 'codice_intervento' ],
2018-08-31 12:33:48 +02:00
'_DATE_' => Translator :: dateToLocale ( $intervento [ 'data' ]),
'_STATE_' => $intervento [ 'stato' ],
]);
2018-03-22 15:40:20 +01:00
2018-08-31 12:33:48 +02:00
aggiungi_intervento_in_fattura ( $intervento [ 'id' ], $id_documento , $descrizione , $id_iva , $id_conto );
2021-06-11 12:05:19 +02:00
$fatturati [] = $intervento [ 'id' ];
2018-09-03 16:49:43 +02:00
++ $n_interventi ;
2018-03-22 15:40:20 +01:00
}
if ( $n_interventi > 0 ) {
2018-09-03 16:01:05 +02:00
flash () -> info ( tr ( '_NUM_ interventi fatturati.' , [
2018-03-22 15:40:20 +01:00
'_NUM_' => $n_interventi ,
2018-07-07 13:56:22 +02:00
]));
2021-03-30 15:18:15 +02:00
}
2021-06-11 12:05:19 +02:00
if ( ! empty ( array_diff ( $id_records , $fatturati ))) {
2021-03-30 15:18:15 +02:00
flash () -> warning ( tr ( '_NUM_ interventi non sono stati fatturati.' , [
2021-06-11 12:05:19 +02:00
'_NUM_' => sizeof ( array_diff ( $id_records , $fatturati )),
2021-03-30 15:18:15 +02:00
]));
2018-03-08 17:29:32 +01:00
}
2018-03-22 15:40:20 +01:00
break ;
2020-03-09 16:36:35 +01:00
case 'cambia_stato' :
$id_stato = post ( 'id_stato' );
$n_interventi = 0 ;
$stato = Stato :: find ( $id_stato );
// Lettura righe selezionate
foreach ( $id_records as $id ) {
$intervento = Intervento :: find ( $id );
2021-02-18 18:48:44 +01:00
if ( ! $intervento -> stato -> is_completato || ( $intervento -> stato -> is_completato == $stato -> is_completato )) {
2020-03-09 16:36:35 +01:00
$intervento -> stato () -> associate ( $stato );
$intervento -> save ();
++ $n_interventi ;
}
}
if ( $n_interventi > 0 ) {
flash () -> info ( tr ( 'Stato cambiato a _NUM_ attività!' , [
'_NUM_' => $n_interventi ,
]));
} else {
flash () -> warning ( tr ( 'Nessuna attività modificata!' ));
}
2020-06-09 13:08:36 +02:00
break ;
2020-06-09 16:59:26 +02:00
2020-08-07 17:37:39 +02:00
case 'copy-bulk' :
2020-09-22 13:59:50 +02:00
$id_stato = post ( 'idstatointervento' );
2020-06-09 13:08:36 +02:00
$data_richiesta = post ( 'data_richiesta' );
$copia_sessioni = post ( 'sessioni' );
$copia_righe = post ( 'righe' );
foreach ( $id_records as $idintervento ) {
$intervento = Intervento :: find ( $idintervento );
$new = $intervento -> replicate ();
2020-09-22 13:59:50 +02:00
$new -> idstatointervento = $id_stato ;
2020-06-09 13:08:36 +02:00
2020-09-22 13:59:50 +02:00
// Calcolo del nuovo codice sulla base della data di richiesta
2020-06-09 13:08:36 +02:00
$new -> codice = Intervento :: getNextCodice ( $data_richiesta );
$new -> save ();
2020-06-09 16:59:26 +02:00
2020-06-09 13:08:36 +02:00
$id_record = $new -> id ;
2020-06-09 16:59:26 +02:00
2020-09-22 13:59:50 +02:00
// Copio le righe
if ( ! empty ( $copia_righe )) {
$righe = $intervento -> getRighe ();
foreach ( $righe as $riga ) {
$new_riga = $riga -> replicate ();
2020-09-22 20:28:37 +02:00
$new_riga -> setDocument ( $new );
2020-06-09 16:59:26 +02:00
2020-09-22 13:59:50 +02:00
$new_riga -> qta_evasa = 0 ;
$new_riga -> save ();
2020-06-09 13:08:36 +02:00
}
}
2020-06-09 16:59:26 +02:00
2020-09-22 13:59:50 +02:00
// Copia delle sessioni
$numero_sessione = 0 ;
if ( ! empty ( $copia_sessioni )) {
2020-06-09 13:08:36 +02:00
$sessioni = $intervento -> sessioni ;
foreach ( $sessioni as $sessione ) {
2020-09-22 13:59:50 +02:00
// Se è la prima sessione che copio importo la data con quella della richiesta
if ( $numero_sessione == 0 ) {
2020-06-09 16:59:26 +02:00
$orario_inizio = date ( 'Y-m-d' , strtotime ( $data_richiesta )) . ' ' . date ( 'H:i:s' , strtotime ( $sessione -> orario_inizio ));
} else {
2020-06-09 13:08:36 +02:00
$diff = strtotime ( $sessione -> orario_inizio ) - strtotime ( $inizio_old );
2020-09-22 13:59:50 +02:00
$orario_inizio = date ( 'Y-m-d H:i:s' , ( strtotime ( $sessione -> orario_inizio ) + $diff ));
2020-06-09 13:08:36 +02:00
}
2020-06-09 16:59:26 +02:00
2020-06-09 13:08:36 +02:00
$diff_fine = strtotime ( $sessione -> orario_fine ) - strtotime ( $sessione -> orario_inizio );
2020-06-09 16:59:26 +02:00
$orario_fine = date ( 'Y-m-d H:i:s' , ( strtotime ( $orario_inizio ) + $diff_fine ));
2020-06-09 13:08:36 +02:00
$new_sessione = $sessione -> replicate ();
$new_sessione -> idintervento = $new -> id ;
2020-06-09 16:59:26 +02:00
2020-06-09 13:08:36 +02:00
$new_sessione -> orario_inizio = $orario_inizio ;
$new_sessione -> orario_fine = $orario_fine ;
$new_sessione -> save ();
2020-06-09 16:59:26 +02:00
2020-09-22 13:59:50 +02:00
++ $numero_sessione ;
2020-06-09 13:08:36 +02:00
$inizio_old = $sessione -> orario_inizio ;
}
}
}
flash () -> info ( tr ( 'Attività duplicate correttamente!' ));
2020-03-09 16:36:35 +01:00
break ;
2021-04-13 14:43:54 +02:00
case 'delete-bulk' :
foreach ( $id_records as $id ) {
$intervento = Intervento :: find ( $id );
try {
$intervento -> delete ();
} catch ( InvalidArgumentException $e ) {
}
}
2021-04-26 09:16:16 +02:00
2021-04-13 14:43:54 +02:00
flash () -> info ( tr ( 'Interventi eliminati!' ));
break ;
2018-02-09 15:43:51 +01:00
}
2021-04-13 14:43:54 +02:00
if ( App :: debug ()) {
$operations [ 'delete-bulk' ] = [
'text' => '<span><i class="fa fa-trash"></i> ' . tr ( 'Elimina selezionati' ) . '</span> <span class="label label-danger" >beta</span>' ,
];
}
$operations [ 'export-bulk' ] = [
2020-07-09 18:50:10 +02:00
'text' => '<span><i class="fa fa-file-archive-o"></i> ' . tr ( 'Esporta stampe' ),
2018-02-09 15:43:51 +01:00
'data' => [
2020-11-12 10:13:23 +01:00
'title' => tr ( 'Vuoi davvero esportare queste stampe in un archivio ZIP?' ),
2018-07-23 13:47:09 +02:00
'msg' => '' ,
2020-11-12 10:13:23 +01:00
'button' => tr ( 'Procedi' ),
2018-02-09 15:43:51 +01:00
'class' => 'btn btn-lg btn-warning' ,
'blank' => true ,
],
2021-04-13 14:43:54 +02:00
];
2018-03-22 15:40:20 +01:00
2021-04-13 14:43:54 +02:00
$operations [ 'crea_fattura' ] = [
2020-11-12 17:11:09 +01:00
'text' => '<span><i class="fa fa-file-code-o"></i> ' . tr ( 'Fattura _TYPE_' , [ '_TYPE_' => strtolower ( $module [ 'name' ])]),
2018-03-08 17:29:32 +01:00
'data' => [
2020-11-12 17:11:09 +01:00
'title' => tr ( 'Fatturare gli _TYPE_ selezionati?' , [ '_TYPE_' => strtolower ( $module [ 'name' ])]) . ' <small><i class="fa fa-question-circle-o tip" title="' . tr ( 'Verranno fatturati solo gli interventi completati non collegati a contratti o preventivi' ) . '."></i></small>' ,
'msg' => '{[ "type": "checkbox", "label": "<small>' . tr ( 'Aggiungere alle fatture di vendita non ancora emesse?' ) . '</small>", "placeholder": "' . tr ( 'Aggiungere alle fatture di vendita nello stato bozza?' ) . ' " , " name " : " accodare " ]}<br>
2020-11-12 10:13:23 +01:00
{[ " type " : " select " , " label " : " '.tr('Sezionale').' " , " name " : " id_segment " , " required " : 1 , " values " : " query=SELECT id, name AS descrizione FROM zz_segments WHERE id_module= \ ''. $id_fatture .' \ ' AND is_fiscale = 1 ORDER BY name " , " value " : " '. $id_segment .' " ]} ' ,
'button' => tr ( 'Procedi' ),
2018-03-08 17:29:32 +01:00
'class' => 'btn btn-lg btn-warning' ,
'blank' => false ,
],
2021-04-13 14:43:54 +02:00
];
2020-03-09 16:36:35 +01:00
2021-04-13 14:43:54 +02:00
$operations [ 'cambia_stato' ] = [
2020-07-09 18:50:10 +02:00
'text' => '<span><i class="fa fa-refresh"></i> ' . tr ( 'Cambia stato' ),
2020-03-09 16:36:35 +01:00
'data' => [
2021-01-19 12:20:06 +01:00
'title' => tr ( 'Vuoi davvero cambiare lo stato per questi interventi?' ),
2020-03-09 16:36:35 +01:00
'msg' => tr ( 'Seleziona lo stato in cui spostare tutti gli interventi non completati' ) . ' .< br >
< br > {[ " type " : " select " , " label " : " '.tr('Stato').' " , " name " : " id_stato " , " required " : 1 , " values " : " query=SELECT idstatointervento AS id, descrizione, colore AS _bgcolor_ FROM in_statiintervento WHERE deleted_at IS NULL " ]} ' ,
2020-11-12 10:13:23 +01:00
'button' => tr ( 'Procedi' ),
2020-03-09 16:36:35 +01:00
'class' => 'btn btn-lg btn-warning' ,
'blank' => false ,
],
2021-04-13 14:43:54 +02:00
];
2020-06-09 16:59:26 +02:00
2021-04-13 14:43:54 +02:00
$operations [ 'copy-bulk' ] = [
2020-07-09 18:50:10 +02:00
'text' => '<span><i class="fa fa-clone"></i> ' . tr ( 'Duplica attività' ),
2020-06-09 13:08:36 +02:00
'data' => [
'title' => tr ( 'Vuoi davvero fare una copia degli interventi selezionati?' ),
'msg' => '<br>{[ "type": "timestamp", "label": "' . tr ( 'Data/ora richiesta' ) . ' " , " name " : " data_richiesta " , " required " : 0, " value " : " - now - " , " required " :1 ]}
< br > {[ " type " : " select " , " label " : " '.tr('Stato').' " , " name " : " idstatointervento " , " required " : 1 , " values " : " query=SELECT idstatointervento AS id, descrizione, colore AS _bgcolor_ FROM in_statiintervento WHERE deleted_at IS NULL " , " value " : " " ]}
< br > {[ " type " : " checkbox " , " label " : " '.tr('Duplica righe').' " , " name " : " righe " , " value " : " " ]}
< br > {[ " type " : " checkbox " , " label " : " '.tr('Duplica sessioni').' " , " name " : " sessioni " , " value " : " " ]} ' ,
2020-11-12 10:13:23 +01:00
'button' => tr ( 'Procedi' ),
2020-06-09 13:08:36 +02:00
'class' => 'btn btn-lg btn-warning' ,
'blank' => false ,
],
2021-04-13 14:43:54 +02:00
];
return $operations ;