diff --git a/addons/stock/models/product.py b/addons/stock/models/product.py index cd838f35c93..0a0c6faf05a 100644 --- a/addons/stock/models/product.py +++ b/addons/stock/models/product.py @@ -471,7 +471,7 @@ class Product(models.Model): self.ensure_one() action = self.env.ref('stock.action_production_lot_form').read()[0] action['domain'] = [('product_id', '=', self.id)] - action['context'] = {'default_product_id': self.id} + action['context'] = {'default_product_id': self.id, 'set_product_readonly': True} return action # Be aware that the exact same function exists in product.template @@ -769,11 +769,11 @@ class ProductTemplate(models.Model): self.ensure_one() action = self.env.ref('stock.action_production_lot_form').read()[0] action['domain'] = [('product_id.product_tmpl_id', '=', self.id)] + action['context'] = {'default_product_tmpl_id': self.id} if self.product_variant_count == 1: - action['context'] = { + action['context'].update({ 'default_product_id': self.product_variant_id.id, - } - + }) return action def action_product_tmpl_forecast_report(self): diff --git a/addons/stock/models/stock_production_lot.py b/addons/stock/models/stock_production_lot.py index 82c50565c39..17f0275c84e 100644 --- a/addons/stock/models/stock_production_lot.py +++ b/addons/stock/models/stock_production_lot.py @@ -3,6 +3,7 @@ from odoo import api, fields, models, _ from odoo.exceptions import UserError +from odoo.osv import expression class ProductionLot(models.Model): @@ -16,7 +17,7 @@ class ProductionLot(models.Model): ref = fields.Char('Internal Reference', help="Internal reference number in case it differs from the manufacturer's lot/serial number") product_id = fields.Many2one( 'product.product', 'Product', - domain=[('type', '=', 'product')], required=True) + domain=lambda self: self._domain_product_id(), required=True) product_uom_id = fields.Many2one( 'uom.uom', 'Unit of Measure', related='product_id.uom_id', store=True, readonly=False) @@ -29,6 +30,12 @@ class ProductionLot(models.Model): ('name_ref_uniq', 'unique (name, product_id)', 'The combination of serial number and product must be unique !'), ] + def _domain_product_id(self): + domain = [('type', '=', 'product')] + if self.env.context.get('default_product_tmpl_id'): + domain = expression.AND([domain, [('product_tmpl_id', '=', self.env.context['default_product_tmpl_id'])]]) + return domain + def _check_create(self): active_picking_id = self.env.context.get('active_picking_id', False) if active_picking_id: diff --git a/addons/stock/views/stock_production_lot_views.xml b/addons/stock/views/stock_production_lot_views.xml index 7caf76f7dde..937cdde3dcf 100644 --- a/addons/stock/views/stock_production_lot_views.xml +++ b/addons/stock/views/stock_production_lot_views.xml @@ -21,9 +21,7 @@ - +