diff --git a/mrp_flattened_bom_xlsx/README.rst b/mrp_flattened_bom_xlsx/README.rst new file mode 100644 index 00000000..c882e809 --- /dev/null +++ b/mrp_flattened_bom_xlsx/README.rst @@ -0,0 +1,97 @@ +============================= +Export Flattened BOM to Excel +============================= + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fmanufacture--reporting-lightgray.png?logo=github + :target: https://github.com/OCA/manufacture-reporting/tree/13.0/mrp_flattened_bom_xlsx + :alt: OCA/manufacture-reporting +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/manufacture-reporting-13-0/manufacture-reporting-13-0-mrp_flattened_bom_xlsx + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/131/13.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends the functionality of the MRP capabilities of Odoo, +and allows you to export the flattened BOM to MS Excel .XLSX format. + +A flattened bill of material removes the intermediate levels in the BOM +and connect the lowest levels directly to the highest level. + +A list of the sum of lowest levels will be shown for every +BoM you export using this method. + +It also maintains units correctly across all nested BOM's and take units +that have been defined in product Unit of Measure field. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module, you need to: + +#. Go to 'Manufacturing / Products / Bill of Materials' + +#. Select a BOM or more BOMS + + *(Could be interesting to modify quantities of these BOMs)* + +#. Go to 'Print / Export Flattened BOM to Excel'. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* ForgeFlow + +Contributors +~~~~~~~~~~~~ + +* Héctor Villarreal +* Lois Rilo + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/manufacture-reporting `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mrp_flattened_bom_xlsx/__init__.py b/mrp_flattened_bom_xlsx/__init__.py new file mode 100644 index 00000000..bf588bc8 --- /dev/null +++ b/mrp_flattened_bom_xlsx/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import report diff --git a/mrp_flattened_bom_xlsx/__manifest__.py b/mrp_flattened_bom_xlsx/__manifest__.py new file mode 100644 index 00000000..79714629 --- /dev/null +++ b/mrp_flattened_bom_xlsx/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2018 ForgeFlow S.L. +# (http://www.forgeflow.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +{ + "name": "Export Flattened BOM to Excel", + "version": "13.0.1.0.0", + "category": "Manufacturing", + "author": "ForgeFlow, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/manufacture-reporting", + "license": "AGPL-3", + "depends": ["report_xlsx", "mrp"], + "data": ["report/flattened_bom_xlsx.xml"], + "installable": True, +} diff --git a/mrp_flattened_bom_xlsx/i18n/mrp_flattened_bom_xlsx.pot b/mrp_flattened_bom_xlsx/i18n/mrp_flattened_bom_xlsx.pot new file mode 100644 index 00000000..02bbe4cc --- /dev/null +++ b/mrp_flattened_bom_xlsx/i18n/mrp_flattened_bom_xlsx.pot @@ -0,0 +1,87 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * mrp_flattened_bom_xlsx +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: mrp_flattened_bom_xlsx +#: code:addons/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py:48 +#, python-format +msgid "BOM Name" +msgstr "" + +#. module: mrp_flattened_bom_xlsx +#: model:ir.model,name:mrp_flattened_bom_xlsx.model_mrp_bom +msgid "Bill of Material" +msgstr "" + +#. module: mrp_flattened_bom_xlsx +#: model:ir.model.fields,field_description:mrp_flattened_bom_xlsx.field_report_mrp_flattened_bom_xlsx_flattened_bom_xlsx__display_name +msgid "Display Name" +msgstr "" + +#. module: mrp_flattened_bom_xlsx +#: model:ir.actions.report,name:mrp_flattened_bom_xlsx.flattened_bom_xlsx +msgid "Export Flattened BOM Structure to Excel" +msgstr "" + +#. module: mrp_flattened_bom_xlsx +#: code:addons/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py:37 +#, python-format +msgid "Flattened BOM" +msgstr "" + +#. module: mrp_flattened_bom_xlsx +#: model:ir.model.fields,field_description:mrp_flattened_bom_xlsx.field_report_mrp_flattened_bom_xlsx_flattened_bom_xlsx__id +msgid "ID" +msgstr "" + +#. module: mrp_flattened_bom_xlsx +#: model:ir.model.fields,field_description:mrp_flattened_bom_xlsx.field_report_mrp_flattened_bom_xlsx_flattened_bom_xlsx____last_update +msgid "Last Modified on" +msgstr "" + +#. module: mrp_flattened_bom_xlsx +#: code:addons/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py:50 +#, python-format +msgid "Product Name" +msgstr "" + +#. module: mrp_flattened_bom_xlsx +#: code:addons/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py:49 +#, python-format +msgid "Product Reference" +msgstr "" + +#. module: mrp_flattened_bom_xlsx +#: code:addons/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py:51 +#, python-format +msgid "Quantity" +msgstr "" + +#. module: mrp_flattened_bom_xlsx +#: code:addons/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py:53 +#, python-format +msgid "Reference" +msgstr "" + +#. module: mrp_flattened_bom_xlsx +#: code:addons/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py:52 +#, python-format +msgid "Unit of Measure" +msgstr "" + +#. module: mrp_flattened_bom_xlsx +#: model:ir.model,name:mrp_flattened_bom_xlsx.model_report_mrp_flattened_bom_xlsx_flattened_bom_xlsx +msgid "report.mrp_flattened_bom_xlsx.flattened_bom_xlsx" +msgstr "" + diff --git a/mrp_flattened_bom_xlsx/models/__init__.py b/mrp_flattened_bom_xlsx/models/__init__.py new file mode 100644 index 00000000..a352efe9 --- /dev/null +++ b/mrp_flattened_bom_xlsx/models/__init__.py @@ -0,0 +1 @@ +from . import mrp_bom diff --git a/mrp_flattened_bom_xlsx/models/mrp_bom.py b/mrp_flattened_bom_xlsx/models/mrp_bom.py new file mode 100644 index 00000000..1e76ef06 --- /dev/null +++ b/mrp_flattened_bom_xlsx/models/mrp_bom.py @@ -0,0 +1,48 @@ +# Copyright 2018 ForgeFlow S.L. +# (http://www.forgeflow.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import models + + +class MrpBom(models.Model): + """ Defines bills of material for a product or a product template """ + + _inherit = "mrp.bom" + + def _get_flattened_totals(self, factor=1, totals=None): + """Calculate the **unitary** product requirements of flattened BOM. + *Unit* means that the requirements are computed for one unit of the + default UoM of the product. + :returns: dict: keys are components and values are aggregated quantity + in the product default UoM. + """ + self.ensure_one() + if totals is None: + totals = {} + factor /= self.product_uom_id._compute_quantity( + self.product_qty, self.product_tmpl_id.uom_id, round=False + ) + for line in self.bom_line_ids: + sub_bom = self._bom_find(product=line.product_id) + if sub_bom: + new_factor = factor * line.product_uom_id._compute_quantity( + line.product_qty, line.product_id.uom_id, round=False + ) + sub_bom._get_flattened_totals(new_factor, totals) + else: + if totals.get(line.product_id): + totals[line.product_id] += ( + factor + * line.product_uom_id._compute_quantity( + line.product_qty, line.product_id.uom_id, round=False + ) + ) + else: + totals[line.product_id] = ( + factor + * line.product_uom_id._compute_quantity( + line.product_qty, line.product_id.uom_id, round=False + ) + ) + return totals diff --git a/mrp_flattened_bom_xlsx/readme/CONTRIBUTORS.rst b/mrp_flattened_bom_xlsx/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..5953bf14 --- /dev/null +++ b/mrp_flattened_bom_xlsx/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Héctor Villarreal +* Lois Rilo diff --git a/mrp_flattened_bom_xlsx/readme/DESCRIPTION.rst b/mrp_flattened_bom_xlsx/readme/DESCRIPTION.rst new file mode 100644 index 00000000..39e2c8b4 --- /dev/null +++ b/mrp_flattened_bom_xlsx/readme/DESCRIPTION.rst @@ -0,0 +1,11 @@ +This module extends the functionality of the MRP capabilities of Odoo, +and allows you to export the flattened BOM to MS Excel .XLSX format. + +A flattened bill of material removes the intermediate levels in the BOM +and connect the lowest levels directly to the highest level. + +A list of the sum of lowest levels will be shown for every +BoM you export using this method. + +It also maintains units correctly across all nested BOM's and take units +that have been defined in product Unit of Measure field. diff --git a/mrp_flattened_bom_xlsx/readme/USAGE.rst b/mrp_flattened_bom_xlsx/readme/USAGE.rst new file mode 100644 index 00000000..4a0f5c00 --- /dev/null +++ b/mrp_flattened_bom_xlsx/readme/USAGE.rst @@ -0,0 +1,9 @@ +To use this module, you need to: + +#. Go to 'Manufacturing / Products / Bill of Materials' + +#. Select a BOM or more BOMS + + *(Could be interesting to modify quantities of these BOMs)* + +#. Go to 'Print / Export Flattened BOM to Excel'. diff --git a/mrp_flattened_bom_xlsx/report/__init__.py b/mrp_flattened_bom_xlsx/report/__init__.py new file mode 100644 index 00000000..06df3655 --- /dev/null +++ b/mrp_flattened_bom_xlsx/report/__init__.py @@ -0,0 +1,2 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import flattened_bom_xlsx diff --git a/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py b/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py new file mode 100644 index 00000000..9bbde11b --- /dev/null +++ b/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py @@ -0,0 +1,69 @@ +# Copyright 2018 ForgeFlow S.L. +# (http://www.forgeflow.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +import logging + +from odoo import models +from odoo.tools.translate import _ + +_logger = logging.getLogger(__name__) + + +class FlattenedBomXlsx(models.AbstractModel): + _name = "report.mrp_flattened_bom_xlsx.flattened_bom_xlsx" + _inherit = "report.report_xlsx.abstract" + + def print_flattened_bom_lines(self, bom, requirements, sheet, row): + i = row + sheet.write(i, 0, bom.product_tmpl_id.name or "") + sheet.write(i, 1, bom.code or "") + sheet.write(i, 2, bom.display_name or "") + sheet.write(i, 3, bom.product_qty) + sheet.write(i, 4, bom.product_uom_id.name or "") + sheet.write(i, 5, bom.code or "") + i += 1 + for product, total_qty in requirements.items(): + sheet.write(i, 1, product.default_code or "") + sheet.write(i, 2, product.display_name or "") + sheet.write(i, 3, total_qty or 0.0) + sheet.write(i, 4, product.uom_id.name or "") + sheet.write(i, 5, product.code or "") + i += 1 + return i + + def generate_xlsx_report(self, workbook, data, objects): + workbook.set_properties( + {"comments": "Created with Python and XlsxWriter from Odoo 11.0"} + ) + sheet = workbook.add_worksheet(_("Flattened BOM")) + sheet.set_landscape() + sheet.fit_to_pages(1, 0) + sheet.set_zoom(80) + sheet.set_column(0, 0, 40) + sheet.set_column(1, 2, 20) + sheet.set_column(3, 3, 40) + sheet.set_column(4, 6, 20) + title_style = workbook.add_format( + {"bold": True, "bg_color": "#FFFFCC", "bottom": 1} + ) + sheet_title = [ + _("BOM Name"), + _("Product Reference"), + _("Product Name"), + _("Quantity"), + _("Unit of Measure"), + _("Reference"), + ] + sheet.set_row(0, None, None, {"collapsed": 1}) + sheet.write_row(1, 0, sheet_title, title_style) + sheet.freeze_panes(2, 0) + i = 2 + + for o in objects: + # We need to calculate the totals for the BoM qty and UoM: + starting_factor = o.product_uom_id._compute_quantity( + o.product_qty, o.product_tmpl_id.uom_id, round=False + ) + totals = o._get_flattened_totals(factor=starting_factor) + i = self.print_flattened_bom_lines(o, totals, sheet, i) diff --git a/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.xml b/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.xml new file mode 100644 index 00000000..5613888b --- /dev/null +++ b/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.xml @@ -0,0 +1,16 @@ + + + + + + + diff --git a/mrp_flattened_bom_xlsx/static/description/icon.png b/mrp_flattened_bom_xlsx/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/mrp_flattened_bom_xlsx/static/description/icon.png differ diff --git a/mrp_flattened_bom_xlsx/static/description/index.html b/mrp_flattened_bom_xlsx/static/description/index.html new file mode 100644 index 00000000..c4ade398 --- /dev/null +++ b/mrp_flattened_bom_xlsx/static/description/index.html @@ -0,0 +1,441 @@ + + + + + + +Export Flattened BOM to Excel + + + +
+

