fixup! [ADD] mrp_lot_number_propagation

This commit is contained in:
Sébastien Alix
2022-10-22 13:01:04 +02:00
committed by Thierry Ducrest
parent f1e4bfe52c
commit 55eccc2e4b
7 changed files with 148 additions and 30 deletions

View File

@@ -1,4 +1,6 @@
from . import mrp_bom from . import mrp_bom
from . import mrp_bom_line from . import mrp_bom_line
from . import mrp_production from . import mrp_production
from . import product_product
from . import product_template
from . import stock_move from . import stock_move

View File

@@ -1,7 +1,8 @@
# Copyright 2022 Camptocamp SA # Copyright 2022 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from odoo import api, fields, models, tools from odoo import _, api, fields, models, tools
from odoo.exceptions import ValidationError
class MrpBom(models.Model): class MrpBom(models.Model):
@@ -72,3 +73,16 @@ class MrpBom(models.Model):
def onchange_display_lot_number_propagation(self): def onchange_display_lot_number_propagation(self):
if not self.display_lot_number_propagation: if not self.display_lot_number_propagation:
self.lot_number_propagation = False self.lot_number_propagation = False
@api.constrains("lot_number_propagation")
def _check_propagate_lot_number(self):
for bom in self:
if not bom.lot_number_propagation:
continue
if not bom.bom_line_ids.filtered("propagate_lot_number"):
raise ValidationError(
_(
"With 'Lot Number Propagation' enabled, a line has "
"to be configured with the 'Propagate Lot Number' option."
)
)

View File

