From 0cd6ecd958d9b1a409070bd5191ea7f301c6efd9 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Thu, 16 Jun 2022 18:07:53 +0200 Subject: [PATCH] purchase_usability: warning when price and/or delay is auto-updated following a qty change It is similar to the feature present in sale_usability when the qty is updated and the price unit changes --- purchase_usability/purchase.py | 50 ++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/purchase_usability/purchase.py b/purchase_usability/purchase.py index ac42171..51bfe42 100644 --- a/purchase_usability/purchase.py +++ b/purchase_usability/purchase.py @@ -3,8 +3,9 @@ # @author Alexis de Lattre # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import models, fields, api +from odoo import models, fields, api, _ from odoo.tools.misc import formatLang +from odoo.tools import float_compare class PurchaseOrder(models.Model): @@ -42,6 +43,51 @@ class PurchaseOrder(models.Model): if po.partner_ref: name += ' (' + po.partner_ref + ')' if self.env.context.get('show_total_amount') and po.amount_total: - name += ': ' + formatLang(self.env, po.amount_untaxed, currency_obj=po.currency_id) + name += ': ' + formatLang( + self.env, po.amount_untaxed, currency_obj=po.currency_id) result.append((po.id, name)) return result + + +class PurchaseOrderLine(models.Model): + _inherit = 'purchase.order.line' + + @api.onchange('product_qty', 'product_uom') + def _onchange_quantity(self): + # When the user has manually set a price and/or planned_date + # he is often upset when Odoo changes it when he changes the qty + # So we add a warning... + res = {} + old_price = self.price_unit + old_date_planned = self.date_planned + super()._onchange_quantity() + new_price = self.price_unit + new_date_planned = self.date_planned + prec = self.env['decimal.precision'].precision_get('Product Price') + price_compare = float_compare(old_price, new_price, precision_digits=prec) + if price_compare or old_date_planned != new_date_planned: + res['warning'] = { + 'title': _('Updates'), + 'message': _( + "Due to the update of the ordered quantity on line '%s', " + "the following data has been updated using the supplier info " + "of the product:" + ) % self.name + } + if price_compare: + res['warning']['message'] += _( + "\nOld price: %s\nNew price: %s") % ( + formatLang( + self.env, old_price, + currency_obj=self.order_id.currency_id), + formatLang( + self.env, new_price, + currency_obj=self.order_id.currency_id)) + + if old_date_planned != new_date_planned: + res['warning']['message'] += _( + "\nOld delivery date: %s\nNew delivery date: %s") % ( + old_date_planned, + new_date_planned, + ) + return res