Export Flattened BOM to Excel

+ + +

Beta License: AGPL-3 OCA/manufacture-reporting Translate me on Weblate Try me on Runbot

+

This module extends the functionality of the MRP capabilities of Odoo, +and allows you to export the flattened BOM to MS Excel .XLSX format.

+

A flattened bill of material removes the intermediate levels in the BOM +and connect the lowest levels directly to the highest level.

+

A list of the sum of lowest levels will be shown for every +BoM you export using this method.

+

It also maintains units correctly across all nested BOM’s and take units +that have been defined in product Unit of Measure field.

+

Table of contents

+ +
+

Usage

+

To use this module, you need to:

+
    +
  1. Go to ‘Manufacturing / Products / Bill of Materials’

    +
  2. +
  3. Select a BOM or more BOMS

    +

    (Could be interesting to modify quantities of these BOMs)

    +
  4. +
  5. Go to ‘Print / Export Flattened BOM to Excel’.

    +
  6. +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • ForgeFlow
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/manufacture-reporting project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/mrp_flattened_bom_xlsx/tests/__init__.py b/mrp_flattened_bom_xlsx/tests/__init__.py new file mode 100644 index 00000000..9554abdb --- /dev/null +++ b/mrp_flattened_bom_xlsx/tests/__init__.py @@ -0,0 +1 @@ +from . import test_flattened_bom diff --git a/mrp_flattened_bom_xlsx/tests/test_flattened_bom.py b/mrp_flattened_bom_xlsx/tests/test_flattened_bom.py new file mode 100644 index 00000000..5c1f9335 --- /dev/null +++ b/mrp_flattened_bom_xlsx/tests/test_flattened_bom.py @@ -0,0 +1,115 @@ +# Copyright 2018 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +import odoo.tests.common as common + + +class TestFlattenedBom(common.SavepointCase): + @classmethod + def setUpClass(cls): + super(TestFlattenedBom, cls).setUpClass() + + cls.product_obj = cls.env["product.product"] + cls.bom_obj = cls.env["mrp.bom"] + cls.bom_line_obj = cls.env["mrp.bom.line"] + + cls.uom_dozen = cls.env.ref("uom.product_uom_dozen") + + # Create products: + cls.product_top = cls.product_obj.create( + {"name": "Final Product", "type": "product", "standard_price": 300.0} + ) + cls.product_sub_1 = cls.product_obj.create( + {"name": "L01-01", "type": "product", "standard_price": 300.0} + ) + cls.product_sub_2 = cls.product_obj.create( + {"name": "L01-02", "type": "product", "standard_price": 300.0} + ) + cls.component_1 = cls.product_obj.create( + {"name": "RM 01", "type": "product", "standard_price": 100.0} + ) + cls.component_2 = cls.product_obj.create( + {"name": "RM 01", "type": "product", "standard_price": 75.0} + ) + cls.component_3 = cls.product_obj.create( + {"name": "RM 03", "type": "product", "standard_price": 75.0} + ) + + # Create Bills of Materials: + cls.bom_top = cls.bom_obj.create( + {"product_tmpl_id": cls.product_top.product_tmpl_id.id} + ) + cls.line_top_1 = cls.bom_line_obj.create( + { + "product_id": cls.product_sub_1.id, + "bom_id": cls.bom_top.id, + "product_qty": 2.0, + } + ) + cls.line_top_2 = cls.bom_line_obj.create( + { + "product_id": cls.product_sub_2.id, + "bom_id": cls.bom_top.id, + "product_qty": 5.0, + } + ) + + cls.bom_sub_1 = cls.bom_obj.create( + {"product_tmpl_id": cls.product_sub_1.product_tmpl_id.id} + ) + cls.line_sub_1_1 = cls.bom_line_obj.create( + { + "product_id": cls.component_1.id, + "bom_id": cls.bom_sub_1.id, + "product_qty": 2.0, + } + ) + cls.line_sub_1_2 = cls.bom_line_obj.create( + { + "product_id": cls.component_2.id, + "bom_id": cls.bom_sub_1.id, + "product_qty": 5.0, + } + ) + + cls.bom_sub_2 = cls.bom_obj.create( + {"product_tmpl_id": cls.product_sub_2.product_tmpl_id.id} + ) + cls.line_sub_2_1 = cls.bom_line_obj.create( + { + "product_id": cls.component_1.id, + "bom_id": cls.bom_sub_2.id, + "product_qty": 3.0, + } + ) + cls.line_sub_2_2 = cls.bom_line_obj.create( + { + "product_id": cls.component_3.id, + "bom_id": cls.bom_sub_2.id, + "product_qty": 3.0, + } + ) + + def test_01_flattened_totals(self): + """Test totals computation with a multi level BoM.""" + flat_tot = self.bom_top._get_flattened_totals() + self.assertEqual(len(flat_tot), 3) + # Component 1 = 2*2 + 5*3 = 19 + self.assertEqual(flat_tot.get(self.component_1), 19) + # Component 2 = 2*5 = 10 + self.assertEqual(flat_tot.get(self.component_2), 10) + # Component 3 = 5*3 = 15 + self.assertEqual(flat_tot.get(self.component_3), 15) + + def test_02_different_uom(self): + """Test totals computation with a multi level BoM and different UoM.""" + self.bom_top.product_uom_id = self.uom_dozen + self.line_sub_2_1.product_uom_id = self.uom_dozen + flat_tot = self.bom_top._get_flattened_totals() + self.assertEqual(len(flat_tot), 3) + # Component 1 = 2*2 + 5*3*12 = 184 units -> 184/12 dozens + self.assertAlmostEqual(flat_tot.get(self.component_1), 184 / 12) + # Component 2 = 2*5 = 10 units -> 10/12 dozens + self.assertAlmostEqual(flat_tot.get(self.component_2), 10 / 12) + # Component 3 = 5*3 = 15 units -> 15/12 dozens + self.assertAlmostEqual(flat_tot.get(self.component_3), 15 / 12)