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
This commit is contained in:
Alexis de Lattre
2022-06-16 18:07:53 +02:00
parent 0d689b1e95
commit 0cd6ecd958

View File

@@ -3,8 +3,9 @@
# @author Alexis de Lattre <alexis.delattre@akretion.com>
# 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