Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0][ADD] mrp_production_force_bom #1294

Open
wants to merge 1 commit into
base: 15.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions mrp_production_force_bom/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
===================
Mrp BOM Enforcement
===================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fmanufacture-lightgray.png?logo=github
:target: https://github.com/OCA/manufacture/tree/15.0/mrp_bom_enforcement
:alt: OCA/manufacture
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/manufacture-15-0/manufacture-15-0-mrp_bom_enforcement
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/129/15.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

This module restricts adding components in MO if the bom_id is not set.

Background
~~~~~~~~~~~

Users previously could manually populate manufacturing orders, regardless of an existing BOM for a product.
This often led to inaccuracies. This module enforces the use of the set BOM when creating manufacturing orders,
ensuring consistency and reducing potential errors.

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/manufacture/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/manufacture/issues/new?body=module:%20mrp_bom_enforcement%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Quartile Limited

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/manufacture <https://github.com/OCA/manufacture/tree/15.0/mrp_bom_enforcement>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions mrp_production_force_bom/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
15 changes: 15 additions & 0 deletions mrp_production_force_bom/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Mrp Production Force BOM",
"version": "15.0.1.0.0",
"category": "Manufacturing",
"author": "Quartile Limited , Odoo Community Association (OCA)",
"depends": ["mrp"],
"license": "AGPL-3",
"website": "https://github.com/OCA/manufacture",
"data": [
"data/ir_config_parameter.xml",
],
"demo": ["demo/demo_ir_config_parameter.xml"],
}
8 changes: 8 additions & 0 deletions mrp_production_force_bom/data/ir_config_parameter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<!-- System Parameter for forcing BOM -->
<record id="param_mrp_force_bom_enable" model="ir.config_parameter">
<field name="key">mrp_production_force_bom.force_bom</field>
<field name="value">1</field> <!-- Enable by default -->
</record>
</odoo>
8 changes: 8 additions & 0 deletions mrp_production_force_bom/demo/demo_ir_config_parameter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<!-- System Parameter for forcing BOM -->
<record id="param_mrp_force_bom_enable" model="ir.config_parameter">
<field name="key">mrp_production_force_bom.force_bom</field>
<field name="value">0</field> <!-- Disable for tests -->
</record>
</odoo>
27 changes: 27 additions & 0 deletions mrp_production_force_bom/i18n/ja.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * mrp_production_force_bom
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 15.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-01-17 09:10+0000\n"
"PO-Revision-Date: 2024-01-17 09:10+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: mrp_production_force_bom
#: code:addons/mrp_production_force_bom/models/mrp_production.py:0
#, python-format
msgid "'%s': You cannot proceed without setting a BOM."
msgstr "'%s': 進むには部品表を設定する必要があります。"

#. module: mrp_production_force_bom
#: model:ir.model,name:mrp_production_force_bom.model_mrp_production
msgid "Production Order"
msgstr "製造オーダ"
1 change: 1 addition & 0 deletions mrp_production_force_bom/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import mrp_production
88 changes: 88 additions & 0 deletions mrp_production_force_bom/models/mrp_production.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Copyright 2023 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _, api, models
from odoo.exceptions import ValidationError


class MrpProduction(models.Model):
_inherit = "mrp.production"

@api.model
def _force_bom(self):
param = (
self.env["ir.config_parameter"]
.sudo()
.get_param("mrp_production_force_bom.force_bom")
)
if param == "1":
return True
return False

def _skip_bom_check(self):
"""Extend this method as necessary to control whether or not to check the
components against BoM.
"""
self.ensure_one()
return False

def _check_component_discrepancies(self, error_messages):
# Get all the components and their quantities using explode()
factor = (
self.product_uom_id._compute_quantity(
self.product_qty, self.bom_id.product_uom_id
)
/ self.bom_id.product_qty
)
_, lines_done = self.bom_id.explode(self.product_id, factor)
expected_components = {}
for line, data in lines_done:
product_id = line.product_id.id
expected_components[product_id] = (
expected_components.get(product_id, 0) + data["qty"]
)
# Prepare a dictionary for the components and
# their total quantities in the MO's raw materials
mo_components = {}
for move in self.move_raw_ids:
product_id = move.product_id.id
mo_components[product_id] = (
mo_components.get(product_id, 0) + move.product_qty
)
# Check for any discrepancies
for product_id, qty in expected_components.items():
if product_id not in mo_components or mo_components[product_id] != qty:
product_name = self.env["product.product"].browse(product_id).name
error_messages.append(
"'%s': Discrepancy detected for product '%s'. Expected quantity: '%s',"
" Found quantity: '%s'."
% (self.name, product_name, qty, mo_components.get(product_id, 0))
)
# Check if there are extra products in move_raw_ids not present in the BOM
for product_id in mo_components:
if product_id not in expected_components:
product_name = self.env["product.product"].browse(product_id).name
error_messages.append(
"'%s': Product '%s' is not part of the BOM components."
% (self.name, product_name)
)
return error_messages

def action_confirm(self):
if not self._force_bom():
return super(MrpProduction, self).action_confirm()
error_messages = []
for production in self:
if production._skip_bom_check():
continue

Check warning on line 77 in mrp_production_force_bom/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_force_bom/models/mrp_production.py#L77

Added line #L77 was not covered by tests
if not production.bom_id:
error_messages.append(
_("'%s': You cannot proceed without setting a BOM.")
% production.name
)
continue
# Check for discrepancies
error_messages = production._check_component_discrepancies(error_messages)
if error_messages:
raise ValidationError("\n".join(error_messages))
return super(MrpProduction, self).action_confirm()

Check warning on line 88 in mrp_production_force_bom/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_force_bom/models/mrp_production.py#L88

Added line #L88 was not covered by tests
3 changes: 3 additions & 0 deletions mrp_production_force_bom/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This module ensures that every Manufacturing Order (MO) is linked to a Bill of Materials (BOM)
before confirmation. It further restricts the addition of non-BOM products and prevents
the removal of BOM-listed products.
Loading
Loading