Skip to content

Commit 145dad7

Browse files
committed
fix: plugin movimenti e giacenze per sede
1 parent 4d4121d commit 145dad7

4 files changed

Lines changed: 183 additions & 101 deletions

File tree

modules/articoli/modals/add_movimento.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
use Models\Locale;
4+
25
/*
36
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
47
* Copyright (C) DevCode s.r.l.
@@ -22,10 +25,21 @@
2225
// Imposto come azienda l'azienda predefinita per selezionare le sedi a cui ho accesso
2326
// select-options
2427

28+
// Recupera l'ID della causale di spostamento
29+
$rs_spostamento = $dbo->fetchArray('SELECT id, tipo_movimento FROM mg_causali_movimenti');
30+
$id_causale_spostamento = null;
31+
foreach ($rs_spostamento as $row) {
32+
if ($row['tipo_movimento'] == 'spostamento') {
33+
$id_causale_spostamento = $row['id'];
34+
break;
35+
}
36+
}
37+
2538
?>
2639
<form action="" method="post" id="add-form">
2740
<input type="hidden" name="op" value="add-movimento">
2841
<input type="hidden" name="backto" value="record-edit">
42+
<input type="hidden" id="id_causale_spostamento" value="<?php echo $id_causale_spostamento; ?>">
2943

3044
<div class="row">
3145
<div class="col-md-4">
@@ -41,7 +55,7 @@
4155
</div>
4256

4357
<div class="col-md-4">
44-
{["type": "select", "label": "<?php echo tr('Causale'); ?>", "name": "causale", "values": "query=SELECT `mg_causali_movimenti`.`id`, `title` as text, `description` as descrizione, `tipo_movimento` FROM `mg_causali_movimenti` LEFT JOIN `mg_causali_movimenti_lang` ON (`mg_causali_movimenti`.`id` = `mg_causali_movimenti_lang`.`id_record` AND `mg_causali_movimenti_lang`.`id_lang` = <?php echo prepare(Models\Locale::getDefault()->id); ?>)", "value": 1, "required": 1 ]}
58+
{["type": "select", "label": "<?php echo tr('Causale'); ?>", "name": "causale", "values": "query=SELECT `mg_causali_movimenti`.`id`, `title` as text, `description` as descrizione, `tipo_movimento` FROM `mg_causali_movimenti` LEFT JOIN `mg_causali_movimenti_lang` ON (`mg_causali_movimenti`.`id` = `mg_causali_movimenti_lang`.`id_record` AND `mg_causali_movimenti_lang`.`id_lang` = <?php echo prepare(Locale::getDefault()->id); ?>)", "value": 1, "required": 1 ]}
4559
<input type="hidden" name="tipo_movimento" id="tipo_movimento" value="carico">
4660
</div>
4761
</div>
@@ -96,6 +110,22 @@ function disabilitaSede(id){
96110
.attr("required", false);
97111
}
98112
113+
function controllaSpostamento() {
114+
let idsede_partenza = $("#idsede_partenza").val();
115+
let idsede_destinazione = $("#idsede_destinazione").val();
116+
let id_causale_spostamento = $("#id_causale_spostamento").val();
117+
118+
// Se le sedi sono diverse e esiste la causale di spostamento
119+
if (idsede_partenza !== "" && idsede_destinazione !== "" &&
120+
idsede_partenza !== idsede_destinazione &&
121+
id_causale_spostamento) {
122+
// Seleziona la causale di spostamento solo se non è già selezionata
123+
if ($("#causale").val() != id_causale_spostamento) {
124+
$("#causale").val(id_causale_spostamento).trigger("change");
125+
}
126+
}
127+
}
128+
99129
$(document).ready(function () {
100130
init();
101131
@@ -121,6 +151,30 @@ function disabilitaSede(id){
121151
}
122152
});
123153
154+
// Quando cambia la sede destinazione, controlla se impostare spostamento
155+
$("#idsede_destinazione").on("change", function() {
156+
let idsede_destinazione = $(this).val();
157+
let idsede_partenza = $("#idsede_partenza").val();
158+
159+
// Se la sede destinazione è diversa dalla sede partenza
160+
if (idsede_destinazione !== "" && idsede_partenza !== "" &&
161+
idsede_destinazione !== idsede_partenza) {
162+
controllaSpostamento();
163+
}
164+
});
165+
166+
// Quando cambia la sede partenza, controlla se impostare spostamento
167+
$("#idsede_partenza").on("change", function() {
168+
let idsede_partenza = $(this).val();
169+
let idsede_destinazione = $("#idsede_destinazione").val();
170+
171+
// Se la sede partenza è diversa dalla sede destinazione
172+
if (idsede_partenza !== "" && idsede_destinazione !== "" &&
173+
idsede_partenza !== idsede_destinazione) {
174+
controllaSpostamento();
175+
}
176+
});
177+
124178
setTimeout(function() {
125179
$("#causale").trigger("change");
126180
}, 100);

modules/articoli/plugins/articoli.giacenze.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
$impegnato = 0;
2424
$ordinato = 0;
25+
$giacenze = $articolo->getGiacenze();
2526

2627
$query = 'SELECT
2728
`or_ordini`.`id` AS id,
@@ -182,13 +183,9 @@
182183
</div>
183184
</div>';
184185

185-
/**
186+
/*
186187
** Da ordinare.
187188
*/
188-
$qta_presente = $articolo->qta > 0 ? $articolo->qta : 0;
189-
$diff = ($qta_presente - $impegnato + $ordinato) * -1;
190-
$da_ordinare = (($diff <= 0) ? 0 : $diff);
191-
192189
echo '
193190
<div class="col-md-3">
194191
<div class="card card-primary">
@@ -197,19 +194,28 @@
197194
<i class="fa fa-question-circle-o"></i></span></h3>
198195
</div>
199196
<div class="card-body">
200-
<div class="row">
201-
<div class="col-md-12 text-center" style="font-size:35pt;">
202-
'.numberFormat($da_ordinare, 'qta').' '.$articolo->um.'
203-
</div>
204-
</div>
197+
<table class="table table-sm table-striped">';
198+
199+
foreach ($sedi as $sede) {
200+
$qta_presente = isset($giacenze[$sede['id']][0]) ? $giacenze[$sede['id']][0] : 0;
201+
$diff = ($qta_presente - $impegnato + $ordinato) * -1;
202+
$da_ordinare = (($diff <= 0) ? 0 : $diff);
203+
echo '
204+
<tr>
205+
<td>'.$sede['nomesede'].'</td>
206+
<td class="text-right">'.numberFormat($da_ordinare, 'qta').'</td>
207+
</tr>';
208+
}
209+
210+
echo '
211+
</table>
205212
</div>
206213
</div>
207214
</div>';
208215

