2018-02-04 16:00:47 +01:00
< ? php
include_once __DIR__ . '/../../../core.php' ;
switch ( $resource ) {
case 'articoli' :
2018-09-27 12:54:57 +02:00
$query = 'SELECT mg_articoli.*, (SELECT CONCAT(co_pianodeiconti2.numero, ".", co_pianodeiconti3.numero, " ", co_pianodeiconti3.descrizione) FROM co_pianodeiconti3 INNER JOIN co_pianodeiconti2 ON co_pianodeiconti3.idpianodeiconti2=co_pianodeiconti2.id WHERE co_pianodeiconti3.id = idconto_vendita) AS idconto_vendita_title, (SELECT CONCAT(co_pianodeiconti2.numero, ".", co_pianodeiconti3.numero, " ", co_pianodeiconti3.descrizione) FROM co_pianodeiconti3 INNER JOIN co_pianodeiconti2 ON co_pianodeiconti3.idpianodeiconti2=co_pianodeiconti2.id WHERE co_pianodeiconti3.id = idconto_acquisto) AS idconto_acquisto_title, 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' ;
2018-02-04 16:00:47 +01:00
2018-11-23 16:08:23 +01:00
// Individuazione di eventuali listini
if ( ! empty ( $superselect [ 'dir' ]) && ! empty ( $superselect [ 'idanagrafica' ])) {
$listino = $dbo -> fetchOne ( 'SELECT prc_guadagno as percentuale FROM mg_listini WHERE id=(SELECT idlistino_' . ( $superselect [ 'dir' ] == 'uscita' ? 'acquisti' : 'vendite' ) . ' FROM an_anagrafiche WHERE idanagrafica=' . prepare ( $superselect [ 'idanagrafica' ]) . ')' );
}
2018-10-04 17:41:31 +02:00
//per le vendite leggo iva predefinita da anagrafica, se settata
2018-11-23 16:08:23 +01:00
if ( ! empty ( $superselect [ 'dir' ]) && $superselect [ 'dir' ] == 'entrata' && ! empty ( $superselect [ 'idanagrafica' ])) {
2018-11-30 16:10:15 +01:00
$idiva_predefinita_anagrafica = $dbo -> fetchOne ( 'SELECT idiva_vendite FROM an_anagrafiche WHERE idanagrafica = ' . prepare ( $superselect [ 'idanagrafica' ]))[ 'idiva_vendite' ];
$iva_predefinita_anagrafica = $dbo -> fetchOne ( 'SELECT descrizione FROM co_iva WHERE id = ' . prepare ( $idiva_predefinita_anagrafica ))[ 'descrizione' ];
2018-11-23 16:08:23 +01:00
}
2018-11-30 09:41:12 +01:00
// IVA da impostazioni
$idiva_predefinita = get_var ( 'Iva predefinita' );
2018-11-30 16:10:15 +01:00
$iva_predefinita = $dbo -> fetchOne ( 'SELECT descrizione FROM co_iva WHERE id=' . prepare ( $idiva_predefinita ))[ 'descrizione' ];
2018-02-04 16:00:47 +01:00
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' => []];
}
2018-11-30 09:41:12 +01:00
// Iva dell'articolo
2018-11-30 09:49:40 +01:00
if ( ! empty ( $idiva_predefinita_anagrafica )) {
2018-11-30 09:41:12 +01:00
$idiva = $idiva_predefinita_anagrafica ;
$iva = $iva_predefinita_anagrafica ;
} elseif ( empty ( $r [ 'idiva_vendita' ])) {
2018-02-04 16:00:47 +01:00
$idiva = $idiva_predefinita ;
$iva = $iva_predefinita ;
} else {
$idiva = $r [ 'idiva_vendita' ];
$iva = $r [ 'iva_vendita' ];
}
2018-11-23 16:08:23 +01:00
$prezzo_vendita = $r [ 'prezzo_vendita' ];
if ( ! empty ( $listino )) {
$prezzo_vendita = sum ( $prezzo_vendita , - calcola_sconto ([
'sconto' => $listino [ 'percentuale' ],
'prezzo' => $prezzo_vendita ,
'tipo' => 'PRC' ,
'qta' => 1 ,
]));
}
2018-02-04 16:00:47 +01:00
$results [ count ( $results ) - 1 ][ 'children' ][] = [
'id' => $r [ 'id' ],
'text' => $r [ 'codice' ] . ' - ' . $r [ 'descrizione' ],
2018-09-28 16:43:40 +02:00
'codice' => $r [ 'codice' ],
2018-02-04 16:00:47 +01:00
'descrizione' => $r [ 'descrizione' ],
'um' => $r [ 'um' ],
'idiva_vendita' => $idiva ,
'iva_vendita' => $iva ,
2018-07-09 17:21:25 +02:00
'idconto_vendita' => $r [ 'idconto_vendita' ],
'idconto_vendita_title' => $r [ 'idconto_vendita_title' ],
'idconto_acquisto' => $r [ 'idconto_acquisto' ],
'idconto_acquisto_title' => $r [ 'idconto_acquisto_title' ],
2018-11-23 16:08:23 +01:00
'prezzo_acquisto' => $r [ 'prezzo_acquisto' ],
'prezzo_vendita' => $prezzo_vendita ,
2018-02-04 16:00:47 +01:00
];
}
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 ;
}