|
| 1 | +from collections import defaultdict |
| 2 | + |
| 3 | +from odoo import _, models |
| 4 | +from odoo.exceptions import UserError |
| 5 | + |
| 6 | + |
| 7 | +def _prepare_data(env, data): |
| 8 | + # change product ids by actual product object to get access to fields in xml |
| 9 | + # template we needed to pass ids because reports only accepts native python |
| 10 | + # types (int, float, strings, ...) |
| 11 | + if data.get("active_model") == "product.template": |
| 12 | + Product = env["product.template"].with_context(display_default_code=False) |
| 13 | + elif data.get("active_model") == "product.product": |
| 14 | + Product = env["product.product"].with_context(display_default_code=False) |
| 15 | + else: |
| 16 | + raise UserError( |
| 17 | + _("Product model not defined, Please contact your administrator.") |
| 18 | + ) |
| 19 | + |
| 20 | + total, quantity_by_product = 0, defaultdict(list) |
| 21 | + for p, q in data.get("quantity_by_product", {}).items(): |
| 22 | + product = Product.browse(int(p)) |
| 23 | + quantity_by_product[product].append((product.barcode, q)) |
| 24 | + total += q |
| 25 | + if data.get("custom_barcodes"): |
| 26 | + # we expect custom barcodes format as: {product: [(barcode, qty_of_barcode)]} |
| 27 | + for product, barcodes_qtys in data.get("custom_barcodes").items(): |
| 28 | + quantity_by_product[Product.browse(int(product))] += barcodes_qtys |
| 29 | + total += sum(qty for _, qty in barcodes_qtys) |
| 30 | + |
| 31 | + layout_wizard = env["product.label.layout"].browse(data.get("layout_wizard")) |
| 32 | + if not layout_wizard: |
| 33 | + return {} |
| 34 | + |
| 35 | + return { |
| 36 | + "quantity": quantity_by_product, |
| 37 | + "rows": layout_wizard.rows, |
| 38 | + "columns": layout_wizard.columns, |
| 39 | + "page_numbers": (total - 1) // (layout_wizard.rows * layout_wizard.columns) + 1, |
| 40 | + "price_included": data.get("price_included"), |
| 41 | + "extra_html": layout_wizard.extra_html, |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | +class ReportProductTemplateLabelImage4x6(models.AbstractModel): |
| 46 | + _name = "report.product_label_image.producttemplatelabel_image_4x6" |
| 47 | + _description = "Product Label Report" |
| 48 | + |
| 49 | + def _get_report_values(self, docids, data): |
| 50 | + return _prepare_data(self.env, data) |
0 commit comments