From d1ccfdc4097debe63aa6c9d42965a7801c1331cd Mon Sep 17 00:00:00 2001
From: "Touati Djamel (otd)" <otd@odoo.com>
Date: Tue, 31 Aug 2021 16:28:25 +0000
Subject: [PATCH] =?UTF-8?q?[FIX]=20product=5Fexpiry:=20set=20the=20expirat?=
 =?UTF-8?q?ion=5Fdate=20when=20creating=20the=20=E2=80=9Cstock=5Fmove=5Fli?=
 =?UTF-8?q?ne=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Steps to reproduce the bug:
- Install inventory, purchase and product_expiry
- Create a new storable product that is tracked by lots or serial numbers and expiration date
- Go to inventory > configuration > Warehouse Management > Operations Types
- In “San Francisco: Receipts” > Enable "Create New Lots/Serial Numbers", “Pre-fill Detailed Operations”, and "Show Detailed Operations"
- Create a new RFQ > select the created product > Confirm the order
- Click on “Receipt”

Problem:
In the created “stock_move_line” the expiration date field is not set

Solution:
When we add a new "stock_move_line" and choose a product, an onchange is triggered in order to set
the expiration_date field even if we do not choose a "lot /serial number name".
So we can replace the onchange with a compute function to do the same thing in the creation.

opw-2634583

closes odoo/odoo#75806

Signed-off-by: Arnold Moyaux <amoyaux@users.noreply.github.com>
---
 .../product_expiry/models/stock_move_line.py  | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/addons/product_expiry/models/stock_move_line.py b/addons/product_expiry/models/stock_move_line.py
index b71993d89a6b..765c5052f839 100644
--- a/addons/product_expiry/models/stock_move_line.py
+++ b/addons/product_expiry/models/stock_move_line.py
@@ -9,19 +9,18 @@ from odoo import api, fields, models
 class StockMoveLine(models.Model):
     _inherit = "stock.move.line"
 
-    expiration_date = fields.Datetime(string='Expiration Date',
+    expiration_date = fields.Datetime(string='Expiration Date', compute='_compute_expiration_date', store=True,
         help='This is the date on which the goods with this Serial Number may'
         ' become dangerous and must not be consumed.')
 
-    @api.onchange('product_id', 'product_uom_id')
-    def _onchange_product_id(self):
-        res = super(StockMoveLine, self)._onchange_product_id()
-        if self.picking_type_use_create_lots:
-            if self.product_id.use_expiration_date:
-                self.expiration_date = fields.Datetime.today() + datetime.timedelta(days=self.product_id.expiration_time)
-            else:
-                self.expiration_date = False
-        return res
+    @api.depends('product_id', 'picking_type_use_create_lots')
+    def _compute_expiration_date(self):
+        for move_line in self:
+            if move_line.picking_type_use_create_lots:
+                if move_line.product_id.use_expiration_date:
+                    move_line.expiration_date = fields.Datetime.today() + datetime.timedelta(days=move_line.product_id.expiration_time)
+                else:
+                    move_line.expiration_date = False
 
     @api.onchange('lot_id')
     def _onchange_lot_id(self):
-- 
GitLab