@@ -26,12 +26,7 @@ class MrpBomLine(models.Model):
and line.bom_id.lot_number_propagation and line.bom_id.lot_number_propagation
) )
@api.constrains( @api.constrains("propagate_lot_number")
"propagate_lot_number",
"bom_id.lot_number_propagation",
"product_id.tracking",
"bom_id.product_tmpl_id.tracking",
)
def _check_propagate_lot_number(self): def _check_propagate_lot_number(self):
""" """
This function should check: This function should check:
@@ -42,10 +37,11 @@ class MrpBomLine(models.Model):
tracking type as the finished product tracking type as the finished product
""" """
for line in self: for line in self:
if not line.bom_id.lot_number_propagation:
continue
lines_to_propagate = line.bom_id.bom_line_ids.filtered( lines_to_propagate = line.bom_id.bom_line_ids.filtered(
lambda o: o.propagate_lot_number lambda o: o.propagate_lot_number
) )
if line.bom_id.lot_number_propagation:
if len(lines_to_propagate) > 1: if len(lines_to_propagate) > 1:
raise ValidationError( raise ValidationError(
_( _(

View File

@@ -0,0 +1,13 @@
# Copyright 2022 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from odoo import api, models
class ProductProduct(models.Model):
_inherit = "product.product"
@api.constrains("tracking")
def _check_bom_propagate_lot_number(self):
for product in self:
product.product_tmpl_id._check_bom_propagate_lot_number()

View File

@@ -0,0 +1,42 @@
# Copyright 2022 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from odoo import _, api, models
from odoo.exceptions import ValidationError
class ProductTemplate(models.Model):
_inherit = "product.template"
@api.constrains("tracking")
def _check_bom_propagate_lot_number(self):
"""Block tracking type updates if the product is used by a BoM."""
for product in self:
if product.tracking == "serial":
continue
# Check BoMs
for bom in product.bom_ids:
if bom.lot_number_propagation:
raise ValidationError(
_(
"A BoM propagating serial numbers requires "
"this product to be tracked as such."
)
)
# Check lines of BoMs
bom_lines = self.env["mrp.bom.line"].search(
[
("product_id", "in", product.product_variant_ids.ids),
("propagate_lot_number", "=", True),
("bom_id.lot_number_propagation", "=", True),
]
)
if bom_lines:
boms = "\n- ".join(bom_lines.mapped("bom_id.display_name"))
boms = "\n- " + boms
raise ValidationError(
_(
"This component is configured to propagate its "
"serial number in the following Bill of Materials:{boms}'"
).format(boms=boms)
)

View File

@@ -2,6 +2,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
from odoo.tests.common import Form
from .common import Common from .common import Common
@@ -13,24 +14,70 @@ class TestMrpBom(Common):
self.assertFalse(self.bom.display_lot_number_propagation) self.assertFalse(self.bom.display_lot_number_propagation)
def test_bom_line_check_propagate_lot_number_multi(self): def test_bom_line_check_propagate_lot_number_multi(self):
self.bom.lot_number_propagation = True form = Form(self.bom)
form.lot_number_propagation = True
# Flag more than one line to propagate # Flag more than one line to propagate
for i in range(len(form.bom_line_ids)):
line_form = form.bom_line_ids.edit(i)
line_form.propagate_lot_number = True
line_form.save()
with self.assertRaisesRegex(ValidationError, "Only one BoM"): with self.assertRaisesRegex(ValidationError, "Only one BoM"):
self.bom.bom_line_ids.write({"propagate_lot_number": True}) form.save()
def test_bom_line_check_propagate_lot_number_not_tracked(self): def test_bom_line_check_propagate_lot_number_not_tracked(self):
self.bom.lot_number_propagation = True form = Form(self.bom)
form.lot_number_propagation = True
# Flag a line that can't be propagated # Flag a line that can't be propagated
line_form = form.bom_line_ids.edit(2) # line without tracking
line_form.propagate_lot_number = True
line_form.save()
with self.assertRaisesRegex(ValidationError, "Only components tracked"): with self.assertRaisesRegex(ValidationError, "Only components tracked"):
self.line_no_tracking.propagate_lot_number = True form.save()
def test_bom_line_check_propagate_lot_number_tracked_by_lot(self): def test_bom_line_check_propagate_lot_number_tracked_by_lot(self):
self.bom.lot_number_propagation = True form = Form(self.bom)
form.lot_number_propagation = True
# Flag a line tracked by lot (not SN) which is not supported # Flag a line tracked by lot (not SN) which is not supported
line_form = form.bom_line_ids.edit(1)
line_form.propagate_lot_number = True
line_form.save()
with self.assertRaisesRegex(ValidationError, "Only components tracked"): with self.assertRaisesRegex(ValidationError, "Only components tracked"):
self.line_tracked_by_lot.propagate_lot_number = True form.save()
def test_bom_line_check_propagate_lot_number_same_tracking(self): def test_bom_line_check_propagate_lot_number_same_tracking(self):
self.bom.lot_number_propagation = True form = Form(self.bom)
form.lot_number_propagation = True
# Flag a line whose tracking type is the same than the finished product # Flag a line whose tracking type is the same than the finished product
self.line_tracked_by_sn.propagate_lot_number = True line_form = form.bom_line_ids.edit(0)
line_form.propagate_lot_number = True
line_form.save()
form.save()
def test_bom_check_propagate_lot_number(self):
# Configure the BoM to propagate the lot/SN without enabling any line
with self.assertRaisesRegex(ValidationError, "a line has to be configured"):
self.bom.lot_number_propagation = True
def test_reset_tracking_on_bom_product(self):
# Configure the BoM to propagate the lot/SN
with Form(self.bom) as form:
form.lot_number_propagation = True
line_form = form.bom_line_ids.edit(0) # Line tracked by SN
line_form.propagate_lot_number = True
line_form.save()
form.save()
# Reset the tracking on the finished product
with self.assertRaisesRegex(ValidationError, "A BoM propagating"):
self.bom.product_tmpl_id.tracking = "none"
def test_reset_tracking_on_bom_component(self):
# Configure the BoM to propagate the lot/SN
with Form(self.bom) as form:
form.lot_number_propagation = True
line_form = form.bom_line_ids.edit(0) # Line tracked by SN
line_form.propagate_lot_number = True
line_form.save()
form.save()
# Reset the tracking on the component which propagates the SN
with self.assertRaisesRegex(ValidationError, "This component is"):
self.line_tracked_by_sn.product_id.tracking = "none"

View File

@@ -12,8 +12,12 @@ class TestMrpProduction(Common):
def setUpClass(cls): def setUpClass(cls):
super().setUpClass() super().setUpClass()
# Configure the BoM to propagate lot number # Configure the BoM to propagate lot number
cls.bom.lot_number_propagation = True with Form(cls.bom) as form:
cls.line_tracked_by_sn.propagate_lot_number = True form.lot_number_propagation = True
line_form = form.bom_line_ids.edit(0) # Line tracked by SN
line_form.propagate_lot_number = True
line_form.save()
form.save()
with Form(cls.env["mrp.production"]) as form: with Form(cls.env["mrp.production"]) as form:
form.bom_id = cls.bom form.bom_id = cls.bom
cls.order = form.save() cls.order = form.save()