blob: 83764ae50d8367464f85c09bd8ec017c2f8945a5 [file] [log] [blame]
vishdhad3ec1c12020-03-24 11:31:41 +05301import traceback
vishdhad3ec1c12020-03-24 11:31:41 +05302import frappe
Subin Tom70049442021-08-31 18:33:16 +05303import taxjar
vishdhad3ec1c12020-03-24 11:31:41 +05304from erpnext import get_default_company
5from frappe import _
6from frappe.contacts.doctype.address.address import get_company_address
Subin Tom70049442021-08-31 18:33:16 +05307from frappe.utils import cint
vishdhad3ec1c12020-03-24 11:31:41 +05308
9TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
10SHIP_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "shipping_account_head")
11TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value("TaxJar Settings", "taxjar_create_transactions")
12TAXJAR_CALCULATE_TAX = frappe.db.get_single_value("TaxJar Settings", "taxjar_calculate_tax")
13SUPPORTED_COUNTRY_CODES = ["AT", "AU", "BE", "BG", "CA", "CY", "CZ", "DE", "DK", "EE", "ES", "FI",
14 "FR", "GB", "GR", "HR", "HU", "IE", "IT", "LT", "LU", "LV", "MT", "NL", "PL", "PT", "RO",
15 "SE", "SI", "SK", "US"]
Subin Tom70049442021-08-31 18:33:16 +053016SUPPORTED_STATE_CODES = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL',
17 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE',
18 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD',
19 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY']
vishdhad3ec1c12020-03-24 11:31:41 +053020
21
22def get_client():
23 taxjar_settings = frappe.get_single("TaxJar Settings")
24
25 if not taxjar_settings.is_sandbox:
26 api_key = taxjar_settings.api_key and taxjar_settings.get_password("api_key")
27 api_url = taxjar.DEFAULT_API_URL
28 else:
29 api_key = taxjar_settings.sandbox_api_key and taxjar_settings.get_password("sandbox_api_key")
30 api_url = taxjar.SANDBOX_API_URL
31
32 if api_key and api_url:
Subin Tom70049442021-08-31 18:33:16 +053033 client = taxjar.Client(api_key=api_key, api_url=api_url)
34 client.set_api_config('headers', {
35 'x-api-version': '2020-08-07'
36 })
37 return client
vishdhad3ec1c12020-03-24 11:31:41 +053038
39
40def create_transaction(doc, method):
41 """Create an order transaction in TaxJar"""
42
43 if not TAXJAR_CREATE_TRANSACTIONS:
44 return
45
46 client = get_client()
47
48 if not client:
49 return
50
51 sales_tax = sum([tax.tax_amount for tax in doc.taxes if tax.account_head == TAX_ACCOUNT_HEAD])
52
53 if not sales_tax:
54 return
55
56 tax_dict = get_tax_data(doc)
57
58 if not tax_dict:
59 return
60
61 tax_dict['transaction_id'] = doc.name
62 tax_dict['transaction_date'] = frappe.utils.today()
63 tax_dict['sales_tax'] = sales_tax
64 tax_dict['amount'] = doc.total + tax_dict['shipping']
65
66 try:
Subin Tom70049442021-08-31 18:33:16 +053067 if doc.is_return:
68 client.create_refund(tax_dict)
69 else:
70 client.create_order(tax_dict)
vishdhad3ec1c12020-03-24 11:31:41 +053071 except taxjar.exceptions.TaxJarResponseError as err:
72 frappe.throw(_(sanitize_error_response(err)))
73 except Exception as ex:
74 print(traceback.format_exc(ex))
75
76
77def delete_transaction(doc, method):
78 """Delete an existing TaxJar order transaction"""
79
80 if not TAXJAR_CREATE_TRANSACTIONS:
81 return
82
83 client = get_client()
84
85 if not client:
86 return
87
88 client.delete_order(doc.name)
89
90
91def get_tax_data(doc):
92 from_address = get_company_address_details(doc)
93 from_shipping_state = from_address.get("state")
94 from_country_code = frappe.db.get_value("Country", from_address.country, "code")
95 from_country_code = from_country_code.upper()
96
97 to_address = get_shipping_address_details(doc)
98 to_shipping_state = to_address.get("state")
99 to_country_code = frappe.db.get_value("Country", to_address.country, "code")
100 to_country_code = to_country_code.upper()
101
vishdhad3ec1c12020-03-24 11:31:41 +0530102 shipping = sum([tax.tax_amount for tax in doc.taxes if tax.account_head == SHIP_ACCOUNT_HEAD])
103
Subin Tom70049442021-08-31 18:33:16 +0530104 line_items = [get_line_item_dict(item) for item in doc.items]
vishdhad3ec1c12020-03-24 11:31:41 +0530105
Subin Tom70049442021-08-31 18:33:16 +0530106 if from_shipping_state not in SUPPORTED_STATE_CODES:
107 from_shipping_state = get_state_code(from_address, 'Company')
108
109 if to_shipping_state not in SUPPORTED_STATE_CODES:
110 to_shipping_state = get_state_code(to_address, 'Shipping')
111
vishdhad3ec1c12020-03-24 11:31:41 +0530112 tax_dict = {
113 'from_country': from_country_code,
114 'from_zip': from_address.pincode,
115 'from_state': from_shipping_state,
116 'from_city': from_address.city,
117 'from_street': from_address.address_line1,
118 'to_country': to_country_code,
119 'to_zip': to_address.pincode,
120 'to_city': to_address.city,
121 'to_street': to_address.address_line1,
122 'to_state': to_shipping_state,
123 'shipping': shipping,
Subin Tom70049442021-08-31 18:33:16 +0530124 'amount': doc.net_total,
125 'plugin': 'erpnext',
126 'line_items': line_items
vishdhad3ec1c12020-03-24 11:31:41 +0530127 }
Subin Tom70049442021-08-31 18:33:16 +0530128 return tax_dict
vishdhad3ec1c12020-03-24 11:31:41 +0530129
Subin Tom70049442021-08-31 18:33:16 +0530130def get_state_code(address, location):
131 if address is not None:
132 state_code = get_iso_3166_2_state_code(address)
133 if state_code not in SUPPORTED_STATE_CODES:
134 frappe.throw(_("Please enter a valid State in the {0} Address").format(location))
135 else:
136 frappe.throw(_("Please enter a valid State in the {0} Address").format(location))
137
138 return state_code
vishdhad3ec1c12020-03-24 11:31:41 +0530139
Subin Tom70049442021-08-31 18:33:16 +0530140def get_line_item_dict(item):
141 return dict(
142 id = item.get('idx'),
143 quantity = item.get('qty'),
144 unit_price = item.get('rate'),
145 product_tax_code = item.get('product_tax_category')
146 )
vishdhad3ec1c12020-03-24 11:31:41 +0530147
148def set_sales_tax(doc, method):
149 if not TAXJAR_CALCULATE_TAX:
150 return
151
152 if not doc.items:
153 return
154
Subin Tom70049442021-08-31 18:33:16 +0530155 if check_sales_tax_exemption(doc):
vishdhad3ec1c12020-03-24 11:31:41 +0530156 return
157
158 tax_dict = get_tax_data(doc)
159
160 if not tax_dict:
161 # Remove existing tax rows if address is changed from a taxable state/country
162 setattr(doc, "taxes", [tax for tax in doc.taxes if tax.account_head != TAX_ACCOUNT_HEAD])
163 return
164
165 tax_data = validate_tax_request(tax_dict)
vishdhad3ec1c12020-03-24 11:31:41 +0530166 if tax_data is not None:
167 if not tax_data.amount_to_collect:
168 setattr(doc, "taxes", [tax for tax in doc.taxes if tax.account_head != TAX_ACCOUNT_HEAD])
169 elif tax_data.amount_to_collect > 0:
170 # Loop through tax rows for existing Sales Tax entry
171 # If none are found, add a row with the tax amount
172 for tax in doc.taxes:
173 if tax.account_head == TAX_ACCOUNT_HEAD:
174 tax.tax_amount = tax_data.amount_to_collect
175
176 doc.run_method("calculate_taxes_and_totals")
177 break
178 else:
179 doc.append("taxes", {
180 "charge_type": "Actual",
181 "description": "Sales Tax",
182 "account_head": TAX_ACCOUNT_HEAD,
183 "tax_amount": tax_data.amount_to_collect
184 })
Subin Tom70049442021-08-31 18:33:16 +0530185 # Assigning values to tax_collectable and taxable_amount fields in sales item table
186 for item in tax_data.breakdown.line_items:
187 doc.get('items')[cint(item.id)-1].tax_collectable = item.tax_collectable
188 doc.get('items')[cint(item.id)-1].taxable_amount = item.taxable_amount
vishdhad3ec1c12020-03-24 11:31:41 +0530189
190 doc.run_method("calculate_taxes_and_totals")
191
Subin Tom70049442021-08-31 18:33:16 +0530192def check_sales_tax_exemption(doc):
193 # if the party is exempt from sales tax, then set all tax account heads to zero
194 sales_tax_exempted = hasattr(doc, "exempt_from_sales_tax") and doc.exempt_from_sales_tax \
195 or frappe.db.has_column("Customer", "exempt_from_sales_tax") \
196 and frappe.db.get_value("Customer", doc.customer, "exempt_from_sales_tax")
197
198 if sales_tax_exempted:
199 for tax in doc.taxes:
200 if tax.account_head == TAX_ACCOUNT_HEAD:
201 tax.tax_amount = 0
202 break
203 doc.run_method("calculate_taxes_and_totals")
204 return True
205 else:
206 return False
vishdhad3ec1c12020-03-24 11:31:41 +0530207
208def validate_tax_request(tax_dict):
209 """Return the sales tax that should be collected for a given order."""
210
211 client = get_client()
212
213 if not client:
214 return
215
216 try:
217 tax_data = client.tax_for_order(tax_dict)
218 except taxjar.exceptions.TaxJarResponseError as err:
219 frappe.throw(_(sanitize_error_response(err)))
220 else:
221 return tax_data
222
223
224def get_company_address_details(doc):
225 """Return default company address details"""
226
227 company_address = get_company_address(get_default_company()).company_address
228
229 if not company_address:
230 frappe.throw(_("Please set a default company address"))
231
232 company_address = frappe.get_doc("Address", company_address)
233 return company_address
234
235
236def get_shipping_address_details(doc):
237 """Return customer shipping address details"""
238
239 if doc.shipping_address_name:
240 shipping_address = frappe.get_doc("Address", doc.shipping_address_name)
Subin Tom70049442021-08-31 18:33:16 +0530241 elif doc.customer_address:
242 shipping_address = frappe.get_doc("Address", doc.customer_address_name)
vishdhad3ec1c12020-03-24 11:31:41 +0530243 else:
244 shipping_address = get_company_address_details(doc)
245
246 return shipping_address
247
248
249def get_iso_3166_2_state_code(address):
Deepesh Gargdcb462f2020-08-01 13:47:09 +0530250 import pycountry
vishdhad3ec1c12020-03-24 11:31:41 +0530251 country_code = frappe.db.get_value("Country", address.get("country"), "code")
252
253 error_message = _("""{0} is not a valid state! Check for typos or enter the ISO code for your state.""").format(address.get("state"))
254 state = address.get("state").upper().strip()
255
256 # The max length for ISO state codes is 3, excluding the country code
257 if len(state) <= 3:
258 # PyCountry returns state code as {country_code}-{state-code} (e.g. US-FL)
259 address_state = (country_code + "-" + state).upper()
260
261 states = pycountry.subdivisions.get(country_code=country_code.upper())
262 states = [pystate.code for pystate in states]
263
264 if address_state in states:
265 return state
266
267 frappe.throw(_(error_message))
268 else:
269 try:
270 lookup_state = pycountry.subdivisions.lookup(state)
271 except LookupError:
272 frappe.throw(_(error_message))
273 else:
274 return lookup_state.code.split('-')[1]
275
276
277def sanitize_error_response(response):
278 response = response.full_response.get("detail")
279 response = response.replace("_", " ")
280
281 sanitized_responses = {
282 "to zip": "Zipcode",
283 "to city": "City",
284 "to state": "State",
285 "to country": "Country"
286 }
287
288 for k, v in sanitized_responses.items():
289 response = response.replace(k, v)
290
291 return response