Skip to content
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

[14.0][IMP] custom_mrp_line_cost: Add dependencies to compute. #433

Draft
wants to merge 1 commit into
base: 14.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 custom_mrp_line_cost/models/stock_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ def _compute_expense_kg(self):
"production_id.is_deconstruction",
"production_id.move_line_ids",
"production_id.move_line_ids.amount",
"production_id.move_line_ids.lot_id",
"lot_id",
)
def _compute_base_price(self):
for line in self:
Expand Down
53 changes: 53 additions & 0 deletions mrp_bom_structure_xlsx_cost/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

===========================
MRP BOM Structure XLSX Cost
===========================

This module provides an XLSX report detailing the BOM (Bill of Materials) structure at the product level. It utilizes Odoo’s XLSX reporting engine and XlsxWriter library.

Overview
--------

### Class `BomStructureXlsxL1`

- **Purpose**: Generates a Level 1 BOM structure report in XLSX format.
- **Inherits**: Extends `report.mrp_bom_structure_xlsx.bom_structure_xlsx`.

### Key Methods

- **`print_bom_children(self, ch, sheet, row, level)`**:
- Writes BOM line details to the XLSX sheet, including product code, name, quantity, unit of measure, reference, and cost.
- Recursively processes child BOM lines.

- **`generate_xlsx_report(self, workbook, data, objects)`**:
- Configures the XLSX workbook and sheet, sets column widths, applies formatting, and writes report data.
- Calls `print_bom_children` to include child BOM lines.

### Report Configuration

- **Sheet Layout**: Landscape orientation, 80% zoom, with adjusted column widths.
- **Formatting**: Bold headers and data rows.
- **Freezing Panes**: Keeps the header row visible when scrolling.

This setup allows for a detailed and organized BOM report in an easy-to-read XLSX format.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/avanzosc/mrp-addons/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smash it by providing detailed and welcomed feedback.

Credits
=======

Contributors
------------
* Ana Juaristi <[email protected]>
* Unai Beristain <[email protected]>

Do not contact contributors directly about support or help with technical issues.
1 change: 1 addition & 0 deletions mrp_bom_structure_xlsx_cost/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import report
16 changes: 16 additions & 0 deletions mrp_bom_structure_xlsx_cost/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "MRP BOM Structure XLSX Cost",
"version": "14.0.1.0.0",
"website": "https://github.com/avanzosc/mrp-addons",
"summary": "Customization to add cost information to MRP BOM structure XLSX.",
"category": "Manufacturing",
"author": "Avanzosc",
"license": "AGPL-3",
"depends": ["mrp_bom_structure_xlsx"],
"data": [
"report/bom_structure_xlsx.xml",
],
"installable": True,
"application": False,
"auto_install": False,
}
1 change: 1 addition & 0 deletions mrp_bom_structure_xlsx_cost/report/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import bom_structure_xlsx
98 changes: 98 additions & 0 deletions mrp_bom_structure_xlsx_cost/report/bom_structure_xlsx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import logging

from odoo import _, models
from odoo.exceptions import CacheMiss

_logger = logging.getLogger(__name__)


class BomStructureXlsxL1(models.AbstractModel):
_name = "report.mrp_bom_structure_xlsx_cost.bom_structure_xlsx_cost"
_description = "BOM Structure XLSX Level 1 Report"
_inherit = "report.mrp_bom_structure_xlsx.bom_structure_xlsx"

def print_bom_children(self, ch, sheet, row, level):
i, j = row, level
j += 1
sheet.write(i, 1, "> " * j)
sheet.write(i, 2, ch.product_id.default_code or "")
sheet.write(i, 3, ch.product_id.display_name or "")
sheet.write(
i,
4,
ch.product_uom_id._compute_quantity(ch.product_qty, ch.product_id.uom_id)
or "",
)
sheet.write(i, 5, ch.product_id.uom_id.name or "")
sheet.write(i, 6, ch.bom_id.code or "")
sheet.write(
i,
7,
"{:.4f}".format(
ch.product_uom_id._compute_quantity(
ch.product_qty, ch.product_id.uom_id
)
* ch.product_id.standard_price
or 0
).replace(".", ","),
)

i += 1
# self.env.cache.invalidate()
try:
for child in ch.child_line_ids:
i = self.print_bom_children(child, sheet, i, j)

except CacheMiss:
# The Bom has no childs, thus it is the last level.
# When a BoM has no childs, chlid_line_ids is None, this creates a
# CacheMiss Error. However, this is expected because there really
# cannot be child_line_ids.
pass

j -= 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(_("BOM Structure"))
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)
bold = workbook.add_format({"bold": True})
title_style = workbook.add_format(
{"bold": True, "bg_color": "#FFFFCC", "bottom": 1}
)
sheet_title = [
_("BOM Name"),
_("Level"),
_("Product Reference"),
_("Product Name"),
_("Quantity"),
_("Unit of Measure"),
_("Reference"),
_("Cost"),
]
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:
sheet.write(i, 0, o.product_tmpl_id.name or "", bold)
sheet.write(i, 1, "", bold)
sheet.write(i, 2, o.product_id.default_code or "", bold)
sheet.write(i, 3, o.product_id.name or "", bold)
sheet.write(i, 4, o.product_qty, bold)
sheet.write(i, 5, o.product_uom_id.name or "", bold)
sheet.write(i, 6, o.code or "", bold)
sheet.write(i, 7, o.product_id.standard_price or "", bold)
i += 1
j = 0
for ch in o.bom_line_ids:
i = self.print_bom_children(ch, sheet, i, j)
14 changes: 14 additions & 0 deletions mrp_bom_structure_xlsx_cost/report/bom_structure_xlsx.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="bom_structure_xlsx" model="ir.actions.report">
<field name="name">Export BoM Structure to Excel Cost</field>
<field name="model">mrp.bom</field>
<field name="binding_model_id" ref="mrp.model_mrp_bom" />
<field name="type">ir.actions.report</field>
<field
name="report_name"
>mrp_bom_structure_xlsx_cost.bom_structure_xlsx_cost</field>
<field name="report_type">xlsx</field>
<field name="report_file">mrp_bom</field>
</record>
</odoo>
6 changes: 6 additions & 0 deletions setup/mrp_bom_structure_xlsx_cost/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
Loading