From e291de723eee7b4e30129f472a7875152bf9e060 Mon Sep 17 00:00:00 2001 From: Sanket322 Date: Thu, 22 Aug 2024 11:36:06 +0530 Subject: [PATCH] fix: add banner to update Item GST Details after change in GST Accounts --- .../doctype/gst_settings/gst_settings.js | 61 +++++++++++++++++++ .../doctype/gst_settings/gst_settings.py | 61 +++++++++++++++++++ 2 files changed, 122 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..638e3f6fa8 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,61 @@ 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.has_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.has_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..17eba78e83 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 @@ -346,6 +348,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 ["company", "account_type", *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():