From 97cf376e90ffe0b6251796372835a4d508ae0507 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Thu, 17 Oct 2024 10:57:35 +0200 Subject: [PATCH] purchase_usability: take into account ir.config_parameter usability.line_name_no_product_code when using _prepare_purchase_order_line() --- purchase_usability/models/purchase_order.py | 40 +++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/purchase_usability/models/purchase_order.py b/purchase_usability/models/purchase_order.py index 3d792d9..b46e84a 100644 --- a/purchase_usability/models/purchase_order.py +++ b/purchase_usability/models/purchase_order.py @@ -2,9 +2,8 @@ # @author Alexis de Lattre # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import api, fields, models, _ -from odoo.tools.misc import format_datetime, format_amount -from odoo.tools import float_compare +from odoo import api, fields, models +from odoo.tools.misc import format_amount class PurchaseOrder(models.Model): @@ -108,6 +107,41 @@ class PurchaseOrderLine(models.Model): product_lang = product_lang.with_context(display_default_code=False) return super()._get_product_purchase_description(product_lang) + @api.model + def _prepare_purchase_order_line(self, product, product_qty, product_uom, company_id, supplier, po): + # _prepare_purchase_order_line() doesn't use _get_product_purchase_description() + # because this method is @api.model and _get_product_purchase_description() has self._ensure_one() + # I tried to inject display_default_code in the context via super(xxx, self.with_context()), + # but it doesn't work. That's why I rewrite vals['name'] + # The native proto uses "product_id", but it's in fact a product record set, + # that's why I use 'product' in the proto here + vals = super()._prepare_purchase_order_line( + product, product_qty, product_uom, company_id, supplier, po) + # START copy-paste from original code (I just modified product_id => product + partner = supplier.partner_id + uom_po_qty = product_uom._compute_quantity(product_qty, product.uom_po_id, rounding_method='HALF-UP') + today = fields.Date.today() + seller = product.with_company(company_id)._select_seller( + partner_id=partner, + quantity=uom_po_qty, + date=po.date_order and max(po.date_order.date(), today) or today, + uom_id=product.uom_po_id) + # END copy-paste from original code + product_lang = product.with_context( + lang=partner.lang, + partner_id=partner.id, + seller_id=seller.id + ) + no_product_code_param = self.env['ir.config_parameter'].sudo().get_param( + 'usability.line_name_no_product_code') + if no_product_code_param and no_product_code_param == 'True': + product_lang = product_lang.with_context(display_default_code=False) + name = product_lang.display_name + if product_lang.description_purchase: + name += '\n' + product_lang.description_purchase + vals['name'] = name + return vals + # TODO see how we could restore this feature # @api.onchange('product_qty', 'product_uom') # def _onchange_quantity(self):