Skip to content

[ADD] module_types: Implemented BOM components multiplication from sales #915

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

Draft
wants to merge 5 commits into
base: 18.0
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions module_types/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizard
17 changes: 17 additions & 0 deletions module_types/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
'name': 'Product Module type',
'version': '1.0',
'category': 'Sales/Manufacturing',
'summary': 'Enable Modular types feature for Manufacturing orders',
'depends': ['sale_mrp', 'sale_management'],
'data': [
'security/ir.model.access.csv',
'wizard/sale_module_types_wizard.xml',
'views/product_template_views.xml',
'views/mrp_bom_views.xml',
'views/sales_order_views.xml',
'views/stock_move_views.xml'
],
'installable': True,
'license': 'LGPL-3'
}
6 changes: 6 additions & 0 deletions module_types/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from . import module_types
from . import product_template
from . import mrp_bom
from . import sales_order_line
from . import sales_order
from . import stock_move
9 changes: 9 additions & 0 deletions module_types/models/module_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from odoo import fields, models


class ModuleTypes(models.Model):
_name = "module.types"
_description = "Module Types"

name = fields.Char("Module type name", required=True)
value = fields.Integer("Module type value", default=0)
42 changes: 42 additions & 0 deletions module_types/models/mrp_bom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from odoo import models, fields, api


class MrpBom(models.Model):
_inherit = 'mrp.bom'

module_type_ids = fields.Many2many(
'module.types',
string="Available Module Types",
related='product_tmpl_id.module_types',
readonly=True
)

@api.onchange('product_tmpl_id')
def _onchange_parent_product(self):
if self.bom_line_ids:
for line in self.bom_line_ids:
line.module_types_id = False


class MrpBomLine(models.Model):
_inherit = 'mrp.bom.line'

module_types_id = fields.Many2one(
'module.types',
string="Module Type",
domain="[('id', 'in', available_module_type_ids)] or []"
)

available_module_type_ids = fields.Many2many(
'module.types',
compute='_compute_available_module_type_ids',
store=False
)

@api.depends('bom_id.product_tmpl_id')
def _compute_available_module_type_ids(self):
for line in self:
if line.bom_id.product_tmpl_id:
line.available_module_type_ids = line.bom_id.product_tmpl_id.module_types
else:
line.available_module_type_ids = False
7 changes: 7 additions & 0 deletions module_types/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from odoo import fields, models


class ProductTemplate(models.Model):
_inherit = "product.template"

module_types = fields.Many2many("module.types", string="Module types")
14 changes: 14 additions & 0 deletions module_types/models/sales_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from odoo import models


class SaleOrder(models.Model):
_inherit = 'sale.order'

def action_confirm(self):
res = super().action_confirm()
for production in self.mrp_production_ids:
selected_order_line = self.order_line.filtered(lambda line: line.product_template_id == production.product_tmpl_id and line.product_uom_qty == production.product_qty)
for stock_line in production.move_raw_ids.filtered(lambda component: component.module_type_id):
have_module_types = bool(selected_order_line.product_template_id.module_types)
stock_line.product_uom_qty *= stock_line.module_type_id.value if have_module_types else 0
return res
14 changes: 14 additions & 0 deletions module_types/models/sales_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from odoo import api, fields, models


class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

have_module_types = fields.Integer(
compute="_compute_module_types_count"
)

@api.depends('product_template_id.module_types')
def _compute_module_types_count(self):
for line in self:
line.have_module_types = bool(line.product_id.module_types)
15 changes: 15 additions & 0 deletions module_types/models/stock_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from odoo import api, fields, models


class StockMove(models.Model):
_inherit = 'stock.move'

module_type_id = fields.Many2one(
comodel_name='module.types',
compute="_compute_modular_type_id",
string='Modular Type')

@api.depends('production_id.bom_id.bom_line_ids')
def _compute_modular_type_id(self):
for line in self:
line.module_type_id = line.raw_material_production_id.bom_id.bom_line_ids.filtered(lambda bom_line: bom_line.product_id == line.product_id).module_types_id
4 changes: 4 additions & 0 deletions module_types/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
module_types.access_module_types,access_module_types,module_types.model_module_types,base.group_user,1,1,1,1
module_types.access_sale_module_type_wizard,access_sale_module_type_wizard,module_types.model_sale_module_type_wizard,base.group_user,1,1,1,1
module_types.access_sale_module_type_wizard_line,access_sale_module_type_wizard_line,module_types.model_sale_module_type_wizard_line,base.group_user,1,1,1,1
18 changes: 18 additions & 0 deletions module_types/views/mrp_bom_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<odoo>

