Skip to content

Commit

Permalink
fix: add banner to update Item GST Details after change in GST Accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanket322 committed Aug 22, 2024
1 parent 48572d2 commit e291de7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
61 changes: 61 additions & 0 deletions india_compliance/gst_india/doctype/gst_settings/gst_settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors
// For license information, please see license.txt
const ALERT_HTML = `
<div class="gst-account-changed-alert alert alert-primary fade show d-flex align-items-center justify-content-between border-0" role="alert">
<div>
Do you want to update Item GST Details based on GST Accounts
<button id="run-patch-button" class="btn btn-primary btn-sm ml-2">
Run patches
</button>
</div>
<button type="button" class="close" data-dismiss="alert">
<span aria-hidden="true">&times;</span>
</button>
</div>
`;

frappe.ui.form.on("GST Settings", {
setup(frm) {
Expand Down Expand Up @@ -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);
Expand Down
61 changes: 61 additions & 0 deletions india_compliance/gst_india/doctype/gst_settings/gst_settings.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit e291de7

Please sign in to comment.