Skip to content

Commit

Permalink
refactor: GST Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sagarvora committed Mar 30, 2022
1 parent 6cab203 commit 55f2940
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 108 deletions.
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
tags
.vscode/
__pycache__/

# docs
india_compliance/docs/current

# JS
dist/
node_modules/
india_compliance/docs/current
node_modules
7 changes: 6 additions & 1 deletion india_compliance/gst_india/client_scripts/delivery_note.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ frappe.ui.form.on(DOCTYPE, {
});
},
refresh(frm) {
if(frm.doc.docstatus != 1 || frm.is_dirty() || frm.doc.ewaybill || !frappe.boot.gst_settings.enable_e_waybill) return;
if (
!frappe.boot.gst_settings.enable_e_waybill
||frm.doc.docstatus != 1
|| frm.is_dirty()
|| frm.doc.ewaybill
) return;

frm.add_custom_button('e-Waybill JSON', () => {
open_url_post(frappe.request.url, {
Expand Down
4 changes: 2 additions & 2 deletions india_compliance/gst_india/client_scripts/sales_invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ frappe.ui.form.on(DOCTYPE, {
if (frm.doc.ewaybill && frm.doc.ewaybill.length == 12 && settings.enable_api) {
frm.set_df_property('ewaybill', 'allow_on_submit', 0);
}
if(frm.doc.docstatus != 1 || frm.is_dirty() || frm.doc.ewaybill || !settings.enable_e_waybill || !is_e_waybill_applicable(frm, settings.e_waybill_criteria)) return;
if(frm.doc.docstatus != 1 || frm.is_dirty() || frm.doc.ewaybill || !settings.enable_e_waybill || !is_e_waybill_applicable(frm, settings.e_waybill_threshold)) return;
// ewaybill is applicable and not created or updated.
frm.dashboard.add_comment("e-Waybill is applicable for this invoice and not yet generated or updated.", "yellow");

Expand All @@ -48,7 +48,7 @@ frappe.ui.form.on(DOCTYPE, {

on_submit(frm) {
let settings = frappe.boot.gst_settings
if (frm.doc.ewaybill || !settings.enable_api || !settings.generate_e_waybill_on_submit || !is_e_waybill_applicable(frm, settings.e_waybill_criteria)) return;
if (frm.doc.ewaybill || !settings.enable_api || !settings.auto_generate_e_waybill || !is_e_waybill_applicable(frm, settings.e_waybill_threshold)) return;
frappe.call({
method: "india_compliance.gst_india.utils.e_waybill.generate_e_waybill_if_possible",
args: {
Expand Down
19 changes: 3 additions & 16 deletions india_compliance/gst_india/doctype/gst_settings/gst_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,9 @@ frappe.ui.form.on('GST Settings', {
return get_gstin_query(row.company);
});
},

filter_accounts: function(frm, account_field) {
frm.set_query(account_field, "gst_accounts", function(doc, cdt, cdn) {
var row = locals[cdt][cdn];
return {
filters: {
company: row.company,
account_type: "Tax",
is_group: 0
}
};
});
},
attach_e_waybill_print: function(frm) {
if(!frm.doc.attach_e_waybill_print || frm.doc.get_data_for_print) return;
frm.set_value("get_data_for_print", 1);
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);
},
after_save(frm) {
// sets latest values in frappe.boot for current user
Expand Down
104 changes: 52 additions & 52 deletions india_compliance/gst_india/doctype/gst_settings/gst_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@
"min_hsn_digits",
"e_waybill_section",
"enable_e_waybill",
"e_waybill_criteria",
"e_waybill_threshold",
"column_break_10",
"generate_e_waybill_on_submit",
"get_data_for_print",
"auto_generate_e_waybill",
"fetch_e_waybill_data",
"attach_e_waybill_print",
"e_invoice_section",
"enable_e_invoice",
"generate_e_invoice_on_submit",
"enable_e_invoicing",
"auto_generate_e_invoice",
"column_break_17",
"e_invoice_applicable_from",
"accounts_tab",
"gst_accounts",
"credentials_tab",
"api_secret",
"enable_api",
"credentials",
"api_secret"
"credentials"
],
"fields": [
{
Expand Down Expand Up @@ -98,7 +98,6 @@
"label": "General"
},
{
"depends_on": "eval:doc.api_secret",
"fieldname": "e_waybill_section",
"fieldtype": "Section Break",
"label": "e-Waybill"
Expand All @@ -107,88 +106,89 @@
"default": "0",
"fieldname": "enable_e_waybill",
"fieldtype": "Check",
"label": "Enable e-Waybill"
"label": "Enable e-Waybill Features"
},
{
"fieldname": "column_break_10",
"fieldtype": "Column Break"
},
{
"default": "0",
"depends_on": "eval: doc.api_secret && doc.enable_e_waybill && doc.enable_api",
"description": "e-Waybill shall be generated on submit of Sales Invoice if criteria is met, and data is valid and available.",
"fieldname": "generate_e_waybill_on_submit",
"fieldtype": "Check",
"label": "Generate e-Waybill on Submit"
},
{
"default": "50000",
"depends_on": "eval: doc.enable_e_waybill",
"description": "You would be alerted if you are selling goods with total invoice value above this criteria and e-Waybill is not generated.",
"fieldname": "e_waybill_criteria",
"fieldtype": "Int",
"label": "e-Waybill Criteria"
},
{
"default": "0",
"depends_on": "eval: doc.api_secret && doc.enable_e_waybill && doc.enable_api",
"description": "Would you like to make print for e-Waybill? This will automatically fetch data from NIC immediately after you generate e-Waybill for you to make prints for e-Waybill. This would lead to additional API requests.",
"fieldname": "get_data_for_print",
"fieldtype": "Check",
"label": "Get e-Waybill Data for Manual Print",
"read_only_depends_on": "eval: doc.attach_e_waybill_print"
},
{
"default": "0",
"depends_on": "eval: doc.api_secret && doc.enable_e_waybill && doc.enable_api",
"description": "Would you like to save on your storage? Disable this to manually print the e-Waybill. Else, we shall attach the e-Waybill print on its generation automatically to the relevant document.",
"depends_on": "eval: doc.enable_api && doc.enable_e_waybill",
"description": "If checked, a PDF of e-Waybill will be automatically attached to the Invoice after generation",
"fieldname": "attach_e_waybill_print",
"fieldtype": "Check",
"label": "Attach e-Waybill Print on Generation"
"label": "Attach e-Waybill Print After Generation"
},
{
"default": "0",
"description": "Enable this to use API features including e-Waybill, e-Invoice or Autofill using Public APIs",
"fieldname": "enable_api",
"fieldtype": "Check",
"label": "Enable API"
"label": "Enable API Features"
},
{
"depends_on": "eval: doc.enable_api",
"fieldname": "e_invoice_section",
"fieldtype": "Section Break",
"label": "e-Invoice"
},
{
"depends_on": "eval: doc.enable_e_invoicing",
"description": "Posting Date from which e-Invoicing features should be enabled",
"fieldname": "e_invoice_applicable_from",
"fieldtype": "Date",
"label": "e-Invoice Applicable From",
"mandatory_depends_on": "eval: doc.enable_e_invoicing"
},
{
"fieldname": "column_break_17",
"fieldtype": "Column Break"
},
{
"default": "50000",
"depends_on": "eval: doc.enable_e_waybill",
"description": "When selling goods with total Sales Invoice value greater than this amount, a prompt will be displayed if e-Waybill hasn't been generated upon submission",
"fieldname": "e_waybill_threshold",
"fieldtype": "Currency",
"label": "Invoice Value Threshold for e-Waybill Generation"
},
{
"default": "0",
"fieldname": "enable_e_invoice",
"depends_on": "eval: doc.enable_api && doc.enable_e_waybill",
"description": "e-Waybill will be automatically generated after Sales Invoice submission if the invoice value threshold is met, and data is available and valid",
"fieldname": "auto_generate_e_waybill",
"fieldtype": "Check",
"label": "Enable e-Invoice"
"label": "Automatically Generate e-Waybill on Invoice Submission"
},
{
"depends_on": "eval: doc.enable_e_invoice",
"fieldname": "e_invoice_applicable_from",
"fieldtype": "Date",
"label": "Applicable From",
"mandatory_depends_on": "eval: doc.enable_e_invoice"
"default": "0",
"depends_on": "eval: doc.enable_api && doc.enable_e_waybill",
"description": "Automatically fetch e-Waybill data after e-Waybill generation for printing or logging purposes. This will lead to one additional API request.",
"fieldname": "fetch_e_waybill_data",
"fieldtype": "Check",
"label": "Fetch e-Waybill Data After Generation",
"read_only_depends_on": "eval: doc.attach_e_waybill_print"
},
{
"default": "0",
"depends_on": "eval: doc.enable_e_invoice",
"description": "e-Invoice shall be generated on submit of Sales Invoice if criteria is met, and data is valid and available.",
"fieldname": "generate_e_invoice_on_submit",
"fieldname": "enable_e_invoicing",
"fieldtype": "Check",
"label": "Generate e-Invoice on Submit"
"label": "Enable e-Invoicing"
},
{
"fieldname": "column_break_17",
"fieldtype": "Column Break"
"default": "0",
"depends_on": "eval: doc.enable_e_invoicing",
"description": "e-Invoice will be automatically generated after Sales Invoice submission if applicable",
"fieldname": "auto_generate_e_invoice",
"fieldtype": "Check",
"label": "Automatically Generate e-Invoice on Invoice Submission"
}
],
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2022-03-29 19:00:18.162546",
"modified": "2022-03-30 06:17:50.827943",
"modified_by": "Administrator",
"module": "GST India",
"name": "GST Settings",
Expand Down
54 changes: 39 additions & 15 deletions india_compliance/gst_india/doctype/gst_settings/gst_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from frappe import _
from frappe.custom.doctype.custom_field.custom_field import create_custom_fields
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.e_waybill import (
Expand All @@ -18,9 +19,17 @@ class GSTSettings(Document):
def validate(self):
self.validate_gst_accounts()
self.validate_e_invoice_applicability_date()
self.update_dependant_fields()

def update_dependant_fields(self):
if not self.api_secret:
self.enable_api = 0

if self.attach_e_waybill_print:
self.fetch_e_waybill_data = 1

def on_update(self):
frappe.enqueue(self.create_or_delete_fields)
frappe.enqueue(self.update_custom_fields)

# clear session boot cache
frappe.cache().delete_keys("bootinfo")
Expand Down Expand Up @@ -62,24 +71,39 @@ def validate_gst_accounts(self):

account_types.append(row.account_type)

def create_or_delete_fields(self):
if not self.has_value_changed("enable_e_waybill"):
return
def update_custom_fields(self):
if self.has_value_changed("enable_e_waybill"):
_update_custom_fields(E_WAYBILL_FIELDS, self.enable_e_waybill)
_update_custom_fields(
E_WAYBILL_API_FIELDS, self.enable_api and self.enable_e_waybill
)

if self.enable_e_waybill:
create_custom_fields(E_WAYBILL_FIELDS, update=True)
if self.enable_api:
create_custom_fields(E_WAYBILL_API_FIELDS, update=True)
else:
delete_custom_fields(E_WAYBILL_FIELDS)
delete_custom_fields(E_WAYBILL_API_FIELDS)
if self.has_value_changed("enable_api"):
_update_custom_fields(
E_WAYBILL_API_FIELDS, self.enable_api and self.enable_e_waybill
)

def validate_e_invoice_applicability_date(self):
if not self.enable_api or not self.enable_e_invoice:
if not self.enable_api or not self.enable_e_invoicing:
return

if not self.e_invoice_applicable_from:
frappe.throw(_("Applicable from date is mandatory for enabling e-Invoice"))
frappe.throw(
_("{0} is mandatory for enabling e-Invoice").format(
frappe.bold(self.meta.get_label("e_invoice_applicable_from"))
)
)

if getdate(self.e_invoice_applicable_from) < getdate("2021-01-01"):
frappe.throw(
_("{0} cannot be before 2021-01-01").format(
frappe.bold(self.meta.get_label("e_invoice_applicable_from"))
)
)


if self.e_invoice_applicable_from < "2021-01-01":
frappe.throw(_("Applicable from date cannot be before 2021-01-01"))
def _update_custom_fields(fields, condition):
if condition:
create_custom_fields(fields)
else:
delete_custom_fields(fields)
2 changes: 1 addition & 1 deletion india_compliance/gst_india/setup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def after_install():
# Validation ignored for faster creation
# Will not fail if a core field with same name already exists (!)
# Will update a custom field if it already exists
create_custom_fields(CUSTOM_FIELDS, ignore_validate=True, update=True)
create_custom_fields(CUSTOM_FIELDS, ignore_validate=True)

create_property_setters()
create_address_template()
Expand Down
16 changes: 8 additions & 8 deletions india_compliance/gst_india/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,15 @@ def delete_custom_fields(custom_fields):
doctypes = (doctypes,)

for doctype in doctypes:
for df in fields:
try:
frappe.delete_doc(
"Custom Field", doctype + "-" + df.get("fieldname")
)
except Exception:
pass
frappe.db.delete(
"Custom Field",
{
"fieldname": ("in", [field.fieldname for field in fields]),
"dt": doctype,
},
)

frappe.clear_cache(doctype=doctype)
frappe.clear_cache(doctype=doctype)


def add_spacing(value, num):
Expand Down
2 changes: 1 addition & 1 deletion india_compliance/gst_india/utils/e_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def check_e_invoice_applicability(self):

if not self.settings.enable_api:
frappe.throw(_("Enable GST API in GST Settings"))
if not self.settings.enable_e_invoice:
if not self.settings.enable_e_invoicing:
frappe.throw(_("Enable e-Invoice in GST Settings"))
if self.settings.e_invoice_applicable_from > self.doc.posting_date:
frappe.throw(
Expand Down
Loading

0 comments on commit 55f2940

Please sign in to comment.