<record id="view_mrp_bom_form_inherit_properties" model="ir.ui.view">
<field name="name">res.mrp.bom.form.inherit.module_types</field>
<field name="model">mrp.bom</field>
<field name="inherit_id" ref="mrp.mrp_bom_form_view"/>
<field name="arch" type="xml">
<xpath expr="//page[@name='components']//field[@name='product_qty']" position="before">
<field
name="module_types_id"
options="{'no_create': true}"
/>
</xpath>
</field>
</record>

</odoo>
15 changes: 15 additions & 0 deletions module_types/views/product_template_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<odoo>

<record id="view_product_form_inherit_properties" model="ir.ui.view">
<field name="name">res.product.form.inherit.module_types</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view"/>
<field name="arch" type="xml">
<xpath expr="//page[@name='general_information']//group[@name='group_general']" position="inside">
<field name="module_types" widget="many2many_tags" invisible="type != 'consu'"/>
</xpath>
</field>
</record>

</odoo>
20 changes: 20 additions & 0 deletions module_types/views/sales_order_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<odoo>

<record id="view_order_line_form_inherit_module" model="ir.ui.view">
<field name="name">sale.order.line.form.module.button</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='product_template_id']" position="after">
<button
name="%(action_modular_types_wizard)d"
type="action"
icon="fa-flask"
invisible="have_module_types == False"
/>
</xpath>
</field>
</record>

</odoo>
13 changes: 13 additions & 0 deletions module_types/views/stock_move_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="mrp_production_form_view" model="ir.ui.view">
<field name="name">mrp.production.form.view.inherit.modular.types</field>
<field name="model">mrp.production</field>
<field name="inherit_id" ref="mrp.mrp_production_form_view" />
<field name="arch" type="xml">
<xpath expr="//page[@name='components']//field[@name='product_uom_qty']" position="before">
<field name="module_type_id"/>
</xpath>
</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions module_types/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import sale_module_types_wizard
43 changes: 43 additions & 0 deletions module_types/wizard/sale_module_types_wizard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from odoo import Command, api, fields, models


class SaleModuleTypeWizard(models.TransientModel):
_name = "sale.module.type.wizard"
_description = "Wizard to set Module Types for Sales Order Line"

wizard_lines = fields.One2many(
comodel_name='sale.module.type.wizard.line',
inverse_name='wizard_id',
string='Modular Wizard Lines'
)

@api.model
def default_get(self, fields_list):
res = super().default_get(fields_list)
sales_order_line = self.env['sale.order.line'].browse(self.env.context.get('active_id'))
if sales_order_line.product_template_id:
res.update({
'wizard_lines': [
Command.create({
'module_type_id': modular_type.id,
'value': modular_type.value
})
for modular_type in sales_order_line.product_template_id.module_types
],
})
return res

def action_confirm(self):
for lines in self.wizard_lines:
lines.module_type_id.write({
'value': lines.value
})


class SaleModuleTypeWizardLine(models.TransientModel):
_name = "sale.module.type.wizard.line"
_description = "Wizard Lines for Module Types"

wizard_id = fields.Many2one('sale.module.type.wizard')
module_type_id = fields.Many2one('module.types', string="Module Type")
value = fields.Integer()
30 changes: 30 additions & 0 deletions module_types/wizard/sale_module_types_wizard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<odoo>

<record id="view_sale_module_type_wizard_form" model="ir.ui.view">
<field name="name">sale.module.type.wizard.form</field>
<field name="model">sale.module.type.wizard</field>
<field name="arch" type="xml">
<form string="Set Modular Type Values">
<field name="wizard_lines">
<list editable="bottom">
<field name="module_type_id"/>
<field name="value"/>
</list>
</field>
<footer>
<button string="Add" type="object" name="action_confirm" class="oe_highlight"/>
<button string="Cancel" class="oe_link" special="cancel"/>
</footer>
</form>
</field>
</record>

<record id="action_modular_types_wizard" model="ir.actions.act_window">
<field name="name">Set Modular Types Values</field>
<field name="res_model">sale.module.type.wizard</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>

</odoo>