Skip to content
Snippets Groups Projects
Commit 62b47b31 authored by Adrien Widart's avatar Adrien Widart
Browse files

[FIX] {purchase_,}mrp: prevent kit structure changes

When changing the structure of a kit, it lead to undesirable behaviors
on Odoo

Issue 01:
1. Create 3 products P_kit, P_compo01, P_compo02
    - Type: Storable
    - Category: PC
2. Create a bill of materials:
    - Product: P_kit
    - Type: Kit
    - Components:
        - 1 x P_compo01
3. Create a purchase order PO with 2 x P_kit
4. Confirm PO and process the receipt
5. Edit the bill of materials of P_kit:
    - Add 1 x P_compo02 in components
6. Return 1 x P_compo01
7. Go back to the PO

Error: The received quantity is 0 while it should be 1.0

When processing the return, the received quantity is recomputed, which
lead to:
https://github.com/odoo/odoo/blob/59fcb31f5a0b8136dae26b70ca0087f9b5cf3d24/addons/purchase_mrp/models/purchase_mrp.py#L29
However, since in the mean time the user added a new line in the BoM,
`_compute_kit_quantities` doesn't find any associated SM
(`bom_line_moves` is empty in [3]) and thus returns 0.

Issue 02:
(Need account_accountant. Use demo data)
1. Create a product category PC:
    - Costing Method: AVCO
    - Inventory Valuation: Automated
    - Set up the Price Difference Account
2. Create 3 products P_kit, P_compo01, P_compo02
    - Type: Storable
    - Category: PC
3. Create a bill of materials:
    - Product: P_kit
    - Type: Kit
    - Components:
        - 1 x P_compo01
4. Create a purchase order PO with 1 x P_kit
5. Confirm PO and process the receipt
6. Edit the bill of materials of P_kit:
    - Add 1 x P_compo02 in components
7. Create and Post the bill

Error: an Odoo Error is raised "ZeroDivisionError: float division by
zero"

While confirming the bill, some anglo saxo lines are generated. To do
so, the valuation of the kit is computed: [1]. In the above case, it
will lead to [2]. However, since in the mean time the user added a new
line in the BoM, `_compute_kit_quantities` doesn't find any associated
SM (`bom_line_moves` is empty in [3]) and thus returns 0. Back to [1],
the quantity is used to divide the total price -> it will raise an error
if this quantity is zero

Suggestion:
Such situations should not happen: once a product is used at least once,
it should not become a kit nor have a new structure (if it was already a
kit). Otherwise, `_compute_kit_quantities` will not correctly work since
it is not possible to take the BoM changes into consideration.

[1]
https://github.com/odoo/odoo/blob/abfe37fcea5b20f77799d9331d4d011530880669/addons/purchase_stock/models/account_invoice.py#L72-L74
[2]
https://github.com/odoo/odoo/blob/75191404788ab83645ee35b779991ea6fcdfa406/addons/purchase_mrp/models/stock_move.py#L19
[3]
https://github.com/odoo/odoo/blob/7d1af314320547ab5e37c1d97cad22992c98565b/addons/mrp/models/stock_move.py#L275-L294



OPW-2780855

closes odoo/odoo#86821

X-original-commit: 33fa43f7
Signed-off-by: default avatarWilliam Henrotin (whe) <whe@odoo.com>
Signed-off-by: default avatarAdrien Widart <awt@odoo.com>
parent c721d069
No related branches found
No related tags found
No related merge requests found
......@@ -4417,6 +4417,15 @@ msgid ""
"date, please correct this to save the work order."
msgstr ""
#. module: mrp
#: code:addons/mrp/models/mrp_bom.py:0
#, python-format
msgid ""
"The product has already been used at least once, editing its structure may "
"lead to undesirable behaviours. You should rather archive the product and "
"create a new one with a new bill of materials."
msgstr ""
#. module: mrp
#: code:addons/mrp/models/mrp_production.py:0
#, python-format
......
......@@ -132,6 +132,18 @@ class MrpBom(models.Model):
if sum(bom.byproduct_ids.mapped('cost_share')) > 100:
raise ValidationError(_("The total cost share for a BoM's by-products cannot exceed 100."))
@api.onchange('bom_line_ids', 'product_qty')
def onchange_bom_structure(self):
if self.type == 'phantom' and self._origin and self.env['stock.move'].search([('bom_line_id', 'in', self._origin.bom_line_ids.ids)], limit=1):
return {
'warning': {
'title': _('Warning'),
'message': _(
'The product has already been used at least once, editing its structure may lead to undesirable behaviours. '
'You should rather archive the product and create a new one with a new bill of materials.'),
}
}
@api.onchange('product_uom_id')
def onchange_product_uom_id(self):
res = {}
......
......@@ -41,6 +41,14 @@ msgstr ""
msgid "Manufacturing Source of %s"
msgstr ""
#. module: purchase_mrp
#: code:addons/purchase_mrp/models/stock_move.py:0
#, python-format
msgid ""
"Odoo is not able to generate the anglo saxon entries. The total valuation of"
" %s is zero."
msgstr ""
#. module: purchase_mrp
#: model:ir.model,name:purchase_mrp.model_mrp_production
msgid "Production Order"
......
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models
from odoo import _, models
from odoo.tools.float_utils import float_is_zero
from odoo.exceptions import UserError
class StockMove(models.Model):
......@@ -25,4 +27,6 @@ class StockMove(models.Model):
}
valuation_total_qty = self._compute_kit_quantities(related_aml.product_id, order_qty, kit_bom, filters)
valuation_total_qty = kit_bom.product_uom_id._compute_quantity(valuation_total_qty, related_aml.product_id.uom_id)
if float_is_zero(valuation_total_qty, precision_rounding=related_aml.product_uom_id.rounding or related_aml.product_id.uom_id.rounding):
raise UserError(_('Odoo is not able to generate the anglo saxon entries. The total valuation of %s is zero.') % related_aml.product_id.display_name)
return valuation_price_unit_total, valuation_total_qty
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment