[FIX] product_expiry: lot expiration_date

This commit adresses to two issues:

Since 0c83542a93dde8dcaead04fadb7db95cf627a8b4, the move line's `expiration_date`
set by the user is always overrided at the creation of the move which
means the user has to modify it after the move was created.

To fix that, the compute doesn't override the `expiration_date` if there
is already one set.
Therefore, the onchange on the product who was removed is re-added to
force to recompute the `expiration_date` if the move line's product is
changed and it has already an `expiration_date` set.

closes odoo/odoo#78189

X-original-commit: db2881c4b6dc02b68ad61c1be8464d874f9115db
Signed-off-by: William Henrotin <Whenrow@users.noreply.github.com>
Signed-off-by: Steve Van Essche <svs-odoo@users.noreply.github.com>
This commit is contained in:
svs-odoo
2021-10-14 08:12:43 +00:00
parent 714f2b49ce
commit cdcdcd882a
2 changed files with 20 additions and 8 deletions
@@ -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)
@@ -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...