2018-09-24 18:10:16 +02:00
< ? php
include_once __DIR__ . '/../../core.php' ;
2018-11-30 15:33:25 +01:00
use Plugins\ImportFE\FatturaElettronica ;
use Plugins\ImportFE\Interaction ;
2019-07-22 15:45:36 +02:00
$file = null ;
2018-09-24 18:10:16 +02:00
switch ( filter ( 'op' )) {
2019-07-24 17:17:54 +02:00
case 'list' :
$list = Interaction :: getRemoteList ();
echo json_encode ( $list );
break ;
2018-09-24 18:10:16 +02:00
case 'save' :
2019-11-15 15:11:20 +01:00
$temp_name = $_FILES [ 'blob' ][ 'tmp_name' ];
$name = $_FILES [ 'blob' ][ 'name' ];
if ( ends_with ( $name , '.zip' )) {
$directory = FatturaElettronica :: getImportDirectory ();
Util\Zip :: extract ( $temp_name , $directory );
// Redirect forzato per l'importazione
echo json_encode ([
'id' => 1 ,
]);
exit ();
} else {
$content = file_get_contents ( $temp_name );
$file = FatturaElettronica :: store ( $_FILES [ 'blob' ][ 'name' ], $content );
}
2018-09-25 11:55:52 +02:00
2019-07-10 15:02:09 +02:00
// no break
2018-11-30 15:33:25 +01:00
case 'prepare' :
2019-07-22 12:52:48 +02:00
if ( ! isset ( $file )) {
2019-07-22 15:45:36 +02:00
$name = filter ( 'name' );
2019-07-24 17:17:54 +02:00
$file = Interaction :: getInvoiceFile ( $name );
2019-07-22 11:35:44 +02:00
}
2018-09-24 18:10:16 +02:00
2019-07-24 17:39:11 +02:00
try {
if ( ! FatturaElettronica :: isValid ( $file )) {
echo json_encode ([
'already' => 1 ,
]);
return ;
2019-07-22 18:35:13 +02:00
}
2019-07-24 17:39:11 +02:00
} catch ( Exception $e ) {
}
// Individuazione ID fisico
$files = Interaction :: getFileList ();
foreach ( $files as $key => $value ) {
if ( $value [ 'name' ] == $file ) {
$index = $key ;
2019-07-22 18:35:13 +02:00
2019-07-24 17:39:11 +02:00
break ;
}
2018-11-30 15:33:25 +01:00
}
2018-09-24 18:10:16 +02:00
2019-07-24 17:39:11 +02:00
echo json_encode ([
'id' => $index + 1 ,
]);
2018-11-30 15:33:25 +01:00
break ;
2019-02-12 11:42:48 +01:00
case 'delete' :
2019-07-24 17:39:11 +02:00
$file_id = get ( 'file_id' );
2019-07-24 17:54:50 +02:00
$directory = FatturaElettronica :: getImportDirectory ();
2019-07-24 17:39:11 +02:00
$files = Interaction :: getFileList ();
$file = $files [ $file_id ];
2019-07-24 17:54:50 +02:00
if ( ! empty ( $file )) {
delete ( $directory . '/' . $file [ 'name' ]);
}
2019-07-24 17:39:11 +02:00
break ;
2019-07-24 17:54:50 +02:00
2019-07-24 17:39:11 +02:00
case 'download' :
$file_id = get ( 'file_id' );
2019-07-24 17:54:50 +02:00
$directory = FatturaElettronica :: getImportDirectory ();
2019-07-24 17:39:11 +02:00
$files = Interaction :: getFileList ();
$file = $files [ $file_id ];
2019-07-24 17:54:50 +02:00
if ( ! empty ( $file )) {
download ( $directory . '/' . $file [ 'name' ]);
}
2019-02-12 11:42:48 +01:00
break ;
2018-11-30 15:33:25 +01:00
case 'generate' :
2019-01-25 10:44:03 +01:00
$filename = post ( 'filename' );
2019-04-19 03:18:05 +02:00
$info = [
'id_pagamento' => post ( 'pagamento' ),
'id_segment' => post ( 'id_segment' ),
'id_tipo' => post ( 'id_tipo' ),
2019-07-22 11:35:44 +02:00
'ref_fattura' => post ( 'ref_fattura' ),
2019-05-17 05:56:10 +02:00
'data_registrazione' => post ( 'data_registrazione' ),
2019-04-19 03:18:05 +02:00
'articoli' => post ( 'articoli' ),
'iva' => post ( 'iva' ),
'conto' => post ( 'conto' ),
2020-02-24 18:30:05 +01:00
'tipo_riga_riferimento' => post ( 'tipo_riga_riferimento' ),
'id_riga_riferimento' => post ( 'id_riga_riferimento' ),
2019-04-19 03:18:05 +02:00
'movimentazione' => post ( 'movimentazione' ),
2019-10-18 16:40:15 +02:00
'crea_articoli' => post ( 'crea_articoli' ),
2019-04-19 03:18:05 +02:00
];
$fattura_pa = FatturaElettronica :: manage ( $filename );
2019-07-24 17:17:54 +02:00
$id_fattura = $fattura_pa -> save ( $info );
2018-09-24 18:10:16 +02:00
2019-07-24 17:17:54 +02:00
ricalcola_costiagg_fattura ( $id_fattura );
2019-07-25 17:20:24 +02:00
elimina_scadenze ( $id_fattura );
elimina_movimenti ( $id_fattura , 0 );
2019-07-24 17:17:54 +02:00
aggiungi_scadenza ( $id_fattura );
aggiungi_movimento ( $id_fattura , 'uscita' );
2018-10-29 22:23:29 +01:00
2018-11-30 15:33:25 +01:00
$fattura_pa -> delete ();
2019-11-13 11:02:45 +01:00
//Aggiorno la tipologia di anagrafica fornitore
2019-11-15 15:11:20 +01:00
$anagrafica = $dbo -> fetchOne ( 'SELECT idanagrafica FROM co_documenti WHERE co_documenti.id=' . prepare ( $id_fattura ));
2019-11-13 12:29:48 +01:00
$rs_t = $dbo -> fetchOne ( " SELECT * FROM an_tipianagrafiche_anagrafiche WHERE idtipoanagrafica=(SELECT an_tipianagrafiche.idtipoanagrafica FROM an_tipianagrafiche WHERE an_tipianagrafiche.descrizione='Fornitore') AND idanagrafica= " . prepare ( $anagrafica [ 'idanagrafica' ]));
2019-11-13 11:02:45 +01:00
2019-12-05 15:50:13 +01:00
//Se non trovo corrispondenza aggiungo all'anagrafica la tipologia fornitore
if ( empty ( $rs_t )) {
2019-12-13 10:08:17 +01:00
$dbo -> query ( " INSERT INTO an_tipianagrafiche_anagrafiche (idtipoanagrafica, idanagrafica) VALUES ((SELECT an_tipianagrafiche.idtipoanagrafica FROM an_tipianagrafiche WHERE an_tipianagrafiche.descrizione='Fornitore'), " . prepare ( $anagrafica [ 'idanagrafica' ]) . ')' );
2019-11-13 11:02:45 +01:00
}
2019-02-14 10:58:43 +01:00
// Processo il file ricevuto
if ( Interaction :: isEnabled ()) {
2019-07-24 17:17:54 +02:00
$process_result = Interaction :: processInvoice ( $filename );
2019-02-14 10:58:43 +01:00
if ( $process_result != '' ) {
flash () -> error ( $process_result );
redirect ( ROOTDIR . '/controller.php?id_module=' . $id_module );
2019-07-22 11:35:44 +02:00
return ;
2019-02-14 10:58:43 +01:00
}
}
2019-07-24 17:17:54 +02:00
$files = Interaction :: getFileList ();
$file = $files [ $id_record - 1 ];
2018-09-26 12:06:24 +02:00
2019-07-24 17:17:54 +02:00
if ( get ( 'sequence' ) == null ) {
redirect ( ROOTDIR . '/editor.php?id_module=' . $id_module . '&id_record=' . $id_fattura );
} elseif ( ! empty ( $file )) {
redirect ( ROOTDIR . '/editor.php?id_module=' . $id_module . '&id_plugin=' . $id_plugin . '&id_record=' . $id_record . '&sequence=1' );
} else {
flash () -> info ( tr ( 'Tutte le fatture salvate sono state importate!' ));
redirect ( ROOTDIR . '/controller.php?id_module=' . $id_module );
}
2018-09-26 12:06:24 +02:00
break ;
2019-04-19 18:50:16 +02:00
case 'process' :
$name = get ( 'name' );
// Processo il file ricevuto
if ( Interaction :: isEnabled ()) {
2019-07-24 17:17:54 +02:00
$process_result = Interaction :: processInvoice ( $name );
2019-07-10 15:02:09 +02:00
if ( ! empty ( $process_result )) {
2019-04-19 18:50:16 +02:00
flash () -> error ( $process_result );
}
}
break ;
2020-02-24 12:21:47 +01:00
case 'compile' :
2020-03-13 10:27:20 +01:00
if ( empty ( $anagrafica )) {
echo json_encode ([]);
return ;
}
2020-02-24 12:21:47 +01:00
$fatture = $anagrafica -> fattureAcquisto ()
-> contabile ()
-> orderByDesc ( 'created_at' )
-> take ( 10 )
-> get ();
$righe = collect ();
foreach ( $fatture as $fattura ) {
$righe -> push ( $fattura -> righe );
$righe -> push ( $fattura -> articoli );
}
$righe = $righe -> flatten ();
// Ricerca del tipo di documento più utilizzato
$tipi = $fatture -> groupBy ( function ( $item , $key ) {
return $item -> tipo -> id ;
}) -> transform ( function ( $item , $key ) {
return $item -> count ();
});
$id_tipo = $tipi -> sort () -> keys () -> last ();
// Ricerca del tipo di pagamento più utilizzato
$pagamenti = $fatture -> mapToGroups ( function ( $item , $key ) {
return [ $item -> pagamento -> id => $item -> pagamento ];
});
$id_pagamento = $pagamenti -> map ( function ( $item , $key ) {
return $item -> count ();
}) -> sort () -> keys () -> last ();
$pagamento = $pagamenti [ $id_pagamento ] -> first ();
// Ricerca del conto più utilizzato
$conti = $righe -> groupBy ( function ( $item , $key ) {
return $item -> idconto ;
}) -> transform ( function ( $item , $key ) {
return $item -> count ();
});
$id_conto = $conti -> sort () -> keys () -> last ();
$conto = $dbo -> fetchOne ( 'SELECT * FROM co_pianodeiconti3 WHERE id = ' . prepare ( $id_conto ));
// Ricerca dell'IVA più utilizzata secondo percentuali
$iva = [];
$percentuali_iva = $righe -> groupBy ( function ( $item , $key ) {
return $item -> aliquota -> percentuale ;
});
foreach ( $percentuali_iva as $key => $values ) {
$aliquote = $values -> mapToGroups ( function ( $item , $key ) {
return [ $item -> aliquota -> id => $item -> aliquota ];
});
$id_aliquota = $aliquote -> map ( function ( $item , $key ) {
return $item -> count ();
}) -> sort () -> keys () -> last ();
$aliquota = $aliquote [ $id_aliquota ] -> first ();
$iva [ $key ] = [
'id' => $aliquota -> id ,
'descrizione' => $aliquota -> descrizione ,
];
}
echo json_encode ([
'id_tipo' => $id_tipo ,
'pagamento' => [
'id' => $pagamento -> id ,
'descrizione' => $pagamento -> descrizione ,
],
'conto' => [
'id' => $conto [ 'id' ],
'descrizione' => $conto [ 'descrizione' ],
],
'iva' => $iva ,
]);
break ;
2018-09-24 18:10:16 +02:00
}