From 301f74159176b499e80194c06034b40c39e6082c Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Fri, 17 May 2019 09:27:08 -0700 Subject: [PATCH] IMP `mrp_bom_add` Optionally replace all existing lines on BoM for the selected product. --- mrp_bom_add/__manifest__.py | 9 +++++++-- mrp_bom_add/tests/test_mrp_bom_add.py | 22 ++++++++++++++++++++++ mrp_bom_add/wizard/mrp_bom_add.py | 17 ++++++++++++++++- mrp_bom_add/wizard/mrp_bom_add_views.xml | 2 ++ 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/mrp_bom_add/__manifest__.py b/mrp_bom_add/__manifest__.py index cfaf978d..f05bc690 100755 --- a/mrp_bom_add/__manifest__.py +++ b/mrp_bom_add/__manifest__.py @@ -2,14 +2,19 @@ 'name': 'BoM Mass Add', 'author': 'Hibou Corp. ', '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', diff --git a/mrp_bom_add/tests/test_mrp_bom_add.py b/mrp_bom_add/tests/test_mrp_bom_add.py index 4fa94769..38ac9bd0 100644 --- a/mrp_bom_add/tests/test_mrp_bom_add.py +++ b/mrp_bom_add/tests/test_mrp_bom_add.py @@ -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) diff --git a/mrp_bom_add/wizard/mrp_bom_add.py b/mrp_bom_add/wizard/mrp_bom_add.py index 2f447584..5317b854 100644 --- a/mrp_bom_add/wizard/mrp_bom_add.py +++ b/mrp_bom_add/wizard/mrp_bom_add.py @@ -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: diff --git a/mrp_bom_add/wizard/mrp_bom_add_views.xml b/mrp_bom_add/wizard/mrp_bom_add_views.xml index 85b24077..c4e664a2 100644 --- a/mrp_bom_add/wizard/mrp_bom_add_views.xml +++ b/mrp_bom_add/wizard/mrp_bom_add_views.xml @@ -14,6 +14,8 @@ + +