Skip to content

Commit

Permalink
fix: dynamic tax rate for default tax template in setup wizard
Browse files Browse the repository at this point in the history
  • Loading branch information
ljain112 committed Dec 15, 2023
1 parent 46bc6ec commit b99454a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 6 deletions.
9 changes: 9 additions & 0 deletions india_compliance/gst_india/constants/custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
"insert_after": "parent_company",
},
*party_fields[1:],
{
"fieldname": "default_gst_rate",
"label": "Default GST Rate (%)",
"fieldtype": "Select",
"options": "5\n12\n18\n28",
"default": 18,
"depends_on": "eval:doc.country == 'India' && doc.__islocal",
"insert_after": "country",
},
{
"fieldname": "default_customs_expense_account",
"label": "Default Customs Duty Expense Account",
Expand Down
40 changes: 35 additions & 5 deletions india_compliance/gst_india/overrides/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ def make_company_fixtures(doc, method=None):
if not frappe.flags.country_change or doc.country != "India":
return

create_company_fixtures(doc.name)
create_company_fixtures(doc.name, doc.default_gst_rate)


def create_company_fixtures(company):
make_default_tax_templates(company)
def create_company_fixtures(company, tax_rate=None):
if not frappe.flags.in_setup_wizard:
# Manual Trigger in Setup Wizard with custom rate
make_default_tax_templates(company, tax_rate=tax_rate)

make_default_customs_accounts(company)
make_default_gst_expense_accounts(company)

Expand Down Expand Up @@ -57,14 +60,41 @@ def make_default_gst_expense_accounts(company):


@frappe.whitelist()
def make_default_tax_templates(company: str):
def make_default_tax_templates(company: str, tax_rate=None):
frappe.has_permission("Company", ptype="write", doc=company, throw=True)

default_taxes = frappe.get_file_json(get_data_file_path("tax_defaults.json"))
default_taxes = get_tax_defaults(tax_rate)
from_detailed_data(company, default_taxes)
update_gst_settings(company)


def get_tax_defaults(tax_rate):
if not tax_rate:
tax_rate = 18

tax_rate = int(tax_rate)
default_taxes = frappe.get_file_json(get_data_file_path("tax_defaults.json"))
if tax_rate == 18:
return default_taxes

return modify_tax_defaults(default_taxes, tax_rate)


def modify_tax_defaults(default_taxes, tax_rate):
# Identifying new_rate based on existing rate
for template_type in ("sales_tax_templates", "purchase_tax_templates"):
template = default_taxes["chart_of_accounts"]["*"][template_type]
for tax in template:
for row in tax.get("taxes"):
rate = (
tax_rate if row["account_head"]["tax_rate"] == 18 else tax_rate / 2
)

row["account_head"]["tax_rate"] = rate

return default_taxes


def update_gst_settings(company):
# Will only add default GST accounts if present
input_account_names = ["Input Tax CGST", "Input Tax SGST", "Input Tax IGST"]
Expand Down
17 changes: 17 additions & 0 deletions india_compliance/gst_india/overrides/test_company.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import frappe
from frappe.tests.utils import FrappeTestCase

from india_compliance.gst_india.overrides.company import get_tax_defaults


class TestCompanyFixtures(FrappeTestCase):
@classmethod
Expand Down Expand Up @@ -31,3 +33,18 @@ def tearDownClass(cls):
def test_tax_defaults_setup(self):
# Check for tax category creations.
self.assertTrue(frappe.db.exists("Tax Category", "Reverse Charge In-State"))

def test_get_tax_defaults(self):
tax_rate = 12
default_taxes = get_tax_defaults(tax_rate=tax_rate)

for template_type in ("sales_tax_templates", "purchase_tax_templates"):
template = default_taxes["chart_of_accounts"]["*"][template_type]
for tax in template:
for row in tax.get("taxes"):
expected_rate = (
tax_rate
if "IGST " in row["account_head"]["account_name"]
else tax_rate / 2
)
self.assertEqual(row["account_head"]["tax_rate"], expected_rate)
2 changes: 1 addition & 1 deletion india_compliance/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ execute:import frappe; frappe.delete_doc_if_exists("DocType", "GSTIN")

[post_model_sync]
india_compliance.patches.v14.set_default_for_overridden_accounts_setting
execute:from india_compliance.gst_india.setup import create_custom_fields; create_custom_fields() #37
execute:from india_compliance.gst_india.setup import create_custom_fields; create_custom_fields() #38
execute:from india_compliance.gst_india.setup import create_property_setters; create_property_setters() #5
india_compliance.patches.post_install.remove_old_fields
india_compliance.patches.post_install.update_company_gstin
Expand Down
8 changes: 8 additions & 0 deletions india_compliance/public/js/setup_wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ function update_erpnext_slides_settings() {

slide.fields.splice(_index, 0, company_gstin_field);

slide.fields.splice(4, 0, {
fieldname: "default_gst_rate",
fieldtype: "Select",
label: __("Default GST Rate (%)"),
options: [5, 12, 18, 28],
default: 18,
});

slide.fields.push({
fieldname: "enable_audit_trail",
fieldtype: "Check",
Expand Down
25 changes: 25 additions & 0 deletions india_compliance/setup_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from frappe import _

from india_compliance.audit_trail.utils import enable_audit_trail
from india_compliance.gst_india.overrides.company import make_default_tax_templates
from india_compliance.gst_india.overrides.party import validate_pan
from india_compliance.gst_india.utils import guess_gst_category, is_api_enabled
from india_compliance.gst_india.utils.gstin_info import get_gstin_info
Expand Down Expand Up @@ -36,6 +37,17 @@ def get_setup_wizard_stages(params=None):
}
],
},
{
"status": _("Wrapping up"),
"fail_msg": _("Failed to Create Tax Template"),
"tasks": [
{
"fn": setup_tax_template,
"args": params,
"fail_msg": _("Failed to Create Tax Template"),
}
],
},
]

return stages
Expand Down Expand Up @@ -96,3 +108,16 @@ def can_fetch_gstin_info():
return is_api_enabled() and not frappe.get_cached_value(
"GST Settings", None, "sandbox_mode"
)


def setup_tax_template(params):
if not (params.company_name and frappe.db.exists("Company", params.company_name)):
return

if not params.default_gst_rate:
params.default_gst_rate = 18

make_default_tax_templates(params.company_name, int(params.default_gst_rate))
frappe.db.set_value(
"Company", params.company_name, "default_gst_rate", params.default_gst_rate
)

0 comments on commit b99454a

Please sign in to comment.