From ca3dfec135a535fae35e8893421c353d930a320f Mon Sep 17 00:00:00 2001 From: Goffin Simon Date: Tue, 9 Feb 2021 13:01:48 +0000 Subject: [PATCH] [FIX] mrp: Print BOM / BOM structure & cost Steps to reproduce the bug: - Install manufacturing app - Create a product A - Create a Bill of Materials for product A - Archive Product A (Only the final product not any of the components) - Print BOM or Print BOM Structure & Cost Bug: A traceback was raised opw:2447514 closes odoo/odoo#68116 X-original-commit: 81ee677bd52db388e29c26e255ccab27a286cf94 Related: odoo/enterprise#17186 Signed-off-by: Simon Goffin (sig) --- addons/mrp/report/mrp_report_bom_structure.py | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/addons/mrp/report/mrp_report_bom_structure.py b/addons/mrp/report/mrp_report_bom_structure.py index e3dd273c76d..51cc4d0219b 100644 --- a/addons/mrp/report/mrp_report_bom_structure.py +++ b/addons/mrp/report/mrp_report_bom_structure.py @@ -16,7 +16,7 @@ class ReportBomStructure(models.AbstractModel): bom = self.env['mrp.bom'].browse(bom_id) candidates = bom.product_id or bom.product_tmpl_id.product_variant_ids quantity = float(data.get('quantity', 1)) - for product_variant_id in candidates: + for product_variant_id in candidates.ids: if data and data.get('childs'): doc = self._get_pdf_line(bom_id, product_id=product_variant_id, qty=quantity, child_bom_ids=json.loads(data.get('childs'))) else: @@ -91,6 +91,7 @@ class ReportBomStructure(models.AbstractModel): def _get_bom(self, bom_id=False, product_id=False, line_qty=False, line_id=False, level=False): bom = self.env['mrp.bom'].browse(bom_id) + company = bom.company_id or self.env.company bom_quantity = line_qty if line_id: current_line = self.env['mrp.bom.line'].browse(int(line_id)) @@ -101,13 +102,14 @@ class ReportBomStructure(models.AbstractModel): else: product = bom.product_id or bom.product_tmpl_id.product_variant_id if product: + price = product.uom_id._compute_price(product.with_company(company).standard_price, bom.product_uom_id) * bom_quantity attachments = self.env['mrp.document'].search(['|', '&', ('res_model', '=', 'product.product'), ('res_id', '=', product.id), '&', ('res_model', '=', 'product.template'), ('res_id', '=', product.product_tmpl_id.id)]) else: - product = bom.product_tmpl_id - attachments = self.env['mrp.document'].search([('res_model', '=', 'product.template'), ('res_id', '=', product.id)]) + # Use the product template instead of the variant + price = bom.product_tmpl_id.uom_id._compute_price(bom.product_tmpl_id.with_company(company).standard_price, bom.product_uom_id) * bom_quantity + attachments = self.env['mrp.document'].search([('res_model', '=', 'product.template'), ('res_id', '=', bom.product_tmpl_id.id)]) operations = self._get_operation_line(bom, float_round(bom_quantity / bom.product_qty, precision_rounding=1, rounding_method='UP'), 0) - company = bom.company_id or self.env.company lines = { 'bom': bom, 'bom_qty': bom_quantity, @@ -115,7 +117,7 @@ class ReportBomStructure(models.AbstractModel): 'currency': company.currency_id, 'product': product, 'code': bom and bom.display_name or '', - 'price': product.uom_id._compute_price(product.with_company(company).standard_price, bom.product_uom_id) * bom_quantity, + 'price': price, 'total': sum([op['total'] for op in operations]), 'level': level or 0, 'operations': operations, @@ -209,7 +211,7 @@ class ReportBomStructure(models.AbstractModel): def _get_pdf_line(self, bom_id, product_id=False, qty=1, child_bom_ids=[], unfolded=False): def get_sub_lines(bom, product_id, line_qty, line_id, level): - data = self._get_bom(bom_id=bom.id, product_id=product_id.id, line_qty=line_qty, line_id=line_id, level=level) + data = self._get_bom(bom_id=bom.id, product_id=product_id, line_qty=line_qty, line_id=line_id, level=level) bom_lines = data['components'] lines = [] for bom_line in bom_lines: @@ -227,7 +229,7 @@ class ReportBomStructure(models.AbstractModel): }) if bom_line['child_bom'] and (unfolded or bom_line['child_bom'] in child_bom_ids): line = self.env['mrp.bom.line'].browse(bom_line['line_id']) - lines += (get_sub_lines(line.child_bom_id, line.product_id, bom_line['prod_qty'], line, level + 1)) + lines += (get_sub_lines(line.child_bom_id, line.product_id.id, bom_line['prod_qty'], line, level + 1)) if data['operations']: lines.append({ 'name': _('Operations'), @@ -250,9 +252,9 @@ class ReportBomStructure(models.AbstractModel): return lines bom = self.env['mrp.bom'].browse(bom_id) - product = product_id or bom.product_id or bom.product_tmpl_id.product_variant_id - data = self._get_bom(bom_id=bom_id, product_id=product.id, line_qty=qty) - pdf_lines = get_sub_lines(bom, product, qty, False, 1) + product_id = product_id or bom.product_id.id or bom.product_tmpl_id.product_variant_id.id + data = self._get_bom(bom_id=bom_id, product_id=product_id, line_qty=qty) + pdf_lines = get_sub_lines(bom, product_id, qty, False, 1) data['components'] = [] data['lines'] = pdf_lines return data