blob: f960998c3c981649cf3959c54de5c1ef9283e5db [file] [log] [blame]
vishdhad3ec1c12020-03-24 11:31:41 +05301import traceback
2
Sagar Vora38e46352020-11-29 20:26:26 +05303import taxjar
4
vishdhad3ec1c12020-03-24 11:31:41 +05305import frappe
6from erpnext import get_default_company
7from frappe import _
8from frappe.contacts.doctype.address.address import get_company_address
9
10TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
11SHIP_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "shipping_account_head")
12TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value("TaxJar Settings", "taxjar_create_transactions")
13TAXJAR_CALCULATE_TAX = frappe.db.get_single_value("TaxJar Settings", "taxjar_calculate_tax")
14SUPPORTED_COUNTRY_CODES = ["AT", "AU", "BE", "BG", "CA", "CY", "CZ", "DE", "DK", "EE", "ES", "FI",
15 "FR", "GB", "GR", "HR", "HU", "IE", "IT", "LT", "LU", "LV", "MT", "NL", "PL", "PT", "RO",
16 "SE", "SI", "SK", "US"]
17
18
19def get_client():
20 taxjar_settings = frappe.get_single("TaxJar Settings")
21
22 if not taxjar_settings.is_sandbox:
23 api_key = taxjar_settings.api_key and taxjar_settings.get_password("api_key")
24 api_url = taxjar.DEFAULT_API_URL
25 else:
26 api_key = taxjar_settings.sandbox_api_key and taxjar_settings.get_password("sandbox_api_key")
27 api_url = taxjar.SANDBOX_API_URL
28
29 if api_key and api_url:
30 return taxjar.Client(api_key=api_key, api_url=api_url)
31
32
33def create_transaction(doc, method):
34 """Create an order transaction in TaxJar"""
35
36 if not TAXJAR_CREATE_TRANSACTIONS:
37 return
38
39 client = get_client()
40
41 if not client:
42 return
43
44 sales_tax = sum([tax.tax_amount for tax in doc.taxes if tax.account_head == TAX_ACCOUNT_HEAD])
45
46 if not sales_tax:
47 return
48
49 tax_dict = get_tax_data(doc)
50
51 if not tax_dict:
52 return
53
54 tax_dict['transaction_id'] = doc.name
55 tax_dict['transaction_date'] = frappe.utils.today()
56 tax_dict['sales_tax'] = sales_tax
57 tax_dict['amount'] = doc.total + tax_dict['shipping']
58
59 try:
60 client.create_order(tax_dict)
61 except taxjar.exceptions.TaxJarResponseError as err:
62 frappe.throw(_(sanitize_error_response(err)))
63 except Exception as ex:
64 print(traceback.format_exc(ex))
65
66
67def delete_transaction(doc, method):
68 """Delete an existing TaxJar order transaction"""
69
70 if not TAXJAR_CREATE_TRANSACTIONS:
71 return
72
73 client = get_client()
74
75 if not client:
76 return
77
78 client.delete_order(doc.name)
79
80
81def get_tax_data(doc):
82 from_address = get_company_address_details(doc)
83 from_shipping_state = from_address.get("state")
84 from_country_code = frappe.db.get_value("Country", from_address.country, "code")
85 from_country_code = from_country_code.upper()
86
87 to_address = get_shipping_address_details(doc)
88 to_shipping_state = to_address.get("state")
89 to_country_code = frappe.db.get_value("Country", to_address.country, "code")
90 to_country_code = to_country_code.upper()
91
92 if to_country_code not in SUPPORTED_COUNTRY_CODES:
93 return
94
95 shipping = sum([tax.tax_amount for tax in doc.taxes if tax.account_head == SHIP_ACCOUNT_HEAD])
96
97 if to_shipping_state is not None:
98 to_shipping_state = get_iso_3166_2_state_code(to_address)
99
100 tax_dict = {
101 'from_country': from_country_code,
102 'from_zip': from_address.pincode,
103 'from_state': from_shipping_state,
104 'from_city': from_address.city,
105 'from_street': from_address.address_line1,
106 'to_country': to_country_code,
107 'to_zip': to_address.pincode,
108 'to_city': to_address.city,
109 'to_street': to_address.address_line1,
110 'to_state': to_shipping_state,
111 'shipping': shipping,
112 'amount': doc.net_total
113 }
114
115 return tax_dict
116
117
118def set_sales_tax(doc, method):
119 if not TAXJAR_CALCULATE_TAX:
120 return
121
122 if not doc.items:
123 return
124
125 # if the party is exempt from sales tax, then set all tax account heads to zero
126 sales_tax_exempted = hasattr(doc, "exempt_from_sales_tax") and doc.exempt_from_sales_tax \
127 or frappe.db.has_column("Customer", "exempt_from_sales_tax") and frappe.db.get_value("Customer", doc.customer, "exempt_from_sales_tax")
128
129 if sales_tax_exempted:
130 for tax in doc.taxes:
131 if tax.account_head == TAX_ACCOUNT_HEAD:
132 tax.tax_amount = 0
133 break
134
135 doc.run_method("calculate_taxes_and_totals")
136 return
137
138 tax_dict = get_tax_data(doc)
139
140 if not tax_dict:
141 # Remove existing tax rows if address is changed from a taxable state/country
142 setattr(doc, "taxes", [tax for tax in doc.taxes if tax.account_head != TAX_ACCOUNT_HEAD])
143 return
144
145 tax_data = validate_tax_request(tax_dict)
146
147 if tax_data is not None:
148 if not tax_data.amount_to_collect:
149 setattr(doc, "taxes", [tax for tax in doc.taxes if tax.account_head != TAX_ACCOUNT_HEAD])
150 elif tax_data.amount_to_collect > 0:
151 # Loop through tax rows for existing Sales Tax entry
152 # If none are found, add a row with the tax amount
153 for tax in doc.taxes:
154 if tax.account_head == TAX_ACCOUNT_HEAD:
155 tax.tax_amount = tax_data.amount_to_collect
156
157 doc.run_method("calculate_taxes_and_totals")
158 break
159 else:
160 doc.append("taxes", {
161 "charge_type": "Actual",
162 "description": "Sales Tax",
163 "account_head": TAX_ACCOUNT_HEAD,
164 "tax_amount": tax_data.amount_to_collect
165 })
166
167 doc.run_method("calculate_taxes_and_totals")
168
169
170def validate_tax_request(tax_dict):
171 """Return the sales tax that should be collected for a given order."""
172
173 client = get_client()
174
175 if not client:
176 return
177
178 try:
179 tax_data = client.tax_for_order(tax_dict)
180 except taxjar.exceptions.TaxJarResponseError as err:
181 frappe.throw(_(sanitize_error_response(err)))
182 else:
183 return tax_data
184
185
186def get_company_address_details(doc):
187 """Return default company address details"""
188
189 company_address = get_company_address(get_default_company()).company_address
190
191 if not company_address:
192 frappe.throw(_("Please set a default company address"))
193
194 company_address = frappe.get_doc("Address", company_address)
195 return company_address
196
197
198def get_shipping_address_details(doc):
199 """Return customer shipping address details"""
200
201 if doc.shipping_address_name:
202 shipping_address = frappe.get_doc("Address", doc.shipping_address_name)
203 else:
204 shipping_address = get_company_address_details(doc)
205
206 return shipping_address
207
208
209def get_iso_3166_2_state_code(address):
Deepesh Gargdcb462f2020-08-01 13:47:09 +0530210 import pycountry
vishdhad3ec1c12020-03-24 11:31:41 +0530211 country_code = frappe.db.get_value("Country", address.get("country"), "code")
212
213 error_message = _("""{0} is not a valid state! Check for typos or enter the ISO code for your state.""").format(address.get("state"))
214 state = address.get("state").upper().strip()
215
216 # The max length for ISO state codes is 3, excluding the country code
217 if len(state) <= 3:
218 # PyCountry returns state code as {country_code}-{state-code} (e.g. US-FL)
219 address_state = (country_code + "-" + state).upper()
220
221 states = pycountry.subdivisions.get(country_code=country_code.upper())
222 states = [pystate.code for pystate in states]
223
224 if address_state in states:
225 return state
226
227 frappe.throw(_(error_message))
228 else:
229 try:
230 lookup_state = pycountry.subdivisions.lookup(state)
231 except LookupError:
232 frappe.throw(_(error_message))
233 else:
234 return lookup_state.code.split('-')[1]
235
236
237def sanitize_error_response(response):
238 response = response.full_response.get("detail")
239 response = response.replace("_", " ")
240
241 sanitized_responses = {
242 "to zip": "Zipcode",
243 "to city": "City",
244 "to state": "State",
245 "to country": "Country"
246 }
247
248 for k, v in sanitized_responses.items():
249 response = response.replace(k, v)
250
251 return response