209-
/**
216+
/*
210217
** Disponibile.
211218
*/
212-
$disponibile = $qta_presente - $impegnato;
213219
echo '
214220
<div class="col-md-3">
215221
<div class="card card-primary">
@@ -218,20 +224,26 @@
218224
<i class="fa fa-question-circle-o"></i></span></h3>
219225
</div>
220226
<div class="card-body">
227+
<table class="table table-sm table-striped">';
221228

222-
<div class="row">
223-
<div class="col-md-12 text-center" style="font-size:35pt;">
224-
'.numberFormat($disponibile, 'qta').' '.$articolo->um.'
225-
</div>
226-
</div>
229+
foreach ($sedi as $sede) {
230+
$giacenza_sede = isset($giacenze[$sede['id']][0]) ? $giacenze[$sede['id']][0] : 0;
231+
echo '
232+
<tr>
233+
<td>'.$sede['nomesede'].'</td>
234+
<td class="text-right">'.numberFormat($giacenza_sede, 'qta').'</td>
235+
</tr>';
236+
}
227237

238+
echo '
239+
</table>
228240
</div>
229241
</div>
230242
</div>
231243
</div>';
232244

233-
$sedi = $dbo->fetchArray('(SELECT "0" AS id, IF(indirizzo!=\'\', CONCAT_WS(" - ", "'.tr('Sede legale').'", CONCAT(citta, \' (\', indirizzo, \')\')), CONCAT_WS(" - ", "'.tr('Sede legale').'", citta)) AS nomesede FROM an_anagrafiche WHERE idanagrafica = '.prepare(setting('Azienda predefinita')).') UNION (SELECT id, IF(indirizzo!=\'\',CONCAT_WS(" - ", nomesede, CONCAT(citta, \' (\', indirizzo, \')\')), CONCAT_WS(" - ", nomesede, citta )) AS nomesede FROM an_sedi WHERE idanagrafica='.prepare(setting('Azienda predefinita')).')');
234245
$giacenze = $articolo->getGiacenze();
246+
$sedi = $dbo->fetchArray('(SELECT "0" AS id, IF(indirizzo!=\'\', CONCAT_WS(" - ", "'.tr('Sede legale').'", CONCAT(citta, \' (\', indirizzo, \')\')), CONCAT_WS(" - ", "'.tr('Sede legale').'", citta)) AS nomesede FROM an_anagrafiche WHERE idanagrafica = '.prepare(setting('Azienda predefinita')).') UNION (SELECT id, IF(indirizzo!=\'\',CONCAT_WS(" - ", nomesede, CONCAT(citta, \' (\', indirizzo, \')\')), CONCAT_WS(" - ", nomesede, citta )) AS nomesede FROM an_sedi WHERE idanagrafica='.prepare(setting('Azienda predefinita')).')');
235247

236248
echo '
237249
<div class="row">

modules/articoli/plugins/articoli.movimenti.php

Lines changed: 91 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,12 @@
6262
<div class="card-body">';
6363

6464
// Calcolo la quantità dai movimenti in magazzino
65-
$qta_totale = $dbo->fetchOne('SELECT SUM(qta) AS qta FROM mg_movimenti WHERE idarticolo='.prepare($id_record))['qta'];
66-
$qta_totale_attuale = $dbo->fetchOne('SELECT SUM(qta) AS qta FROM mg_movimenti WHERE idarticolo='.prepare($id_record).' AND data <= CURDATE()')['qta'];
67-
68-
echo '
69-
<p>'.tr('Quantità calcolata dai movimenti').': <b>'.Translator::numberToLocale($qta_totale, 'qta').' '.$record['um'].'</b> <span class="tip" title="'.tr('Quantità calcolata da tutti i movimenti registrati').'." ><i class="fa fa-question-circle-o"></i></span></p>';
70-
71-
echo '
72-
<p>'.tr('Quantità calcolata attuale').': <b>'.Translator::numberToLocale($qta_totale_attuale, 'qta').' '.$record['um'].'</b> <span class="tip" title="'.tr('Quantità calcolata secondo i movimenti registrati con data oggi o date trascorse').'." ><i class="fa fa-question-circle-o"></i></span></p>';
65+
$giacenze = $articolo->getGiacenze();
66+
$sedi = $dbo->fetchArray('(SELECT "0" AS id, IF(indirizzo!=\'\', CONCAT_WS(" - ", "'.tr('Sede legale').'", CONCAT(citta, \' (\', indirizzo, \')\')), CONCAT_WS(" - ", "'.tr('Sede legale').'", citta)) AS nomesede FROM an_anagrafiche WHERE idanagrafica = '.prepare(setting('Azienda predefinita')).') UNION (SELECT id, IF(indirizzo!=\'\',CONCAT_WS(" - ", nomesede, CONCAT(citta, \' (\', indirizzo, \')\')), CONCAT_WS(" - ", nomesede, citta )) AS nomesede FROM an_sedi WHERE idanagrafica='.prepare(setting('Azienda predefinita')).')');
7367

7468
// Individuazione movimenti
7569
$movimenti = $articolo->movimentiComposti();
7670

77-
$giacenze = $articolo->getGiacenze();
78-
7971
// Raggruppamento per documento e ordinamento
8072
$movimenti = $movimenti->leftJoin('an_sedi', 'mg_movimenti.idsede', 'an_sedi.id')
8173
->orderByDesc('mg_movimenti.data')
@@ -89,88 +81,106 @@
8981
$movimenti = $movimenti->take(20);
9082
}
9183

