From 769c9a9b66f6643716b96888ef625a9f16ee039e Mon Sep 17 00:00:00 2001
From: Cedric <c.gross@kreiz-it.fr>
Date: Tue, 15 Jul 2014 11:22:16 +0200
Subject: [PATCH] FIX stock inconsistency when activating batch management on
 product which already have stock

---
 htdocs/langs/en_US/productbatch.lang   |  1 +
 htdocs/langs/fr_FR/productbatch.lang   |  4 ++-
 htdocs/product/class/product.class.php | 34 ++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/htdocs/langs/en_US/productbatch.lang b/htdocs/langs/en_US/productbatch.lang
index 8b93ba92fb8..07fb45985d5 100644
--- a/htdocs/langs/en_US/productbatch.lang
+++ b/htdocs/langs/en_US/productbatch.lang
@@ -16,3 +16,4 @@ printEatby= Eat-by: %s
 printSellby= Sell-by: %s
 printQty= Qty: %d
 AddDispatchBatchLine=Add a line for Shelf Life dispatching
+BatchDefaultNumber= Undefined
\ No newline at end of file
diff --git a/htdocs/langs/fr_FR/productbatch.lang b/htdocs/langs/fr_FR/productbatch.lang
index 488e1f2fbb9..68e76dca46b 100644
--- a/htdocs/langs/fr_FR/productbatch.lang
+++ b/htdocs/langs/fr_FR/productbatch.lang
@@ -14,4 +14,6 @@ DetailBatchFormat= C:%s UO: %s LOT: %s (Qté : %d)
 printBatch= Lot: %s
 printEatby= DLC: %s
 printSellby= DLUO: %s
-printQty= Qté: %d
\ No newline at end of file
+printQty= Qté: %d
+AddDispatchBatchLine=Ajouter une ligne de dispatch
+BatchDefaultNumber= Indéfini
\ No newline at end of file
diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php
index 32d96ec60fc..cc824bce3c6 100644
--- a/htdocs/product/class/product.class.php
+++ b/htdocs/product/class/product.class.php
@@ -589,6 +589,39 @@ class Product extends CommonObject
 
         if ($result >= 0)
         {
+            if (empty($this->oldcopy))
+            {
+                $org=new self($db);
+                $org->fetch($this->id);
+                $this->oldcopy=$org;
+            }
+            // test if batch management is activated on existing product
+            if ($this->hasbatch() && !$this->oldcopy->hasbatch())
+            {
+                $this->load_stock();
+                foreach ($this->stock_warehouse as $idW => $ObjW)
+                {
+                    $qty_batch = 0;
+                    foreach ($ObjW->detail_batch as $detail)    
+                    {
+                        $qty_batch += $detail->qty;
+                    }
+                    // Quantities in batch details are not same same as stock quantity
+                    // So we add a default batch record
+                    if ($ObjW->real <> $qty_batch)
+                    {
+                        $ObjBatch = new Productbatch($this->db);
+                        $ObjBatch->batch = $langs->trans('BatchDefaultNumber');
+                        $ObjBatch->qty = $ObjW->real - $qty_batch;
+                        $ObjBatch->fk_product_stock = $ObjW->id;
+                        if ($ObjBatch->create($user,1) < 0)
+                        { 
+                            $error++;
+                            $this->errors=$ObjBatch->errors;
+                        }
+                    }
+                }    
+            }
 	        // For automatic creation
 	        if ($this->barcode == -1) $this->barcode = $this->get_barcode($this,$this->barcode_type_code);
 
@@ -2918,6 +2951,7 @@ class Product extends CommonObject
 					$this->stock_warehouse[$row->fk_entrepot] = new stdClass();
 					$this->stock_warehouse[$row->fk_entrepot]->real = $row->reel;
 					$this->stock_warehouse[$row->fk_entrepot]->pmp = $row->pmp;
+					$this->stock_warehouse[$row->fk_entrepot]->id = $row->rowid;
 					if ($this->hasbatch()) $this->stock_warehouse[$row->fk_entrepot]->detail_batch=Productbatch::findAll($this->db,$row->rowid,1);
 					$this->stock_reel+=$row->reel;
 					$i++;
-- 
GitLab