Hi All,
New developer here. I’ve been adding my custom scripts recently and came across this problem.
Client Side :
I created the following listeners
frappe.ui.form.on(“Quotation Item”, {
rate: function(frm,cdt,cdn){
var child = locals[cdt][cdn];
calculate_line_total_cost(child);
call_python_control(frm);
},
});
Which calls this function
function call_python_control(frm){
/* Sends json of item array to backend control for calculations */
console.log(frm.doc.items);
frappe.call({
method: “erpnext_core_customizations.erpnext_core_customizations.doctype.quotation_custom.quotation_custom.calculate_mro_cost”,
args: {
‘doc’: frm.doc.items
},
callback: function (r){
var result = r.message;
frm.doc.mro_total_cost = result.total_current_cost;
frm.doc.gp_amount = result.gp_amount;
frm.doc.gp_percentage = result.gp_percentage * 100;
}
});
}
Server Side: I dont think the problem is on the server side but here is the code for the python script
@frappe.whitelist()
def calculate_mro_cost(doc):
# @param doc type: json#
# function: calculate gp percentage and amount
doc = json.loads(doc)
total_resale = 0.0
to_set = {‘total_current_cost’: 0.0, ‘gp_amount’: 0.0, ‘gp_percentage’: 0.0, ‘total_resale’: 0.0}
for i in doc:
to_set[‘total_current_cost’] += flt(i.get(‘total_item_cost’, 0.0))
total_resale += flt(i.get(‘base_amount’))
# Calculate GP
to_set[‘gp_amount’] = flt((total_resale - to_set.get(‘total_current_cost’, 0.0)), 2)
to_set[‘gp_percentage’] = flt((to_set.get(‘gp_amount’) / (total_resale or 1)), 2)
to_set[‘total_resale’] = total_resale
return to_set
Now here is where the problem occurs. The result of the console.log on the client side script is below:
Note: base_amount has a value in it which is 20
But when I look at the params that is being passed to the frappe.call the result is below
Note: base_amount has no value in it. As a matter of fact the only value that was changed when the listener was triggered was the rate field.
Because the parameters being received by the server side is wrong the result it returns is wrong. Im just wondering if anyone knows whats going on here. I’ve tried wrapping the call inside frappe.after_ajax and also using async:false but it still doesn’t work.
Any help will be appreciated! Thanks!