Skip to content

Commit

Permalink
Merge pull request #846 from resilient-tech/version-14-hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
sagarvora authored Jul 18, 2023
2 parents 147b0d1 + 589bd82 commit 827e160
Show file tree
Hide file tree
Showing 18 changed files with 843 additions and 47 deletions.
46 changes: 32 additions & 14 deletions india_compliance/gst_india/client_scripts/journal_entry.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
frappe.ui.form.on("Journal Entry", {
refresh: function(frm) {
frm.set_query('company_address', function(doc) {
if(!doc.company) {
frappe.throw(__('Please set Company'));
}
company: set_gstin_options,
});

async function set_gstin_options(frm) {
const options = await india_compliance.get_gstin_options(frm.doc.company);
frm.get_field("company_gstin").set_data(options);

return {
query: 'frappe.contacts.doctype.address.address.address_query',
filters: {
link_doctype: 'Company',
link_name: doc.company
}
};
});
}
frm.set_value("company_gstin", options.length === 1 ? options[0] : "");
}

frappe.ui.form.on("Journal Entry Account", {
account: toggle_company_gstin,
accounts_remove: toggle_company_gstin,
});

async function toggle_company_gstin(frm) {
_toggle_company_gstin(frm, await contains_gst_account(frm));
}

async function contains_gst_account(frm) {
if (!frm.gst_accounts || frm.company !== frm.doc.company) {
frm.gst_accounts = await india_compliance.get_account_options(frm.doc.company);
frm.company = frm.doc.company;
}

return frm.doc.accounts.some(row => frm.gst_accounts.includes(row.account));
}

function _toggle_company_gstin(frm, reqd) {
if (frm.get_field("company_gstin").df.reqd !== reqd) {
frm.set_df_property("company_gstin", "reqd", reqd);
frm.refresh_field("company_gstin");
}
}
38 changes: 16 additions & 22 deletions india_compliance/gst_india/constants/custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,22 @@
"depends_on": "eval:doc.gst_category == 'Overseas' && doc.place_of_supply == '96-Other Countries'",
},
],
("Journal Entry", "GL Entry"): [
{
"fieldname": "company_gstin",
"label": "Company GSTIN",
"fieldtype": "Autocomplete",
"insert_after": "company",
"hidden": 0,
# clear original default values
"read_only": 0,
"print_hide": 0,
"fetch_from": "",
"depends_on": "",
"mandatory_depends_on": "",
"translatable": 0,
}
],
# Transaction Item Fields
(
"Material Request Item",
Expand Down Expand Up @@ -541,28 +557,6 @@
"mandatory_depends_on": "eval:doc.voucher_type == 'Reversal Of ITC'",
"translatable": 0,
},
{
"fieldname": "company_address",
"label": "Company Address",
"fieldtype": "Link",
"options": "Address",
"insert_after": "reversal_type",
"print_hide": 1,
"depends_on": "eval:doc.voucher_type == 'Reversal Of ITC'",
"mandatory_depends_on": "eval:doc.voucher_type == 'Reversal Of ITC'",
},
{
"fieldname": "company_gstin",
"label": "Company GSTIN",
"fieldtype": "Data",
"read_only": 1,
"insert_after": "company_address",
"print_hide": 1,
"fetch_from": "company_address.gstin",
"depends_on": "eval:doc.voucher_type == 'Reversal Of ITC'",
"mandatory_depends_on": "eval:doc.voucher_type=='Reversal Of ITC'",
"translatable": 0,
},
],
"Tax Category": [
{
Expand Down
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)
25 changes: 25 additions & 0 deletions india_compliance/gst_india/overrides/gl_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import frappe
from frappe import _

from india_compliance.gst_india.overrides.transaction import (
is_indian_registered_company,
)
from india_compliance.gst_india.utils import get_all_gst_accounts


def validate(doc, method=None):
if doc.company_gstin or not is_indian_registered_company(doc):
return

gst_accounts = get_all_gst_accounts(doc.company)
if doc.account not in gst_accounts:
return

frappe.throw(
_("Company GSTIN is a mandatory field for accounting of GST Accounts.")
)


def update_gl_dict_with_regional_fields(doc, gl_dict):
if doc.get("company_gstin"):
gl_dict["company_gstin"] = doc.company_gstin
23 changes: 23 additions & 0 deletions india_compliance/gst_india/overrides/journal_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import frappe
from frappe import _

from india_compliance.gst_india.overrides.transaction import (
is_indian_registered_company,
)
from india_compliance.gst_india.utils import get_all_gst_accounts


def validate(doc, method=None):
if doc.company_gstin or not is_indian_registered_company(doc):
return

# validate company_gstin
contains_gst_account = False
gst_accounts = get_all_gst_accounts(doc.company)
for row in doc.accounts:
if row.account in gst_accounts:
contains_gst_account = True
break

if contains_gst_account:
frappe.throw(_("Company GSTIN is mandatory if any GST account is present."))
Empty file.
Loading

0 comments on commit 827e160

Please sign in to comment.