diff --git a/addons/product_expiry/models/stock_move_line.py b/addons/product_expiry/models/stock_move_line.py index b299030fae6..2da77544ae8 100644 --- a/addons/product_expiry/models/stock_move_line.py +++ b/addons/product_expiry/models/stock_move_line.py @@ -9,7 +9,8 @@ from odoo import api, fields, models class StockMoveLine(models.Model): _inherit = "stock.move.line" - expiration_date = fields.Datetime(string='Expiration Date', compute='_compute_expiration_date', store=True, + 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.') @@ -18,7 +19,8 @@ class StockMoveLine(models.Model): 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) + if not move_line.expiration_date: + move_line.expiration_date = fields.Datetime.today() + datetime.timedelta(days=move_line.product_id.expiration_time) else: move_line.expiration_date = False @@ -31,6 +33,16 @@ class StockMoveLine(models.Model): else: self.expiration_date = False + @api.onchange('product_id', 'product_uom_id') + def _onchange_product_id(self): + res = super()._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 + def _assign_production_lot(self, lot): super()._assign_production_lot(lot) self.lot_id._update_date_values(self[0].expiration_date) diff --git a/addons/product_expiry/tests/test_stock_production_lot.py b/addons/product_expiry/tests/test_stock_production_lot.py index 170846f546c..dc641d57bd2 100644 --- a/addons/product_expiry/tests/test_stock_production_lot.py +++ b/addons/product_expiry/tests/test_stock_production_lot.py @@ -293,12 +293,12 @@ class TestStockProductionLot(TestStockCommon): receipt.action_confirm() # Defines a date during the receipt. - move = receipt.move_ids_without_package[0] - line = move.move_line_ids[0] - self.assertEqual(move.use_expiration_date, True) - line.lot_name = 'Apple Box #2' - line.expiration_date = expiration_date - line.qty_done = 4 + move_form = Form(receipt.move_ids_without_package, view="stock.view_stock_move_operations") + with move_form.move_line_ids.new() as line: + line.lot_name = 'Apple Box #2' + line.expiration_date = expiration_date + line.qty_done = 4 + move = move_form.save() receipt._action_done() # Get back the lot created when the picking was done...