Try ERPNext Buy Support Partners Foundation

Frappe call passing wrong parameters even though console.log before it shows right parameters

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 ®{
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!

Hi all,

I’ve solved the problem by adding an onblur event on the rate field like so.

frappe.ui.form.on(“Quotation”, {
onload_post_render: function(frm,cdt,cdn){
frm.fields_dict.items.grid.wrapper.on(‘blur’, ‘input[data-fieldname=“rate”][data-doctype=“Quotation Item”]’,
function(e){
var child = locals[cdt][cdn];
call_python_control(frm, child);
})
}
});

and I also added an option to process the local cache and fail back to the frm object.

function call_python_control(frm, child=[]){
/*@param {Object} frm = frm object
@param {Object} child = local cached data
Sends json of item array to backend control for calculations */
var data_process = ‘’;
if (child.length > 0){
data_process = child;
}
else{
data_process = frm.doc.items;
}
frappe.call({
method: “erpnext_core_customizations.erpnext_core_customizations.doctype.quotation_custom.quotation_custom.calculate_mro_cost”,
args: {
‘doc’: data_process
},
callback: function ®{
var result = r.message;
frm.set_value(‘mro_total_cost’, result.total_current_cost);
frm.set_value(‘gp_amount’,result.gp_amount);
frm.set_value(‘gp_percentage’, result.gp_percentage * 100);
}
});
}

This post can be closed now

2 Likes

Hi Dan, unfortunately you’re only getting moral support from me, in return I’m learning something :wink:

It may help others if you further describe your problem. E.g. what is the use case you are trying to address, and is your solution modeled on something already existing? (or a contra-example if not). Is the specific error symptom only that base_amount in the frappe call is “0”? Are there more parameter values that are being changed?

When you say that only “rate” changed when the listener was triggered, was this expected? I.e. did the rate intentionally change?

Good luck!
Dale

1 Like

Hi Dale,

Thanks for your response. What I’m trying to do is calculate cost of all items per line according to the valuation rate. Sum all the cost and calculate the Gross Profit percentage against the rate of the item. The rate change is expected. In the quotation form if you change the rate of the item it automatically fills up some data fields for you (eg. total amount, base rate).
My custom script takes those data that is automatically filled up and process it but it looks like when the script is run the data isn’t there yet. I suspect this has something to do with how those fields are filled up when an onchange event is detected. So my solution was instead of hooking into the onchange event I hooked in to the on blur event instead

1 Like