blob: 14c86d56328e053bfe1230faf67508ad8d755b26 [file] [log] [blame]
vishdhad3ec1c12020-03-24 11:31:41 +05301import traceback
Chillar Anand915b3432021-09-02 16:44:59 +05302
vishdhad3ec1c12020-03-24 11:31:41 +05303import frappe
Subin Tom70049442021-08-31 18:33:16 +05304import taxjar
vishdhad3ec1c12020-03-24 11:31:41 +05305from frappe import _
6from frappe.contacts.doctype.address.address import get_company_address
Subin Tom5c186542021-09-17 00:10:41 +05307from frappe.utils import cint, flt
vishdhad3ec1c12020-03-24 11:31:41 +05308
Subin Tomd2915c62021-11-08 17:59:03 +05309from erpnext import get_default_company, get_region
Chillar Anand915b3432021-09-02 16:44:59 +053010
vishdhad3ec1c12020-03-24 11:31:41 +053011SUPPORTED_COUNTRY_CODES = ["AT", "AU", "BE", "BG", "CA", "CY", "CZ", "DE", "DK", "EE", "ES", "FI",
12 "FR", "GB", "GR", "HR", "HU", "IE", "IT", "LT", "LU", "LV", "MT", "NL", "PL", "PT", "RO",
13 "SE", "SI", "SK", "US"]
Subin Tom70049442021-08-31 18:33:16 +053014SUPPORTED_STATE_CODES = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL',
15 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE',
Ankush Menatb147b852021-09-01 16:45:57 +053016 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD',
Subin Tom70049442021-08-31 18:33:16 +053017 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY']
Subin Tome51c4ba2021-11-08 15:16:20 +053018
vishdhad3ec1c12020-03-24 11:31:41 +053019
20
21def get_client():
22 taxjar_settings = frappe.get_single("TaxJar Settings")
23
24 if not taxjar_settings.is_sandbox:
25 api_key = taxjar_settings.api_key and taxjar_settings.get_password("api_key")
26 api_url = taxjar.DEFAULT_API_URL
27 else:
28 api_key = taxjar_settings.sandbox_api_key and taxjar_settings.get_password("sandbox_api_key")
29 api_url = taxjar.SANDBOX_API_URL
30
31 if api_key and api_url:
Subin Tom70049442021-08-31 18:33:16 +053032 client = taxjar.Client(api_key=api_key, api_url=api_url)
33 client.set_api_config('headers', {
Subin Tom1682a262022-02-22 17:20:48 +053034 'x-api-version': '2022-01-24'
Subin Tom70049442021-08-31 18:33:16 +053035 })
36 return client
vishdhad3ec1c12020-03-24 11:31:41 +053037
38
39def create_transaction(doc, method):
Subin Tom1682a262022-02-22 17:20:48 +053040 TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value("TaxJar Settings", "taxjar_create_transactions")
41
vishdhad3ec1c12020-03-24 11:31:41 +053042 """Create an order transaction in TaxJar"""
43
44 if not TAXJAR_CREATE_TRANSACTIONS:
45 return
46
47 client = get_client()
48
49 if not client:
50 return
51
Subin Tom1682a262022-02-22 17:20:48 +053052 TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
vishdhad3ec1c12020-03-24 11:31:41 +053053 sales_tax = sum([tax.tax_amount for tax in doc.taxes if tax.account_head == TAX_ACCOUNT_HEAD])
54
55 if not sales_tax:
56 return
57
58 tax_dict = get_tax_data(doc)
59
60 if not tax_dict:
61 return
62
63 tax_dict['transaction_id'] = doc.name
64 tax_dict['transaction_date'] = frappe.utils.today()
65 tax_dict['sales_tax'] = sales_tax
66 tax_dict['amount'] = doc.total + tax_dict['shipping']
67
68 try:
Subin Tom70049442021-08-31 18:33:16 +053069 if doc.is_return:
70 client.create_refund(tax_dict)
Ankush Menatb147b852021-09-01 16:45:57 +053071 else:
Subin Tom70049442021-08-31 18:33:16 +053072 client.create_order(tax_dict)
vishdhad3ec1c12020-03-24 11:31:41 +053073 except taxjar.exceptions.TaxJarResponseError as err:
74 frappe.throw(_(sanitize_error_response(err)))
75 except Exception as ex:
76 print(traceback.format_exc(ex))
77
78
79def delete_transaction(doc, method):
80 """Delete an existing TaxJar order transaction"""
Subin Tom1682a262022-02-22 17:20:48 +053081 TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value("TaxJar Settings", "taxjar_create_transactions")
vishdhad3ec1c12020-03-24 11:31:41 +053082
83 if not TAXJAR_CREATE_TRANSACTIONS:
84 return
85
86 client = get_client()
87
88 if not client:
89 return
90
91 client.delete_order(doc.name)
92
93
94def get_tax_data(doc):
Subin Tom1682a262022-02-22 17:20:48 +053095 SHIP_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "shipping_account_head")
96
vishdhad3ec1c12020-03-24 11:31:41 +053097 from_address = get_company_address_details(doc)
98 from_shipping_state = from_address.get("state")
99 from_country_code = frappe.db.get_value("Country", from_address.country, "code")
100 from_country_code = from_country_code.upper()
101
102 to_address = get_shipping_address_details(doc)
103 to_shipping_state = to_address.get("state")
104 to_country_code = frappe.db.get_value("Country", to_address.country, "code")
105 to_country_code = to_country_code.upper()
106
vishdhad3ec1c12020-03-24 11:31:41 +0530107 shipping = sum([tax.tax_amount for tax in doc.taxes if tax.account_head == SHIP_ACCOUNT_HEAD])
108
Subin Tom0e527312021-09-16 14:41:38 +0530109 line_items = [get_line_item_dict(item, doc.docstatus) for item in doc.items]
vishdhad3ec1c12020-03-24 11:31:41 +0530110
Subin Tom70049442021-08-31 18:33:16 +0530111 if from_shipping_state not in SUPPORTED_STATE_CODES:
112 from_shipping_state = get_state_code(from_address, 'Company')
113
114 if to_shipping_state not in SUPPORTED_STATE_CODES:
115 to_shipping_state = get_state_code(to_address, 'Shipping')
Ankush Menatb147b852021-09-01 16:45:57 +0530116
vishdhad3ec1c12020-03-24 11:31:41 +0530117 tax_dict = {
Subin Tom1682a262022-02-22 17:20:48 +0530118 "from_country": from_country_code,
119 "from_zip": from_address.pincode,
120 "from_state": from_shipping_state,
121 "from_city": from_address.city,
122 "from_street": from_address.address_line1,
123 "to_country": to_country_code,
124 "to_zip": to_address.pincode,
125 "to_city": to_address.city,
126 "to_street": to_address.address_line1,
127 "to_state": to_shipping_state,
128 "shipping": shipping,
129 "amount": doc.net_total,
130 "plugin": "erpnext",
131 "line_items": line_items
vishdhad3ec1c12020-03-24 11:31:41 +0530132 }
Ankush Menatb147b852021-09-01 16:45:57 +0530133 return tax_dict
vishdhad3ec1c12020-03-24 11:31:41 +0530134
Subin Tom70049442021-08-31 18:33:16 +0530135def get_state_code(address, location):
136 if address is not None:
137 state_code = get_iso_3166_2_state_code(address)
138 if state_code not in SUPPORTED_STATE_CODES:
139 frappe.throw(_("Please enter a valid State in the {0} Address").format(location))
140 else:
141 frappe.throw(_("Please enter a valid State in the {0} Address").format(location))
Ankush Menatb147b852021-09-01 16:45:57 +0530142
Subin Tom70049442021-08-31 18:33:16 +0530143 return state_code
vishdhad3ec1c12020-03-24 11:31:41 +0530144
Subin Tom3bb60a42021-09-14 22:04:57 +0530145def get_line_item_dict(item, docstatus):
146 tax_dict = dict(
Subin Tom70049442021-08-31 18:33:16 +0530147 id = item.get('idx'),
148 quantity = item.get('qty'),
149 unit_price = item.get('rate'),
150 product_tax_code = item.get('product_tax_category')
Ankush Menatb147b852021-09-01 16:45:57 +0530151 )
vishdhad3ec1c12020-03-24 11:31:41 +0530152
Subin Tom3bb60a42021-09-14 22:04:57 +0530153 if docstatus == 1:
154 tax_dict.update({
155 'sales_tax':item.get('tax_collectable')
156 })
157
158 return tax_dict
159
vishdhad3ec1c12020-03-24 11:31:41 +0530160def set_sales_tax(doc, method):
Subin Tom1682a262022-02-22 17:20:48 +0530161 TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
162 TAXJAR_CALCULATE_TAX = frappe.db.get_single_value("TaxJar Settings", "taxjar_calculate_tax")
163
vishdhad3ec1c12020-03-24 11:31:41 +0530164 if not TAXJAR_CALCULATE_TAX:
165 return
166
Subin Tom45fd8192021-11-08 18:03:44 +0530167 if get_region(doc.company) != 'United States':
Subin Tome51c4ba2021-11-08 15:16:20 +0530168 return
169
vishdhad3ec1c12020-03-24 11:31:41 +0530170 if not doc.items:
171 return
172
Subin Tom70049442021-08-31 18:33:16 +0530173 if check_sales_tax_exemption(doc):
vishdhad3ec1c12020-03-24 11:31:41 +0530174 return
175
176 tax_dict = get_tax_data(doc)
177
178 if not tax_dict:
179 # Remove existing tax rows if address is changed from a taxable state/country
180 setattr(doc, "taxes", [tax for tax in doc.taxes if tax.account_head != TAX_ACCOUNT_HEAD])
181 return
182
Subin Tomb01fe1c2021-09-14 20:42:47 +0530183 # check if delivering within a nexus
Subin Tom5c186542021-09-17 00:10:41 +0530184 check_for_nexus(doc, tax_dict)
Subin Tomb01fe1c2021-09-14 20:42:47 +0530185
vishdhad3ec1c12020-03-24 11:31:41 +0530186 tax_data = validate_tax_request(tax_dict)
vishdhad3ec1c12020-03-24 11:31:41 +0530187 if tax_data is not None:
188 if not tax_data.amount_to_collect:
189 setattr(doc, "taxes", [tax for tax in doc.taxes if tax.account_head != TAX_ACCOUNT_HEAD])
190 elif tax_data.amount_to_collect > 0:
191 # Loop through tax rows for existing Sales Tax entry
192 # If none are found, add a row with the tax amount
193 for tax in doc.taxes:
194 if tax.account_head == TAX_ACCOUNT_HEAD:
195 tax.tax_amount = tax_data.amount_to_collect
196
197 doc.run_method("calculate_taxes_and_totals")
198 break
199 else:
200 doc.append("taxes", {
201 "charge_type": "Actual",
202 "description": "Sales Tax",
203 "account_head": TAX_ACCOUNT_HEAD,
204 "tax_amount": tax_data.amount_to_collect
205 })
Subin Tom70049442021-08-31 18:33:16 +0530206 # Assigning values to tax_collectable and taxable_amount fields in sales item table
207 for item in tax_data.breakdown.line_items:
208 doc.get('items')[cint(item.id)-1].tax_collectable = item.tax_collectable
209 doc.get('items')[cint(item.id)-1].taxable_amount = item.taxable_amount
vishdhad3ec1c12020-03-24 11:31:41 +0530210
211 doc.run_method("calculate_taxes_and_totals")
212
Subin Tom5c186542021-09-17 00:10:41 +0530213def check_for_nexus(doc, tax_dict):
Subin Tom1682a262022-02-22 17:20:48 +0530214 TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
Subin Tom5c186542021-09-17 00:10:41 +0530215 if not frappe.db.get_value('TaxJar Nexus', {'region_code': tax_dict["to_state"]}):
216 for item in doc.get("items"):
217 item.tax_collectable = flt(0)
218 item.taxable_amount = flt(0)
219
220 for tax in doc.taxes:
221 if tax.account_head == TAX_ACCOUNT_HEAD:
222 doc.taxes.remove(tax)
223 return
224
Subin Tom70049442021-08-31 18:33:16 +0530225def check_sales_tax_exemption(doc):
226 # if the party is exempt from sales tax, then set all tax account heads to zero
Subin Tom1682a262022-02-22 17:20:48 +0530227 TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
228
Subin Tom70049442021-08-31 18:33:16 +0530229 sales_tax_exempted = hasattr(doc, "exempt_from_sales_tax") and doc.exempt_from_sales_tax \
230 or frappe.db.has_column("Customer", "exempt_from_sales_tax") \
231 and frappe.db.get_value("Customer", doc.customer, "exempt_from_sales_tax")
232
233 if sales_tax_exempted:
234 for tax in doc.taxes:
235 if tax.account_head == TAX_ACCOUNT_HEAD:
236 tax.tax_amount = 0
237 break
238 doc.run_method("calculate_taxes_and_totals")
239 return True
Ankush Menatb147b852021-09-01 16:45:57 +0530240 else:
Subin Tom70049442021-08-31 18:33:16 +0530241 return False
vishdhad3ec1c12020-03-24 11:31:41 +0530242
243def validate_tax_request(tax_dict):
244 """Return the sales tax that should be collected for a given order."""
245
246 client = get_client()
247
248 if not client:
249 return
250
251 try:
252 tax_data = client.tax_for_order(tax_dict)
253 except taxjar.exceptions.TaxJarResponseError as err:
254 frappe.throw(_(sanitize_error_response(err)))
255 else:
256 return tax_data
257
258
259def get_company_address_details(doc):
260 """Return default company address details"""
261
262 company_address = get_company_address(get_default_company()).company_address
263
264 if not company_address:
265 frappe.throw(_("Please set a default company address"))
266
267 company_address = frappe.get_doc("Address", company_address)
268 return company_address
269
270
271def get_shipping_address_details(doc):
272 """Return customer shipping address details"""
273
274 if doc.shipping_address_name:
275 shipping_address = frappe.get_doc("Address", doc.shipping_address_name)
Subin Tom70049442021-08-31 18:33:16 +0530276 elif doc.customer_address:
Subin Tom75e91002021-11-08 09:49:11 +0530277 shipping_address = frappe.get_doc("Address", doc.customer_address)
vishdhad3ec1c12020-03-24 11:31:41 +0530278 else:
279 shipping_address = get_company_address_details(doc)
280
281 return shipping_address
282
283
284def get_iso_3166_2_state_code(address):
Deepesh Gargdcb462f2020-08-01 13:47:09 +0530285 import pycountry
vishdhad3ec1c12020-03-24 11:31:41 +0530286 country_code = frappe.db.get_value("Country", address.get("country"), "code")
287
288 error_message = _("""{0} is not a valid state! Check for typos or enter the ISO code for your state.""").format(address.get("state"))
289 state = address.get("state").upper().strip()
290
291 # The max length for ISO state codes is 3, excluding the country code
292 if len(state) <= 3:
293 # PyCountry returns state code as {country_code}-{state-code} (e.g. US-FL)
294 address_state = (country_code + "-" + state).upper()
295
296 states = pycountry.subdivisions.get(country_code=country_code.upper())
297 states = [pystate.code for pystate in states]
298
299 if address_state in states:
300 return state
301
302 frappe.throw(_(error_message))
303 else:
304 try:
305 lookup_state = pycountry.subdivisions.lookup(state)
306 except LookupError:
307 frappe.throw(_(error_message))
308 else:
309 return lookup_state.code.split('-')[1]
310
311
312def sanitize_error_response(response):
313 response = response.full_response.get("detail")
314 response = response.replace("_", " ")
315
316 sanitized_responses = {
317 "to zip": "Zipcode",
318 "to city": "City",
319 "to state": "State",
320 "to country": "Country"
321 }
322
323 for k, v in sanitized_responses.items():
324 response = response.replace(k, v)
325
326 return response