For anyone looking to do something similar in the future, I ended up just faking it. The downside to this approach is that the standard rate from the item does not get pulled into the material request automatically, and must be set manually by whoever is making the request.
I added 2 custom fields, “Price” and “Currency” and changed the label of “Rate” to “Price in RON” (the default currency for my company). You’ll also notice “total” in the custom script below, this is just a thing I include all over the place because it’s helpful.
I then wrote a python script in my custom app, making sure it was whitelisted and added to the SOURCES.txt file:
@frappe.whitelist()
def get_exchange_rate(from_currency=None,to_currency=None):
value = frappe.get_list("Currency Exchange", fields=["exchange_rate"], filters={'to_currency':to_currency,'from_currency':from_currency}, order_by="date desc",limit=1)
if(value[0]):
return value[0]['exchange_rate']
else:
return 1
Then I created this custom script to move the data around correctly with exchange rate added whenever the currency or price changed:
frappe.ui.form.on("Material Request",{
validate: function(frm)
{
var grand_total = 0;
$.each(frm.doc.items, function(i, d) {
grand_total += d.amount;
})
frm.set_value("total",grand_total);
frm.refresh_field("total");
}});
//GRAND TOTAL AND EXCHANGE RATE CALCULATIONS
// d.rate is an inbuilt field that moves to other doctypes and should be set
// Other doctypes (until you get to purchase orders) expect this value to be in RON, so it must be converted
// d.currency and d.price are custom fields used to set this value
// grand_total is used to set the total on the whole material request
frappe.ui.form.on("Material Request Item", "price", function(frm, cdt, cdn) {
var item = frappe.get_doc(cdt,cdn);
if(item.currency=="RON") {
item.rate = flt(item.price);
item.amount = item.rate*item.qty;
item.refresh_field("amount");
}
else {
frappe.call({
method:"britishschool.get_exchange_rate.get_exchange_rate",
args:{
to_currency:"RON",from_currency:item.currency,
},
callback: function(r) {
item.rate = flt(item.price*r.message);
item.amount = item.rate*item.qty;
item.refresh_field("amount");
}
});
}
});
frappe.ui.form.on("Material Request Item", "currency", function(frm, cdt, cdn) {
var item = frappe.get_doc(cdt,cdn);
if(item.currency=="RON") {
item.rate = flt(item.price);
item.amount = item.rate*item.qty;
item.refresh_field("amount");
}
else {
frappe.call({
method:"MYCUSTOMAPP.get_exchange_rate.get_exchange_rate",
args:{
to_currency:"RON",from_currency:item.currency,
},
callback: function(r) {
console.log(item.price);
item.rate = flt(item.price*r.message);
item.amount = item.rate*item.qty;
item.refresh_field("amount");
}
});
}
});