From d3ec1c1d89bc5474775c283eb719b94e390ab335 Mon Sep 17 00:00:00 2001 From: vishdha Date: Tue, 24 Mar 2020 11:31:41 +0530 Subject: [PATCH 1/3] feat: Taxjar Integration Added --- .../doctype/taxjar_settings/__init__.py | 0 .../taxjar_settings/taxjar_settings.js | 9 + .../taxjar_settings/taxjar_settings.json | 110 ++++++++ .../taxjar_settings/taxjar_settings.py | 10 + .../taxjar_settings/test_taxjar_settings.py | 10 + .../taxjar_integration.py | 251 ++++++++++++++++++ erpnext/hooks.py | 14 +- erpnext/patches.txt | 1 + .../v12_0/add_taxjar_integration_field.py | 12 + erpnext/regional/united_states/setup.py | 16 ++ .../doctype/quotation/test_quotation.py | 2 - requirements.txt | 4 +- 12 files changed, 434 insertions(+), 5 deletions(-) create mode 100644 erpnext/erpnext_integrations/doctype/taxjar_settings/__init__.py create mode 100644 erpnext/erpnext_integrations/doctype/taxjar_settings/taxjar_settings.js create mode 100644 erpnext/erpnext_integrations/doctype/taxjar_settings/taxjar_settings.json create mode 100644 erpnext/erpnext_integrations/doctype/taxjar_settings/taxjar_settings.py create mode 100644 erpnext/erpnext_integrations/doctype/taxjar_settings/test_taxjar_settings.py create mode 100644 erpnext/erpnext_integrations/taxjar_integration.py create mode 100644 erpnext/patches/v12_0/add_taxjar_integration_field.py diff --git a/erpnext/erpnext_integrations/doctype/taxjar_settings/__init__.py b/erpnext/erpnext_integrations/doctype/taxjar_settings/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/erpnext/erpnext_integrations/doctype/taxjar_settings/taxjar_settings.js b/erpnext/erpnext_integrations/doctype/taxjar_settings/taxjar_settings.js new file mode 100644 index 0000000000..62d5709f51 --- /dev/null +++ b/erpnext/erpnext_integrations/doctype/taxjar_settings/taxjar_settings.js @@ -0,0 +1,9 @@ +// Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors +// For license information, please see license.txt + +frappe.ui.form.on('TaxJar Settings', { + is_sandbox: (frm) => { + frm.toggle_reqd("api_key", !frm.doc.is_sandbox); + frm.toggle_reqd("sandbox_api_key", frm.doc.is_sandbox); + } +}); diff --git a/erpnext/erpnext_integrations/doctype/taxjar_settings/taxjar_settings.json b/erpnext/erpnext_integrations/doctype/taxjar_settings/taxjar_settings.json new file mode 100644 index 0000000000..c0d60f7a31 --- /dev/null +++ b/erpnext/erpnext_integrations/doctype/taxjar_settings/taxjar_settings.json @@ -0,0 +1,110 @@ +{ + "actions": [], + "creation": "2017-06-15 08:21:24.624315", + "doctype": "DocType", + "document_type": "Setup", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "is_sandbox", + "taxjar_calculate_tax", + "taxjar_create_transactions", + "credentials", + "api_key", + "cb_keys", + "sandbox_api_key", + "configuration", + "tax_account_head", + "configuration_cb", + "shipping_account_head" + ], + "fields": [ + { + "fieldname": "credentials", + "fieldtype": "Section Break", + "label": "Credentials" + }, + { + "fieldname": "api_key", + "fieldtype": "Password", + "in_list_view": 1, + "label": "Live API Key", + "reqd": 1 + }, + { + "fieldname": "configuration", + "fieldtype": "Section Break", + "label": "Configuration" + }, + { + "fieldname": "tax_account_head", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Tax Account Head", + "options": "Account", + "reqd": 1 + }, + { + "fieldname": "shipping_account_head", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Shipping Account Head", + "options": "Account", + "reqd": 1 + }, + { + "default": "0", + "fieldname": "is_sandbox", + "fieldtype": "Check", + "label": "Sandbox Mode" + }, + { + "fieldname": "sandbox_api_key", + "fieldtype": "Password", + "label": "Sandbox API Key" + }, + { + "fieldname": "configuration_cb", + "fieldtype": "Column Break" + }, + { + "default": "0", + "fieldname": "taxjar_create_transactions", + "fieldtype": "Check", + "label": "Create TaxJar Transaction" + }, + { + "default": "0", + "fieldname": "taxjar_calculate_tax", + "fieldtype": "Check", + "label": "Enable Tax Calculation" + }, + { + "fieldname": "cb_keys", + "fieldtype": "Column Break" + } + ], + "issingle": 1, + "links": [], + "modified": "2020-04-30 04:38:03.311089", + "modified_by": "Administrator", + "module": "ERPNext Integrations", + "name": "TaxJar Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 1 +} \ No newline at end of file diff --git a/erpnext/erpnext_integrations/doctype/taxjar_settings/taxjar_settings.py b/erpnext/erpnext_integrations/doctype/taxjar_settings/taxjar_settings.py new file mode 100644 index 0000000000..7f5f0f0e7a --- /dev/null +++ b/erpnext/erpnext_integrations/doctype/taxjar_settings/taxjar_settings.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + +from __future__ import unicode_literals +# import frappe +from frappe.model.document import Document + +class TaxJarSettings(Document): + pass diff --git a/erpnext/erpnext_integrations/doctype/taxjar_settings/test_taxjar_settings.py b/erpnext/erpnext_integrations/doctype/taxjar_settings/test_taxjar_settings.py new file mode 100644 index 0000000000..7cdfd00956 --- /dev/null +++ b/erpnext/erpnext_integrations/doctype/taxjar_settings/test_taxjar_settings.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors +# See license.txt +from __future__ import unicode_literals + +# import frappe +import unittest + +class TestTaxJarSettings(unittest.TestCase): + pass diff --git a/erpnext/erpnext_integrations/taxjar_integration.py b/erpnext/erpnext_integrations/taxjar_integration.py new file mode 100644 index 0000000000..633692dd24 --- /dev/null +++ b/erpnext/erpnext_integrations/taxjar_integration.py @@ -0,0 +1,251 @@ +import traceback + +import pycountry +import taxjar + +import frappe +from erpnext import get_default_company +from frappe import _ +from frappe.contacts.doctype.address.address import get_company_address + +TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head") +SHIP_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "shipping_account_head") +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") +SUPPORTED_COUNTRY_CODES = ["AT", "AU", "BE", "BG", "CA", "CY", "CZ", "DE", "DK", "EE", "ES", "FI", + "FR", "GB", "GR", "HR", "HU", "IE", "IT", "LT", "LU", "LV", "MT", "NL", "PL", "PT", "RO", + "SE", "SI", "SK", "US"] + + +def get_client(): + taxjar_settings = frappe.get_single("TaxJar Settings") + + if not taxjar_settings.is_sandbox: + api_key = taxjar_settings.api_key and taxjar_settings.get_password("api_key") + api_url = taxjar.DEFAULT_API_URL + else: + api_key = taxjar_settings.sandbox_api_key and taxjar_settings.get_password("sandbox_api_key") + api_url = taxjar.SANDBOX_API_URL + + if api_key and api_url: + return taxjar.Client(api_key=api_key, api_url=api_url) + + +def create_transaction(doc, method): + """Create an order transaction in TaxJar""" + + if not TAXJAR_CREATE_TRANSACTIONS: + return + + client = get_client() + + if not client: + return + + sales_tax = sum([tax.tax_amount for tax in doc.taxes if tax.account_head == TAX_ACCOUNT_HEAD]) + + if not sales_tax: + return + + tax_dict = get_tax_data(doc) + + if not tax_dict: + return + + tax_dict['transaction_id'] = doc.name + tax_dict['transaction_date'] = frappe.utils.today() + tax_dict['sales_tax'] = sales_tax + tax_dict['amount'] = doc.total + tax_dict['shipping'] + + try: + client.create_order(tax_dict) + except taxjar.exceptions.TaxJarResponseError as err: + frappe.throw(_(sanitize_error_response(err))) + except Exception as ex: + print(traceback.format_exc(ex)) + + +def delete_transaction(doc, method): + """Delete an existing TaxJar order transaction""" + + if not TAXJAR_CREATE_TRANSACTIONS: + return + + client = get_client() + + if not client: + return + + client.delete_order(doc.name) + + +def get_tax_data(doc): + from_address = get_company_address_details(doc) + from_shipping_state = from_address.get("state") + from_country_code = frappe.db.get_value("Country", from_address.country, "code") + from_country_code = from_country_code.upper() + + to_address = get_shipping_address_details(doc) + to_shipping_state = to_address.get("state") + to_country_code = frappe.db.get_value("Country", to_address.country, "code") + to_country_code = to_country_code.upper() + + if to_country_code not in SUPPORTED_COUNTRY_CODES: + return + + shipping = sum([tax.tax_amount for tax in doc.taxes if tax.account_head == SHIP_ACCOUNT_HEAD]) + + if to_shipping_state is not None: + to_shipping_state = get_iso_3166_2_state_code(to_address) + + tax_dict = { + 'from_country': from_country_code, + 'from_zip': from_address.pincode, + 'from_state': from_shipping_state, + 'from_city': from_address.city, + 'from_street': from_address.address_line1, + 'to_country': to_country_code, + 'to_zip': to_address.pincode, + 'to_city': to_address.city, + 'to_street': to_address.address_line1, + 'to_state': to_shipping_state, + 'shipping': shipping, + 'amount': doc.net_total + } + + return tax_dict + + +def set_sales_tax(doc, method): + if not TAXJAR_CALCULATE_TAX: + return + + if not doc.items: + return + + # if the party is exempt from sales tax, then set all tax account heads to zero + sales_tax_exempted = hasattr(doc, "exempt_from_sales_tax") and doc.exempt_from_sales_tax \ + or frappe.db.has_column("Customer", "exempt_from_sales_tax") and frappe.db.get_value("Customer", doc.customer, "exempt_from_sales_tax") + + if sales_tax_exempted: + for tax in doc.taxes: + if tax.account_head == TAX_ACCOUNT_HEAD: + tax.tax_amount = 0 + break + + doc.run_method("calculate_taxes_and_totals") + return + + tax_dict = get_tax_data(doc) + + if not tax_dict: + # Remove existing tax rows if address is changed from a taxable state/country + setattr(doc, "taxes", [tax for tax in doc.taxes if tax.account_head != TAX_ACCOUNT_HEAD]) + return + + tax_data = validate_tax_request(tax_dict) + + if tax_data is not None: + if not tax_data.amount_to_collect: + setattr(doc, "taxes", [tax for tax in doc.taxes if tax.account_head != TAX_ACCOUNT_HEAD]) + elif tax_data.amount_to_collect > 0: + # Loop through tax rows for existing Sales Tax entry + # If none are found, add a row with the tax amount + for tax in doc.taxes: + if tax.account_head == TAX_ACCOUNT_HEAD: + tax.tax_amount = tax_data.amount_to_collect + + doc.run_method("calculate_taxes_and_totals") + break + else: + doc.append("taxes", { + "charge_type": "Actual", + "description": "Sales Tax", + "account_head": TAX_ACCOUNT_HEAD, + "tax_amount": tax_data.amount_to_collect + }) + + doc.run_method("calculate_taxes_and_totals") + + +def validate_tax_request(tax_dict): + """Return the sales tax that should be collected for a given order.""" + + client = get_client() + + if not client: + return + + try: + tax_data = client.tax_for_order(tax_dict) + except taxjar.exceptions.TaxJarResponseError as err: + frappe.throw(_(sanitize_error_response(err))) + else: + return tax_data + + +def get_company_address_details(doc): + """Return default company address details""" + + company_address = get_company_address(get_default_company()).company_address + + if not company_address: + frappe.throw(_("Please set a default company address")) + + company_address = frappe.get_doc("Address", company_address) + return company_address + + +def get_shipping_address_details(doc): + """Return customer shipping address details""" + + if doc.shipping_address_name: + shipping_address = frappe.get_doc("Address", doc.shipping_address_name) + else: + shipping_address = get_company_address_details(doc) + + return shipping_address + + +def get_iso_3166_2_state_code(address): + country_code = frappe.db.get_value("Country", address.get("country"), "code") + + error_message = _("""{0} is not a valid state! Check for typos or enter the ISO code for your state.""").format(address.get("state")) + state = address.get("state").upper().strip() + + # The max length for ISO state codes is 3, excluding the country code + if len(state) <= 3: + # PyCountry returns state code as {country_code}-{state-code} (e.g. US-FL) + address_state = (country_code + "-" + state).upper() + + states = pycountry.subdivisions.get(country_code=country_code.upper()) + states = [pystate.code for pystate in states] + + if address_state in states: + return state + + frappe.throw(_(error_message)) + else: + try: + lookup_state = pycountry.subdivisions.lookup(state) + except LookupError: + frappe.throw(_(error_message)) + else: + return lookup_state.code.split('-')[1] + + +def sanitize_error_response(response): + response = response.full_response.get("detail") + response = response.replace("_", " ") + + sanitized_responses = { + "to zip": "Zipcode", + "to city": "City", + "to state": "State", + "to country": "Country" + } + + for k, v in sanitized_responses.items(): + response = response.replace(k, v) + + return response diff --git a/erpnext/hooks.py b/erpnext/hooks.py index 2a695896ed..835d92ef5c 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -234,8 +234,15 @@ doc_events = { "validate": "erpnext.portal.doctype.products_settings.products_settings.home_page_is_products" }, "Sales Invoice": { - "on_submit": ["erpnext.regional.create_transaction_log", "erpnext.regional.italy.utils.sales_invoice_on_submit"], - "on_cancel": "erpnext.regional.italy.utils.sales_invoice_on_cancel", + "on_submit": [ + "erpnext.regional.create_transaction_log", + "erpnext.regional.italy.utils.sales_invoice_on_submit", + "erpnext.erpnext_integrations.taxjar_integration.create_transaction" + ], + "on_cancel": [ + "erpnext.regional.italy.utils.sales_invoice_on_cancel", + "erpnext.erpnext_integrations.taxjar_integration.delete_transaction" + ], "on_trash": "erpnext.regional.check_deletion_permission" }, "Purchase Invoice": { @@ -261,6 +268,9 @@ doc_events = { }, "Email Unsubscribe": { "after_insert": "erpnext.crm.doctype.email_campaign.email_campaign.unsubscribe_recipient" + }, + ('Quotation', 'Sales Order', 'Sales Invoice'): { + 'validate': ["erpnext.erpnext_integrations.taxjar_integration.set_sales_tax"] } } diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 17fbcc2190..c7a7abf819 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -706,3 +706,4 @@ execute:frappe.delete_doc_if_exists("DocType", "Bank Reconciliation") erpnext.patches.v13_0.move_doctype_reports_and_notification_from_hr_to_payroll #22-06-2020 erpnext.patches.v13_0.move_payroll_setting_separately_from_hr_settings #22-06-2020 erpnext.patches.v13_0.check_is_income_tax_component #22-06-2020 +erpnext.patches.v12_0.add_taxjar_integration_field diff --git a/erpnext/patches/v12_0/add_taxjar_integration_field.py b/erpnext/patches/v12_0/add_taxjar_integration_field.py new file mode 100644 index 0000000000..4c823e13bd --- /dev/null +++ b/erpnext/patches/v12_0/add_taxjar_integration_field.py @@ -0,0 +1,12 @@ +from __future__ import unicode_literals + +import frappe +from erpnext.regional.united_states.setup import make_custom_fields + + +def execute(): + company = frappe.get_all('Company', filters={'country': 'United States'}) + if not company: + return + + make_custom_fields() diff --git a/erpnext/regional/united_states/setup.py b/erpnext/regional/united_states/setup.py index cae28bee8b..2b0ecafebc 100644 --- a/erpnext/regional/united_states/setup.py +++ b/erpnext/regional/united_states/setup.py @@ -14,6 +14,22 @@ def make_custom_fields(update=True): 'Supplier': [ dict(fieldname='irs_1099', fieldtype='Check', insert_after='tax_id', label='Is IRS 1099 reporting required for supplier?') + ], + 'Sales Order': [ + dict(fieldname='exempt_from_sales_tax', fieldtype='Check', insert_after='taxes_and_charges', + label='Is customer exempted from sales tax?') + ], + 'Sales Invoice': [ + dict(fieldname='exempt_from_sales_tax', fieldtype='Check', insert_after='taxes_section', + label='Is customer exempted from sales tax?') + ], + 'Customer': [ + dict(fieldname='exempt_from_sales_tax', fieldtype='Check', insert_after='represents_company', + label='Is customer exempted from sales tax?') + ], + 'Quotation': [ + dict(fieldname='exempt_from_sales_tax', fieldtype='Check', insert_after='taxes_and_charges', + label='Is customer exempted from sales tax?') ] } create_custom_fields(custom_fields, update=update) diff --git a/erpnext/selling/doctype/quotation/test_quotation.py b/erpnext/selling/doctype/quotation/test_quotation.py index ee6b429cca..b4c3d79f31 100644 --- a/erpnext/selling/doctype/quotation/test_quotation.py +++ b/erpnext/selling/doctype/quotation/test_quotation.py @@ -280,5 +280,3 @@ def make_quotation(**args): qo.submit() return qo - - diff --git a/requirements.txt b/requirements.txt index 9da537e493..cfd0ab8e07 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,8 +4,10 @@ gocardless-pro==1.11.0 googlemaps==3.1.1 pandas==0.24.2 plaid-python==3.4.0 +pycountry==19.8.18 PyGithub==1.44.1 python-stdnum==1.12 +taxjar==1.9.0 +tweepy==3.8.0 Unidecode==1.1.1 WooCommerce==2.1.1 -tweepy==3.8.0 \ No newline at end of file From eca2c1cd936a982fd842df927e67aa31e881076b Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Sun, 28 Jun 2020 18:53:55 +0530 Subject: [PATCH 2/3] fix: patch --- erpnext/patches/v12_0/create_irs_1099_field_united_states.py | 1 + 1 file changed, 1 insertion(+) diff --git a/erpnext/patches/v12_0/create_irs_1099_field_united_states.py b/erpnext/patches/v12_0/create_irs_1099_field_united_states.py index 82c8f5c414..4fb66b07a4 100644 --- a/erpnext/patches/v12_0/create_irs_1099_field_united_states.py +++ b/erpnext/patches/v12_0/create_irs_1099_field_united_states.py @@ -5,6 +5,7 @@ from erpnext.regional.united_states.setup import make_custom_fields def execute(): frappe.reload_doc('accounts', 'doctype', 'allowed_to_transact_with', force=True) + frappe.reload_doc('accounts', 'doctype', 'pricing_rule_detail', force=True) company = frappe.get_all('Company', filters = {'country': 'United States'}) if not company: From 50e6e47e66238c3c10be53c0c633fa357130ebf0 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Sun, 28 Jun 2020 21:32:08 +0530 Subject: [PATCH 3/3] fix: Patch --- erpnext/patches/v12_0/create_irs_1099_field_united_states.py | 1 + 1 file changed, 1 insertion(+) diff --git a/erpnext/patches/v12_0/create_irs_1099_field_united_states.py b/erpnext/patches/v12_0/create_irs_1099_field_united_states.py index 4fb66b07a4..43bd0ccdd7 100644 --- a/erpnext/patches/v12_0/create_irs_1099_field_united_states.py +++ b/erpnext/patches/v12_0/create_irs_1099_field_united_states.py @@ -6,6 +6,7 @@ def execute(): frappe.reload_doc('accounts', 'doctype', 'allowed_to_transact_with', force=True) frappe.reload_doc('accounts', 'doctype', 'pricing_rule_detail', force=True) + frappe.reload_doc('crm', 'doctype', 'lost_reason_detail', force=True) company = frappe.get_all('Company', filters = {'country': 'United States'}) if not company: