Skip to content

Commit

Permalink
fix: provision to update gst category using API (#834)
Browse files Browse the repository at this point in the history
Co-authored-by: Smit Vora <[email protected]>
  • Loading branch information
DaizyModi and vorasmit authored Jul 14, 2023
1 parent b25cb94 commit 11c0a6a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 11 deletions.
35 changes: 35 additions & 0 deletions india_compliance/gst_india/doctype/gst_settings/gst_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ frappe.ui.form.on("GST Settings", {
});
},
onload: show_ic_api_promo,
refresh: show_update_gst_category_button,
attach_e_waybill_print(frm) {
if (!frm.doc.attach_e_waybill_print || frm.doc.fetch_e_waybill_data) return;
frm.set_value("fetch_e_waybill_data", 1);
Expand Down Expand Up @@ -92,6 +93,40 @@ function show_ic_api_promo(frm) {
});
}

function show_update_gst_category_button(frm) {
if (
!frm.doc.__onload?.has_missing_gst_category ||
!india_compliance.is_api_enabled() ||
!frm.doc.autofill_party_info
)
return;

frm.add_custom_button(__("Update GST Category"), () => {
frappe.msgprint({
title: __("Update GST Category"),
message: __(
"Confirm to update GST Category for all Address where its missing using API. It is missing for these <a><span class='custom-link' data-fieldtype='Link' data-doctype='Address'>Addresses</span><a>."
),
primary_action: {
label: __("Update"),
server_action:
"india_compliance.gst_india.doctype.gst_settings.gst_settings.enqueue_update_gst_category",
hide_on_success: true,
},
});

$(document).on("click", ".custom-link", function () {
const doctype = $(this).attr("data-doctype");

frappe.route_options = {
gst_category: ["is", "not set"],
};

frappe.set_route("List", doctype);
});
});
}

function set_auto_generate_e_waybill(frm) {
if (!frm.doc.enable_e_invoice) return;

Expand Down
53 changes: 48 additions & 5 deletions india_compliance/gst_india/doctype/gst_settings/gst_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from frappe.model.document import Document
from frappe.utils import getdate

from india_compliance.gst_india.constants import GST_ACCOUNT_FIELDS
from india_compliance.gst_india.constants import GST_ACCOUNT_FIELDS, GST_PARTY_TYPES
from india_compliance.gst_india.constants.custom_fields import (
E_INVOICE_FIELDS,
E_WAYBILL_FIELDS,
Expand All @@ -16,18 +16,20 @@
_disable_api_promo,
post_login,
)
from india_compliance.gst_india.utils import can_enable_api
from india_compliance.gst_india.utils import can_enable_api, is_api_enabled
from india_compliance.gst_india.utils.custom_fields import toggle_custom_fields
from india_compliance.gst_india.utils.gstin_info import get_gstin_info

E_INVOICE_START_DATE = "2021-01-01"


class GSTSettings(Document):
def onload(self):
if can_enable_api(self) or frappe.db.get_global("ic_api_promo_dismissed"):
return
if is_api_enabled(self) and frappe.db.get_global("has_missing_gst_category"):
self.set_onload("has_missing_gst_category", True)

self.set_onload("can_show_promo", True)
if not (can_enable_api(self) or frappe.db.get_global("ic_api_promo_dismissed")):
self.set_onload("can_show_promo", True)

def validate(self):
self.update_dependant_fields()
Expand Down Expand Up @@ -221,3 +223,44 @@ def validate_e_invoice_applicable_companies(self):
def disable_api_promo():
if frappe.has_permission("GST Settings", "write"):
_disable_api_promo()


@frappe.whitelist()
def enqueue_update_gst_category():
frappe.msgprint(
_("Updating GST Category in background"),
alert=True,
)

frappe.enqueue(update_gst_category, queue="long", timeout=6000)


def update_gst_category():
if not is_api_enabled():
return

# get all Addresses with linked party
address_without_category = frappe.get_all(
"Address",
fields=("name", "gstin"),
filters={
"link_doctype": ("in", GST_PARTY_TYPES),
"link_name": ("!=", ""),
"gst_category": ("in", ("", None)),
},
)

# party-wise addresses
category_map = {}
for address in address_without_category:
gstin_info = get_gstin_info(address.gstin)
gst_category = gstin_info.gst_category

category_map.setdefault(gst_category, []).append(address.name)

for gst_category, addresses in category_map.items():
frappe.db.set_value(
"Address", {"name": ("in", addresses)}, "gst_category", gst_category
)

frappe.db.set_global("has_missing_gst_category", None)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import frappe

from india_compliance.gst_india.constants import GST_PARTY_TYPES
from india_compliance.gst_india.doctype.gst_settings.gst_settings import (
enqueue_update_gst_category,
)
from india_compliance.gst_india.utils import is_api_enabled


def execute():
Expand Down Expand Up @@ -85,11 +89,10 @@ def update_gstin_and_gst_category():
# update gstin in party only where there is one gstin per party
if len(gstins) == 1:
default_gstin = next(iter(gstins))
new_gstins.setdefault((doctype, default_gstin), []).append(
party.name
)
else:
print_warning = True
default_gstin = list(gstins)[0]

new_gstins.setdefault((doctype, default_gstin), []).append(party.name)

for address in address_list:
# User may have already set GST category in Address
Expand All @@ -115,10 +118,14 @@ def update_gstin_and_gst_category():
)

if print_warning:
frappe.db.set_global("has_missing_gst_category", 1)

click.secho(
"We have identified multiple GSTINs for a few parties and couldn't set"
" newly created fields automatically for these. Please check for parties"
" Please check for parties"
" without GSTINs or addresses without GST Category and set approporiate"
" values.\n",
fg="yellow",
)

if is_api_enabled():
enqueue_update_gst_category()

0 comments on commit 11c0a6a

Please sign in to comment.