vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 1 | import traceback |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 2 | |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 3 | import frappe |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 4 | import taxjar |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 5 | from frappe import _ |
| 6 | from frappe.contacts.doctype.address.address import get_company_address |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 7 | from frappe.utils import cint |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 8 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 9 | from erpnext import get_default_company |
| 10 | |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 11 | TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head") |
| 12 | SHIP_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "shipping_account_head") |
| 13 | TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value("TaxJar Settings", "taxjar_create_transactions") |
| 14 | TAXJAR_CALCULATE_TAX = frappe.db.get_single_value("TaxJar Settings", "taxjar_calculate_tax") |
| 15 | SUPPORTED_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 Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 18 | SUPPORTED_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 Menat | b147b85 | 2021-09-01 16:45:57 +0530 | [diff] [blame] | 20 | 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 21 | 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'] |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def get_client(): |
| 25 | taxjar_settings = frappe.get_single("TaxJar Settings") |
| 26 | |
| 27 | if not taxjar_settings.is_sandbox: |
| 28 | api_key = taxjar_settings.api_key and taxjar_settings.get_password("api_key") |
| 29 | api_url = taxjar.DEFAULT_API_URL |
| 30 | else: |
| 31 | api_key = taxjar_settings.sandbox_api_key and taxjar_settings.get_password("sandbox_api_key") |
| 32 | api_url = taxjar.SANDBOX_API_URL |
| 33 | |
| 34 | if api_key and api_url: |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 35 | client = taxjar.Client(api_key=api_key, api_url=api_url) |
| 36 | client.set_api_config('headers', { |
| 37 | 'x-api-version': '2020-08-07' |
| 38 | }) |
| 39 | return client |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def create_transaction(doc, method): |
| 43 | """Create an order transaction in TaxJar""" |
| 44 | |
| 45 | if not TAXJAR_CREATE_TRANSACTIONS: |
| 46 | return |
| 47 | |
| 48 | client = get_client() |
| 49 | |
| 50 | if not client: |
| 51 | return |
| 52 | |
| 53 | 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 Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 69 | if doc.is_return: |
| 70 | client.create_refund(tax_dict) |
Ankush Menat | b147b85 | 2021-09-01 16:45:57 +0530 | [diff] [blame] | 71 | else: |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 72 | client.create_order(tax_dict) |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 73 | 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 | |
| 79 | def delete_transaction(doc, method): |
| 80 | """Delete an existing TaxJar order transaction""" |
| 81 | |
| 82 | if not TAXJAR_CREATE_TRANSACTIONS: |
| 83 | return |
| 84 | |
| 85 | client = get_client() |
| 86 | |
| 87 | if not client: |
| 88 | return |
| 89 | |
| 90 | client.delete_order(doc.name) |
| 91 | |
| 92 | |
| 93 | def get_tax_data(doc): |
| 94 | from_address = get_company_address_details(doc) |
| 95 | from_shipping_state = from_address.get("state") |
| 96 | from_country_code = frappe.db.get_value("Country", from_address.country, "code") |
| 97 | from_country_code = from_country_code.upper() |
| 98 | |
| 99 | to_address = get_shipping_address_details(doc) |
| 100 | to_shipping_state = to_address.get("state") |
| 101 | to_country_code = frappe.db.get_value("Country", to_address.country, "code") |
| 102 | to_country_code = to_country_code.upper() |
| 103 | |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 104 | shipping = sum([tax.tax_amount for tax in doc.taxes if tax.account_head == SHIP_ACCOUNT_HEAD]) |
| 105 | |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 106 | line_items = [get_line_item_dict(item) for item in doc.items] |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 107 | |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 108 | if from_shipping_state not in SUPPORTED_STATE_CODES: |
| 109 | from_shipping_state = get_state_code(from_address, 'Company') |
| 110 | |
| 111 | if to_shipping_state not in SUPPORTED_STATE_CODES: |
| 112 | to_shipping_state = get_state_code(to_address, 'Shipping') |
Ankush Menat | b147b85 | 2021-09-01 16:45:57 +0530 | [diff] [blame] | 113 | |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 114 | tax_dict = { |
| 115 | 'from_country': from_country_code, |
| 116 | 'from_zip': from_address.pincode, |
| 117 | 'from_state': from_shipping_state, |
| 118 | 'from_city': from_address.city, |
| 119 | 'from_street': from_address.address_line1, |
| 120 | 'to_country': to_country_code, |
| 121 | 'to_zip': to_address.pincode, |
| 122 | 'to_city': to_address.city, |
| 123 | 'to_street': to_address.address_line1, |
| 124 | 'to_state': to_shipping_state, |
| 125 | 'shipping': shipping, |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 126 | 'amount': doc.net_total, |
| 127 | 'plugin': 'erpnext', |
| 128 | 'line_items': line_items |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 129 | } |
Ankush Menat | b147b85 | 2021-09-01 16:45:57 +0530 | [diff] [blame] | 130 | return tax_dict |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 131 | |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 132 | def get_state_code(address, location): |
| 133 | if address is not None: |
| 134 | state_code = get_iso_3166_2_state_code(address) |
| 135 | if state_code not in SUPPORTED_STATE_CODES: |
| 136 | frappe.throw(_("Please enter a valid State in the {0} Address").format(location)) |
| 137 | else: |
| 138 | frappe.throw(_("Please enter a valid State in the {0} Address").format(location)) |
Ankush Menat | b147b85 | 2021-09-01 16:45:57 +0530 | [diff] [blame] | 139 | |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 140 | return state_code |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 141 | |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 142 | def get_line_item_dict(item): |
Ankush Menat | b147b85 | 2021-09-01 16:45:57 +0530 | [diff] [blame] | 143 | return dict( |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 144 | id = item.get('idx'), |
| 145 | quantity = item.get('qty'), |
| 146 | unit_price = item.get('rate'), |
| 147 | product_tax_code = item.get('product_tax_category') |
Ankush Menat | b147b85 | 2021-09-01 16:45:57 +0530 | [diff] [blame] | 148 | ) |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 149 | |
| 150 | def set_sales_tax(doc, method): |
| 151 | if not TAXJAR_CALCULATE_TAX: |
| 152 | return |
| 153 | |
| 154 | if not doc.items: |
| 155 | return |
| 156 | |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 157 | if check_sales_tax_exemption(doc): |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 158 | return |
| 159 | |
| 160 | tax_dict = get_tax_data(doc) |
| 161 | |
| 162 | if not tax_dict: |
| 163 | # Remove existing tax rows if address is changed from a taxable state/country |
| 164 | setattr(doc, "taxes", [tax for tax in doc.taxes if tax.account_head != TAX_ACCOUNT_HEAD]) |
| 165 | return |
| 166 | |
| 167 | tax_data = validate_tax_request(tax_dict) |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 168 | if tax_data is not None: |
| 169 | if not tax_data.amount_to_collect: |
| 170 | setattr(doc, "taxes", [tax for tax in doc.taxes if tax.account_head != TAX_ACCOUNT_HEAD]) |
| 171 | elif tax_data.amount_to_collect > 0: |
| 172 | # Loop through tax rows for existing Sales Tax entry |
| 173 | # If none are found, add a row with the tax amount |
| 174 | for tax in doc.taxes: |
| 175 | if tax.account_head == TAX_ACCOUNT_HEAD: |
| 176 | tax.tax_amount = tax_data.amount_to_collect |
| 177 | |
| 178 | doc.run_method("calculate_taxes_and_totals") |
| 179 | break |
| 180 | else: |
| 181 | doc.append("taxes", { |
| 182 | "charge_type": "Actual", |
| 183 | "description": "Sales Tax", |
| 184 | "account_head": TAX_ACCOUNT_HEAD, |
| 185 | "tax_amount": tax_data.amount_to_collect |
| 186 | }) |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 187 | # Assigning values to tax_collectable and taxable_amount fields in sales item table |
| 188 | for item in tax_data.breakdown.line_items: |
| 189 | doc.get('items')[cint(item.id)-1].tax_collectable = item.tax_collectable |
| 190 | doc.get('items')[cint(item.id)-1].taxable_amount = item.taxable_amount |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 191 | |
| 192 | doc.run_method("calculate_taxes_and_totals") |
| 193 | |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 194 | def check_sales_tax_exemption(doc): |
| 195 | # if the party is exempt from sales tax, then set all tax account heads to zero |
| 196 | sales_tax_exempted = hasattr(doc, "exempt_from_sales_tax") and doc.exempt_from_sales_tax \ |
| 197 | or frappe.db.has_column("Customer", "exempt_from_sales_tax") \ |
| 198 | and frappe.db.get_value("Customer", doc.customer, "exempt_from_sales_tax") |
| 199 | |
| 200 | if sales_tax_exempted: |
| 201 | for tax in doc.taxes: |
| 202 | if tax.account_head == TAX_ACCOUNT_HEAD: |
| 203 | tax.tax_amount = 0 |
| 204 | break |
| 205 | doc.run_method("calculate_taxes_and_totals") |
| 206 | return True |
Ankush Menat | b147b85 | 2021-09-01 16:45:57 +0530 | [diff] [blame] | 207 | else: |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 208 | return False |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 209 | |
| 210 | def validate_tax_request(tax_dict): |
| 211 | """Return the sales tax that should be collected for a given order.""" |
| 212 | |
| 213 | client = get_client() |
| 214 | |
| 215 | if not client: |
| 216 | return |
| 217 | |
| 218 | try: |
| 219 | tax_data = client.tax_for_order(tax_dict) |
| 220 | except taxjar.exceptions.TaxJarResponseError as err: |
| 221 | frappe.throw(_(sanitize_error_response(err))) |
| 222 | else: |
| 223 | return tax_data |
| 224 | |
| 225 | |
| 226 | def get_company_address_details(doc): |
| 227 | """Return default company address details""" |
| 228 | |
| 229 | company_address = get_company_address(get_default_company()).company_address |
| 230 | |
| 231 | if not company_address: |
| 232 | frappe.throw(_("Please set a default company address")) |
| 233 | |
| 234 | company_address = frappe.get_doc("Address", company_address) |
| 235 | return company_address |
| 236 | |
| 237 | |
| 238 | def get_shipping_address_details(doc): |
| 239 | """Return customer shipping address details""" |
| 240 | |
| 241 | if doc.shipping_address_name: |
| 242 | shipping_address = frappe.get_doc("Address", doc.shipping_address_name) |
Subin Tom | 7004944 | 2021-08-31 18:33:16 +0530 | [diff] [blame] | 243 | elif doc.customer_address: |
| 244 | shipping_address = frappe.get_doc("Address", doc.customer_address_name) |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 245 | else: |
| 246 | shipping_address = get_company_address_details(doc) |
| 247 | |
| 248 | return shipping_address |
| 249 | |
| 250 | |
| 251 | def get_iso_3166_2_state_code(address): |
Deepesh Garg | dcb462f | 2020-08-01 13:47:09 +0530 | [diff] [blame] | 252 | import pycountry |
vishdha | d3ec1c1 | 2020-03-24 11:31:41 +0530 | [diff] [blame] | 253 | country_code = frappe.db.get_value("Country", address.get("country"), "code") |
| 254 | |
| 255 | error_message = _("""{0} is not a valid state! Check for typos or enter the ISO code for your state.""").format(address.get("state")) |
| 256 | state = address.get("state").upper().strip() |
| 257 | |
| 258 | # The max length for ISO state codes is 3, excluding the country code |
| 259 | if len(state) <= 3: |
| 260 | # PyCountry returns state code as {country_code}-{state-code} (e.g. US-FL) |
| 261 | address_state = (country_code + "-" + state).upper() |
| 262 | |
| 263 | states = pycountry.subdivisions.get(country_code=country_code.upper()) |
| 264 | states = [pystate.code for pystate in states] |
| 265 | |
| 266 | if address_state in states: |
| 267 | return state |
| 268 | |
| 269 | frappe.throw(_(error_message)) |
| 270 | else: |
| 271 | try: |
| 272 | lookup_state = pycountry.subdivisions.lookup(state) |
| 273 | except LookupError: |
| 274 | frappe.throw(_(error_message)) |
| 275 | else: |
| 276 | return lookup_state.code.split('-')[1] |
| 277 | |
| 278 | |
| 279 | def sanitize_error_response(response): |
| 280 | response = response.full_response.get("detail") |
| 281 | response = response.replace("_", " ") |
| 282 | |
| 283 | sanitized_responses = { |
| 284 | "to zip": "Zipcode", |
| 285 | "to city": "City", |
| 286 | "to state": "State", |
| 287 | "to country": "Country" |
| 288 | } |
| 289 | |
| 290 | for k, v in sanitized_responses.items(): |
| 291 | response = response.replace(k, v) |
| 292 | |
| 293 | return response |