2018-02-04 16:00:47 +01:00
< ? php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright ( C ) DevCode s . r . l .
2020-09-07 15:04:06 +02:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
2018-02-04 16:00:47 +01:00
include_once __DIR__ . '/../../../core.php' ;
2019-06-04 20:45:40 +02:00
switch ( $resource ) {
2020-08-14 09:38:17 +02:00
/*
* Opzioni utilizzate :
* - permetti_movimento_a_zero
* - idsede_partenza e idsede_destinazione
* - dir
* - idanagrafica
*/
2018-02-04 16:00:47 +01:00
case 'articoli' :
2020-07-06 13:32:43 +02:00
$sedi_non_impostate = ! isset ( $superselect [ 'idsede_partenza' ]) && ! isset ( $superselect [ 'idsede_destinazione' ]);
2020-07-06 13:57:45 +02:00
$prezzi_ivati = setting ( 'Utilizza prezzi di vendita comprensivi di IVA' );
2020-07-22 13:15:15 +02:00
$usare_dettaglio_fornitore = $superselect [ 'dir' ] == 'uscita' ;
2023-06-22 15:54:50 +02:00
$ricerca_codici_fornitore = 1 ;
2020-10-20 11:33:34 +02:00
$usare_iva_anagrafica = $superselect [ 'dir' ] == 'entrata' && ! empty ( $superselect [ 'idanagrafica' ]);
2021-12-01 10:44:02 +01:00
$solo_non_varianti = $superselect [ 'solo_non_varianti' ];
2022-05-19 17:42:41 +02:00
$idagente = $superselect [ 'idagente' ];
2022-11-15 17:38:42 +01:00
$id_listino = $superselect [ 'id_listino' ];
2020-07-06 13:32:43 +02:00
2020-10-20 11:33:34 +02:00
$query = " SELECT
2023-06-22 15:54:50 +02:00
DISTINCT `mg_articoli` . `id` ,
2024-03-01 15:07:42 +01:00
IF ( `categoria_lang` . `name` IS NOT NULL , CONCAT ( `categoria_lang` . `name` , IF ( `sottocategoria_lang` . `name` IS NOT NULL , CONCAT ( ' (' , `sottocategoria_lang` . `name` , ')' ), '-' )), '<i>".tr(' Nessuna categoria ')."</i>' ) AS optgroup ,
2023-06-22 15:54:50 +02:00
`mg_articoli` . `barcode` ,
2023-08-04 14:54:28 +02:00
`mg_articoli` . " .( $prezzi_ivati ? '`prezzo_vendita_ivato`' : '`prezzo_vendita`').' AS prezzo_vendita,
2023-06-22 15:54:50 +02:00
`mg_articoli` . `prezzo_vendita_ivato` AS prezzo_vendita_ivato ,
2023-08-04 14:54:28 +02:00
`mg_articoli` . '.($prezzi_ivati ? ' `minimo_vendita_ivato` ' : ' `minimo_vendita` ').' AS minimo_vendita , ' ;
2020-07-22 13:15:15 +02:00
// Informazioni relative al fornitore specificato dal documenti di acquisto
if ( $usare_dettaglio_fornitore ) {
$query .= '
2023-06-23 11:17:31 +02:00
IFNULL ( `mg_fornitore_articolo` . `codice_fornitore` , `mg_articoli` . `codice` ) AS codice ,
2024-02-29 15:10:55 +01:00
IFNULL ( `mg_fornitore_articolo` . `descrizione` , `mg_articoli_lang` . `name` ) AS descrizione ,
2023-06-23 11:17:31 +02:00
IFNULL ( `mg_fornitore_articolo` . `prezzo_acquisto` , `mg_articoli` . `prezzo_acquisto` ) AS prezzo_acquisto ,
2023-06-22 15:54:50 +02:00
IFNULL ( `mg_fornitore_articolo` . `qta_minima` , 0 ) AS qta_minima ,
`mg_fornitore_articolo` . `id` AS id_dettaglio_fornitore , ' ;
2020-07-22 13:15:15 +02:00
}
// Informazioni dell'articolo per i documenti di vendita
else {
$query .= '
2023-06-22 15:54:50 +02:00
`mg_articoli` . `codice` AS codice ,
2024-02-29 15:10:55 +01:00
`mg_articoli_lang` . `name` AS descrizione ,
2023-06-22 15:54:50 +02:00
`mg_articoli` . `prezzo_acquisto` AS prezzo_acquisto ,
2020-07-22 13:15:15 +02:00
0 AS qta_minima ,
2023-06-22 15:54:50 +02:00
`mg_fornitore_articolo` . `codice_fornitore` AS codice_fornitore ,
`mg_fornitore_articolo` . `id` AS id_dettaglio_fornitore , ' ;
2020-07-22 13:15:15 +02:00
}
2020-11-06 10:46:42 +01:00
if ( $usare_iva_anagrafica ) {
2020-10-20 11:33:34 +02:00
$query .= '
2023-06-22 15:54:50 +02:00
IFNULL ( `iva_anagrafica` . `id` , IFNULL ( `iva_articolo` . `id` , `iva_predefinita` . `id` )) AS idiva_vendita ,
2024-02-06 17:36:05 +01:00
IFNULL ( `iva_anagrafica_lang` . `name` , IFNULL ( `iva_articolo_lang` . `name` , `iva_predefinita_lang` . `name` )) AS iva_vendita ,
2023-06-22 15:54:50 +02:00
IFNULL ( `iva_anagrafica` . `percentuale` , IFNULL ( `iva_articolo` . `percentuale` , `iva_predefinita` . `percentuale` )) AS percentuale , ' ;
2020-11-06 10:46:42 +01:00
} else {
2020-10-20 11:33:34 +02:00
$query .= '
2023-06-22 15:54:50 +02:00
IFNULL ( `iva_articolo` . `id` , `iva_predefinita` . `id` ) AS idiva_vendita ,
2024-02-06 17:36:05 +01:00
IFNULL ( `iva_articolo_lang` . `name` , `iva_predefinita_lang` . `name` ) AS iva_vendita ,
2023-06-22 15:54:50 +02:00
IFNULL ( `iva_articolo` . `percentuale` , `iva_predefinita` . `percentuale` ) AS percentuale , ' ;
2020-10-20 11:33:34 +02:00
}
2022-05-19 17:42:41 +02:00
if ( $idagente ) {
$query .= '
2023-07-06 12:15:46 +02:00
`co_provvigioni` . `provvigione` AS provvigione ,
2023-06-22 15:54:50 +02:00
`co_provvigioni` . `tipo_provvigione` AS tipo_provvigione , ' ;
2022-05-19 17:42:41 +02:00
}
2020-07-22 13:15:15 +02:00
$query .= '
2023-06-22 15:54:50 +02:00
round ( `mg_articoli` . `qta` , '.setting(' Cifre decimali per quantità ' ) . " ) AS qta,
`mg_articoli` . `um` ,
`mg_articoli` . `fattore_um_secondaria` ,
`mg_articoli` . `servizio` ,
2023-09-28 12:31:03 +02:00
`mg_articoli` . `abilita_serial` ,
2023-06-22 15:54:50 +02:00
`mg_articoli` . `idconto_vendita` ,
`mg_articoli` . `idconto_acquisto` ,
2024-03-01 15:07:42 +01:00
`categoria_lang` . `name` AS categoria ,
`sottocategoria_lang` . `name` AS sottocategoria ,
2023-06-22 15:54:50 +02:00
`righe` . `media_ponderata` ,
CONCAT ( `conto_vendita_categoria` . `numero` , '.' , `conto_vendita_sottocategoria` . `numero` , ' ' , `conto_vendita_sottocategoria` . `descrizione` ) AS idconto_vendita_title ,
CONCAT ( `conto_acquisto_categoria` . `numero` , '.' , `conto_acquisto_sottocategoria` . `numero` , ' ' , `conto_acquisto_sottocategoria` . `descrizione` ) AS idconto_acquisto_title
2024-02-26 15:53:39 +01:00
FROM
`mg_articoli`
2024-02-29 15:10:55 +01:00
LEFT JOIN `mg_articoli_lang` ON ( `mg_articoli` . `id` = `mg_articoli_lang` . `id_record` AND `mg_articoli_lang` . `id_lang` = " .prepare(setting('Lingua')). " )
2020-07-06 13:32:43 +02:00
LEFT JOIN `mg_categorie` AS categoria ON `categoria` . `id` = `mg_articoli` . `id_categoria`
2024-03-01 09:08:08 +01:00
LEFT JOIN `mg_categorie_lang` AS categoria_lang ON ( `categoria` . `id` = `categoria_lang` . `id_record` AND `categoria_lang` . `id_lang` = " .prepare(setting('Lingua')). " )
2020-07-06 13:32:43 +02:00
LEFT JOIN `mg_categorie` AS sottocategoria ON `sottocategoria` . `id` = `mg_articoli` . `id_sottocategoria`
2024-03-01 09:08:08 +01:00
LEFT JOIN `mg_categorie_lang` AS sottocategoria_lang ON ( `sottocategoria` . `id` = `sottocategoria_lang` . `id_record` AND `sottocategoria_lang` . `id_lang` = " .prepare(setting('Lingua')). " )
2023-06-22 15:54:50 +02:00
LEFT JOIN `co_pianodeiconti3` AS conto_vendita_sottocategoria ON `conto_vendita_sottocategoria` . `id` = `mg_articoli` . `idconto_vendita`
LEFT JOIN `co_pianodeiconti2` AS conto_vendita_categoria ON `conto_vendita_sottocategoria` . `idpianodeiconti2` = `conto_vendita_categoria` . `id`
LEFT JOIN `co_pianodeiconti3` AS conto_acquisto_sottocategoria ON `conto_acquisto_sottocategoria` . `id` = `mg_articoli` . `idconto_acquisto`
LEFT JOIN `co_pianodeiconti2` AS conto_acquisto_categoria ON `conto_acquisto_sottocategoria` . `idpianodeiconti2` = `conto_acquisto_categoria` . `id`
2024-02-26 15:53:39 +01:00
LEFT JOIN ( SELECT `co_righe_documenti` . `idarticolo` AS id , ( SUM (( `co_righe_documenti` . `prezzo_unitario` - `co_righe_documenti` . `sconto_unitario` ) * `co_righe_documenti` . `qta` ) / SUM ( `co_righe_documenti` . `qta` )) AS media_ponderata FROM `co_righe_documenti` LEFT JOIN `co_documenti` ON `co_documenti` . `id` = `co_righe_documenti` . `iddocumento` LEFT JOIN `co_tipidocumento` ON `co_tipidocumento` . `id` = `co_documenti` . `idtipodocumento` WHERE `co_tipidocumento` . `dir` = 'uscita' GROUP BY `co_righe_documenti` . `idarticolo` ) AS righe
2023-06-22 15:54:50 +02:00
ON `righe` . `id` = `mg_articoli` . `id`
LEFT JOIN `co_iva` AS iva_articolo ON `iva_articolo` . `id` = `mg_articoli` . `idiva_vendita`
2024-02-06 17:36:05 +01:00
LEFT JOIN `co_iva_lang` AS iva_articolo_lang on ( `iva_articolo` . `id` = `iva_articolo_lang` . `id_record` AND `iva_articolo_lang` . `id_lang` = " .prepare(setting('Lingua')). " )
2024-03-01 15:07:42 +01:00
LEFT JOIN `co_iva` AS `iva_predefinita` ON `iva_predefinita` . `id` = ( SELECT `valore` FROM `zz_settings` WHERE `zz_settings` . `nome` = 'Iva predefinita' )
2024-02-06 17:36:05 +01:00
LEFT JOIN `co_iva_lang` AS iva_predefinita_lang on ( `iva_predefinita` . `id` = `iva_predefinita_lang` . `id_record` AND `iva_predefinita_lang` . `id_lang` = " .prepare(setting('Lingua')).')';
2020-10-20 11:33:34 +02:00
if ( $usare_iva_anagrafica ) {
2020-11-06 10:46:42 +01:00
$query .= '
2024-02-06 17:36:05 +01:00
LEFT JOIN `co_iva` AS iva_anagrafica ON `iva_anagrafica` . `id` = ( SELECT `idiva_vendite` FROM `an_anagrafiche` WHERE `idanagrafica` = '.prepare($superselect[' idanagrafica ']).' )
LEFT JOIN `co_iva_lang` AS iva_anagrafica_lang on ( `iva_anagrafica` . `id` = `iva_anagrafica_lang` . `id_record` AND `iva_anagrafica_lang` . `id_lang` = '.prepare(setting(' Lingua ')).' ) ' ;
2020-11-06 10:46:42 +01:00
}
2020-10-20 11:33:34 +02:00
2022-05-19 17:42:41 +02:00
if ( $idagente ) {
$query .= '
2023-06-22 15:54:50 +02:00
LEFT JOIN `co_provvigioni` ON `co_provvigioni` . `idarticolo` = `mg_articoli` . `id` AND `co_provvigioni` . `idagente` = ' . prepare ( $idagente );
2022-05-19 17:42:41 +02:00
}
2023-06-22 15:54:50 +02:00
if ( $dir == 'uscita' ) {
2020-10-20 11:33:34 +02:00
$query .= '
2023-06-22 15:54:50 +02:00
LEFT JOIN `mg_fornitore_articolo` ON `mg_fornitore_articolo` . `id_articolo` = `mg_articoli` . `id` AND `mg_fornitore_articolo` . `deleted_at` IS NULL AND `mg_fornitore_articolo` . `id_fornitore` = '.prepare($superselect[' idanagrafica ' ]);
} else {
$query .= '
LEFT JOIN `mg_fornitore_articolo` ON ( `mg_fornitore_articolo` . `id_articolo` = `mg_articoli` . `id` AND `mg_fornitore_articolo` . `deleted_at` IS NULL AND `mg_fornitore_articolo` . `id_fornitore` = `mg_articoli` . `id_fornitore` ) ' ;
}
2020-07-06 13:32:43 +02:00
// Se c'è una sede settata, carico tutti gli articoli presenti in quella sede
if ( ! $sedi_non_impostate ) {
$query .= '
2023-06-22 15:54:50 +02:00
LEFT JOIN ( SELECT `idarticolo` , `idsede` FROM `mg_movimenti` GROUP BY `idarticolo` ) movimenti ON `movimenti` . `idarticolo` = `mg_articoli` . `id`
LEFT JOIN `an_sedi` ON `an_sedi` . `id` = `movimenti` . `idsede` ' ;
2019-05-29 19:17:57 +02:00
}
2019-06-04 20:45:40 +02:00
2020-07-06 13:32:43 +02:00
$query .= '
| where | ' ;
2019-05-29 19:17:57 +02:00
// Se c'è una sede settata, carico tutti gli articoli presenti in quella sede
2020-07-06 13:32:43 +02:00
if ( ! $sedi_non_impostate ) {
$query .= '
GROUP BY
2023-06-22 15:54:50 +02:00
`mg_articoli` . `id` ' ;
2019-05-29 19:17:57 +02:00
}
2018-02-04 16:00:47 +01:00
2020-07-06 13:32:43 +02:00
$query .= '
ORDER BY
2023-06-22 15:54:50 +02:00
`mg_articoli` . `id_categoria` ASC ,
`mg_articoli` . `id_sottocategoria` ASC ,
`mg_articoli` . `codice` ASC ,
2024-02-29 15:10:55 +01:00
`mg_articoli_lang` . `name` ASC ' ;
2020-07-06 13:32:43 +02:00
2018-02-04 16:00:47 +01:00
foreach ( $elements as $element ) {
2023-06-22 15:54:50 +02:00
$filter [] = '`mg_articoli`.`id`=' . prepare ( $element );
2018-02-04 16:00:47 +01:00
}
2023-06-22 15:54:50 +02:00
$where [] = '`mg_articoli`.`attivo` = 1' ;
$where [] = '`mg_articoli`.`deleted_at` IS NULL' ;
2019-07-08 12:25:51 +02:00
2021-12-01 10:44:02 +01:00
if ( $solo_non_varianti ) {
2023-06-22 15:54:50 +02:00
$where [] = '`mg_articoli`.`id_combinazione` IS NULL' ;
2021-12-01 10:44:02 +01:00
}
2022-11-15 17:38:42 +01:00
if ( $id_listino ) {
2023-06-22 15:54:50 +02:00
$where [] = '`mg_articoli`.`id` NOT IN (SELECT `id_articolo` FROM `mg_listini_articoli` WHERE `id_listino`=' . prepare ( $id_listino ) . ')' ;
2022-11-15 17:38:42 +01:00
}
2018-02-04 16:00:47 +01:00
if ( ! empty ( $search )) {
2024-02-29 15:10:55 +01:00
$search_fields [] = '`mg_articoli_lang`.`name` LIKE ' . prepare ( '%' . $search . '%' );
2023-06-22 15:54:50 +02:00
$search_fields [] = '`mg_articoli`.`codice` LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = '`mg_articoli`.`barcode` LIKE ' . prepare ( '%' . $search . '%' );
2024-03-01 15:07:42 +01:00
$search_fields [] = '`categoria_lang`.`name` LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = '`sottocategoria_lang`.`name` LIKE ' . prepare ( '%' . $search . '%' );
2020-07-22 13:15:15 +02:00
if ( $usare_dettaglio_fornitore ) {
2023-06-22 15:54:50 +02:00
$search_fields [] = '`mg_fornitore_articolo`.`descrizione` LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = '`mg_fornitore_articolo`.`codice_fornitore` LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = '`mg_fornitore_articolo`.`barcode_fornitore` LIKE ' . prepare ( '%' . $search . '%' );
2020-07-22 13:15:15 +02:00
}
2022-08-05 17:13:18 +02:00
if ( $ricerca_codici_fornitore ) {
2023-06-22 15:54:50 +02:00
$search_fields [] = '`mg_fornitore_articolo`.`descrizione` LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = '`mg_fornitore_articolo`.`codice_fornitore` LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = '`mg_fornitore_articolo`.`barcode_fornitore` LIKE ' . prepare ( '%' . $search . '%' );
2022-08-05 17:13:18 +02:00
}
2018-02-04 16:00:47 +01:00
}
2019-02-22 10:37:37 +01:00
$data = AJAX :: selectResults ( $query , $where , $filter , $search_fields , $limit , $custom );
$rs = $data [ 'results' ];
2018-02-04 16:00:47 +01:00
2020-12-04 16:00:21 +01:00
// Utilizzo dell'impostazione per disabilitare articoli con quantità <= 0
$permetti_movimenti_sotto_zero = setting ( 'Permetti selezione articoli con quantità minore o uguale a zero in Documenti di Vendita' ) ? true : $superselect [ 'permetti_movimento_a_zero' ];
2022-03-07 15:41:04 +01:00
// Eventuali articoli disabilitati
2020-10-20 11:33:34 +02:00
foreach ( $rs as $k => $r ) {
2019-07-08 10:27:56 +02:00
// Lettura movimenti delle mie sedi
2023-06-22 15:54:50 +02:00
$qta_sede = $dbo -> fetchOne ( 'SELECT SUM(`mg_movimenti`.`qta`) AS qta FROM `mg_movimenti` LEFT JOIN `an_sedi` ON `an_sedi`.`id` = `mg_movimenti`.`idsede` WHERE `mg_movimenti`.`idarticolo` = ' . prepare ( $r [ 'id' ]) . ' AND `idsede` = ' . prepare ( $superselect [ 'idsede_partenza' ]))[ 'qta' ];
2019-07-08 10:27:56 +02:00
2020-10-20 11:33:34 +02:00
$rs [ $k ] = array_merge ( $r , [
2023-06-22 15:54:50 +02:00
'text' => $r [ 'codice' ] . ' - ' . $r [ 'descrizione' ] . ' ' . ( ! $r [ 'servizio' ] ? '(' . Translator :: numberToLocale ( $qta_sede ) . ( ! empty ( $r [ 'um' ]) ? ' ' . $r [ 'um' ] : '' ) . ')' : '' ) . ( $r [ 'codice_fornitore' ] ? ' (' . $r [ 'codice_fornitore' ] . ')' : '' ),
2021-02-05 13:27:18 +01:00
'disabled' => $qta_sede <= 0 && ! $permetti_movimenti_sotto_zero && ! $r [ 'servizio' ],
2020-10-20 11:33:34 +02:00
]);
2018-02-04 16:00:47 +01:00
}
2019-02-22 10:37:37 +01:00
$results = [
2020-10-20 11:33:34 +02:00
'results' => $rs ,
2019-02-22 10:37:37 +01:00
'recordsFiltered' => $data [ 'recordsFiltered' ],
];
2018-02-04 16:00:47 +01:00
break ;
case 'categorie' :
2024-03-01 09:08:08 +01:00
$query = 'SELECT `mg_categorie`.`id`, `name` AS descrizione FROM `mg_categorie` LEFT JOIN `mg_categorie_lang` ON (`mg_categorie`.`id` = `mg_categorie_lang`.`id_record` AND `mg_categorie_lang`.`id_lang` = ' . prepare ( setting ( 'Lingua' )) . ') |where| ORDER BY `name`' ;
2018-02-04 16:00:47 +01:00
foreach ( $elements as $element ) {
2024-03-01 09:08:08 +01:00
$filter [] = '`mg_categorie`.`id`=' . prepare ( $element );
2018-02-04 16:00:47 +01:00
}
$where [] = '`parent` IS NULL' ;
if ( ! empty ( $search )) {
2024-03-01 09:08:08 +01:00
$search_fields [] = '`name` LIKE ' . prepare ( '%' . $search . '%' );
2018-02-04 16:00:47 +01:00
}
break ;
2024-01-15 15:30:45 +01:00
/*
* Opzioni utilizzate :
* - id_categoria
*/
2018-02-04 16:00:47 +01:00
case 'sottocategorie' :
if ( isset ( $superselect [ 'id_categoria' ])) {
2024-03-01 09:08:08 +01:00
$query = 'SELECT `mg_categorie`.`id`, `name` AS descrizione FROM `mg_categorie` LEFT JOIN `mg_categorie_lang` ON (`mg_categorie`.`id` = `mg_categorie_lang`.`id_record` AND `mg_categorie_lang`.`id_lang` = ' . prepare ( setting ( 'Lingua' )) . ') |where| ORDER BY `name`' ;
2018-02-04 16:00:47 +01:00
foreach ( $elements as $element ) {
2024-03-01 09:08:08 +01:00
$filter [] = '`mg_categorie`.`id`=' . prepare ( $element );
2018-02-04 16:00:47 +01:00
}
2023-06-23 16:38:38 +02:00
$where [] = '`parent`=' . prepare ( $superselect [ 'id_categoria' ]);
2018-02-04 16:00:47 +01:00
if ( ! empty ( $search )) {
2024-03-01 09:08:08 +01:00
$search_fields [] = '`name` LIKE ' . prepare ( '%' . $search . '%' );
2018-02-04 16:00:47 +01:00
}
}
break ;
case 'misure' :
2023-06-22 15:54:50 +02:00
$query = 'SELECT `valore` AS id, `valore` AS descrizione FROM `mg_unitamisura` |where| ORDER BY `valore`' ;
2018-02-04 16:00:47 +01:00
foreach ( $elements as $element ) {
2023-06-22 15:54:50 +02:00
$filter [] = '`valore`=' . prepare ( $element );
2018-02-04 16:00:47 +01:00
}
if ( ! empty ( $search )) {
2023-06-22 15:54:50 +02:00
$search_fields [] = '`valore` LIKE ' . prepare ( '%' . $search . '%' );
2018-02-04 16:00:47 +01:00
}
2020-07-20 14:40:11 +02:00
break ;
2024-01-15 15:30:45 +01:00
/*
* Opzioni utilizzate :
* - idanagrafica
*/
2020-07-20 14:40:11 +02:00
case 'articoli_barcode' :
2020-08-17 10:57:51 +02:00
$id_anagrafica = filter ( 'id_anagrafica' ); // ID passato via URL in modo fisso
2020-07-20 14:40:11 +02:00
$prezzi_ivati = setting ( 'Utilizza prezzi di vendita comprensivi di IVA' );
2023-06-22 15:54:50 +02:00
$query = ' SELECT `mg_articoli` .* ,
`mg_articoli` . `id` ,
`mg_articoli` . `qta` ,
`mg_articoli` . `um` ,
`mg_articoli` . `id` ,
`mg_articoli` . `id` ,
IFNULL ( `mg_fornitore_articolo` . `codice_fornitore` , `mg_articoli` . `codice` ) AS codice ,
2024-02-29 15:10:55 +01:00
IFNULL ( `mg_fornitore_articolo` . `descrizione` , `mg_articoli_lang` . `name` ) AS descrizione ,
2023-06-22 15:54:50 +02:00
IFNULL ( `mg_fornitore_articolo` . `prezzo_acquisto` , `mg_articoli` . `prezzo_acquisto` ) AS prezzo_acquisto ,
`mg_articoli` . '.($prezzi_ivati ? ' `prezzo_vendita_ivato` ' : ' `prezzo_vendita` ').' AS prezzo_vendita ,
`mg_articoli` . `prezzo_vendita_ivato` AS prezzo_vendita_ivato ,
IFNULL ( `mg_fornitore_articolo` . `qta_minima` , 0 ) AS qta_minima ,
`mg_fornitore_articolo` . `id` AS id_dettaglio_fornitore
FROM `mg_articoli`
LEFT JOIN `mg_fornitore_articolo` ON `mg_fornitore_articolo` . `id_articolo` = `mg_articoli` . `id` AND `mg_fornitore_articolo` . `deleted_at` IS NULL AND `mg_fornitore_articolo` . `id_fornitore` = '.prepare($id_anagrafica).'
2020-07-20 14:40:11 +02:00
| where | ' ;
2023-06-22 15:54:50 +02:00
$where [] = '`mg_articoli`.`attivo` = 1' ;
$where [] = '`mg_articoli`.`deleted_at` IS NULL' ;
2020-07-20 14:40:11 +02:00
2020-11-11 15:01:48 +01:00
if ( ! empty ( $search )) {
2023-06-22 15:54:50 +02:00
$search_fields [] = '`mg_articoli`.`codice` LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = '`mg_articoli`.`barcode` LIKE ' . prepare ( '%' . $search . '%' );
2020-11-11 15:01:48 +01:00
}
2018-02-04 16:00:47 +01:00
break ;
2020-12-04 15:40:47 +01:00
2020-12-04 14:30:29 +01:00
case 'fornitori-articolo' :
2024-01-16 15:43:29 +01:00
$query = 'SELECT `an_anagrafiche`.`idanagrafica` AS id, CONCAT(`an_anagrafiche`.`ragione_sociale`, IF(`mg_fornitore_articolo`.`codice_fornitore` IS NOT NULL, CONCAT( " (", `mg_fornitore_articolo`.`codice_fornitore`, ")" ), "-") ) AS descrizione, (`mg_prezzi_articoli`.`prezzo_unitario`-(`mg_prezzi_articoli`.`prezzo_unitario`*`mg_prezzi_articoli`.`sconto_percentuale`)/100) AS prezzo_unitario FROM `mg_prezzi_articoli` LEFT JOIN `an_anagrafiche` ON `mg_prezzi_articoli`.`id_anagrafica`=`an_anagrafiche`.`idanagrafica` LEFT JOIN `mg_fornitore_articolo` ON (`mg_fornitore_articolo`.`id_articolo`=`mg_prezzi_articoli`.`id_articolo` AND `mg_fornitore_articolo`.`id_fornitore` = `mg_prezzi_articoli`.`id_anagrafica`) |where| GROUP BY id ORDER BY `an_anagrafiche`.`ragione_sociale`' ;
2020-12-04 14:30:29 +01:00
foreach ( $elements as $element ) {
2023-06-22 15:54:50 +02:00
$filter [] = '`an_anagrafiche`.`idanagrafica`=' . prepare ( $element );
2020-12-04 14:30:29 +01:00
}
2020-12-04 15:40:47 +01:00
2023-06-22 15:54:50 +02:00
$where [] = '`dir`="uscita"' ;
2023-06-22 15:06:28 +02:00
$where [] = '`mg_prezzi_articoli`.`id_articolo`=' . prepare ( $superselect [ 'id_articolo' ]);
2020-12-04 14:30:29 +01:00
if ( ! empty ( $search )) {
2023-06-22 15:54:50 +02:00
$search_fields [] = '`an_anagrafiche`.`ragione_sociale` LIKE ' . prepare ( '%' . $search . '%' );
2020-12-04 14:30:29 +01:00
}
break ;
2023-07-05 11:43:07 +02:00
case 'serial-articolo' :
$query = 'SELECT serial AS id, serial AS descrizione FROM mg_prodotti |where| AND mg_prodotti.dir=\'uscita\' AND id=(SELECT MAX(id) FROM mg_prodotti AS prodotti WHERE prodotti.id_articolo=mg_prodotti.id_articolo AND prodotti.serial=mg_prodotti.serial)' ;
$where [] = 'id_articolo=' . prepare ( $superselect [ 'idarticolo' ]);
break ;
2018-02-04 16:00:47 +01:00
}