Ottimizzazione stampa inventario + scelta tipologia stampa

This commit is contained in:
Fabio Lovato 2020-04-07 16:10:00 +02:00
parent adc715844b
commit d351246b78
3 changed files with 75 additions and 62 deletions

View File

@ -0,0 +1,31 @@
<?php
include_once __DIR__.'/../../../core.php';
use Carbon\Carbon;
// Trovo id_print della stampa
$id_print = $dbo->fetchOne('SELECT zz_prints.id FROM zz_prints INNER JOIN zz_modules ON zz_prints.id_module=zz_modules.id WHERE zz_modules.name="Articoli" AND zz_prints.name="Inventario magazzino"')['id'];
echo '
<form action="'.$rootdir.'/pdfgen.php?id_print='.$id_print.'" method="post" target="_blank">
<div class="row">
<div class="col-md-3">
{[ "type": "select", "label": "'.tr('Articoli da stampare').'", "name": "tipo", "required": "1", "values": "list=\"full\": \"'.tr('Tutti').'\", \"nozero\": \"'.tr('Solo esistenti').'\"", "value": "nozero" ]}
</div>
<div class="col-md-2">
<p style="line-height:14px;">&nbsp;</p>
<button type="submit" class="btn btn-primary btn-block">
<i class="fa fa-print"></i> '.tr('Stampa').'
</button>
</div>
</div>
</form>
<script>$(document).ready(init)</script>';

View File

