I have deployed a client script to calculate some values and update the values in custom fields while saving sales invoice. Everything works well when I manually generate a new sales invoice.
However, when I generate new sales invoice via data import tool. The script does not work.
frappe.ui.form.on(‘Sales Invoice’, {
validate: function(frm) {
$.each(frm.doc.items || [], function(i, item) {
var customer_gst = frm.doc.billing_address_gstin;
//var company_gst = frappe.sys_defaults.company_gstin;
var tax_template_name = item.item_tax_template;
var matches = tax_template_name.match(/\d+/g);
var tax_rate = matches ? parseFloat(matches[0]) : 0;
var tax_amount = (item.net_amount * tax_rate) / 100;
var total_amt = (item.net_amount + tax_amount);
var igst = 0;
var sgst = 0;
var cgst = 0;
if (customer_gst && customer_gst.substring(0, 2) !== '03') {
igst = tax_amount;
} else {
sgst = tax_amount / 2;
cgst = tax_amount / 2;
}
frappe.model.set_value(item.doctype, item.name, 'tax_amount', tax_amount);
frappe.model.set_value(item.doctype, item.name, 'tax_rate', tax_rate);
frappe.model.set_value(item.doctype, item.name, 'igst_amount', igst);
frappe.model.set_value(item.doctype, item.name, 'sgst_amount', sgst);
frappe.model.set_value(item.doctype, item.name, 'cgst_amount', cgst);
frappe.model.set_value(item.doctype, item.name, 'total_amt', total_amt);
});
}
});
can anyone please help me to have the script run even when I import data via data import tool.
Thanks
NCP
May 22, 2023, 5:18am
#2
Hi @dollar_singh ,
I don’t think, import time client script works but again try and check it.
frappe.ui.form.on('Sales Invoice', {
onload: function(frm) {
updateFields(frm);
},
validate: function(frm) {
updateFields(frm);
}
});
function updateFields(frm) {
$.each(frm.doc.items || [], function(i, item) {
var customer_gst = frm.doc.billing_address_gstin;
var tax_template_name = item.item_tax_template;
var matches = tax_template_name.match(/\d+/g);
var tax_rate = matches ? parseFloat(matches[0]) : 0;
var tax_amount = (item.net_amount * tax_rate) / 100;
var total_amt = item.net_amount + tax_amount;
var igst = 0;
var sgst = 0;
var cgst = 0;
if (customer_gst && customer_gst.substring(0, 2) !== '03') {
igst = tax_amount;
} else {
sgst = tax_amount / 2;
cgst = tax_amount / 2;
}
frappe.model.set_value(item.doctype, item.name, 'tax_amount', tax_amount);
frappe.model.set_value(item.doctype, item.name, 'tax_rate', tax_rate);
frappe.model.set_value(item.doctype, item.name, 'igst_amount', igst);
frappe.model.set_value(item.doctype, item.name, 'sgst_amount', sgst);
frappe.model.set_value(item.doctype, item.name, 'cgst_amount', cgst);
frappe.model.set_value(item.doctype, item.name, 'total_amt', total_amt);
});
}
Otherwise, you can use the server-side script. and use before_save/before_validate/after_save doctype event.
for item in doc.items:
customer_gst = doc.billing_address_gstin
tax_template_name = item.item_tax_template
matches = re.findall(r'\d+', tax_template_name)
tax_rate = float(matches[0]) if matches else 0
tax_amount = (item.net_amount * tax_rate) / 100
total_amt = item.net_amount + tax_amount
igst = 0
sgst = 0
cgst = 0
if customer_gst and customer_gst[:2] != '03':
igst = tax_amount
else:
sgst = tax_amount / 2
cgst = tax_amount / 2
item.tax_amount = tax_amount
item.tax_rate = tax_rate
item.igst_amount = igst
item.sgst_amount = sgst
item.cgst_amount = cgst
item.total_amt = total_amt
Please check and verify the server script, field name, and other attributes.
I hope this helps.
Thank You!
mars
May 22, 2023, 3:08pm
#3
Can confirm. You have to use server-side script for Data Imports.
I have a custom script deleted (created on item doctype) but it still works on item form. And I tried on other browser, it still works, and i reset chrome but it still works. I have a self hosted on virtual box. In fa t the other vms also give same script but it is not in script list.