Skip to content

Commit

Permalink
fix: improved on_update method, added validation for tax calculation,…
Browse files Browse the repository at this point in the history
… sandbox mode checks
  • Loading branch information
nemesis189 committed Oct 6, 2021
1 parent 254b20b commit 7114659
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"is_sandbox",
"taxjar_calculate_tax",
"is_sandbox",
"taxjar_create_transactions",
"credentials",
"api_key",
Expand Down Expand Up @@ -57,6 +57,7 @@
},
{
"default": "0",
"depends_on": "taxjar_calculate_tax",
"fieldname": "is_sandbox",
"fieldtype": "Check",
"label": "Sandbox Mode"
Expand All @@ -72,6 +73,7 @@
},
{
"default": "0",
"depends_on": "taxjar_calculate_tax",
"fieldname": "taxjar_create_transactions",
"fieldtype": "Check",
"label": "Create TaxJar Transaction"
Expand Down Expand Up @@ -106,7 +108,7 @@
],
"issingle": 1,
"links": [],
"modified": "2021-09-16 08:54:48.444487",
"modified": "2021-10-06 10:59:13.475442",
"modified_by": "Administrator",
"module": "ERPNext Integrations",
"name": "TaxJar Settings",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ def on_update(self):
TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value("TaxJar Settings", "taxjar_create_transactions")
TAXJAR_CALCULATE_TAX = frappe.db.get_single_value("TaxJar Settings", "taxjar_calculate_tax")
TAXJAR_SANDBOX_MODE = frappe.db.get_single_value("TaxJar Settings", "is_sandbox")
fields_hidden = 0

custom_fields = []
for dt in ['Item', 'Sales Invoice Item']:
doc = frappe.db.exists('Custom Field', {'dt': dt, 'fieldname':'product_tax_category'})
custom_fields.append(doc)
fields_hidden = frappe.db.get_value('Custom Field', {'dt': dt, 'fieldname':'product_tax_category'},'hidden')
fields_already_exist = True if custom_fields else False

fields_already_exist = frappe.db.exists('Custom Field', {'dt': ('in', ['Item','Sales Invoice Item']), 'fieldname':'product_tax_category'})
fields_hidden = frappe.get_value('Custom Field', {'dt': ('in', ['Sales Invoice Item'])}, 'hidden')

if (TAXJAR_CREATE_TRANSACTIONS or TAXJAR_CALCULATE_TAX or TAXJAR_SANDBOX_MODE):
if not fields_already_exist:
Expand All @@ -39,10 +33,13 @@ def on_update(self):
frappe.enqueue('erpnext.regional.united_states.setup.add_product_tax_categories', now=False)

elif fields_already_exist and fields_hidden:
toggle_tax_category_fields(hidden='1')
toggle_tax_category_fields(hidden='0')

elif fields_already_exist:
toggle_tax_category_fields(hidden='1')

else:
toggle_tax_category_fields(hidden='0')
def validate(self):
self.calculate_taxes_validation_for_create_transactions()

@frappe.whitelist()
def update_nexus_list(self):
Expand All @@ -55,9 +52,14 @@ def update_nexus_list(self):
self.set('nexus', new_nexus_list)
self.save()

def calculate_taxes_validation_for_create_transactions(self):
if not self.taxjar_calculate_tax and (self.taxjar_create_transactions or self.is_sandbox):
frappe.throw('Before enabling <b>Create Transaction</b> or <b>Sandbox Mode</b>, you need to check the <b>Enable Tax Calculation</b> box')


def toggle_tax_category_fields(hidden):
frappe.set_value('Custom Field',{'dt':'Sales Invoice Item', 'fieldname':'product_tax_category'},'hidden',hidden)
frappe.set_value('Custom Field',{'dt':'Item', 'fieldname':'product_tax_category'},'hidden',hidden)
frappe.set_value('Custom Field', {'dt':'Sales Invoice Item', 'fieldname':'product_tax_category'}, 'hidden', hidden)
frappe.set_value('Custom Field', {'dt':'Item', 'fieldname':'product_tax_category'}, 'hidden', hidden)


def add_product_tax_categories():
Expand Down

0 comments on commit 7114659

Please sign in to comment.