diff --git a/india_compliance/gst_india/client_scripts/item.js b/india_compliance/gst_india/client_scripts/item.js index 77a91fa48..9e5ee4aab 100644 --- a/india_compliance/gst_india/client_scripts/item.js +++ b/india_compliance/gst_india/client_scripts/item.js @@ -1,19 +1,45 @@ -frappe.ui.form.on('Item', { - onload: function(frm) { - india_compliance.set_hsn_code_query(frm.get_field("gst_hsn_code")); - }, +frappe.ui.form.on("Item", { + onload: function (frm) { + india_compliance.set_hsn_code_query(frm.get_field("gst_hsn_code")); + }, - gst_hsn_code: function(frm) { - if ((!frm.doc.taxes || !frm.doc.taxes.length) && frm.doc.gst_hsn_code) { - frappe.db.get_doc("GST HSN Code", frm.doc.gst_hsn_code).then(hsn_doc => { - $.each(hsn_doc.taxes || [], function(_, tax) { - let a = frappe.model.add_child(frm.doc, 'Item Tax', 'taxes'); - a.item_tax_template = tax.item_tax_template; - a.tax_category = tax.tax_category; - a.valid_from = tax.valid_from; - frm.refresh_field('taxes'); - }); - }); - } - }, + item_group: async function (frm) { + if (frm.doc.item_group) { + const { message } = await frappe.db.get_value( + "Item Group", + frm.doc.item_group, + "gst_hsn_code" + ); + + if (message.gst_hsn_code && message.gst_hsn_code !== frm.doc.gst_hsn_code) { + frm.set_value("gst_hsn_code", message.gst_hsn_code); + } + } + }, + + before_save: async function (frm) { + if (!frm.doc.gst_hsn_code && frm.doc.item_group) { + const { message } = await frappe.db.get_value( + "Item Group", + frm.doc.item_group, + "gst_hsn_code" + ); + + frm.set_value("gst_hsn_code", message.gst_hsn_code); + } + }, + + gst_hsn_code: function (frm) { + if ((!frm.doc.taxes || !frm.doc.taxes.length) && frm.doc.gst_hsn_code) { + frappe.db.get_doc("GST HSN Code", frm.doc.gst_hsn_code).then(hsn_doc => { + $.each(hsn_doc.taxes || [], function (_, tax) { + let a = frappe.model.add_child(frm.doc, "Item Tax", "taxes"); + a.item_tax_template = tax.item_tax_template; + a.tax_category = tax.tax_category; + a.valid_from = tax.valid_from; + frm.refresh_field("taxes"); + }); + }); + } + }, }); diff --git a/india_compliance/public/js/quick_entry.js b/india_compliance/public/js/quick_entry.js index ba9207193..092f35896 100644 --- a/india_compliance/public/js/quick_entry.js +++ b/india_compliance/public/js/quick_entry.js @@ -24,11 +24,11 @@ class GSTQuickEntryForm extends frappe.ui.form.QuickEntryForm { fieldtype: "Section Break", description: this.api_enabled ? __( - `When you enter a GSTIN, the permanent address linked to it is + `When you enter a GSTIN, the permanent address linked to it is autofilled.
Change the {0} to autofill other addresses.`, - [frappe.meta.get_label("Address", "pincode")] - ) + [frappe.meta.get_label("Address", "pincode")] + ) : "", collapsible: 0, }, @@ -338,7 +338,8 @@ class ItemQuickEntryForm extends frappe.ui.form.QuickEntryForm { } set_hsn_from_item_group() { - let item_group_field = this.dialog.fields_dict.item_group; + const item_group_field = this.dialog.fields_dict.item_group; + const hsn_code_field = this.dialog.fields_dict.gst_hsn_code; item_group_field.df.onchange = async () => { const { message } = await frappe.db.get_value( @@ -346,7 +347,10 @@ class ItemQuickEntryForm extends frappe.ui.form.QuickEntryForm { item_group_field.value, "gst_hsn_code" ); - this.dialog.set_value("gst_hsn_code", message.gst_hsn_code); + + if (message.gst_hsn_code && message.gst_hsn_code !== hsn_code_field.value) { + this.dialog.set_value("gst_hsn_code", message.gst_hsn_code); + } }; } }