IMP mrp_bom_add Optionally replace all existing lines on BoM for the selected product.

This commit is contained in:
Jared Kipe
2019-05-17 09:27:08 -07:00
committed by Mishael Navarro
parent ec9a0417cb
commit 301f741591
4 changed files with 47 additions and 3 deletions

View File

@@ -2,14 +2,19 @@
'name': 'BoM Mass Add',
'author': 'Hibou Corp. <hello@hibou.io>',
'category': 'Hidden',
'version': '12.0.1.0.0',
'version': '12.0.1.1.0',
'description':
"""
Bill of Materials Mass Component Adder
======================================
Helper to add all variants of a Product to a BoM filtered by the attributes on that product.
Adds a button under BoM components named "Add Bulk". This lets you configure a product
Adds a button under BoM components named "Add Bulk". This lets you select a product template
and add one BoM line for every variant of that product.
Optionally you can choose to only add the variants that could be selected by the attributes
on the produced product itself. You can also replace all of the lines for the slected product
to allow you to quickly re-configure qty or operation for bulk products.
""",
'depends': [
'mrp',

View File

@@ -113,3 +113,25 @@ class TestMRPBOMAdd(TransactionCase):
wizard.add_variants()
self.assertEqual(len(self.bom.bom_line_ids), 2)
def test_replace(self):
# Ensure BoM is empty
self.assertEqual(len(self.bom.bom_line_ids), 0)
wizard = self.env['mrp.bom.add'].create({
'bom_id': self.bom.id,
})
wizard.product_tmpl_id = self.component
self.assertEqual(wizard.product_variant_count, 4)
wizard.add_variants()
self.assertEqual(len(self.bom.bom_line_ids), 4)
# Additive
wizard.add_variants()
self.assertEqual(len(self.bom.bom_line_ids), 8)
# remove those 8 lines when adding new ones
wizard.replace_existing = True
wizard.add_variants()
self.assertEqual(len(self.bom.bom_line_ids), 4)

View File

@@ -8,9 +8,14 @@ class ProgramOutputView(models.TransientModel):
bom_id = fields.Many2one('mrp.bom', string='Bill of Materials')
bom_product_tmpl_id = fields.Many2one('product.template', string='BoM Product', related='bom_id.product_tmpl_id')
product_tmpl_id = fields.Many2one('product.template', string='Product')
product_variant_count = fields.Integer(string='Variant Count', compute='_compute_variant_count')
product_variant_count = fields.Integer(string='Variant Count',
compute='_compute_variant_count')
limit_possible = fields.Boolean(string='Limit to possible variants',
help='Only add variants that can be selected by BoM Product')
replace_existing = fields.Boolean(string='Replace existing BoM lines for this template.')
existing_line_count = fields.Integer(string='Existing Lines',
help='Remove any existing lines for this Product Template.',
compute='_compute_variant_count')
product_qty = fields.Float(string='Quantity to Consume', default=1.0)
product_uom_id = fields.Many2one('uom.uom', string='Consume Unit of Measure')
bom_routing_id = fields.Many2one('mrp.routing', related='bom_id.routing_id')
@@ -22,9 +27,12 @@ class ProgramOutputView(models.TransientModel):
self.ensure_one()
if not self.product_tmpl_id or not self.bom_product_tmpl_id:
self.product_variant_count = 0
self.existing_line_count = 0
else:
products = self._compute_product_ids()
lines = self._compute_existing_line_ids()
self.product_variant_count = len(products)
self.existing_line_count = len(lines)
@api.onchange('product_tmpl_id')
def _onchange_default_product_uom(self):
@@ -51,7 +59,14 @@ class ProgramOutputView(models.TransientModel):
products += p
return products
def _compute_existing_line_ids(self):
return self.bom_id.bom_line_ids.filtered(lambda l: l.product_id.product_tmpl_id == self.product_tmpl_id)
def add_variants(self):
if self.replace_existing:
lines = self._compute_existing_line_ids()
lines.unlink()
attribute_values = self._compute_attribute_value_ids()
products = self._compute_product_ids()
if not self.product_uom_id:

View File

@@ -14,6 +14,8 @@
<field name="product_tmpl_id"/>
<field name="limit_possible"/>
<field name="product_variant_count"/>
<field name="replace_existing"/>
<field name="existing_line_count"/>
</group>
<group>
<field name="product_qty"/>