Update price list rate in PO for discount consistency?

Hi !
In purchase order, tell me if I’m wrong, item discount works the following way:
price list rate : price before discount
Discount on price list : % discount to apply on price list rate
rate : the result of the above values.

My issue is: price list rate is not updated (if price has changed) in item price list AFAIK so when the next PO is created, the last purchase rate and price list rate does not have the right discount amount.

I’d like to create a script that updates price list rate on PO submission (or even PI), but I fear this was designed it way for a reason that I’m not aware of.

Can someone confirm I’m not about to break something else by fixing my issue ?

Thank you !!

For those interested, this is how I did it

in your app hooks.py

doc_events = {
    "Purchase Order": {
        "on_submit": "your_app.your_file.update_price_list_rate"
    }
}

in your app your_file.py :

def update_price_list_rate(doc, method):
"""Insert Item Price if Price List and Price List Rate are specified and currency is the same"""
if frappe.db.get_value("Price List", doc.buying_price_list, "currency") == doc.currency:
    if frappe.has_permission("Item Price", "write"):
        for item in doc.items:
            name = frappe.db.get_value('Item Price', {
                'item_code': item.item_code,
                'price_list': doc.buying_price_list,
                'currency': doc.currency
                }, 'name')

            if name:
                item_price = frappe.get_doc('Item Price', name)
                item_price.price_list_rate = item.price_list_rate
                item_price.save()
                frappe.msgprint(_("Item Price updated for {0} in Price List {1}").format(item.item_code,
                                                                                         doc.buying_price_list))
            else:
                item_price = frappe.get_doc({
                    "doctype": "Item Price",
                    "price_list": doc.buying_price_list,
                    "item_code": item.item_code,
                    "currency": doc.currency,
                    "price_list_rate": item.price_list_rate
                })
                item_price.insert()
                frappe.msgprint(_("Item Price added for {0} in Price List {1}").format(item.item_code,
                                                                                       doc.buying_price_list))

It’s probably not the most beautiful way to do, but it works

1 Like