2017-08-04 16:28:16 +02:00
< ? php
include_once __DIR__ . '/core.php' ;
$op = empty ( $op ) ? filter ( 'op' ) : $op ;
$search = filter ( 'q' );
if ( ! isset ( $elements )) {
$elements = [];
}
$elements = ( ! is_array ( $elements )) ? explode ( ',' , $elements ) : $elements ;
$superselect = ! empty ( $_SESSION [ 'superselect' ]) ? $_SESSION [ 'superselect' ] : [];
$where = [];
$filter = [];
$search_fields = [];
$custom = [
'id' => 'id' ,
'text' => 'descrizione' ,
];
if ( ! function_exists ( 'completeResults' )) {
function completeResults ( $query , $where , $filter = [], $search = [], $custom = [])
{
$dbo = Database :: getConnection ();
2017-08-28 15:29:03 +02:00
if ( str_contains ( $query , '|filter|' )) {
2017-08-04 16:28:16 +02:00
$query = str_replace ( '|filter|' , ! empty ( $filter ) ? 'WHERE ' . implode ( ' OR ' , $filter ) : '' , $query );
} elseif ( ! empty ( $filter )) {
$where [] = '(' . implode ( ' OR ' , $filter ) . ')' ;
}
if ( ! empty ( $search )) {
$where [] = '(' . implode ( ' OR ' , $search ) . ')' ;
}
$query = str_replace ( '|where|' , ! empty ( $where ) ? 'WHERE ' . implode ( ' AND ' , $where ) : '' , $query );
$rs = $dbo -> fetchArray ( $query );
$results = [];
foreach ( $rs as $r ) {
$result = [];
foreach ( $custom as $key => $value ) {
$result [ $key ] = $r [ $value ];
}
$results [] = $result ;
}
return $results ;
}
}
switch ( $op ) {
case 'clienti' :
2018-02-15 14:43:34 +01:00
if (( Modules :: get ( 'Anagrafiche' )[ 'permessi' ] != '-' ) or ( Modules :: getAdditionalsQuery ( 'Anagrafiche' ))) {
2017-12-14 16:17:34 +01:00
//$citta_cliente = ", IF(citta IS NULL OR citta = '', '', CONCAT(' (', citta, ')'))";
2017-12-05 00:37:19 +01:00
2018-02-15 12:22:21 +01:00
$query = " SELECT an_anagrafiche.idanagrafica AS id, CONCAT(ragione_sociale $citta_cliente ) AS descrizione, idtipointervento_default, idzona FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica |where| ORDER BY ragione_sociale " ;
2017-08-04 16:28:16 +02:00
foreach ( $elements as $element ) {
$filter [] = 'an_anagrafiche.idanagrafica=' . prepare ( $element );
}
if ( empty ( $filter )) {
$where [] = " descrizione='Cliente' " ;
$where [] = 'deleted=0' ;
}
if ( ! empty ( $search )) {
$search_fields [] = 'ragione_sociale LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'provincia LIKE ' . prepare ( '%' . $search . '%' );
}
$custom [ 'idtipointervento' ] = 'idtipointervento_default' ;
2018-02-15 12:22:21 +01:00
$custom [ 'idzona' ] = 'idzona' ;
2017-08-04 16:28:16 +02:00
}
break ;
case 'fornitori' :
2018-02-15 14:43:34 +01:00
if (( Modules :: get ( 'Anagrafiche' )[ 'permessi' ] != '-' ) or ( Modules :: getAdditionalsQuery ( 'Anagrafiche' ))) {
2017-08-04 16:28:16 +02:00
$query = " SELECT an_anagrafiche.idanagrafica AS id, CONCAT(ragione_sociale, IF(citta IS NULL OR citta = '', '', CONCAT(' (', citta, ')'))) AS descrizione, idtipointervento_default FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica |where| ORDER BY ragione_sociale " ;
foreach ( $elements as $element ) {
$filter [] = 'an_anagrafiche.idanagrafica=' . prepare ( $element );
}
if ( empty ( $filter )) {
$where [] = " descrizione='Fornitore' " ;
$where [] = 'deleted=0' ;
}
if ( ! empty ( $search )) {
$search_fields [] = 'ragione_sociale LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'provincia LIKE ' . prepare ( '%' . $search . '%' );
}
$custom [ 'idtipointervento' ] = 'idtipointervento_default' ;
}
break ;
case 'agenti' :
2018-02-15 14:43:34 +01:00
if (( Modules :: get ( 'Anagrafiche' )[ 'permessi' ] != '-' ) or ( Modules :: getAdditionalsQuery ( 'Anagrafiche' ))) {
2017-08-04 16:28:16 +02:00
$query = " SELECT an_anagrafiche.idanagrafica AS id, CONCAT(ragione_sociale, IF(citta IS NULL OR citta = '', '', CONCAT(' (', citta, ')'))) AS descrizione, idtipointervento_default FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica |where| ORDER BY ragione_sociale " ;
foreach ( $elements as $element ) {
$filter [] = 'an_anagrafiche.idanagrafica=' . prepare ( $element );
}
if ( empty ( $filter )) {
$where [] = " descrizione='Agente' " ;
$where [] = 'deleted=0' ;
}
if ( ! empty ( $search )) {
$search_fields [] = 'ragione_sociale LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'provincia LIKE ' . prepare ( '%' . $search . '%' );
}
$results = completeResults ( $query , $where , $filter , $search , $custom );
// Evidenzia l'agente di default
if ( $superselect [ 'idanagrafica' ]) {
$rsa = $dbo -> fetchArray ( 'SELECT idagente FROM an_anagrafiche WHERE idanagrafica=' . prepare ( $superselect [ 'idanagrafica' ]));
$idagente_default = $rsa [ 0 ][ 'idagente' ];
} else {
$idagente_default = 0 ;
}
$ids = array_column ( $results , $id );
$pos = array_search ( $idagente_default , $ids );
if ( $pos !== false ) {
$results [ $pos ][ '_bgcolor_' ] = '#ff0' ;
}
}
break ;
case 'tecnici' :
2018-02-15 14:43:34 +01:00
if (( Modules :: get ( 'Anagrafiche' )[ 'permessi' ] != '-' ) or ( Modules :: getAdditionalsQuery ( 'Anagrafiche' ))) {
2017-08-04 16:28:16 +02:00
$query = " SELECT an_anagrafiche.idanagrafica AS id, CONCAT(ragione_sociale, IF(citta IS NULL OR citta = '', '', CONCAT(' (', citta, ')'))) AS descrizione, idtipointervento_default FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica |where| ORDER BY ragione_sociale " ;
foreach ( $elements as $element ) {
$filter [] = 'an_anagrafiche.idanagrafica=' . prepare ( $element );
}
if ( empty ( $filter )) {
$where [] = " descrizione='Tecnico' " ;
$where [] = 'deleted=0' ;
}
if ( ! empty ( $search )) {
$search_fields [] = 'ragione_sociale LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'provincia LIKE ' . prepare ( '%' . $search . '%' );
}
// $custom['idtipointervento'] = 'idtipointervento_default';
}
break ;
// Nota Bene: nel campo id viene specificato idtipoanagrafica-idanagrafica -> modulo Utenti e permessi, creazione nuovo utente
case 'anagrafiche' :
2018-02-15 14:43:34 +01:00
if (( Modules :: get ( 'Anagrafiche' )[ 'permessi' ] != '-' ) or ( Modules :: getAdditionalsQuery ( 'Anagrafiche' ))) {
2017-08-04 16:28:16 +02:00
$query = " SELECT CONCAT(an_tipianagrafiche.idtipoanagrafica, '-', an_anagrafiche.idanagrafica) AS id, CONCAT_WS('', ragione_sociale, ' (', citta, ' ', provincia, ')') AS descrizione idtipointervento_default FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica |where| ORDER BY ragione_sociale " ;
foreach ( $elements as $element ) {
$filter [] = 'an_anagrafiche.idanagrafica=' . prepare ( $element );
}
if ( empty ( $filter )) {
$where [] = 'deleted=0' ;
}
if ( ! empty ( $search )) {
$search_fields [] = 'ragione_sociale LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'provincia LIKE ' . prepare ( '%' . $search . '%' );
}
// $custom['idtipointervento'] = 'idtipointervento_default';
}
break ;
case 'sedi' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Anagrafiche' )[ 'permessi' ] != '-' && isset ( $superselect [ 'idanagrafica' ])) {
2018-02-15 12:22:21 +01:00
$query = " SELECT * FROM (SELECT 0 AS id, 'Sede legale' AS descrizione, (SELECT an_anagrafiche.idzona FROM an_anagrafiche WHERE an_anagrafiche.idanagrafica = " . prepare ( $superselect [ 'idanagrafica' ]) . " ) AS idzona UNION SELECT id, CONCAT_WS(' - ', nomesede, citta), idzona AS idzona FROM an_sedi |where|) AS tab |filter| ORDER BY id " ;
2017-08-04 16:28:16 +02:00
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
$where [] = 'idanagrafica=' . prepare ( $superselect [ 'idanagrafica' ]);
if ( ! empty ( $search )) {
$search_fields [] = 'nomesede LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
}
2018-02-15 12:22:21 +01:00
$custom [ 'idzona' ] = 'idzona' ;
2017-08-04 16:28:16 +02:00
}
break ;
case 'referenti' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Anagrafiche' )[ 'permessi' ] != '-' && isset ( $superselect [ 'idanagrafica' ])) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT id, nome AS descrizione FROM an_referenti |where| ORDER BY id' ;
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
$where [] = 'idanagrafica=' . prepare ( $superselect [ 'idanagrafica' ]);
if ( ! empty ( $search )) {
$search_fields [] = 'nome LIKE ' . prepare ( '%' . $search . '%' );
}
}
break ;
case 'articoli' :
2017-12-05 00:37:19 +01:00
$query = 'SELECT mg_articoli.*, co_iva.descrizione AS iva_vendita FROM mg_articoli LEFT OUTER JOIN co_iva ON mg_articoli.idiva_vendita=co_iva.id |where| ORDER BY mg_articoli.id_categoria ASC, mg_articoli.id_sottocategoria ASC' ;
2017-12-14 16:17:34 +01:00
$idiva_predefinita = get_var ( 'Iva predefinita' );
2017-12-05 00:37:19 +01:00
$rs = $dbo -> fetchArray ( " SELECT descrizione FROM co_iva WHERE id=' " . $idiva_predefinita . " ' " );
$iva_predefinita = $rs [ 0 ][ 'descrizione' ];
2017-08-04 16:28:16 +02:00
foreach ( $elements as $element ) {
2017-12-14 16:17:34 +01:00
$filter [] = 'mg_articoli.id=' . prepare ( $element );
2017-08-04 16:28:16 +02:00
}
2018-02-09 14:25:40 +01:00
$where [] = 'attivo = 1' ;
2017-08-04 16:28:16 +02:00
if ( ! empty ( $superselect [ 'dir' ]) && $superselect [ 'dir' ] == 'entrata' ) {
2018-02-09 14:25:40 +01:00
//$where[] = '(qta > 0 OR servizio = 1)';
2017-08-04 16:28:16 +02:00
}
if ( ! empty ( $search )) {
2017-12-16 19:03:26 +01:00
$search_fields [] = 'mg_articoli.descrizione LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'mg_articoli.codice LIKE ' . prepare ( '%' . $search . '%' );
2017-08-04 16:28:16 +02:00
}
if ( ! empty ( $search_fields )) {
$where [] = '(' . implode ( ' OR ' , $search_fields ) . ')' ;
}
2017-12-14 16:17:34 +01:00
if ( ! empty ( $filter )) {
$where [] = '(' . implode ( ' OR ' , $filter ) . ')' ;
}
2018-02-09 14:25:40 +01:00
$wh = '' ;
2017-08-04 16:28:16 +02:00
if ( count ( $where ) != 0 ) {
$wh = 'WHERE ' . implode ( ' AND ' , $where );
}
$query = str_replace ( '|where|' , $wh , $query );
$prev = - 1 ;
$rs = $dbo -> fetchArray ( $query );
foreach ( $rs as $r ) {
if ( $prev != $r [ 'id_sottocategoria' ]) {
2017-12-16 19:03:26 +01:00
$categoria = $dbo -> fetchArray ( 'SELECT `nome` FROM `mg_categorie` WHERE `id`=' . prepare ( $r [ 'id_categoria' ]))[ 0 ][ 'nome' ];
$sottocategoria = $dbo -> fetchArray ( 'SELECT `nome` FROM `mg_categorie` WHERE `id`=' . prepare ( $r [ 'id_sottocategoria' ]))[ 0 ][ 'nome' ];
2017-08-04 16:28:16 +02:00
$prev = $r [ 'id_sottocategoria' ];
2017-12-16 19:03:26 +01:00
$results [] = [ 'text' => $categoria . ' (' . ( ! empty ( $r [ 'id_sottocategoria' ]) ? $sottocategoria : '-' ) . ')' , 'children' => []];
2017-08-04 16:28:16 +02:00
}
2017-12-14 16:17:34 +01:00
if ( empty ( $r [ 'idiva_vendita' ])) {
2017-12-05 00:37:19 +01:00
$idiva = $idiva_predefinita ;
$iva = $iva_predefinita ;
} else {
$idiva = $r [ 'idiva_vendita' ];
$iva = $r [ 'iva_vendita' ];
}
2017-08-04 16:28:16 +02:00
$results [ count ( $results ) - 1 ][ 'children' ][] = [
'id' => $r [ 'id' ],
'text' => $r [ 'codice' ] . ' - ' . $r [ 'descrizione' ],
'descrizione' => $r [ 'descrizione' ],
'um' => $r [ 'um' ],
2017-12-05 00:37:19 +01:00
'idiva_vendita' => $idiva ,
'iva_vendita' => $iva ,
2017-08-04 16:28:16 +02:00
'prezzo_acquisto' => Translator :: numberToLocale ( $r [ 'prezzo_acquisto' ]),
'prezzo_vendita' => Translator :: numberToLocale ( $r [ 'prezzo_vendita' ]),
];
}
break ;
case 'conti' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Piano dei conti' )[ 'permessi' ] != '-' ) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT * FROM co_pianodeiconti2' ;
$rs = $dbo -> fetchArray ( $query );
foreach ( $rs as $r ) {
$results [] = [ 'text' => $r [ 'numero' ] . ' ' . $r [ 'descrizione' ], 'children' => []];
$subquery = 'SELECT * FROM co_pianodeiconti3 |where|' ;
$where = [];
$filter = [];
$search_fields = [];
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
if ( ! empty ( $filter )) {
$where [] = '(' . implode ( ' OR ' , $filter ) . ')' ;
}
$where [] = 'idpianodeiconti2=' . prepare ( $r [ 'id' ]);
if ( ! empty ( $search )) {
$search_fields [] = 'descrizione LIKE ' . prepare ( '%' . $search . '%' );
}
if ( ! empty ( $search_fields )) {
$where [] = '(' . implode ( ' OR ' , $search_fields ) . ')' ;
}
$wh = '' ;
if ( count ( $where ) != 0 ) {
$wh = 'WHERE ' . implode ( ' AND ' , $where );
}
$subquery = str_replace ( '|where|' , $wh , $subquery );
$rs2 = $dbo -> fetchArray ( $subquery );
foreach ( $rs2 as $r2 ) {
$results [ count ( $results ) - 1 ][ 'children' ][] = [ 'id' => $r2 [ 'id' ], 'text' => $r2 [ 'descrizione' ]];
}
}
}
break ;
case 'conti-vendite' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Piano dei conti' )[ 'permessi' ] != '-' ) {
2017-08-04 16:28:16 +02:00
$query = " SELECT co_pianodeiconti3.id, CONCAT_WS( ' ', co_pianodeiconti3.numero, co_pianodeiconti3.descrizione ) AS descrizione FROM co_pianodeiconti3 INNER JOIN (co_pianodeiconti2 INNER JOIN co_pianodeiconti1 ON co_pianodeiconti2.idpianodeiconti1=co_pianodeiconti1.id) ON co_pianodeiconti3.idpianodeiconti2=co_pianodeiconti2.id |where| ORDER BY co_pianodeiconti3.numero ASC " ;
foreach ( $elements as $element ) {
$filter [] = 'co_pianodeiconti3.id=' . prepare ( $element );
}
$where [] = " co_pianodeiconti1.descrizione='Economico' " ;
$where [] = " co_pianodeiconti3.dir='entrata' " ;
if ( ! empty ( $search )) {
$search_fields [] = 'descrizione LIKE ' . prepare ( '%' . $search . '%' );
}
}
break ;
case 'conti-acquisti' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Piano dei conti' )[ 'permessi' ] != '-' ) {
2017-08-04 16:28:16 +02:00
$query = " SELECT co_pianodeiconti3.id, CONCAT_WS( ' ', co_pianodeiconti3.numero, co_pianodeiconti3.descrizione ) AS descrizione FROM co_pianodeiconti3 INNER JOIN (co_pianodeiconti2 INNER JOIN co_pianodeiconti1 ON co_pianodeiconti2.idpianodeiconti1=co_pianodeiconti1.id) ON co_pianodeiconti3.idpianodeiconti2=co_pianodeiconti2.id |where| ORDER BY co_pianodeiconti3.numero ASC " ;
foreach ( $elements as $element ) {
$filter [] = 'co_pianodeiconti3.id=' . prepare ( $element );
}
$where [] = " co_pianodeiconti1.descrizione='Economico' " ;
$where [] = " co_pianodeiconti3.dir='uscita' " ;
if ( ! empty ( $search )) {
$search_fields [] = 'descrizione LIKE ' . prepare ( '%' . $search . '%' );
}
}
break ;
case 'impianti' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'MyImpianti' )[ 'permessi' ] != '-' && isset ( $superselect [ 'idanagrafica' ])) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT id, CONCAT(matricola, " - ", nome) AS descrizione FROM my_impianti |where| ORDER BY idsede' ;
foreach ( $elements as $element ) {
2017-09-18 18:11:19 +02:00
$filter [] = 'id=' . prepare ( $element );
2017-08-04 16:28:16 +02:00
}
$where [] = 'idanagrafica=' . prepare ( $superselect [ 'idanagrafica' ]);
$where [] = 'idsede=' . prepare ( $superselect [ 'idsede' ]);
if ( ! empty ( $search )) {
$search_fields [] = 'nome LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'matricola LIKE ' . prepare ( '%' . $search . '%' );
}
}
break ;
case 'componenti' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Gestione componenti' )[ 'permessi' ] != '-' && isset ( $superselect [ 'marticola' ])) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT id, nome AS descrizione, contenuto FROM my_impianto_componenti |where| ORDER BY id' ;
foreach ( $elements as $element ) {
$filter [] = 'idimpianto=' . prepare ( $element );
}
$temp = [];
$impianti = explode ( ',' , $superselect [ 'marticola' ]);
foreach ( $impianti as $key => $idimpianto ) {
$temp [] = 'idimpianto=' . prepare ( $idimpianto );
}
$where [] = '(' . implode ( ' OR ' , $temp ) . ')' ;
if ( ! empty ( $search )) {
$search_fields [] = 'nome LIKE ' . prepare ( '%' . $search . '%' );
}
$custom [ 'contenuto' ] = 'contenuto' ;
$results = completeResults ( $query , $where , $filter , $search , $custom );
foreach ( $results as $key => $value ) {
$matricola = \Util\Ini :: getValue ( $r [ 'contenuto' ], 'Matricola' );
$results [ $key ][ 'text' ] = ( empty ( $matricola ) ? '' : $matricola . ' - ' ) . $results [ $key ][ 'text' ];
unset ( $results [ $key ][ 'content' ]);
}
}
break ;
case 'categorie' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Magazzino' )[ 'permessi' ] != '-' ) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT id, nome AS descrizione FROM mg_categorie |where| ORDER BY id' ;
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
$where [] = '`parent` IS NULL' ;
if ( ! empty ( $search )) {
$search_fields [] = 'nome LIKE ' . prepare ( '%' . $search . '%' );
}
}
break ;
case 'sottocategorie' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Magazzino' )[ 'permessi' ] != '-' && isset ( $superselect [ 'id_categoria' ])) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT id, nome AS descrizione FROM mg_categorie |where| ORDER BY id' ;
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
$where [] = '`parent`=' . prepare ( $superselect [ 'id_categoria' ]);
if ( ! empty ( $search )) {
$search_fields [] = 'nome LIKE ' . prepare ( '%' . $search . '%' );
}
}
break ;
case 'preventivi' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Preventivi' )[ 'permessi' ] != '-' && isset ( $superselect [ 'idanagrafica' ])) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT co_preventivi.id AS id, an_anagrafiche.idanagrafica, CONCAT(numero, " ", nome) AS descrizione, co_preventivi.idtipointervento, (SELECT descrizione descrizione FROM in_tipiintervento WHERE in_tipiintervento.idtipointervento = co_preventivi.idtipointervento) AS idtipointervento_descrizione FROM co_preventivi INNER JOIN an_anagrafiche ON co_preventivi.idanagrafica=an_anagrafiche.idanagrafica |where| ORDER BY id' ;
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
$where [] = 'an_anagrafiche.idanagrafica=' . prepare ( $superselect [ 'idanagrafica' ]);
$where [] = " idstato NOT IN (SELECT `id` FROM co_statipreventivi WHERE descrizione='Bozza' OR descrizione='Rifiutato' OR descrizione='Pagato') " ;
if ( ! empty ( $search )) {
$search_fields [] = 'nome LIKE ' . prepare ( '%' . $search . '%' );
}
$custom [ 'idtipointervento' ] = 'idtipointervento' ;
$custom [ 'idtipointervento_descrizione' ] = 'idtipointervento_descrizione' ;
}
break ;
case 'preventivi_aperti' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Preventivi' )[ 'permessi' ] != '-' ) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT co_preventivi.id AS id, CONCAT(numero, " ", nome, " (", ragione_sociale, ")") AS descrizione FROM co_preventivi INNER JOIN an_anagrafiche ON co_preventivi.idanagrafica=an_anagrafiche.idanagrafica |where| ORDER BY id' ;
foreach ( $elements as $element ) {
$filter [] = 'idpreventivo=' . prepare ( $element );
}
$where [] = 'idstato IN (1)' ;
if ( ! empty ( $search )) {
$search_fields [] = 'nome LIKE ' . prepare ( '%' . $search . '%' );
}
}
break ;
case 'contratti' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Contratti' )[ 'permessi' ] != '-' ) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT co_contratti.id AS id, CONCAT(numero, " ", nome) AS descrizione FROM co_contratti INNER JOIN an_anagrafiche ON co_contratti.idanagrafica=an_anagrafiche.idanagrafica |where| ORDER BY id' ;
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
$where [] = 'an_anagrafiche.idanagrafica=' . prepare ( $superselect [ 'idanagrafica' ]);
$where [] = 'idstato IN (SELECT `id` FROM co_staticontratti WHERE pianificabile = 1)' ;
if ( ! empty ( $search )) {
$search_fields [] = 'nome LIKE ' . prepare ( '%' . $search . '%' );
}
}
break ;
case 'tipiintervento' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Interventi' )[ 'permessi' ] != '-' ) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT idtipointervento AS id, descrizione FROM in_tipiintervento |where| ORDER BY idtipointervento' ;
foreach ( $elements as $element ) {
$filter [] = 'idtipointervento=' . prepare ( $element );
}
if ( ! empty ( $search )) {
$search_fields [] = 'descrizione LIKE ' . prepare ( '%' . $search . '%' );
}
}
break ;
case 'misure' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Magazzino' )[ 'permessi' ] != '-' ) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT valore AS id, valore AS descrizione FROM mg_unitamisura |where| ORDER BY valore' ;
foreach ( $elements as $element ) {
$filter [] = 'valore=' . prepare ( $element ) . '' ;
}
if ( ! empty ( $search )) {
$search_fields [] = 'valore LIKE ' . prepare ( '%' . $search . '%' );
}
}
break ;
case 'prodotti_lotti' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Magazzino' )[ 'permessi' ] != '-' ) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT DISTINCT lotto AS descrizione FROM mg_prodotti |where|' ;
$where [] = 'idarticolo=' . prepare ( $superselect [ 'idarticolo' ]);
foreach ( $elements as $element ) {
$filter [] = 'lotto=' . prepare ( $element ) . '' ;
}
if ( ! empty ( $search )) {
$search_fields [] = 'lotto LIKE ' . prepare ( '%' . $search . '%' );
}
$custom [ 'id' ] = 'descrizione' ;
}
break ;
case 'prodotti_serial' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Magazzino' )[ 'permessi' ] != '-' ) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT DISTINCT serial AS descrizione FROM mg_prodotti |where|' ;
2017-09-05 17:31:58 +02:00
$where [] = 'id_articolo=' . prepare ( $superselect [ 'idarticolo' ]);
2017-08-04 16:28:16 +02:00
$where [] = 'lotto=' . prepare ( $superselect [ 'lotto' ]);
foreach ( $elements as $element ) {
$filter [] = 'serial=' . prepare ( $element ) . '' ;
}
if ( ! empty ( $search )) {
$search_fields [] = 'serial LIKE ' . prepare ( '%' . $search . '%' );
}
$custom [ 'id' ] = 'descrizione' ;
}
break ;
case 'prodotti_altro' :
2017-09-22 15:19:59 +02:00
if ( Modules :: get ( 'Magazzino' )[ 'permessi' ] != '-' ) {
2017-08-04 16:28:16 +02:00
$query = 'SELECT DISTINCT altro AS descrizione FROM mg_prodotti |where|' ;
2017-09-05 17:31:58 +02:00
$where [] = 'id_articolo=' . prepare ( $superselect [ 'idarticolo' ]);
2017-08-04 16:28:16 +02:00
$where [] = 'lotto=' . prepare ( $superselect [ 'lotto' ]);
$where [] = 'serial=' . prepare ( $superselect [ 'serial' ]);
foreach ( $elements as $element ) {
$filter [] = 'altro=' . prepare ( $element ) . '' ;
}
if ( ! empty ( $search )) {
$search_fields [] = 'altro LIKE ' . prepare ( '%' . $search . '%' );
}
$custom [ 'id' ] = 'descrizione' ;
}
break ;
}
if ( ! isset ( $results ) && ! empty ( $query )) {
$results = completeResults ( $query , $where , $filter , $search_fields , $custom );
}
echo json_encode ( $results );