2018-03-09 11:56:58 +01:00
< ? php
include_once __DIR__ . '/../../core.php' ;
2018-06-26 09:41:43 +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 ;
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' :
$id_documento_cliente = [];
2018-03-15 17:41:09 +01:00
$totale_n_ddt = 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-08-01 15:32:23 +02:00
$idiva = setting ( 'Iva predefinita' );
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 ) {
$id_anagrafica = $dbo -> selectOne ( 'dt_ddt' , 'idanagrafica' , [ 'id' => $id ])[ 'idanagrafica' ];
2018-03-15 17:41:09 +01:00
2018-07-16 13:27:44 +02:00
$righe = $dbo -> fetchArray ( 'SELECT * FROM dt_righe_ddt WHERE idddt=' . prepare ( $id ) . ' AND idddt NOT IN (SELECT idddt FROM co_righe_documenti WHERE idddt IS NOT NULL)' );
2018-03-22 15:40:20 +01:00
2018-07-16 13:27:44 +02:00
// Proseguo solo se i ddt scelti sono fatturabili
if ( ! empty ( $righe )) {
$id_documento = $id_documento_cliente [ $id_anagrafica ];
2018-09-27 17:27:39 +02:00
++ $totale_n_ddt ;
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
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-03-15 17:41:09 +01:00
2018-09-27 17:27:39 +02:00
$id_documento = $fattura -> id ;
2018-07-16 13:27:44 +02:00
$id_documento_cliente [ $id_anagrafica ] = $id_documento ;
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 ) {
2018-07-16 13:27:44 +02:00
$dbo -> insert ( 'co_righe_documenti' , [
'iddocumento' => $id_documento ,
'idarticolo' => $riga [ 'idarticolo' ],
'idddt' => $id ,
'idiva' => $riga [ 'idiva' ],
'desc_iva' => $riga [ 'desc_iva' ],
'iva' => $riga [ 'iva' ],
'iva_indetraibile' => $riga [ 'iva_indetraibile' ],
'descrizione' => $riga [ 'descrizione' ],
'is_descrizione' => $riga [ 'is_descrizione' ],
'subtotale' => $riga [ 'subtotale' ],
'sconto' => $riga [ 'sconto' ],
'sconto_unitario' => $riga [ 'sconto_unitario' ],
'tipo_sconto' => $riga [ 'tipo_sconto' ],
'um' => $riga [ 'um' ],
'qta' => $qta ,
'abilita_serial' => $riga [ 'abilita_serial' ],
2018-09-24 10:51:48 +02:00
'order' => orderValue ( 'co_righe_documenti' , 'iddocumento' , $id_documento ),
2018-07-16 13:27:44 +02:00
]);
2018-07-16 13:37:43 +02:00
$id_riga_documento = $dbo -> lastInsertedID ();
// Copia dei serial tra le righe
if ( ! empty ( $riga [ 'idarticolo' ])) {
$dbo -> query ( 'INSERT INTO mg_prodotti (id_riga_documento, id_articolo, dir, serial, lotto, altro) SELECT ' . prepare ( $id_riga_documento ) . ', ' . prepare ( $riga [ 'idarticolo' ]) . ', ' . prepare ( $dir ) . ', serial, lotto, altro FROM mg_prodotti AS t WHERE id_riga_ddt=' . prepare ( $riga [ 'id' ]));
}
2018-03-09 11:56:58 +01:00
2018-03-15 17:41:09 +01:00
// Aggiorno la quantità evasa
2018-07-16 13:27:44 +02:00
$dbo -> query ( 'UPDATE dt_righe_ddt SET qta_evasa = qta WHERE id=' . prepare ( $riga [ 'id' ]));
2018-03-15 17:41:09 +01:00
// Aggiorno lo stato ddt
2018-07-16 13:27:44 +02:00
$dbo -> query ( 'UPDATE dt_ddt SET idstatoddt = (SELECT id FROM dt_statiddt WHERE descrizione="Fatturato") WHERE id=' . prepare ( $id ));
2018-03-15 17:41:09 +01:00
}
// Ricalcolo inps, ritenuta e bollo
2018-07-16 13:27:44 +02:00
ricalcola_costiagg_fattura ( $id_documento );
2018-03-15 17:41:09 +01:00
}
}
2018-03-09 11:56:58 +01:00
}
2018-03-15 17:41:09 +01:00
2018-03-09 11:56:58 +01:00
if ( $totale_n_ddt > 0 ) {
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( '_NUM_ ddt fatturati!' , [
2018-03-15 17:41:09 +01:00
'_NUM_' => $totale_n_ddt ,
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
2018-08-10 17:14:09 +02:00
if ( App :: debug ()) {
2018-06-26 09:41:43 +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-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Ddt eliminati!' ));
2018-06-26 09:41:43 +02:00
} else {
2018-07-19 17:29:21 +02:00
flash () -> warning ( tr ( 'Procedura in fase di sviluppo. Nessuna modifica apportata.' ));
2018-06-26 09:41:43 +02:00
}
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
}
2018-07-16 13:27:44 +02:00
$operations = [
2018-03-19 18:17:34 +01:00
'delete-bulk' => tr ( 'Elimina selezionati' ),
2018-08-08 19:00:19 +02:00
'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 ,
],
2018-09-03 16:49:43 +02:00
],
2018-08-08 09:32:20 +02:00
];
2018-07-16 13:27:44 +02:00
return $operations ;