From 93ee3298b1cd72b346b0ecd008dbb81555b60305 Mon Sep 17 00:00:00 2001 From: Sanket322 Date: Thu, 22 Aug 2024 11:20:49 +0530 Subject: [PATCH] fix: add banner to update Item GST Details after change in GST Accounts --- .../doctype/gst_settings/gst_settings.js | 62 ++++++++++++++++ .../doctype/gst_settings/gst_settings.py | 71 +++++++++++++++++++ 2 files changed, 133 insertions(+) diff --git a/india_compliance/gst_india/doctype/gst_settings/gst_settings.js b/india_compliance/gst_india/doctype/gst_settings/gst_settings.js index 378ff8f94e..30f4bc056e 100644 --- a/india_compliance/gst_india/doctype/gst_settings/gst_settings.js +++ b/india_compliance/gst_india/doctype/gst_settings/gst_settings.js @@ -1,5 +1,18 @@ // Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors // For license information, please see license.txt +const ALERT_HTML = ` + +`; frappe.ui.form.on("GST Settings", { setup(frm) { @@ -33,13 +46,62 @@ frappe.ui.form.on("GST Settings", { enable_e_invoice: set_auto_generate_e_waybill, auto_generate_e_invoice: set_auto_generate_e_waybill, generate_e_waybill_with_e_invoice: set_auto_generate_e_waybill, + before_save: async function (frm) { + frm.gst_account_changed = await frm + .call("check_gst_account_changes") + .then(r => r.message); + }, after_save(frm) { // sets latest values in frappe.boot for current user // other users will still need to refresh page Object.assign(gst_settings, frm.doc); + show_gst_account_alert(frm); }, }); +function show_gst_account_alert(frm) { + if (!frm.gst_account_changed) return; + //alert already exists + if (frm.layout.wrapper.find(".gst-account-changed-alert").length !== 0) return; + + const alert_element = $(ALERT_HTML).prependTo(frm.layout.wrapper); + + alert_element + .find("#run-patch-button") + .on("click", () => open_patch_schedule_dialog()); +} + +function open_patch_schedule_dialog() { + const dialog = new frappe.ui.Dialog({ + title: __("Schedule Patch Execution Time"), + fields: [ + { + label: "Execution Time", + fieldname: "execution_time", + fieldtype: "Datetime", + default: `${frappe.datetime.add_days( + frappe.datetime.now_date(), + 1 + )} 02:00:00`, + }, + ], + primary_action_label: __("Schedule"), + primary_action(values) { + if (values.execution_time < frappe.datetime.now_datetime()) { + frappe.msgprint(__("Patch run time cannot be in the past")); + return; + } + dialog.hide(); + + frappe.call({ + method: "india_compliance.gst_india.doctype.gst_settings.gst_settings.schedule_gst_patches", + args: { cron_time: values.execution_time }, + }); + }, + }); + dialog.show(); +} + function filter_accounts(frm, account_field) { frm.set_query(account_field, "gst_accounts", (_, cdt, cdn) => { const row = frappe.get_doc(cdt, cdn); diff --git a/india_compliance/gst_india/doctype/gst_settings/gst_settings.py b/india_compliance/gst_india/doctype/gst_settings/gst_settings.py index 4f98e12359..be57749a6c 100644 --- a/india_compliance/gst_india/doctype/gst_settings/gst_settings.py +++ b/india_compliance/gst_india/doctype/gst_settings/gst_settings.py @@ -1,6 +1,8 @@ # Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt +from datetime import datetime + import frappe from frappe import _ from frappe.model.document import Document @@ -24,6 +26,16 @@ E_INVOICE_START_DATE = "2021-01-01" +gst_account_fields = ( + "company", + "account_type", + "cgst_account", + "sgst_account", + "igst_account", + "cess_account", + "cess_non_advol_account", +) + class GSTSettings(Document): def onload(self): @@ -346,6 +358,65 @@ def has_valid_credentials(self, gstin, service, throw=False): return True + @frappe.whitelist() + def check_gst_account_changes(self): + previous_doc = self.get_doc_before_save() + old_gst_accounts = {item.name: item for item in previous_doc.gst_accounts} + new_gst_accounts = {item.name: item for item in self.gst_accounts} + + if len(new_gst_accounts) != len(old_gst_accounts): + return True + + for account_name in new_gst_accounts: + old_account = old_gst_accounts.get(account_name) + + if self.has_row_changed(old_account, new_gst_accounts[account_name]): + return True + + return False + + def has_row_changed(self, old_gst_account, new_gst_account): + if old_gst_account is None: + return True + + return any( + old_gst_account.get(field) != new_gst_account.get(field) + for field in gst_account_fields + ) + + +@frappe.whitelist() +def schedule_gst_patches(cron_time): + dt = datetime.strptime(cron_time, "%Y-%m-%d %H:%M:%S") + cron_format = f"{dt.minute} {dt.hour} {dt.day} {dt.month} {dt.weekday() + 1}" + + doc = frappe.get_doc( + { + "doctype": "Scheduled Job Type", + "method": "india_compliance.gst_india.doctype.gst_settings.gst_settings.apply_gst_patches", + "cron_format": cron_format, + "frequency": "Cron", + "create_log": 1, + } + ) + doc.insert() + + +def apply_gst_patches(): + from india_compliance.patches.post_install.improve_item_tax_template import ( + execute as execute_improve_item_tax_template, + ) + from india_compliance.patches.post_install.set_gst_tax_type import ( + execute as execute_set_gst_tax_type, + ) + from india_compliance.patches.post_install.update_gst_treatment_for_taxable_nil_transaction_item import ( + execute as execute_update_gst_treatment, + ) + + execute_set_gst_tax_type() + execute_update_gst_treatment() + execute_improve_item_tax_template() + @frappe.whitelist() def disable_api_promo():