blob: 794b281c55aa7fcac8e763a6664f36b11b35c08c [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
Chillar Anand915b3432021-09-02 16:44:59 +05309from erpnext import get_default_company
10
vishdhad3ec1c12020-03-24 11:31:41 +053011TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
12SHIP_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "shipping_account_head")
13TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value("TaxJar Settings", "taxjar_create_transactions")
14TAXJAR_CALCULATE_TAX = frappe.db.get_single_value("TaxJar Settings", "taxjar_calculate_tax")
15SUPPORTED_COUNTRY_CODES = ["AT", "AU", "BE", "BG", "CA", "CY", "CZ", "DE", "DK", "EE", "ES", "FI",
16 "FR", "GB", "GR", "HR", "HU", "IE", "IT", "LT", "LU", "LV", "MT", "NL", "PL", "PT", "RO",
17 "SE", "SI", "SK", "US"]
Subin Tom70049442021-08-31 18:33:16 +053018SUPPORTED_STATE_CODES = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL',
19 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE',
Ankush Menatb147b852021-09-01 16:45:57 +053020 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD',
Subin Tom70049442021-08-31 18:33:16 +053021 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY']
Subin Tome51c4ba2021-11-08 15:16:20 +053022USA_COMPANIES = frappe.get_all('Company', filters = {'country': 'United States'}, fields=['name'], pluck='name')
23
vishdhad3ec1c12020-03-24 11:31:41 +053024
25
26def get_client():
27 taxjar_settings = frappe.get_single("TaxJar Settings")
28
29 if not taxjar_settings.is_sandbox:
30 api_key = taxjar_settings.api_key and taxjar_settings.get_password("api_key")
31 api_url = taxjar.DEFAULT_API_URL
32 else:
33 api_key = taxjar_settings.sandbox_api_key and taxjar_settings.get_password("sandbox_api_key")
34 api_url = taxjar.SANDBOX_API_URL
35
36 if api_key and api_url:
Subin Tom70049442021-08-31 18:33:16 +053037 client = taxjar.Client(api_key=api_key, api_url=api_url)
38 client.set_api_config('headers', {
39 'x-api-version': '2020-08-07'
40 })
41 return client
vishdhad3ec1c12020-03-24 11:31:41 +053042
43
44def create_transaction(doc, method):
45 """Create an order transaction in TaxJar"""
46
47 if not TAXJAR_CREATE_TRANSACTIONS:
48 return
49
50 client = get_client()
51
52 if not client:
53 return
54
55 sales_tax = sum([tax.tax_amount for tax in doc.taxes if tax.account_head == TAX_ACCOUNT_HEAD])
56
57 if not sales_tax:
58 return
59
60 tax_dict = get_tax_data(doc)
61
62 if not tax_dict:
63 return
64
65 tax_dict['transaction_id'] = doc.name
66 tax_dict['transaction_date'] = frappe.utils.today()
67 tax_dict['sales_tax'] = sales_tax
68 tax_dict['amount'] = doc.total + tax_dict['shipping']
69
70 try:
Subin Tom70049442021-08-31 18:33:16 +053071 if doc.is_return:
72 client.create_refund(tax_dict)
Ankush Menatb147b852021-09-01 16:45:57 +053073 else:
Subin Tom70049442021-08-31 18:33:16 +053074 client.create_order(tax_dict)
vishdhad3ec1c12020-03-24 11:31:41 +053075 except taxjar.exceptions.TaxJarResponseError as err:
76 frappe.throw(_(sanitize_error_response(err)))
77 except Exception as ex:
78 print(traceback.format_exc(ex))
79
80
81def delete_transaction(doc, method):
82 """Delete an existing TaxJar order transaction"""
83
84 if not TAXJAR_CREATE_TRANSACTIONS:
85 return
86
87 client = get_client()
88
89 if not client:
90 return
91
92 client.delete_order(doc.name)
93
94
95def get_tax_data(doc):
96 from_address = get_company_address_details(doc)
97 from_shipping_state = from_address.get("state")
98 from_country_code = frappe.db.get_value("Country", from_address.country, "code")
99 from_country_code = from_country_code.upper()
100
101 to_address = get_shipping_address_details(doc)
102 to_shipping_state = to_address.get("state")
103 to_country_code = frappe.db.get_value("Country", to_address.country, "code")
104 to_country_code = to_country_code.upper()
105
vishdhad3ec1c12020-03-24 11:31:41 +0530106 shipping = sum([tax.tax_amount for tax in doc.taxes if tax.account_head == SHIP_ACCOUNT_HEAD])
107
Subin Tom0e527312021-09-16 14:41:38 +0530108 line_items = [get_line_item_dict(item, doc.docstatus) for item in doc.items]
vishdhad3ec1c12020-03-24 11:31:41 +0530109
Subin Tom70049442021-08-31 18:33:16 +0530110 if from_shipping_state not in SUPPORTED_STATE_CODES:
111 from_shipping_state = get_state_code(from_address, 'Company')
112
113 if to_shipping_state not in SUPPORTED_STATE_CODES:
114 to_shipping_state = get_state_code(to_address, 'Shipping')
Ankush Menatb147b852021-09-01 16:45:57 +0530115
vishdhad3ec1c12020-03-24 11:31:41 +0530116 tax_dict = {
117 'from_country': from_country_code,
118 'from_zip': from_address.pincode,
119 'from_state': from_shipping_state,
120 'from_city': from_address.city,
121 'from_street': from_address.address_line1,
122 'to_country': to_country_code,
123 'to_zip': to_address.pincode,
124 'to_city': to_address.city,
125 'to_street': to_address.address_line1,
126 'to_state': to_shipping_state,
127 'shipping': shipping,
Subin Tom70049442021-08-31 18:33:16 +0530128 'amount': doc.net_total,
129 'plugin': 'erpnext',
130 'line_items': line_items
vishdhad3ec1c12020-03-24 11:31:41 +0530131 }
Ankush Menatb147b852021-09-01 16:45:57 +0530132 return tax_dict
vishdhad3ec1c12020-03-24 11:31:41 +0530133
Subin Tom70049442021-08-31 18:33:16 +0530134def get_state_code(address, location):
135 if address is not None:
136 state_code = get_iso_3166_2_state_code(address)
137 if state_code not in SUPPORTED_STATE_CODES:
138 frappe.throw(_("Please enter a valid State in the {0} Address").format(location))
139 else:
140 frappe.throw(_("Please enter a valid State in the {0} Address").format(location))
Ankush Menatb147b852021-09-01 16:45:57 +0530141
Subin Tom70049442021-08-31 18:33:16 +0530142 return state_code
vishdhad3ec1c12020-03-24 11:31:41 +0530143
Subin Tom3bb60a42021-09-14 22:04:57 +0530144def get_line_item_dict(item, docstatus):
145 tax_dict = dict(
Subin Tom70049442021-08-31 18:33:16 +0530146 id = item.get('idx'),
147 quantity = item.get('qty'),
148 unit_price = item.get('rate'),
149 product_tax_code = item.get('product_tax_category')
Ankush Menatb147b852021-09-01 16:45:57 +0530150 )
vishdhad3ec1c12020-03-24 11:31:41 +0530151
Subin Tom3bb60a42021-09-14 22:04:57 +0530152 if docstatus == 1:
153 tax_dict.update({
154 'sales_tax':item.get('tax_collectable')
155 })
156
157 return tax_dict
158
vishdhad3ec1c12020-03-24 11:31:41 +0530159def set_sales_tax(doc, method):
160 if not TAXJAR_CALCULATE_TAX:
161 return
162
Subin Tome51c4ba2021-11-08 15:16:20 +0530163 taxjar_settings_company = frappe.db.get_single_value('TaxJar Settings','current_company')
164 if taxjar_settings_company not in USA_COMPANIES:
165 return
166
vishdhad3ec1c12020-03-24 11:31:41 +0530167 if not doc.items:
168 return
169
Subin Tom70049442021-08-31 18:33:16 +0530170 if check_sales_tax_exemption(doc):
vishdhad3ec1c12020-03-24 11:31:41 +0530171 return
172
173 tax_dict = get_tax_data(doc)
174
175 if not tax_dict:
176 # Remove existing tax rows if address is changed from a taxable state/country
177 setattr(doc, "taxes", [tax for tax in doc.taxes if tax.account_head != TAX_ACCOUNT_HEAD])
178 return
179
Subin Tomb01fe1c2021-09-14 20:42:47 +0530180 # check if delivering within a nexus
Subin Tom5c186542021-09-17 00:10:41 +0530181 check_for_nexus(doc, tax_dict)
Subin Tomb01fe1c2021-09-14 20:42:47 +0530182
vishdhad3ec1c12020-03-24 11:31:41 +0530183 tax_data = validate_tax_request(tax_dict)
vishdhad3ec1c12020-03-24 11:31:41 +0530184 if tax_data is not None:
185 if not tax_data.amount_to_collect:
186 setattr(doc, "taxes", [tax for tax in doc.taxes if tax.account_head != TAX_ACCOUNT_HEAD])
187 elif tax_data.amount_to_collect > 0:
188 # Loop through tax rows for existing Sales Tax entry
189 # If none are found, add a row with the tax amount
190 for tax in doc.taxes:
191 if tax.account_head == TAX_ACCOUNT_HEAD:
192 tax.tax_amount = tax_data.amount_to_collect
193
194 doc.run_method("calculate_taxes_and_totals")
195 break
196 else:
197 doc.append("taxes", {
198 "charge_type": "Actual",
199 "description": "Sales Tax",
200 "account_head": TAX_ACCOUNT_HEAD,
201 "tax_amount": tax_data.amount_to_collect
202 })
Subin Tom70049442021-08-31 18:33:16 +0530203 # Assigning values to tax_collectable and taxable_amount fields in sales item table
204 for item in tax_data.breakdown.line_items:
205 doc.get('items')[cint(item.id)-1].tax_collectable = item.tax_collectable
206 doc.get('items')[cint(item.id)-1].taxable_amount = item.taxable_amount
vishdhad3ec1c12020-03-24 11:31:41 +0530207
208 doc.run_method("calculate_taxes_and_totals")
209
Subin Tom5c186542021-09-17 00:10:41 +0530210def check_for_nexus(doc, tax_dict):
211 if not frappe.db.get_value('TaxJar Nexus', {'region_code': tax_dict["to_state"]}):
212 for item in doc.get("items"):
213 item.tax_collectable = flt(0)
214 item.taxable_amount = flt(0)
215
216 for tax in doc.taxes:
217 if tax.account_head == TAX_ACCOUNT_HEAD:
218 doc.taxes.remove(tax)
219 return
220
Subin Tom70049442021-08-31 18:33:16 +0530221def check_sales_tax_exemption(doc):
222 # if the party is exempt from sales tax, then set all tax account heads to zero
223 sales_tax_exempted = hasattr(doc, "exempt_from_sales_tax") and doc.exempt_from_sales_tax \
224 or frappe.db.has_column("Customer", "exempt_from_sales_tax") \
225 and frappe.db.get_value("Customer", doc.customer, "exempt_from_sales_tax")
226
227 if sales_tax_exempted:
228 for tax in doc.taxes:
229 if tax.account_head == TAX_ACCOUNT_HEAD:
230 tax.tax_amount = 0
231 break
232 doc.run_method("calculate_taxes_and_totals")
233 return True
Ankush Menatb147b852021-09-01 16:45:57 +0530234 else:
Subin Tom70049442021-08-31 18:33:16 +0530235 return False
vishdhad3ec1c12020-03-24 11:31:41 +0530236
237def validate_tax_request(tax_dict):
238 """Return the sales tax that should be collected for a given order."""
239
240 client = get_client()
241
242 if not client:
243 return
244
245 try:
246 tax_data = client.tax_for_order(tax_dict)
247 except taxjar.exceptions.TaxJarResponseError as err:
248 frappe.throw(_(sanitize_error_response(err)))
249 else:
250 return tax_data
251
252
253def get_company_address_details(doc):
254 """Return default company address details"""
255
256 company_address = get_company_address(get_default_company()).company_address
257
258 if not company_address:
259 frappe.throw(_("Please set a default company address"))
260
261 company_address = frappe.get_doc("Address", company_address)
262 return company_address
263
264
265def get_shipping_address_details(doc):
266 """Return customer shipping address details"""
267
268 if doc.shipping_address_name:
269 shipping_address = frappe.get_doc("Address", doc.shipping_address_name)
Subin Tom70049442021-08-31 18:33:16 +0530270 elif doc.customer_address:
Subin Tom75e91002021-11-08 09:49:11 +0530271 shipping_address = frappe.get_doc("Address", doc.customer_address)
vishdhad3ec1c12020-03-24 11:31:41 +0530272 else:
273 shipping_address = get_company_address_details(doc)
274
275 return shipping_address
276
277
278def get_iso_3166_2_state_code(address):
Deepesh Gargdcb462f2020-08-01 13:47:09 +0530279 import pycountry
vishdhad3ec1c12020-03-24 11:31:41 +0530280 country_code = frappe.db.get_value("Country", address.get("country"), "code")
281
282 error_message = _("""{0} is not a valid state! Check for typos or enter the ISO code for your state.""").format(address.get("state"))
283 state = address.get("state").upper().strip()
284
285 # The max length for ISO state codes is 3, excluding the country code
286 if len(state) <= 3:
287 # PyCountry returns state code as {country_code}-{state-code} (e.g. US-FL)
288 address_state = (country_code + "-" + state).upper()
289
290 states = pycountry.subdivisions.get(country_code=country_code.upper())
291 states = [pystate.code for pystate in states]
292
293 if address_state in states:
294 return state
295
296 frappe.throw(_(error_message))
297 else:
298 try:
299 lookup_state = pycountry.subdivisions.lookup(state)
300 except LookupError:
301 frappe.throw(_(error_message))
302 else:
303 return lookup_state.code.split('-')[1]
304
305
306def sanitize_error_response(response):
307 response = response.full_response.get("detail")
308 response = response.replace("_", " ")
309
310 sanitized_responses = {
311 "to zip": "Zipcode",
312 "to city": "City",
313 "to state": "State",
314 "to country": "Country"
315 }
316
317 for k, v in sanitized_responses.items():
318 response = response.replace(k, v)
319
320 return response