Skip to content

Commit

Permalink
[IMP] Improved code for scaffolding bom
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikul-OSI committed Aug 6, 2024
1 parent 6058526 commit afef919
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
61 changes: 60 additions & 1 deletion product_configurator_mrp/models/mrp.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions product_configurator_mrp/models/product_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 25 additions & 0 deletions product_configurator_mrp/views/mrp_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@
>{"search_default_todo": True, "default_company_id": allowed_company_ids[0], "custom_create_variant": True}</field>
</record>

<record id="mrp_bom_tree_view" model="ir.ui.view">
<field name="name">product.config.mrp.bom.tree.view</field>
<field name="model">mrp.bom</field>
<field name="inherit_id" ref="mrp.mrp_bom_tree_view" />
<field name="arch" type="xml">
<field name="code" position="after">
<field name="scaffolding_bom" optional="show" />
<field name="existing_scaffolding_bom" optional="hide" />
</field>
</field>
</record>

<record id="mrp_bom_form_view" model="ir.ui.view">
<field name="name">product.config.mrp.bom.form.view</field>
<field name="model">mrp.bom</field>
Expand Down Expand Up @@ -104,6 +116,19 @@
options="{'create_edit': True, 'open': 'True'}"
/>
</xpath>
<field name="code" position="after">
<field name="scaffolding_bom" invisible="product_id" />
<field name="existing_scaffolding_bom" invisible="1" />
</field>
<xpath expr="//form/sheet/group" position="before">
<div
invisible="not existing_scaffolding_bom"
class="alert alert-danger text-center"
role="alert"
>
<p><strong
>There is an active scaffolding BoM for this product.</strong></p></div>
</xpath>
</field>
</record>

Expand Down

0 comments on commit afef919

Please sign in to comment.