[IMP] purchase_mrp: cost share for kit

Be able to define a cost share on kit's components. The purpose, define
a cost repartition closer to the reality.

If the cost share is not define, it will split by the number of lines.

Part-of: odoo/odoo#99411
This commit is contained in:
Arnold Moyaux
2022-09-12 13:49:16 +02:00
parent 35d6c58f86
commit ccc1fa5d37
6 changed files with 60 additions and 1 deletions
+1
View File
@@ -14,6 +14,7 @@ It is basically used when we want to keep track of production orders generated
from purchase order.
""",
'data': [
'views/mrp_bom_views.xml',
'views/purchase_order_views.xml',
'views/mrp_production_views.xml',
'security/ir.model.access.csv',
+1
View File
@@ -4,4 +4,5 @@
from . import account_move
from . import purchase
from . import mrp_production
from . import mrp_bom
from . import stock_move
+37
View File
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models, _
from odoo.exceptions import UserError
class MrpBom(models.Model):
_inherit = 'mrp.bom'
def _check_bom_lines(self):
res = super()._check_bom_lines()
for bom in self:
if all(not bl.cost_share for bl in bom.bom_lines):
continue
if any(bl.cost_share < 0 for bl in bom.bom_lines):
raise UserError(_("Components cost share have to be positive or equals to zero."))
if sum(bom.bom_lines.mapped('cost_share')) != 100:
raise UserError(_("The total cost share for a BoM's component have to be 100"))
return res
class MrpBomLine(models.Model):
_inherit = 'mrp.bom.line'
cost_share = fields.Float(
"Cost Share (%)", digits=(5, 2), # decimal = 2 is important for rounding calculations!!
help="The percentage of the component repartition cost when purchasing a kit."
"The total of all components' cost have to be equal to 100.")
def _get_cost_share(self):
self.ensure_one()
if self.cost_share:
return fields.Float.round(self.cost_share / 100, 2)
bom = self.bom_id
bom_lines_without_cost_share = bom.bom_line_ids.filtered(lambda bl: not bl.cost_share)
return fields.Float.round(1 / len(bom_lines_without_cost_share), 2)
+7
View File
@@ -16,6 +16,13 @@ class StockMove(models.Model):
vals['purchase_line_id'] = self.purchase_line_id.id
return vals
def _get_price_unit(self):
price_unit = super()._get_price_unit()
if self.product_id == self.purchase_line_id.product_id or not self.bom_line_id:
return price_unit
cost_share = self.bom_line_id._get_cost_share()
return price_unit * cost_share
def _get_valuation_price_and_qty(self, related_aml, to_curr):
valuation_price_unit_total, valuation_total_qty = super()._get_valuation_price_and_qty(related_aml, to_curr)
boms = self.env['mrp.bom']._bom_find(related_aml.product_id, company_id=related_aml.company_id.id, bom_type='phantom')
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo><data>
<record id="mrp_bom_form_view" model="ir.ui.view">
<field name="name">mrp.bom.view.form.inherited.purchase.mrp</field>
<field name="model">mrp.bom</field>
<field name="inherit_id" ref="mrp.mrp_bom_form_view"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='bom_line_ids']/tree//field[@name='manual_consumption']" position="after">
<field name="cost_share" optional="hidden" attrs="{'column_invisible': [('parent.type','!=', 'phantom')]}"/>
</xpath>
</field>
</record>
</data></odoo>
+1 -1
View File
@@ -29,7 +29,7 @@ class StockMove(models.Model):
def _get_price_unit(self):
""" Returns the unit price for the move"""
self.ensure_one()
if self.purchase_line_id and self.product_id.id == self.purchase_line_id.product_id.id:
if self.purchase_line_id and self.product_id.id:
price_unit_prec = self.env['decimal.precision'].precision_get('Product Price')
line = self.purchase_line_id
order = line.order_id