92-
if (! empty($movimenti)) {
93-
echo '
94-
<table class="table table-striped table-sm table-bordered">
95-
<tr>
96-
<th class="text-center" width="40">#</th>
97-
<th class="text-center" width="120">'.tr('Carico').'</th>
98-
<th class="text-center" width="120">'.tr('Scarico').'</th>
99-
<th class="text-center">'.tr('Q.tà progressiva').'</th>
100-
<th>'.tr('Operazione').'</th>
101-
<th>'.tr('Controparte').'</th>
102-
<th class="text-center">'.tr('Sede').'</th>
103-
<th class="text-center" width="120">'.tr('Data').'</th>
104-
<th class="text-center" width="80">#</th>
105-
</tr>';
106-
107-
foreach ($movimenti as $i => $movimento) {
108-
// Quantità progressiva
109-
if ($mov[$movimento->idsede]['progressivo_finale'] === null) {
110-
$movimento->progressivo_finale = $giacenze[$movimento->idsede][0];
111-
} else {
112-
$movimento->progressivo_finale = $mov[$movimento->idsede]['progressivo_iniziale'];
113-
}
84+
// Raggruppamento movimenti per sede
85+
$movimenti_per_sede = [];
86+
foreach ($movimenti as $movimento) {
87+
$idsede = (int) $movimento->idsede;
88+
$movimenti_per_sede[$idsede][] = $movimento;
89+
}
11490

115-
$movimento->progressivo_iniziale = $movimento->progressivo_finale - $movimento->qta;
91+
if (!empty($movimenti)) {
92+
foreach ($sedi as $sede) {
93+
$movimenti_sede = $movimenti_per_sede[(int) $sede['id']] ?? [];
11694

117-
$mov[$movimento->idsede]['progressivo_iniziale'] = $movimento->progressivo_iniziale;
118-
$mov[$movimento->idsede]['progressivo_finale'] = $movimento->progressivo_finale;
95+
if (empty($movimenti_sede)) {
96+
continue;
97+
}
11998

120-
// Quantità
121-
echo '
122-
<tr>
123-
<td class="text-center">
124-
'.count($movimenti) - $i.'
125-
</td>
126-
<td class="text-center" style="color: green;">
127-
'.($movimento->qta > 0 ? numberFormat($movimento->qta, 'qta').' '.$record['um'] : '').'
128-
</td>
129-
<td class="text-center" style="color: red;">
130-
'.($movimento->qta < 0 ? numberFormat(abs($movimento->qta), 'qta').' '.$record['um'] : '').'
131-
</td>
132-
<td class="text-center">
133-
'.numberFormat($movimento->progressivo_iniziale, 'qta').' '.$record['um'].'
134-
<i class="fa fa-arrow-circle-right"></i>
135-
'.numberFormat($movimento->progressivo_finale, 'qta').' '.$record['um'].'
136-
</td>
137-
<td>
138-
'.$movimento->descrizione.''.($movimento->hasDocument() ? ' - '.reference($movimento->getDocument()) : '').'
139-
</td>
140-
<td>
141-
'.Anagrafica::find($movimento->getDocument()->idanagrafica)->ragione_sociale.'
142-
</td>
143-
<td class="text-center">
144-
'.($movimento->nomesede ?: tr('Sede legale')).'
145-
</td>';
146-
147-
// Data
148-
$utente = $dbo->table('zz_users')->where('id', $movimento->idutente)->first();
149-
$data = ($movimento->data ?: $movimento->data_movimento);
15099
echo '
151-
<td class="text-center">'.dateFormat($data).' <span class="tip" title="'.tr('Creazione movimento: _DATE_ <br>Creatore movimento: _USER_', [
152-
'_DATE_' => timestampFormat($movimento->data_movimento),
153-
'_USER_' => $utente->username,
154-
]).'"><i class="fa fa-question-circle-o"></i></span> </td>';
100+
<div class="card">
101+
<div class="card-header">
102+
<h3 class="card-title">'.$sede['nomesede'].'</h3>
103+
</div>
104+
<div class="card-body">
105+
<table class="table table-striped table-sm table-bordered">
106+
<tr>
107+
<th class="text-center" width="40">#</th>
108+
<th class="text-center" width="120">'.tr('Carico').'</th>
109+
<th class="text-center" width="120">'.tr('Scarico').'</th>
110+
<th class="text-center">'.tr('Q.tà progressiva').'</th>
111+
<th>'.tr('Operazione').'</th>
112+
<th>'.tr('Controparte').'</th>
113+
<th class="text-center" width="120">'.tr('Data').'</th>
114+
<th class="text-center" width="80">#</th>
115+
</tr>';
116+
117+
foreach ($movimenti_sede as $i => $movimento) {
118+
// Quantità progressiva
119+
if ($mov[$movimento->idsede]['progressivo_finale'] === null) {
120+
$movimento->progressivo_finale = $giacenze[$movimento->idsede][0];
121+
} else {
122+
$movimento->progressivo_finale = $mov[$movimento->idsede]['progressivo_iniziale'];
123+
}
124+
125+
$movimento->progressivo_iniziale = $movimento->progressivo_finale - $movimento->qta;
126+
127+
$mov[$movimento->idsede]['progressivo_iniziale'] = $movimento->progressivo_iniziale;
128+
$mov[$movimento->idsede]['progressivo_finale'] = $movimento->progressivo_finale;
129+
130+
// Quantità
131+
echo '
132+
<tr>
133+
<td class="text-center">
134+
'.count($movimenti_sede) - $i.'
135+
</td>
136+
<td class="text-center" style="color: green;">
137+
'.($movimento->qta > 0 ? numberFormat($movimento->qta, 'qta').' '.$record['um'] : '').'
138+
</td>
139+
<td class="text-center" style="color: red;">
140+
'.($movimento->qta < 0 ? numberFormat(abs($movimento->qta), 'qta').' '.$record['um'] : '').'
141+
</td>
142+
<td class="text-center">
143+
'.numberFormat($movimento->progressivo_iniziale, 'qta').' '.$record['um'].'
144+
<i class="fa fa-arrow-circle-right"></i>
145+
'.numberFormat($movimento->progressivo_finale, 'qta').' '.$record['um'].'
146+
</td>
147+
<td>
148+
'.$movimento->descrizione.''.($movimento->hasDocument() ? ' - '.reference($movimento->getDocument()) : '').'
149+
</td>
150+
<td>
151+
'.Anagrafica::find($movimento->getDocument()->idanagrafica)->ragione_sociale.'
152+
</td>';
153+
154+
// Data
155+
$utente = $dbo->table('zz_users')->where('id', $movimento->idutente)->first();
156+
$data = ($movimento->data ?: $movimento->data_movimento);
157+
echo '
158+
<td class="text-center">'.dateFormat($data).' <span class="tip" title="'.tr('Creazione movimento: _DATE_ <br>Creatore movimento: _USER_', [
159+
'_DATE_' => timestampFormat($movimento->data_movimento),
160+
'_USER_' => $utente->username,
161+
]).'"><i class="fa fa-question-circle-o"></i></span> </td>';
155162

156-
// Operazioni
157-
echo '
158-
<td class="text-center">';
163+
// Operazioni
164+
echo '
165+
<td class="text-center">';
166+
167+
if (AuthOSM::admin() && $movimento->isManuale()) {
168+
echo '
169+
<a class="btn btn-danger btn-xs ask" data-backto="record-edit" data-op="delmovimento" data-idmovimento="'.$movimento->tipo_gruppo.'">
170+
<i class="fa fa-trash"></i>
171+
</a>';
172+
}
159173

160-
if (AuthOSM::admin() && $movimento->isManuale()) {
161174
echo '
162-
<a class="btn btn-danger btn-xs ask" data-backto="record-edit" data-op="delmovimento" data-idmovimento="'.$movimento->tipo_gruppo.'">
163-
<i class="fa fa-trash"></i>
164-
</a>';
175+
</td>
176+
</tr>';
165177
}
166178

167179
echo '
168-
</td>
169-
</tr>';
180+
</table>
181+
</div>
182+
</div>';
170183
}
171-
172-
echo '
173-
</table>';
174184
} else {
175185
echo '
176186
<div class="alert alert-info">

0 commit comments

Comments
 (0)