From 58df6c86e06304b2c5f020e0fb93988e9e388fe0 Mon Sep 17 00:00:00 2001
From: William Henrotin <whe@odoo.com>
Date: Thu, 24 Oct 2019 09:48:21 +0000
Subject: [PATCH] [FIX] mrp: new stock move line in traceability report

Before this commit, The addition of new stock move lines in a
finished production via the unlock button did not impact the
traceability report. This was due to the fact that the new sml
has not been linked to the finished_lot_id.
---
 addons/mrp/models/stock_move.py | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/addons/mrp/models/stock_move.py b/addons/mrp/models/stock_move.py
index a3493ecf8247..0817dbedb573 100644
--- a/addons/mrp/models/stock_move.py
+++ b/addons/mrp/models/stock_move.py
@@ -17,6 +17,22 @@ class StockMoveLine(models.Model):
         help="Informative, not used in matching")
     done_move = fields.Boolean('Move Done', related='move_id.is_done', readonly=False, store=True)  # TDE FIXME: naming
 
+    def create(self, values):
+        res = super(StockMoveLine, self).create(values)
+        for line in res:
+            # If the line is added in a done production, we need to map it
+            # manually to the produced move lines in order to see them in the
+            # traceability report
+            if line.move_id.raw_material_production_id and line.state == 'done':
+                mo = line.move_id.raw_material_production_id
+                if line.lot_produced_ids:
+                    produced_move_lines = mo.move_finished_ids.move_line_ids.filtered(lambda sml: sml.lot_id in line.lot_produced_ids)
+                    line.produce_line_ids = [(6, 0, produced_move_lines.ids)]
+                else:
+                    produced_move_lines = mo.move_finished_ids.move_line_ids
+                    line.produce_line_ids = [(6, 0, produced_move_lines.ids)]
+        return res
+
     def _get_similar_move_lines(self):
         lines = super(StockMoveLine, self)._get_similar_move_lines()
         if self.move_id.production_id:
@@ -272,4 +288,4 @@ class StockMove(models.Model):
             # doesn't make sense.
             return min(qty_ratios) // 1
         else:
-            return 0.0
\ No newline at end of file
+            return 0.0
-- 
GitLab