diff --git a/product_configurator_mrp/models/mrp.py b/product_configurator_mrp/models/mrp.py index 8e8a73356c..bbcb428380 100644 --- a/product_configurator_mrp/models/mrp.py +++ b/product_configurator_mrp/models/mrp.py @@ -1,7 +1,7 @@ # Copyright (C) 2021 Open Source Integrators # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import api, fields, models +from odoo import _, api, exceptions, fields, models class MrpProduction(models.Model): @@ -56,6 +56,19 @@ class MrpBom(models.Model): string="Configurable", readonly=True, ) + scaffolding_bom = fields.Boolean( + string="Scaffolding BoM", + default=False, + help="When checked, this BoM will serve as the main BoM used by the configurator to " + "create the product variant BoM’s. Only one BoM per product can be set as a Scaffolding BoM. " + "If no scaffolding BoM exists, the configurator will then look for a BoM that doesn’t have a " + "Product Variant to use.", + ) + existing_scaffolding_bom = fields.Boolean( + string="Existing Scaffolding BoM", + compute="_compute_existing_scaffolding_bom", + store=True, + ) @api.model def default_get(self, val_list): @@ -69,6 +82,52 @@ def default_get(self, val_list): ) return result + @api.constrains("product_tmpl_id", "scaffolding_bom") + def _check_product_tmpl_scaffolding_bom(self): + """Constraint ensures only one scaffolding BoM exists per product template""" + for rec in self: + if ( + self.search_count( + [ + ("scaffolding_bom", "=", True), + ("product_tmpl_id", "=", rec.product_tmpl_id.id), + ] + ) + > 1 + ): + raise exceptions.ValidationError( + _( + "You can only have one unarchived Scaffolding BOM for a configurable product." + ) + ) + + @api.depends("scaffolding_bom", "active", "product_tmpl_id") + def _compute_existing_scaffolding_bom(self): + for rec in self: + if ( + self.search_count( + [ + ("scaffolding_bom", "=", True), + ("active", "=", True), + ("product_tmpl_id", "=", rec.product_tmpl_id.id), + ("product_id", "=", False), + ] + ) + >= 1 + ): + rec.existing_scaffolding_bom = True + else: + rec.existing_scaffolding_bom = False + + @api.onchange("product_id") + def onchange_scaffolding_bom_product_id(self): + """onchange method to automatically set 'scaffolding_bom' + based on 'product_id'.""" + if self.product_id: + self.scaffolding_bom = False + else: + self.scaffolding_bom = True + class MrpBomLine(models.Model): _inherit = "mrp.bom.line" diff --git a/product_configurator_mrp/models/product_config.py b/product_configurator_mrp/models/product_config.py index 04b0454083..29e346559e 100644 --- a/product_configurator_mrp/models/product_config.py +++ b/product_configurator_mrp/models/product_config.py @@ -42,10 +42,20 @@ def create_get_bom(self, variant, product_tmpl_id=None, values=None): [ ("product_tmpl_id", "=", product_tmpl_id.id), ("product_id", "=", False), + ("scaffolding_bom", "=", True), ], order="sequence asc", limit=1, ) + if not parent_bom: + parent_bom = self.env["mrp.bom"].search( + [ + ("product_tmpl_id", "=", product_tmpl_id.id), + ("product_id", "=", False), + ], + order="sequence asc", + limit=1, + ) bom_type = parent_bom and parent_bom.type or "normal" bom_lines = [] if not parent_bom: diff --git a/product_configurator_mrp/views/mrp_view.xml b/product_configurator_mrp/views/mrp_view.xml index 5253e71533..6f55d22217 100644 --- a/product_configurator_mrp/views/mrp_view.xml +++ b/product_configurator_mrp/views/mrp_view.xml @@ -69,6 +69,18 @@ >{"search_default_todo": True, "default_company_id": allowed_company_ids[0], "custom_create_variant": True} + + product.config.mrp.bom.tree.view + mrp.bom + + + + + + + + + product.config.mrp.bom.form.view mrp.bom @@ -104,6 +116,19 @@ options="{'create_edit': True, 'open': 'True'}" /> + + + + + + +