From ce3a108e75e6d863248f0a1cf9a92b7a1799144f Mon Sep 17 00:00:00 2001
From: MatteoPistorello <pistorellomatteo8@gmail.com>
Date: Tue, 3 Sep 2024 12:44:38 +0200
Subject: [PATCH] =?UTF-8?q?Fix=20visualizzazione=20variazioni=20quantit?=
 =?UTF-8?q?=C3=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 modules/articoli/plugins/articoli.movimenti.php | 12 +++++++-----
 modules/articoli/plugins/dettagli_giacenze.php  | 14 ++++++++------
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/modules/articoli/plugins/articoli.movimenti.php b/modules/articoli/plugins/articoli.movimenti.php
index 6d4d50e20..a324d64ff 100755
--- a/modules/articoli/plugins/articoli.movimenti.php
+++ b/modules/articoli/plugins/articoli.movimenti.php
@@ -97,6 +97,8 @@ if (empty($_GET['movimentazione_completa'])) {
     $movimenti->limit(20);
 }
 
+$giacenze = $articolo->getGiacenze();
+
 // Raggruppamento per documento
 $movimenti = $movimenti->leftJoin('an_sedi', 'mg_movimenti.idsede', 'an_sedi.id')->get();
 if (!empty($movimenti)) {
@@ -115,16 +117,16 @@ if (!empty($movimenti)) {
 
     foreach ($movimenti as $i => $movimento) {
         // Quantità progressiva
-        if ($i == 0) {
-            $movimento['progressivo_finale'] = $articolo->qta;
+        if ($mov[$movimento['idsede']]['progressivo_finale'] === null) {
+            $movimento['progressivo_finale'] = $giacenze[$movimento['idsede']][0];
         } else {
-            $movimento['progressivo_finale'] = $movimenti[$i - 1]['progressivo_iniziale'];
+            $movimento['progressivo_finale'] = $mov[$movimento['idsede']]['progressivo_iniziale'];
         }
 
         $movimento['progressivo_iniziale'] = $movimento['progressivo_finale'] - $movimento->qta;
 
-        $movimenti[$i]['progressivo_iniziale'] = $movimento['progressivo_iniziale'];
-        $movimenti[$i]['progressivo_finale'] = $movimento['progressivo_finale'];
+        $mov[$movimento['idsede']]['progressivo_iniziale'] = $movimento['progressivo_iniziale'];
+        $mov[$movimento['idsede']]['progressivo_finale'] = $movimento['progressivo_finale'];
 
         // Quantità
         echo '
diff --git a/modules/articoli/plugins/dettagli_giacenze.php b/modules/articoli/plugins/dettagli_giacenze.php
index 413876a08..a04f38263 100755
--- a/modules/articoli/plugins/dettagli_giacenze.php
+++ b/modules/articoli/plugins/dettagli_giacenze.php
@@ -29,6 +29,8 @@ $movimenti = $articolo->movimentiComposti()
     ->orderBy('mg_movimenti.data', 'DESC')
     ->orderBy('mg_movimenti.id', 'DESC');
 
+$giacenze = $articolo->getGiacenze();
+
 // Raggruppamento per documento
 $movimenti = $movimenti->get();
 if (!empty($movimenti)) {
@@ -46,16 +48,16 @@ if (!empty($movimenti)) {
 
     foreach ($movimenti as $i => $movimento) {
         // Quantità progressiva
-        if ($i == 0) {
-            $movimento['progressivo_finale'] = $articolo->qta;
+        if ($mov[$movimento['idsede']]['progressivo_finale'] === null) {
+            $movimento['progressivo_finale'] = $giacenze[$movimento['idsede']][0];
         } else {
-            $movimento['progressivo_finale'] = $movimenti[$i - 1]['progressivo_iniziale'];
+            $movimento['progressivo_finale'] = $mov[$movimento['idsede']]['progressivo_iniziale'];
         }
-        $movimento['progressivo_iniziale'] = $movimento['progressivo_finale'] - $movimento->qta;
+
         $movimento['progressivo_iniziale'] = $movimento['progressivo_finale'] - $movimento->qta;
 
-        $movimenti[$i]['progressivo_iniziale'] = $movimento['progressivo_iniziale'];
-        $movimenti[$i]['progressivo_finale'] = $movimento['progressivo_finale'];
+        $mov[$movimento['idsede']]['progressivo_iniziale'] = $movimento['progressivo_iniziale'];
+        $mov[$movimento['idsede']]['progressivo_finale'] = $movimento['progressivo_finale'];
 
         $totale += $movimento->qta;