Here is my problem.
On my custom application, I have a JavaScript that contains one function and one trigger
like so:
Function
function calculate(){
frm.doc.items.forEach((item_row, index) => {
if (item_row.name == cdn) {
stock_qty_for_calculation = (item_row.qty * item_row.conversion_factor);
};
}
Trigger
frappe.ui.form.on(“Sales Invoice”, {
onload_post_render: function(frm, cdt, cdn){
// Code goes here
frm.fields_dict.items.grid.wrapper.on(‘blur’, ‘input[data-fieldname=“conversion_factor”][data-doctype=“Sales Invoice Item”]’, function(e) {
console.log(“Now blurring from the Item Code Field”);
setTimeout(function() { calculate(frm, cdt, cdn) }, 100);
});
frm.fields_dict.items.grid.wrapper.on(‘blur’, ‘input[data-fieldname=“qty”][data-doctype=“Sales Invoice Item”]’, function(e) {
console.log(“Now blurring from the Item Code Field”);
setTimeout(function() { calculate(frm, cdt, cdn) }, 100);
});
frm.fields_dict.items.grid.wrapper.on(‘blur’, ‘input[data-fieldname=“uom”][data-doctype=“Sales Invoice Item”]’, function(e) {
console.log(“Now blurring from the Item Code Field”);
setTimeout(function() { calculate(frm, cdt, cdn) }, 100);
});
}
});
These run on Sales Invoice and Sales Invoice Item Doctype. The user creates a new
sales invoice, adds a customer, dates and fills other fields. Then the user moves down to
the items child table and enters the first item. ERPNext will load a default quantity, rate
and estimate the amount.
*This should also apply for Sales Invoices which have been created from Delivery Notes or
Sales Orders or similar!
At this point the user has not clicked on the arrow control in the child table
that allows you to modify more information on the child table. Once clicked, a pop up
comes up with additional fields for modification.
The expected behavior is for the function to run when the user has just changed data in
the rate, uom or conversion factor field and is now moving onto another field. When the code runs it should calculate
what is the amount of stock units of measure based on the recently entered or loaded conversion factor
multiplied by the quantity of purchase uom entered.
The trigger executes the function precisely on cue (when blurring or leaving the field by
clicking elsewhere, or moving to the next field with TAB). Basically, my triggers and listener events are setup properly and run the code as expected.
The function runs just fine, and otherwise calculates appropriately. Its purpose it to estimate
the stock_qty of the item being entered in that line, so that I can estimate the “special”
or excise tax as the product of the excise tax amount multiplied by the stock quantity.
Now, whenever the user changes the Purchase Unit of Measure, or otherwise adjusts the quantity
purchased in terms of that Unit of Measure, the conversion factor field will automatically behave
as it should, by either:
- Loading a pre-defined conversion factor
- Remaining at zero, for the user to manually enter a conversion factor.
Upon changing the Purchase Unit of Measure, or Conversion Factor field the function is run,
but my calculation comes out with arithmetical errors. The first time it calculates correctly, but for subsequent times
I have found that there is an apparent “lag” in the variables, meaning that the results shown
in the console or target fields refer to the previous quantity, uom or conversion factor values, NOT to the most recently entered. Basically this means that it is using “old” or “stale” variables at some point.
So far I have figured out that somehow this code might be what I need to access:
cur_frm.fields_dict.items.wrapper.innerText
However I am confused and would prefer to access the DOM directly to modify the object values
Or should I get into parsing the actual HTML for the added pop up element, and in that parsing add the
resulting calculation?
Also, my calculations do not run properly upon simply just “adding an item”
When I add an item, with a default quantity of “1”, the calculations will not run. It is only when the quantity field is updated manually, that the calculations run appropriately. How can I circumvent this?
Thanks!