refactor!: Move TaxJar integration from ERPNext
This commit is contained in:
parent
e25d0ead1a
commit
1100816d7a
@ -1,51 +0,0 @@
|
|||||||
{
|
|
||||||
"actions": [],
|
|
||||||
"allow_rename": 1,
|
|
||||||
"creation": "2021-09-11 05:09:53.773838",
|
|
||||||
"doctype": "DocType",
|
|
||||||
"engine": "InnoDB",
|
|
||||||
"field_order": [
|
|
||||||
"region",
|
|
||||||
"region_code",
|
|
||||||
"country",
|
|
||||||
"country_code"
|
|
||||||
],
|
|
||||||
"fields": [
|
|
||||||
{
|
|
||||||
"fieldname": "region",
|
|
||||||
"fieldtype": "Data",
|
|
||||||
"in_list_view": 1,
|
|
||||||
"label": "Region"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "region_code",
|
|
||||||
"fieldtype": "Data",
|
|
||||||
"in_list_view": 1,
|
|
||||||
"label": "Region Code"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "country",
|
|
||||||
"fieldtype": "Data",
|
|
||||||
"in_list_view": 1,
|
|
||||||
"label": "Country"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "country_code",
|
|
||||||
"fieldtype": "Data",
|
|
||||||
"in_list_view": 1,
|
|
||||||
"label": "Country Code"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"index_web_pages_for_search": 1,
|
|
||||||
"istable": 1,
|
|
||||||
"links": [],
|
|
||||||
"modified": "2021-09-14 05:33:06.444710",
|
|
||||||
"modified_by": "Administrator",
|
|
||||||
"module": "ERPNext Integrations",
|
|
||||||
"name": "TaxJar Nexus",
|
|
||||||
"owner": "Administrator",
|
|
||||||
"permissions": [],
|
|
||||||
"sort_field": "modified",
|
|
||||||
"sort_order": "DESC",
|
|
||||||
"track_changes": 1
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors
|
|
||||||
# For license information, please see license.txt
|
|
||||||
|
|
||||||
# import frappe
|
|
||||||
from frappe.model.document import Document
|
|
||||||
|
|
||||||
|
|
||||||
class TaxJarNexus(Document):
|
|
||||||
pass
|
|
File diff suppressed because it is too large
Load Diff
@ -1,37 +0,0 @@
|
|||||||
// 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);
|
|
||||||
},
|
|
||||||
|
|
||||||
on_load: (frm) => {
|
|
||||||
frm.set_query('shipping_account_head', function() {
|
|
||||||
return {
|
|
||||||
filters: {
|
|
||||||
'company': frm.doc.company
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
|
||||||
frm.set_query('tax_account_head', function() {
|
|
||||||
return {
|
|
||||||
filters: {
|
|
||||||
'company': frm.doc.company
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
refresh: (frm) => {
|
|
||||||
frm.add_custom_button(__('Update Nexus List'), function() {
|
|
||||||
frm.call({
|
|
||||||
doc: frm.doc,
|
|
||||||
method: 'update_nexus_list'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
|
|
||||||
});
|
|
@ -1,139 +0,0 @@
|
|||||||
{
|
|
||||||
"actions": [],
|
|
||||||
"creation": "2017-06-15 08:21:24.624315",
|
|
||||||
"doctype": "DocType",
|
|
||||||
"document_type": "Setup",
|
|
||||||
"editable_grid": 1,
|
|
||||||
"engine": "InnoDB",
|
|
||||||
"field_order": [
|
|
||||||
"taxjar_calculate_tax",
|
|
||||||
"is_sandbox",
|
|
||||||
"taxjar_create_transactions",
|
|
||||||
"credentials",
|
|
||||||
"api_key",
|
|
||||||
"cb_keys",
|
|
||||||
"sandbox_api_key",
|
|
||||||
"configuration",
|
|
||||||
"company",
|
|
||||||
"column_break_10",
|
|
||||||
"tax_account_head",
|
|
||||||
"configuration_cb",
|
|
||||||
"shipping_account_head",
|
|
||||||
"section_break_12",
|
|
||||||
"nexus"
|
|
||||||
],
|
|
||||||
"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",
|
|
||||||
"depends_on": "taxjar_calculate_tax",
|
|
||||||
"fieldname": "is_sandbox",
|
|
||||||
"fieldtype": "Check",
|
|
||||||
"label": "Sandbox Mode"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "sandbox_api_key",
|
|
||||||
"fieldtype": "Password",
|
|
||||||
"label": "Sandbox API Key"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"default": "0",
|
|
||||||
"depends_on": "taxjar_calculate_tax",
|
|
||||||
"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"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"depends_on": "nexus",
|
|
||||||
"fieldname": "section_break_12",
|
|
||||||
"fieldtype": "Section Break",
|
|
||||||
"label": "Nexus List"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "nexus",
|
|
||||||
"fieldtype": "Table",
|
|
||||||
"label": "Nexus",
|
|
||||||
"options": "TaxJar Nexus",
|
|
||||||
"read_only": 1
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "configuration_cb",
|
|
||||||
"fieldtype": "Column Break"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "company",
|
|
||||||
"fieldtype": "Link",
|
|
||||||
"label": "Company",
|
|
||||||
"options": "Company"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "column_break_10",
|
|
||||||
"fieldtype": "Column Break"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"issingle": 1,
|
|
||||||
"links": [],
|
|
||||||
"modified": "2021-11-30 12:17:24.647979",
|
|
||||||
"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
|
|
||||||
}
|
|
@ -1,146 +0,0 @@
|
|||||||
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors
|
|
||||||
# For license information, please see license.txt
|
|
||||||
|
|
||||||
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
|
|
||||||
import frappe
|
|
||||||
from frappe.custom.doctype.custom_field.custom_field import create_custom_fields
|
|
||||||
from frappe.model.document import Document
|
|
||||||
from frappe.permissions import add_permission, update_permission_property
|
|
||||||
|
|
||||||
from erpnext.erpnext_integrations.taxjar_integration import get_client
|
|
||||||
|
|
||||||
|
|
||||||
class TaxJarSettings(Document):
|
|
||||||
def on_update(self):
|
|
||||||
TAXJAR_CREATE_TRANSACTIONS = self.taxjar_create_transactions
|
|
||||||
TAXJAR_CALCULATE_TAX = self.taxjar_calculate_tax
|
|
||||||
TAXJAR_SANDBOX_MODE = self.is_sandbox
|
|
||||||
|
|
||||||
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:
|
|
||||||
add_product_tax_categories()
|
|
||||||
make_custom_fields()
|
|
||||||
add_permissions()
|
|
||||||
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="0")
|
|
||||||
|
|
||||||
elif fields_already_exist:
|
|
||||||
toggle_tax_category_fields(hidden="1")
|
|
||||||
|
|
||||||
def validate(self):
|
|
||||||
self.calculate_taxes_validation_for_create_transactions()
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
|
||||||
def update_nexus_list(self):
|
|
||||||
client = get_client()
|
|
||||||
nexus = client.nexus_regions()
|
|
||||||
|
|
||||||
new_nexus_list = [frappe._dict(address) for address in nexus]
|
|
||||||
|
|
||||||
self.set("nexus", [])
|
|
||||||
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(
|
|
||||||
frappe._(
|
|
||||||
"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
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def add_product_tax_categories():
|
|
||||||
with open(os.path.join(os.path.dirname(__file__), "product_tax_category_data.json"), "r") as f:
|
|
||||||
tax_categories = json.loads(f.read())
|
|
||||||
create_tax_categories(tax_categories["categories"])
|
|
||||||
|
|
||||||
|
|
||||||
def create_tax_categories(data):
|
|
||||||
for d in data:
|
|
||||||
if not frappe.db.exists("Product Tax Category", {"product_tax_code": d.get("product_tax_code")}):
|
|
||||||
tax_category = frappe.new_doc("Product Tax Category")
|
|
||||||
tax_category.description = d.get("description")
|
|
||||||
tax_category.product_tax_code = d.get("product_tax_code")
|
|
||||||
tax_category.category_name = d.get("name")
|
|
||||||
tax_category.db_insert()
|
|
||||||
|
|
||||||
|
|
||||||
def make_custom_fields(update=True):
|
|
||||||
custom_fields = {
|
|
||||||
"Sales Invoice Item": [
|
|
||||||
dict(
|
|
||||||
fieldname="product_tax_category",
|
|
||||||
fieldtype="Link",
|
|
||||||
insert_after="description",
|
|
||||||
options="Product Tax Category",
|
|
||||||
label="Product Tax Category",
|
|
||||||
fetch_from="item_code.product_tax_category",
|
|
||||||
),
|
|
||||||
dict(
|
|
||||||
fieldname="tax_collectable",
|
|
||||||
fieldtype="Currency",
|
|
||||||
insert_after="net_amount",
|
|
||||||
label="Tax Collectable",
|
|
||||||
read_only=1,
|
|
||||||
options="currency",
|
|
||||||
),
|
|
||||||
dict(
|
|
||||||
fieldname="taxable_amount",
|
|
||||||
fieldtype="Currency",
|
|
||||||
insert_after="tax_collectable",
|
|
||||||
label="Taxable Amount",
|
|
||||||
read_only=1,
|
|
||||||
options="currency",
|
|
||||||
),
|
|
||||||
],
|
|
||||||
"Item": [
|
|
||||||
dict(
|
|
||||||
fieldname="product_tax_category",
|
|
||||||
fieldtype="Link",
|
|
||||||
insert_after="item_group",
|
|
||||||
options="Product Tax Category",
|
|
||||||
label="Product Tax Category",
|
|
||||||
)
|
|
||||||
],
|
|
||||||
}
|
|
||||||
create_custom_fields(custom_fields, update=update)
|
|
||||||
|
|
||||||
|
|
||||||
def add_permissions():
|
|
||||||
doctype = "Product Tax Category"
|
|
||||||
for role in (
|
|
||||||
"Accounts Manager",
|
|
||||||
"Accounts User",
|
|
||||||
"System Manager",
|
|
||||||
"Item Manager",
|
|
||||||
"Stock Manager",
|
|
||||||
):
|
|
||||||
add_permission(doctype, role, 0)
|
|
||||||
update_permission_property(doctype, role, 0, "write", 1)
|
|
||||||
update_permission_property(doctype, role, 0, "create", 1)
|
|
@ -1,9 +0,0 @@
|
|||||||
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
|
|
||||||
# See license.txt
|
|
||||||
|
|
||||||
# import frappe
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
|
|
||||||
class TestTaxJarSettings(unittest.TestCase):
|
|
||||||
pass
|
|
@ -1,419 +0,0 @@
|
|||||||
import traceback
|
|
||||||
|
|
||||||
import frappe
|
|
||||||
import taxjar
|
|
||||||
from frappe import _
|
|
||||||
from frappe.contacts.doctype.address.address import get_company_address
|
|
||||||
from frappe.utils import cint, flt
|
|
||||||
|
|
||||||
from erpnext import get_default_company, get_region
|
|
||||||
|
|
||||||
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",
|
|
||||||
]
|
|
||||||
SUPPORTED_STATE_CODES = [
|
|
||||||
"AL",
|
|
||||||
"AK",
|
|
||||||
"AZ",
|
|
||||||
"AR",
|
|
||||||
"CA",
|
|
||||||
"CO",
|
|
||||||
"CT",
|
|
||||||
"DE",
|
|
||||||
"DC",
|
|
||||||
"FL",
|
|
||||||
"GA",
|
|
||||||
"HI",
|
|
||||||
"ID",
|
|
||||||
"IL",
|
|
||||||
"IN",
|
|
||||||
"IA",
|
|
||||||
"KS",
|
|
||||||
"KY",
|
|
||||||
"LA",
|
|
||||||
"ME",
|
|
||||||
"MD",
|
|
||||||
"MA",
|
|
||||||
"MI",
|
|
||||||
"MN",
|
|
||||||
"MS",
|
|
||||||
"MO",
|
|
||||||
"MT",
|
|
||||||
"NE",
|
|
||||||
"NV",
|
|
||||||
"NH",
|
|
||||||
"NJ",
|
|
||||||
"NM",
|
|
||||||
"NY",
|
|
||||||
"NC",
|
|
||||||
"ND",
|
|
||||||
"OH",
|
|
||||||
"OK",
|
|
||||||
"OR",
|
|
||||||
"PA",
|
|
||||||
"RI",
|
|
||||||
"SC",
|
|
||||||
"SD",
|
|
||||||
"TN",
|
|
||||||
"TX",
|
|
||||||
"UT",
|
|
||||||
"VT",
|
|
||||||
"VA",
|
|
||||||
"WA",
|
|
||||||
"WV",
|
|
||||||
"WI",
|
|
||||||
"WY",
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
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:
|
|
||||||
client = taxjar.Client(api_key=api_key, api_url=api_url)
|
|
||||||
client.set_api_config("headers", {"x-api-version": "2022-01-24"})
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
def create_transaction(doc, method):
|
|
||||||
TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value(
|
|
||||||
"TaxJar Settings", "taxjar_create_transactions"
|
|
||||||
)
|
|
||||||
|
|
||||||
"""Create an order transaction in TaxJar"""
|
|
||||||
|
|
||||||
if not TAXJAR_CREATE_TRANSACTIONS:
|
|
||||||
return
|
|
||||||
|
|
||||||
client = get_client()
|
|
||||||
|
|
||||||
if not client:
|
|
||||||
return
|
|
||||||
|
|
||||||
TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
|
|
||||||
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:
|
|
||||||
if doc.is_return:
|
|
||||||
client.create_refund(tax_dict)
|
|
||||||
else:
|
|
||||||
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"""
|
|
||||||
TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value(
|
|
||||||
"TaxJar Settings", "taxjar_create_transactions"
|
|
||||||
)
|
|
||||||
|
|
||||||
if not TAXJAR_CREATE_TRANSACTIONS:
|
|
||||||
return
|
|
||||||
|
|
||||||
client = get_client()
|
|
||||||
|
|
||||||
if not client:
|
|
||||||
return
|
|
||||||
|
|
||||||
client.delete_order(doc.name)
|
|
||||||
|
|
||||||
|
|
||||||
def get_tax_data(doc):
|
|
||||||
SHIP_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "shipping_account_head")
|
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
shipping = sum([tax.tax_amount for tax in doc.taxes if tax.account_head == SHIP_ACCOUNT_HEAD])
|
|
||||||
|
|
||||||
line_items = [get_line_item_dict(item, doc.docstatus) for item in doc.items]
|
|
||||||
|
|
||||||
if from_shipping_state not in SUPPORTED_STATE_CODES:
|
|
||||||
from_shipping_state = get_state_code(from_address, "Company")
|
|
||||||
|
|
||||||
if to_shipping_state not in SUPPORTED_STATE_CODES:
|
|
||||||
to_shipping_state = get_state_code(to_address, "Shipping")
|
|
||||||
|
|
||||||
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,
|
|
||||||
"plugin": "erpnext",
|
|
||||||
"line_items": line_items,
|
|
||||||
}
|
|
||||||
return tax_dict
|
|
||||||
|
|
||||||
|
|
||||||
def get_state_code(address, location):
|
|
||||||
if address is not None:
|
|
||||||
state_code = get_iso_3166_2_state_code(address)
|
|
||||||
if state_code not in SUPPORTED_STATE_CODES:
|
|
||||||
frappe.throw(_("Please enter a valid State in the {0} Address").format(location))
|
|
||||||
else:
|
|
||||||
frappe.throw(_("Please enter a valid State in the {0} Address").format(location))
|
|
||||||
|
|
||||||
return state_code
|
|
||||||
|
|
||||||
|
|
||||||
def get_line_item_dict(item, docstatus):
|
|
||||||
tax_dict = dict(
|
|
||||||
id=item.get("idx"),
|
|
||||||
quantity=item.get("qty"),
|
|
||||||
unit_price=item.get("rate"),
|
|
||||||
product_tax_code=item.get("product_tax_category"),
|
|
||||||
)
|
|
||||||
|
|
||||||
if docstatus == 1:
|
|
||||||
tax_dict.update({"sales_tax": item.get("tax_collectable")})
|
|
||||||
|
|
||||||
return tax_dict
|
|
||||||
|
|
||||||
|
|
||||||
def set_sales_tax(doc, method):
|
|
||||||
TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
|
|
||||||
TAXJAR_CALCULATE_TAX = frappe.db.get_single_value("TaxJar Settings", "taxjar_calculate_tax")
|
|
||||||
|
|
||||||
if not TAXJAR_CALCULATE_TAX:
|
|
||||||
return
|
|
||||||
|
|
||||||
if get_region(doc.company) != "United States":
|
|
||||||
return
|
|
||||||
|
|
||||||
if not doc.items:
|
|
||||||
return
|
|
||||||
|
|
||||||
if check_sales_tax_exemption(doc):
|
|
||||||
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
|
|
||||||
|
|
||||||
# check if delivering within a nexus
|
|
||||||
check_for_nexus(doc, tax_dict)
|
|
||||||
|
|
||||||
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,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
# Assigning values to tax_collectable and taxable_amount fields in sales item table
|
|
||||||
for item in tax_data.breakdown.line_items:
|
|
||||||
doc.get("items")[cint(item.id) - 1].tax_collectable = item.tax_collectable
|
|
||||||
doc.get("items")[cint(item.id) - 1].taxable_amount = item.taxable_amount
|
|
||||||
|
|
||||||
doc.run_method("calculate_taxes_and_totals")
|
|
||||||
|
|
||||||
|
|
||||||
def check_for_nexus(doc, tax_dict):
|
|
||||||
TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
|
|
||||||
if not frappe.db.get_value("TaxJar Nexus", {"region_code": tax_dict["to_state"]}):
|
|
||||||
for item in doc.get("items"):
|
|
||||||
item.tax_collectable = flt(0)
|
|
||||||
item.taxable_amount = flt(0)
|
|
||||||
|
|
||||||
for tax in list(doc.taxes):
|
|
||||||
if tax.account_head == TAX_ACCOUNT_HEAD:
|
|
||||||
doc.taxes.remove(tax)
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def check_sales_tax_exemption(doc):
|
|
||||||
# if the party is exempt from sales tax, then set all tax account heads to zero
|
|
||||||
TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
|
|
||||||
|
|
||||||
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 True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
elif doc.customer_address:
|
|
||||||
shipping_address = frappe.get_doc("Address", doc.customer_address)
|
|
||||||
else:
|
|
||||||
shipping_address = get_company_address_details(doc)
|
|
||||||
|
|
||||||
return shipping_address
|
|
||||||
|
|
||||||
|
|
||||||
def get_iso_3166_2_state_code(address):
|
|
||||||
import pycountry
|
|
||||||
|
|
||||||
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
|
|
@ -312,11 +312,9 @@ doc_events = {
|
|||||||
"erpnext.regional.create_transaction_log",
|
"erpnext.regional.create_transaction_log",
|
||||||
"erpnext.regional.italy.utils.sales_invoice_on_submit",
|
"erpnext.regional.italy.utils.sales_invoice_on_submit",
|
||||||
"erpnext.regional.saudi_arabia.utils.create_qr_code",
|
"erpnext.regional.saudi_arabia.utils.create_qr_code",
|
||||||
"erpnext.erpnext_integrations.taxjar_integration.create_transaction",
|
|
||||||
],
|
],
|
||||||
"on_cancel": [
|
"on_cancel": [
|
||||||
"erpnext.regional.italy.utils.sales_invoice_on_cancel",
|
"erpnext.regional.italy.utils.sales_invoice_on_cancel",
|
||||||
"erpnext.erpnext_integrations.taxjar_integration.delete_transaction",
|
|
||||||
"erpnext.regional.saudi_arabia.utils.delete_qr_code_file",
|
"erpnext.regional.saudi_arabia.utils.delete_qr_code_file",
|
||||||
],
|
],
|
||||||
"on_trash": "erpnext.regional.check_deletion_permission",
|
"on_trash": "erpnext.regional.check_deletion_permission",
|
||||||
@ -349,9 +347,6 @@ doc_events = {
|
|||||||
"Email Unsubscribe": {
|
"Email Unsubscribe": {
|
||||||
"after_insert": "erpnext.crm.doctype.email_campaign.email_campaign.unsubscribe_recipient"
|
"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"]
|
|
||||||
},
|
|
||||||
"Company": {"on_trash": ["erpnext.regional.saudi_arabia.utils.delete_vat_settings_for_company"]},
|
"Company": {"on_trash": ["erpnext.regional.saudi_arabia.utils.delete_vat_settings_for_company"]},
|
||||||
"Integration Request": {
|
"Integration Request": {
|
||||||
"validate": "erpnext.accounts.doctype.payment_request.payment_request.validate_payment"
|
"validate": "erpnext.accounts.doctype.payment_request.payment_request.validate_payment"
|
||||||
|
@ -269,6 +269,7 @@ erpnext.patches.v13_0.show_india_localisation_deprecation_warning
|
|||||||
erpnext.patches.v13_0.show_hr_payroll_deprecation_warning
|
erpnext.patches.v13_0.show_hr_payroll_deprecation_warning
|
||||||
erpnext.patches.v13_0.reset_corrupt_defaults
|
erpnext.patches.v13_0.reset_corrupt_defaults
|
||||||
erpnext.patches.v13_0.create_accounting_dimensions_for_asset_repair
|
erpnext.patches.v13_0.create_accounting_dimensions_for_asset_repair
|
||||||
|
erpnext.patches.v15_0.delete_taxjar_doctypes
|
||||||
|
|
||||||
[post_model_sync]
|
[post_model_sync]
|
||||||
execute:frappe.delete_doc_if_exists('Workspace', 'ERPNext Integrations Settings')
|
execute:frappe.delete_doc_if_exists('Workspace', 'ERPNext Integrations Settings')
|
||||||
|
17
erpnext/patches/v15_0/delete_taxjar_doctypes.py
Normal file
17
erpnext/patches/v15_0/delete_taxjar_doctypes.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import click
|
||||||
|
import frappe
|
||||||
|
|
||||||
|
|
||||||
|
def execute():
|
||||||
|
if "taxjar_integration" in frappe.get_installed_apps():
|
||||||
|
return
|
||||||
|
|
||||||
|
doctypes = ["TaxJar Settings", "TaxJar Nexus", "Product Tax Category"]
|
||||||
|
for doctype in doctypes:
|
||||||
|
frappe.delete_doc("DocType", doctype, ignore_missing=True)
|
||||||
|
|
||||||
|
click.secho(
|
||||||
|
"Taxjar Integration is moved to a separate app"
|
||||||
|
"Please install the app to continue using the module: https://github.com/frappe/taxjar_integration",
|
||||||
|
fg="yellow",
|
||||||
|
)
|
@ -1,8 +0,0 @@
|
|||||||
// Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors
|
|
||||||
// For license information, please see license.txt
|
|
||||||
|
|
||||||
frappe.ui.form.on('Product Tax Category', {
|
|
||||||
// refresh: function(frm) {
|
|
||||||
|
|
||||||
// }
|
|
||||||
});
|
|
@ -1,70 +0,0 @@
|
|||||||
{
|
|
||||||
"actions": [],
|
|
||||||
"autoname": "field:product_tax_code",
|
|
||||||
"creation": "2021-08-23 12:33:37.910225",
|
|
||||||
"doctype": "DocType",
|
|
||||||
"editable_grid": 1,
|
|
||||||
"engine": "InnoDB",
|
|
||||||
"field_order": [
|
|
||||||
"product_tax_code",
|
|
||||||
"column_break_2",
|
|
||||||
"category_name",
|
|
||||||
"section_break_4",
|
|
||||||
"description"
|
|
||||||
],
|
|
||||||
"fields": [
|
|
||||||
{
|
|
||||||
"fieldname": "product_tax_code",
|
|
||||||
"fieldtype": "Data",
|
|
||||||
"in_list_view": 1,
|
|
||||||
"label": "Product Tax Code",
|
|
||||||
"reqd": 1,
|
|
||||||
"unique": 1
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "description",
|
|
||||||
"fieldtype": "Small Text",
|
|
||||||
"label": "Description"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "category_name",
|
|
||||||
"fieldtype": "Data",
|
|
||||||
"label": "Category Name",
|
|
||||||
"length": 255
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "column_break_2",
|
|
||||||
"fieldtype": "Column Break"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "section_break_4",
|
|
||||||
"fieldtype": "Section Break"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"index_web_pages_for_search": 1,
|
|
||||||
"links": [],
|
|
||||||
"modified": "2021-08-24 09:10:25.313642",
|
|
||||||
"modified_by": "Administrator",
|
|
||||||
"module": "Regional",
|
|
||||||
"name": "Product Tax Category",
|
|
||||||
"owner": "Administrator",
|
|
||||||
"permissions": [
|
|
||||||
{
|
|
||||||
"create": 1,
|
|
||||||
"delete": 1,
|
|
||||||
"email": 1,
|
|
||||||
"export": 1,
|
|
||||||
"print": 1,
|
|
||||||
"read": 1,
|
|
||||||
"report": 1,
|
|
||||||
"role": "System Manager",
|
|
||||||
"share": 1,
|
|
||||||
"write": 1
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"quick_entry": 1,
|
|
||||||
"sort_field": "modified",
|
|
||||||
"sort_order": "DESC",
|
|
||||||
"title_field": "category_name",
|
|
||||||
"track_changes": 1
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors
|
|
||||||
# For license information, please see license.txt
|
|
||||||
|
|
||||||
# import frappe
|
|
||||||
from frappe.model.document import Document
|
|
||||||
|
|
||||||
|
|
||||||
class ProductTaxCategory(Document):
|
|
||||||
pass
|
|
@ -1,9 +0,0 @@
|
|||||||
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
|
|
||||||
# See license.txt
|
|
||||||
|
|
||||||
# import frappe
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
|
|
||||||
class TestProductTaxCategory(unittest.TestCase):
|
|
||||||
pass
|
|
Loading…
Reference in New Issue
Block a user