2018-02-04 16:00:47 +01:00
< ? php
include_once __DIR__ . '/../../../core.php' ;
switch ( $resource ) {
case 'articoli' :
$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' ;
$idiva_predefinita = get_var ( 'Iva predefinita' );
$rs = $dbo -> fetchArray ( " SELECT descrizione FROM co_iva WHERE id=' " . $idiva_predefinita . " ' " );
$iva_predefinita = $rs [ 0 ][ 'descrizione' ];
foreach ( $elements as $element ) {
$filter [] = 'mg_articoli.id=' . prepare ( $element );
}
2018-02-09 14:33:30 +01:00
$where [] = 'attivo = 1' ;
2018-02-04 16:00:47 +01:00
if ( ! empty ( $superselect [ 'dir' ]) && $superselect [ 'dir' ] == 'entrata' ) {
2018-02-09 14:33:30 +01:00
//$where[] = '(qta > 0 OR servizio = 1)';
2018-02-04 16:00:47 +01:00
}
if ( ! empty ( $search )) {
$search_fields [] = 'mg_articoli.descrizione LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'mg_articoli.codice LIKE ' . prepare ( '%' . $search . '%' );
}
if ( ! empty ( $search_fields )) {
$where [] = '(' . implode ( ' OR ' , $search_fields ) . ')' ;
}
if ( ! empty ( $filter )) {
$where [] = '(' . implode ( ' OR ' , $filter ) . ')' ;
}
2018-02-09 14:33:30 +01:00
$wh = '' ;
2018-02-04 16:00:47 +01: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' ]) {
2018-06-23 18:35:08 +02:00
$categoria = $dbo -> fetchOne ( 'SELECT `nome` FROM `mg_categorie` WHERE `id`=' . prepare ( $r [ 'id_categoria' ]))[ 'nome' ];
2018-02-04 16:00:47 +01:00
2018-06-23 18:35:08 +02:00
$sottocategoria = $dbo -> fetchOne ( 'SELECT `nome` FROM `mg_categorie` WHERE `id`=' . prepare ( $r [ 'id_sottocategoria' ]));
$sottocategoria = isset ( $sottocategoria [ 'nome' ]) ? $sottocategoria [ 'nome' ] : null ;
2018-02-04 16:00:47 +01:00
$prev = $r [ 'id_sottocategoria' ];
$results [] = [ 'text' => $categoria . ' (' . ( ! empty ( $r [ 'id_sottocategoria' ]) ? $sottocategoria : '-' ) . ')' , 'children' => []];
}
if ( empty ( $r [ 'idiva_vendita' ])) {
$idiva = $idiva_predefinita ;
$iva = $iva_predefinita ;
} else {
$idiva = $r [ 'idiva_vendita' ];
$iva = $r [ 'iva_vendita' ];
}
$results [ count ( $results ) - 1 ][ 'children' ][] = [
'id' => $r [ 'id' ],
'text' => $r [ 'codice' ] . ' - ' . $r [ 'descrizione' ],
'descrizione' => $r [ 'descrizione' ],
'um' => $r [ 'um' ],
'idiva_vendita' => $idiva ,
'iva_vendita' => $iva ,
'prezzo_acquisto' => Translator :: numberToLocale ( $r [ 'prezzo_acquisto' ]),
'prezzo_vendita' => Translator :: numberToLocale ( $r [ 'prezzo_vendita' ]),
];
}
break ;
case 'prodotti_lotti' :
$query = 'SELECT DISTINCT lotto AS descrizione FROM mg_prodotti |where|' ;
$where [] = 'idarticolo=' . prepare ( $superselect [ 'idarticolo' ]);
foreach ( $elements as $element ) {
2018-03-24 14:24:17 +01:00
$filter [] = 'lotto=' . prepare ( $element );
2018-02-04 16:00:47 +01:00
}
if ( ! empty ( $search )) {
$search_fields [] = 'lotto LIKE ' . prepare ( '%' . $search . '%' );
}
$custom [ 'id' ] = 'descrizione' ;
break ;
case 'prodotti_serial' :
$query = 'SELECT DISTINCT serial AS descrizione FROM mg_prodotti |where|' ;
$where [] = 'id_articolo=' . prepare ( $superselect [ 'idarticolo' ]);
$where [] = 'lotto=' . prepare ( $superselect [ 'lotto' ]);
foreach ( $elements as $element ) {
2018-03-24 14:24:17 +01:00
$filter [] = 'serial=' . prepare ( $element );
2018-02-04 16:00:47 +01:00
}
if ( ! empty ( $search )) {
$search_fields [] = 'serial LIKE ' . prepare ( '%' . $search . '%' );
}
$custom [ 'id' ] = 'descrizione' ;
break ;
case 'prodotti_altro' :
$query = 'SELECT DISTINCT altro AS descrizione FROM mg_prodotti |where|' ;
$where [] = 'id_articolo=' . prepare ( $superselect [ 'idarticolo' ]);
$where [] = 'lotto=' . prepare ( $superselect [ 'lotto' ]);
$where [] = 'serial=' . prepare ( $superselect [ 'serial' ]);
foreach ( $elements as $element ) {
2018-03-24 14:24:17 +01:00
$filter [] = 'altro=' . prepare ( $element );
2018-02-04 16:00:47 +01:00
}
if ( ! empty ( $search )) {
$search_fields [] = 'altro LIKE ' . prepare ( '%' . $search . '%' );
}
$custom [ 'id' ] = 'descrizione' ;
break ;
case 'categorie' :
$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' :
if ( isset ( $superselect [ 'id_categoria' ])) {
$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 'misure' :
$query = 'SELECT valore AS id, valore AS descrizione FROM mg_unitamisura |where| ORDER BY valore' ;
foreach ( $elements as $element ) {
2018-03-24 14:24:17 +01:00
$filter [] = 'valore=' . prepare ( $element );
2018-02-04 16:00:47 +01:00
}
if ( ! empty ( $search )) {
$search_fields [] = 'valore LIKE ' . prepare ( '%' . $search . '%' );
}
break ;
}