Automatic update Item Price from Purchase Invoice

Need to create a Custom Script to update Item Price as per Purchase Invoice Item price_list_rate value

Please check the script

frappe.ui.form.on("Purchase Invoice Item", {
	item_code: function(frm, cdt, cdn) {
		child = locals[cdt][cdn];
		me.frm.call({
			method: "frappe.client.get_value",
			args: {
				doctype: "Item Price",
				fieldname: "price_list_rate",
				filters: { item_code: child.item_code, price_list_rate: child.price_list_rate },
			},
			callback: function(r, rt) {
				if(r.message) {
					frappe.model.set_value(cdt, cdn, 'price_list_rate', r.message.price_list_rate)
				}
			}
		});
	}
})


Skedia

Should be

frappe.ui.form.on("Purchase Invoice ", { 
} )

Seems wrong filter price_list_rate: child.price_list_rate
You have to pass price list name

Script not working

Price list = MRP

I want to update price list rate of item purchase as per the price list rate in Purchase Invoice.

frappe.ui.form.on("Purchase Invoice", { item_code: function(frm, cdt, cdn) { child = locals[cdt][cdn]; me.frm.call({ method: "frappe.client.get_value", args: { doctype: "Item Price", fieldname: "price_list_rate", filters: { item_code: child.item_code, price_list : "MRP" }, }, callback: function(r, rt) { if(r.message) { frappe.model.set_value(cdt, cdn, 'price_list_rate', r.message.price_list_rate) } } }); } })

I wanted to have the same functionality. Has anyone figured out how to achieve this?

1 Like

you can use server script

def on_submit(self):
        items = self.items
        for i in items:
            item_price = frappe.get_doc('Item Price', {'item_code': i.item_code, 'price_list': 'Standard Selling'})
            if item_price:
                if item_price.price_list_rate != i.custom_estimate_rate:
                    item_price.price_list_rate = i.custom_estimate_rate
                    item_price.save()
                else:
                    pass
            else:
                item_price = frappe.new_doc('Item Price')
                item_price.item_code = i.item_code
                item_price.price_list = 'Standard Selling'
                item_price.price_list_rate = i.custom_estimate_rate
                item_price.save()
1 Like