2018-03-09 11:56:58 +01:00
< ? php
include_once __DIR__ . '/../../core.php' ;
2018-06-26 09:41:43 +02:00
2020-02-14 12:37:55 +01:00
use Modules\DDT\DDT ;
2018-09-27 17:27:39 +02:00
use Modules\Fatture\Fattura ;
use Modules\Fatture\Tipo ;
if ( $module [ 'name' ] == 'Ddt di vendita' ) {
$dir = 'entrata' ;
$module_name = 'Fatture di vendita' ;
} else {
$dir = 'uscita' ;
$module_name = 'Fatture di acquisto' ;
}
// Segmenti
$id_fatture = Modules :: get ( $module_name )[ '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-03-15 17:41:09 +01:00
switch ( post ( 'op' )) {
2018-07-16 13:27:44 +02:00
case 'crea_fattura' :
2020-02-14 12:37:55 +01:00
$documenti = collect ();
2018-07-16 13:27:44 +02:00
$id_documento_cliente = [];
2020-02-14 12:37:55 +01:00
$numero_totale = 0 ;
2018-07-16 13:27:44 +02:00
// Informazioni della fattura
2018-09-03 16:01:05 +02:00
if ( $dir == 'entrata' ) {
2018-09-27 17:27:39 +02:00
$tipo_documento = 'Fattura immediata di vendita' ;
2018-08-08 09:32:20 +02:00
} else {
2018-09-27 17:27:39 +02:00
$tipo_documento = 'Fattura immediata di acquisto' ;
2018-08-08 09:32:20 +02:00
}
2018-08-29 18:15:12 +02:00
2018-09-27 17:27:39 +02:00
$tipo_documento = Tipo :: where ( 'descrizione' , $tipo_documento ) -> first ();
2018-07-16 13:27:44 +02:00
$data = date ( 'Y-m-d' );
2018-09-27 17:27:39 +02:00
$id_segment = post ( 'id_segment' );
2018-09-24 10:51:48 +02:00
2018-03-15 17:41:09 +01:00
// Lettura righe selezionate
2018-07-16 13:27:44 +02:00
foreach ( $id_records as $id ) {
2020-02-14 12:37:55 +01:00
$documento_import = DDT :: find ( $id );
$anagrafica = $documento_import -> anagrafica ;
$id_anagrafica = $anagrafica -> id ;
2018-03-15 17:41:09 +01:00
2020-02-14 12:37:55 +01:00
// Proseguo solo se i documenti scelti sono fatturabili
$righe = $documento_import -> getRighe ();
2018-07-16 13:27:44 +02:00
if ( ! empty ( $righe )) {
2020-02-14 12:37:55 +01:00
++ $numero_totale ;
2018-03-15 17:41:09 +01:00
2018-07-16 13:27:44 +02:00
// Se non c'è già una fattura appena creata per questo cliente, creo una fattura nuova
2020-02-14 12:37:55 +01:00
$fattura = $documenti -> first ( function ( $item , $key ) use ( $id_anagrafica ) {
return $item -> anagrafica -> id == $id_anagrafica ;
});
if ( empty ( $fattura )) {
2019-01-02 14:15:16 +01:00
$fattura = Fattura :: build ( $anagrafica , $tipo_documento , $data , $id_segment );
2020-02-14 12:37:55 +01:00
$documenti -> push ( $fattura );
2018-03-15 17:41:09 +01:00
}
// Inserimento righe
2018-07-16 13:27:44 +02:00
foreach ( $righe as $riga ) {
$qta = $riga [ 'qta' ] - $riga [ 'qta_evasa' ];
2018-03-15 17:41:09 +01:00
if ( $qta > 0 ) {
2020-02-14 12:37:55 +01:00
$copia = $riga -> copiaIn ( $fattura , $qta );
2018-03-09 11:56:58 +01:00
2020-02-14 12:37:55 +01:00
// Aggiornamento seriali dalla riga dell'ordine
if ( $copia -> isArticolo ()) {
$copia -> serials = $riga -> serials ;
}
2018-03-15 17:41:09 +01:00
}
}
}
2018-03-09 11:56:58 +01:00
}
2018-03-15 17:41:09 +01:00
2020-02-14 12:37:55 +01:00
if ( $numero_totale > 0 ) {
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( '_NUM_ ddt fatturati!' , [
2020-02-14 12:37:55 +01:00
'_NUM_' => $numero_totale ,
2018-07-07 13:56:22 +02:00
]));
2018-03-15 17:41:09 +01:00
} else {
2018-07-19 17:29:21 +02:00
flash () -> warning ( tr ( 'Nessun ddt fatturato!' ));
2018-03-15 17:41:09 +01:00
}
break ;
2018-03-19 18:17:34 +01:00
2018-03-22 15:40:20 +01:00
case 'delete-bulk' :
2018-06-26 09:41:43 +02:00
2019-05-16 04:52:16 +02:00
foreach ( $id_records as $id ) {
$dbo -> query ( 'DELETE FROM dt_ddt WHERE id = ' . prepare ( $id ) . Modules :: getAdditionalsQuery ( $id_module ));
$dbo -> query ( 'DELETE FROM dt_righe_ddt WHERE idddt=' . prepare ( $id ) . Modules :: getAdditionalsQuery ( $id_module ));
$dbo -> query ( 'DELETE FROM mg_movimenti WHERE idddt=' . prepare ( $id ) . Modules :: getAdditionalsQuery ( $id_module ));
}
2018-06-26 09:41:43 +02:00
2019-05-16 04:52:16 +02:00
flash () -> info ( tr ( 'Ddt eliminati!' ));
2018-03-19 18:17:34 +01:00
2018-03-22 15:40:20 +01:00
break ;
2018-03-09 11:56:58 +01:00
}
2019-05-13 17:01:43 +02:00
if ( App :: debug ()) {
2019-05-16 04:52:16 +02:00
$operations = [
'delete-bulk' => tr ( 'Elimina selezionati' ),
];
2019-05-13 17:01:43 +02:00
}
2019-05-16 04:52:16 +02:00
2019-05-13 17:01:43 +02:00
$operations [ 'crea_fattura' ] = [
2018-03-09 11:56:58 +01:00
'text' => tr ( 'Crea fattura' ),
'data' => [
2018-09-27 17:27:39 +02:00
'title' => tr ( 'Vuoi davvero creare una fattura per questi interventi?' ),
'msg' => '<br>{[ "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 . '" ]}' ,
2018-03-09 11:56:58 +01:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
'blank' => false ,
],
2019-05-13 17:01:43 +02:00
];
2018-07-16 13:27:44 +02:00
return $operations ;