diff --git a/grap_qweb_report/models/__init__.py b/grap_qweb_report/models/__init__.py index 90f31074..24484388 100644 --- a/grap_qweb_report/models/__init__.py +++ b/grap_qweb_report/models/__init__.py @@ -7,3 +7,4 @@ from . import purchase_order from . import sale_order_line from . import stock_picking +from . import product_product diff --git a/grap_qweb_report/models/product_product.py b/grap_qweb_report/models/product_product.py new file mode 100644 index 00000000..56acdf83 --- /dev/null +++ b/grap_qweb_report/models/product_product.py @@ -0,0 +1,33 @@ +# Copyright (C) 2018 - Today: GRAP (http://www.grap.coop) +# @author: Sylvain LE GAL (https://twitter.com/legalsylvain) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +import re + +from odoo import models + + +class ProductProduct(models.Model): + _inherit = "product.product" + + def _get_supplierinfo_from_purchase_order_line(self, order_line): + self.ensure_one() + supplierinfos = order_line.product_id.seller_ids.filtered( + lambda x: x.name == order_line.order_id.partner_id + and (not x.product_id or x.product_id == order_line.product_id) + ) + if len(supplierinfos) <= 1: + return supplierinfos + # Try to guess supplierinfo, from the code set in the order line name + regex_result = re.search(r"\[(\w+)\].*", order_line.name).groups() + if len(regex_result) == 1: + product_code = regex_result[0] + supplierinfos = supplierinfos.filtered( + lambda x: x.product_code == product_code + ) + if len(supplierinfos) <= 1: + return supplierinfos + + # The code has not been found or there is many supplierinfo with the same code + # Fallback and return the first supplierinfo + return supplierinfos.sorted(key=lambda x: x.min_qty)[0] diff --git a/grap_qweb_report/report/report_purchase_order_xlsx.py b/grap_qweb_report/report/report_purchase_order_xlsx.py index 9e360435..22049ebc 100644 --- a/grap_qweb_report/report/report_purchase_order_xlsx.py +++ b/grap_qweb_report/report/report_purchase_order_xlsx.py @@ -30,11 +30,10 @@ def generate_xlsx_report(self, workbook, data, orders): def _get_columns_spec(self, order_lines): supplierinfo_render = ( - "order_line.product_id.seller_ids.filtered(" - "lambda x: x.name == order_line.order_id.partner_id " - " and (not x.product_id or x.product_id == order_line.product_id))" - ".sorted(key=lambda x: x.min_qty)" + "order_line.product_id" + "._get_supplierinfo_from_purchase_order_line(order_line)" ) + res = [ { "name": "product_code",