@ -1,70 +1,43 @@
<?php
use Util\Query;
include_once __DIR__.'/../../core.php';
$id_module = Modules::get('Articoli')['id'];
// Valori di ricerca
$search = [
'codice' => $_GET['search_codice'],
'descrizione' => $_GET['search_descrizione'],
'categoria' => $_GET['search_categoria'],
'subcategoria' => $_GET['search_subcategoria'],
'tipo' => $_GET['search_tipo'],
'fornitore' => $_GET['search_fornitore'],
'barcode' => $_GET['search_barcode'],
];
$where['servizio'] = '0';
foreach ($search as $name => $value) {
if ($value == 'undefined') {
$search[$name] = null;
foreach( $_SESSION['module_'.$id_module] as $name => $value ){
if( preg_match('/^search_(.+?)$/', $name, $m) ){
$where[ $m[1] ] = $value;
}
}
$search['tipo'] = $search['tipo'] ?: 'solo prodotti attivi';
// Filtri effettivi
$where = [
'deleted_at IS NULL',
'servizio = 0',
];
if ($search['tipo'] == 'solo prodotti attivi') {
$where[] = 'attivo = 1';
} elseif ($search['tipo'] == 'solo prodotti non attivi') {
$where[] = 'attivo = 0';
}
if (!empty($search['codice'])) {
$where[] = "(REPLACE(codice, '.', '') LIKE ".prepare('%'.$search['codice'].'%').' OR codice LIKE '.prepare('%'.$search['codice'].'%').')';
}
if (!empty($search['descrizione'])) {
$where[] .= "REPLACE(descrizione, '.', '') LIKE ".prepare('%'.$search['descrizione'].'%');
}
if (!empty($search['categoria'])) {
$where[] = 'id_categoria IN (SELECT id FROM mg_categorie WHERE nome LIKE '.prepare('%'.$search['categoria'].'%').' AND parent IS NULL)';
}
if (!empty($search['subcategoria'])) {
$where[] = 'id_sottocategoria IN (SELECT id FROM mg_categorie WHERE nome LIKE '.prepare('%'.$search['subcategoria'].'%').' AND parent NOT NULL)';
}
if (!empty($search['fornitore'])) {
$where[] = 'id_fornitore IN (SELECT idanagrafica FROM an_anagrafiche WHERE ragione_sociale LIKE '.prepare('%'.$search['fornitore'].'%').')';
}
if (!empty($search['barcode'])) {
$where[] = "(REPLACE(barcode, '.', '') LIKE ".prepare('%'.$search['barcode'].'%').' OR barcode LIKE '.prepare('%'.$search['barcode'].'%').')';
}
$period_end = $_SESSION['period_end'];
$query = 'SELECT *,
(SELECT SUM(qta) FROM mg_movimenti WHERE mg_movimenti.idarticolo=mg_articoli.id AND data <= '.prepare($period_end).') AS qta
FROM mg_articoli LEFT OUTER JOIN (SELECT id, nome FROM mg_categorie) AS categoria ON mg_articoli.id_categoria = categoria.id WHERE 1=1
ORDER BY codice ASC';
$structure = Modules::get($id_module);
$query = str_replace('1=1', '1=1'.(!empty($where) ? ' AND '.implode(' AND ', $where) : ''), $query);
$rs = $dbo->fetchArray($query);
// RISULTATI VISIBILI
Util\Query::setSegments(false);
$query = Query::getQuery($structure, $where, 0, []);
$query = Modules::replaceAdditionals($id_module, $query);
// Modifiche alla query principale
$query = preg_replace('/ FROM `mg_articoli` /', ' FROM mg_articoli LEFT JOIN (SELECT idarticolo, SUM(qta) AS qta_totale FROM mg_movimenti GROUP BY idarticolo) movimenti ON movimenti.idarticolo=mg_articoli.id ', $query);
$query = preg_replace('/^SELECT /', 'SELECT mg_articoli.prezzo_acquisto,', $query);
$query = preg_replace('/^SELECT /', 'SELECT mg_articoli.prezzo_vendita,', $query);
$query = preg_replace('/^SELECT /', 'SELECT mg_articoli.um,', $query);
$query = preg_replace('/^SELECT /', 'SELECT movimenti.qta_totale,', $query);
if (post('tipo') == 'nozero') {
$query = str_replace('2=2', '2=2 AND movimenti.qta_totale > 0', $query);
}
$data = Query::executeAndCount($query);
echo '
<h3>'.tr('Inventario al _DATE_', [
@ -87,16 +60,16 @@ echo '
<tbody>';
$totali = [];
foreach ($rs as $r) {
$valore_magazzino = $r['prezzo_acquisto'] * $r['qta'];
foreach ($data['results'] as $r) {
$valore_magazzino = $r['prezzo_acquisto'] * $r['qta_totale'];
echo '
<tr>
<td>'.$r['codice'].'</td>
<td>'.$r['nome'].'</td>
<td>'.$r['descrizione'].'</td>
<td>'.$r['Codice'].'</td>
<td>'.$r['Categoria'].'</td>
<td>'.$r['Descrizione'].'</td>
<td class="text-right">'.moneyFormat($r['prezzo_vendita']).'</td>
<td class="text-right">'.Translator::numberToLocale($r['qta']).' '.$r['um'].'</td>
<td class="text-right">'.Translator::numberToLocale($r['qta_totale']).' '.$r['um'].'</td>
<td class="text-right">'.moneyFormat($r['prezzo_acquisto']).'</td>
<td class="text-right">'.moneyFormat($valore_magazzino).'</td>
</tr>';
@ -106,7 +79,7 @@ foreach ($rs as $r) {
// Totali
$totale_acquisto = sum($totali);
$totale_qta = sum(array_column($rs, 'qta'));
$totale_qta = sum(array_column($rs, 'qta_totale'));
echo '
</tbody>

View File

@ -510,3 +510,12 @@ ALTER TABLE `co_preventivi` ADD `numero_revision` INT NOT NULL AFTER `default_re
-- Riordinamento campi Fatture di vendita
UPDATE `zz_views` SET `order` = '11' WHERE `zz_views`.`name` = 'icon_Stato' AND `id_module` = (SELECT `id` FROM `zz_modules` WHERE `name` = 'Fatture di vendita');
-- Aggiornamento stampa inventario
UPDATE `zz_widgets` SET `more_link` = './modules/articoli/widgets/stampa_inventario.php', `more_link_type` = 'popup' WHERE `zz_widgets`.`name` = 'Stampa inventario';
-- Ottimizzazione query articoli
UPDATE `zz_modules` SET `options` = 'SELECT |select| FROM `mg_articoli` LEFT OUTER JOIN an_anagrafiche ON mg_articoli.id_fornitore=an_anagrafiche.idanagrafica LEFT OUTER JOIN co_iva ON mg_articoli.idiva_vendita=co_iva.id LEFT OUTER JOIN (SELECT SUM(qta-qta_evasa) AS qta_impegnata, idarticolo FROM or_righe_ordini INNER JOIN or_ordini ON or_righe_ordini.idordine=or_ordini.id WHERE idstatoordine IN(SELECT id FROM or_statiordine WHERE completato=0) GROUP BY idarticolo) a ON a.idarticolo=mg_articoli.id LEFT JOIN mg_categorie ON mg_articoli.id_categoria=mg_categorie.id LEFT JOIN mg_categorie AS sottocategorie ON mg_articoli.id_sottocategoria=sottocategorie.id WHERE 1=1 AND (`mg_articoli`.`deleted_at`) IS NULL HAVING 2=2 ORDER BY `descrizione`' WHERE `zz_modules`.`name` = 'Articoli';
UPDATE `zz_views` SET `query` = 'mg_categorie.nome' WHERE `zz_views`.`name` = 'Categoria' AND `id_module` = (SELECT `id` FROM `zz_modules` WHERE `name` = 'Articoli');
UPDATE `zz_views` SET `query` = 'sottocategorie.nome' WHERE `zz_views`.`name` = 'Sottocategoria' AND `id_module` = (SELECT `id` FROM `zz_modules` WHERE `name` = 'Articoli');
UPDATE `zz_views` SET `query` = 'an_anagrafiche.ragione_sociale' WHERE `zz_views`.`name` = 'Fornitore' AND `id_module` = (SELECT `id` FROM `zz_modules` WHERE `name` = 'Articoli');