From 8cd6c3c8da8076c6c39109df93f40d28cbdfa3ac Mon Sep 17 00:00:00 2001
From: PNO <pno@odoo.com>
Date: Thu, 6 Jan 2022 10:32:30 +0000
Subject: [PATCH] [FIX] stock: Fix quantity done on lots changing to 1

Issue:
At anypoint we should be able to call the method "_set_lot_ids" from stock.move on moves that are already done and the quantities should not change.
This is not the case for lots that get their quantity changes to 1 as if the product was tracked by serial number.

This commit should be seen a a complement to the commit: https://github.com/odoo/odoo/pull/79565

Fix:
Check the product is tracked by SN before trying to change the quantities to 1.

Related ticket: 2689724

closes odoo/odoo#100640

X-original-commit: e6fa1e24aaeaca543b9acae7f876d30def131a59
Signed-off-by: Arnold Moyaux (arm) <arm@odoo.com>
---
 addons/stock/models/stock_move.py     |  2 ++
 addons/stock/tests/test_robustness.py | 39 +++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py
index c89b5a57c6b2..263e975c5e25 100644
--- a/addons/stock/models/stock_move.py
+++ b/addons/stock/models/stock_move.py
@@ -562,6 +562,8 @@ Please change the quantity done or the rounding precision of your unit of measur
 
     def _set_lot_ids(self):
         for move in self:
+            if move.product_id.tracking != 'serial':
+                continue
             move_lines_commands = []
             if move.picking_type_id.show_reserved is False:
                 mls = move.move_line_nosuggest_ids
diff --git a/addons/stock/tests/test_robustness.py b/addons/stock/tests/test_robustness.py
index 75f78d49863c..bbeb4ec7dbb6 100644
--- a/addons/stock/tests/test_robustness.py
+++ b/addons/stock/tests/test_robustness.py
@@ -221,3 +221,42 @@ class TestRobustness(TransactionCase):
                 'location_id': move2.location_id.id,
                 'location_dest_id': move2.location_dest_id.id,
             })]})
+
+    def test_lot_quantity_remains_unchanged_after_done(self):
+        """ Make sure the method _set_lot_ids does not change the quantities of lots to 1 once they are done.
+        """
+        productA = self.env['product.product'].create({
+            'name': 'ProductA',
+            'type': 'product',
+            'categ_id': self.env.ref('product.product_category_all').id,
+            'tracking': 'lot',
+        })
+        lotA = self.env['stock.lot'].create({
+            'name': 'lotA',
+            'product_id': productA.id,
+            'company_id': self.env.company.id,
+
+        })
+        self.env['stock.quant']._update_available_quantity(productA, self.stock_location, 5, lot_id=lotA)
+        moveA = self.env['stock.move'].create({
+            'name': 'TEST_A',
+            'location_id': self.stock_location.id,
+            'location_dest_id': self.customer_location.id,
+            'product_id': productA.id,
+            'product_uom': self.uom_unit.id,
+            'product_uom_qty': 5.0,
+        })
+
+        moveA._action_confirm()
+        moveA.write({'move_line_ids': [(0, 0, {
+            'product_id': productA.id,
+            'product_uom_id': self.uom_unit.id,
+            'qty_done': 5,
+            'lot_id': lotA.id,
+            'location_id': moveA.location_id.id,
+            'location_dest_id': moveA.location_dest_id.id,
+        })]})
+        moveA._action_done()
+        moveA._set_lot_ids()
+
+        self.assertEqual(moveA.quantity_done, 5)
-- 
GitLab