How to fetch exchange rate from Purchase Invoice

Hi all,

Can someone help me how to make the script check the currency of the price?
I want to store all prices in USD but sometimes the invoice is in different currency.
So I need the script to convert the price to USD and then update it in my price list.

I don’t know how to make it use the current exchange rate from the Purchase Invoice instead of the hardcoded 0.214. Can sombody explain how to do it?

frappe.ui.form.on("Purchase Invoice", "before_save", function(frm) {
            $.each(cur_frm.doc.items || [], function(i, v) { // for each item on table do this
                var precoAntigo;
                
                frappe.call({
                    method: "frappe.client.get_list",
                    args: {
                        doctype: "Item Price",
                        filters: [
                            ['price_list', "=", cur_frm.doc.buying_price_list],
                            ["item_code", "=", v.item_code],
                        ],
                        fields: [

                            "price_list_rate",
                            "name"

                        ]
                    },
                    
                    callback: function(r) { // do this to found price list doc
                        precoAntigo = (r.message[0].price_list_rate);
                        var nomelista = r.message[0].name
                        // cur_frm.add_fetch('Purchase Invoice', 'conversion_rate', 'conversion_rate');
                        // console.log(precoAntigo)
                        if (precoAntigo && precoAntigo != v.rate) {
                            frappe.confirm(
                                `Chcesz zaktualizować cenę ${v.item_name} z ${ precoAntigo } na ${ v.rate*214} w ${cur_frm.doc.buying_price_list}?`, //ask something to update price
                                function() { // do this if ok
                                    frappe.db.set_value("Item Price", nomelista, "price_list_rate", v.rate*0.214)
                                },
                                function() { // do nothing if cancel

                                }

                            )
                        }
                    }
                });
            })
})
1 Like

@ernest You can get the exchange rate of the Purchase Invoice through calling frm.doc.conversion_rate. So your code becomes:

frappe.ui.form.on("Purchase Invoice", "before_save", function(frm) {
            $.each(frm.doc.items || [], function(i, v) { // for each item on table do this
                var precoAntigo;
                
                frappe.call({
                    method: "frappe.client.get_list",
                    args: {
                        doctype: "Item Price",
                        filters: [
                            ['price_list', "=", frm.doc.buying_price_list],
                            ["item_code", "=", v.item_code],
                        ],
                        fields: [

                            "price_list_rate",
                            "name"

                        ]
                    },
                    
                    callback: function(r) { // do this to found price list doc
                        precoAntigo = (r.message[0].price_list_rate);
                        var nomelista = r.message[0].name;
                        var exchange_rate = flt(frm.doc.conversion_rate);
                        // cLfrm.add_fetch('Purchase Invoice', 'conversion_rate', 'conversion_rate');
                        // console.log(precoAntigo)
                        if (precoAntigo && precoAntigo != v.rate) {
                            var price_list_rate = flt(v.rate) * exchange_rate;
                            frappe.confirm(
                                `Chcesz zaktualizować cenę ${v.item_name} z ${ precoAntigo } na ${price_list_rate} w ${frm.doc.buying_price_list}?`, //ask something to update price
                                function() { // do this if ok
                                    frappe.db.set_value("Item Price", nomelista, "price_list_rate", price_list_rate);
                                },
                                function() { // do nothing if cancel

                                }

                            )
                        }
                    }
                });
            })
})
2 Likes