style: format code with black
This commit is contained in:
@@ -8,14 +8,92 @@ from frappe.utils import cint, flt
|
||||
|
||||
from erpnext import get_default_company, get_region
|
||||
|
||||
SUPPORTED_COUNTRY_CODES = ["AT", "AU", "BE", "BG", "CA", "CY", "CZ", "DE", "DK", "EE", "ES", "FI",
|
||||
"FR", "GB", "GR", "HR", "HU", "IE", "IT", "LT", "LU", "LV", "MT", "NL", "PL", "PT", "RO",
|
||||
"SE", "SI", "SK", "US"]
|
||||
SUPPORTED_STATE_CODES = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL',
|
||||
'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE',
|
||||
'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD',
|
||||
'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY']
|
||||
|
||||
SUPPORTED_COUNTRY_CODES = [
|
||||
"AT",
|
||||
"AU",
|
||||
"BE",
|
||||
"BG",
|
||||
"CA",
|
||||
"CY",
|
||||
"CZ",
|
||||
"DE",
|
||||
"DK",
|
||||
"EE",
|
||||
"ES",
|
||||
"FI",
|
||||
"FR",
|
||||
"GB",
|
||||
"GR",
|
||||
"HR",
|
||||
"HU",
|
||||
"IE",
|
||||
"IT",
|
||||
"LT",
|
||||
"LU",
|
||||
"LV",
|
||||
"MT",
|
||||
"NL",
|
||||
"PL",
|
||||
"PT",
|
||||
"RO",
|
||||
"SE",
|
||||
"SI",
|
||||
"SK",
|
||||
"US",
|
||||
]
|
||||
SUPPORTED_STATE_CODES = [
|
||||
"AL",
|
||||
"AK",
|
||||
"AZ",
|
||||
"AR",
|
||||
"CA",
|
||||
"CO",
|
||||
"CT",
|
||||
"DE",
|
||||
"DC",
|
||||
"FL",
|
||||
"GA",
|
||||
"HI",
|
||||
"ID",
|
||||
"IL",
|
||||
"IN",
|
||||
"IA",
|
||||
"KS",
|
||||
"KY",
|
||||
"LA",
|
||||
"ME",
|
||||
"MD",
|
||||
"MA",
|
||||
"MI",
|
||||
"MN",
|
||||
"MS",
|
||||
"MO",
|
||||
"MT",
|
||||
"NE",
|
||||
"NV",
|
||||
"NH",
|
||||
"NJ",
|
||||
"NM",
|
||||
"NY",
|
||||
"NC",
|
||||
"ND",
|
||||
"OH",
|
||||
"OK",
|
||||
"OR",
|
||||
"PA",
|
||||
"RI",
|
||||
"SC",
|
||||
"SD",
|
||||
"TN",
|
||||
"TX",
|
||||
"UT",
|
||||
"VT",
|
||||
"VA",
|
||||
"WA",
|
||||
"WV",
|
||||
"WI",
|
||||
"WY",
|
||||
]
|
||||
|
||||
|
||||
def get_client():
|
||||
@@ -30,14 +108,14 @@ def get_client():
|
||||
|
||||
if api_key and api_url:
|
||||
client = taxjar.Client(api_key=api_key, api_url=api_url)
|
||||
client.set_api_config('headers', {
|
||||
'x-api-version': '2022-01-24'
|
||||
})
|
||||
client.set_api_config("headers", {"x-api-version": "2022-01-24"})
|
||||
return client
|
||||
|
||||
|
||||
def create_transaction(doc, method):
|
||||
TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value("TaxJar Settings", "taxjar_create_transactions")
|
||||
TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value(
|
||||
"TaxJar Settings", "taxjar_create_transactions"
|
||||
)
|
||||
|
||||
"""Create an order transaction in TaxJar"""
|
||||
|
||||
@@ -60,10 +138,10 @@ def create_transaction(doc, method):
|
||||
if not tax_dict:
|
||||
return
|
||||
|
||||
tax_dict['transaction_id'] = doc.name
|
||||
tax_dict['transaction_date'] = frappe.utils.today()
|
||||
tax_dict['sales_tax'] = sales_tax
|
||||
tax_dict['amount'] = doc.total + tax_dict['shipping']
|
||||
tax_dict["transaction_id"] = doc.name
|
||||
tax_dict["transaction_date"] = frappe.utils.today()
|
||||
tax_dict["sales_tax"] = sales_tax
|
||||
tax_dict["amount"] = doc.total + tax_dict["shipping"]
|
||||
|
||||
try:
|
||||
if doc.is_return:
|
||||
@@ -78,7 +156,9 @@ def create_transaction(doc, method):
|
||||
|
||||
def delete_transaction(doc, method):
|
||||
"""Delete an existing TaxJar order transaction"""
|
||||
TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value("TaxJar Settings", "taxjar_create_transactions")
|
||||
TAXJAR_CREATE_TRANSACTIONS = frappe.db.get_single_value(
|
||||
"TaxJar Settings", "taxjar_create_transactions"
|
||||
)
|
||||
|
||||
if not TAXJAR_CREATE_TRANSACTIONS:
|
||||
return
|
||||
@@ -109,10 +189,10 @@ def get_tax_data(doc):
|
||||
line_items = [get_line_item_dict(item, doc.docstatus) for item in doc.items]
|
||||
|
||||
if from_shipping_state not in SUPPORTED_STATE_CODES:
|
||||
from_shipping_state = get_state_code(from_address, 'Company')
|
||||
from_shipping_state = get_state_code(from_address, "Company")
|
||||
|
||||
if to_shipping_state not in SUPPORTED_STATE_CODES:
|
||||
to_shipping_state = get_state_code(to_address, 'Shipping')
|
||||
to_shipping_state = get_state_code(to_address, "Shipping")
|
||||
|
||||
tax_dict = {
|
||||
"from_country": from_country_code,
|
||||
@@ -128,10 +208,11 @@ def get_tax_data(doc):
|
||||
"shipping": shipping,
|
||||
"amount": doc.net_total,
|
||||
"plugin": "erpnext",
|
||||
"line_items": line_items
|
||||
"line_items": line_items,
|
||||
}
|
||||
return tax_dict
|
||||
|
||||
|
||||
def get_state_code(address, location):
|
||||
if address is not None:
|
||||
state_code = get_iso_3166_2_state_code(address)
|
||||
@@ -142,21 +223,21 @@ def get_state_code(address, location):
|
||||
|
||||
return state_code
|
||||
|
||||
|
||||
def get_line_item_dict(item, docstatus):
|
||||
tax_dict = dict(
|
||||
id = item.get('idx'),
|
||||
quantity = item.get('qty'),
|
||||
unit_price = item.get('rate'),
|
||||
product_tax_code = item.get('product_tax_category')
|
||||
id=item.get("idx"),
|
||||
quantity=item.get("qty"),
|
||||
unit_price=item.get("rate"),
|
||||
product_tax_code=item.get("product_tax_category"),
|
||||
)
|
||||
|
||||
if docstatus == 1:
|
||||
tax_dict.update({
|
||||
'sales_tax':item.get('tax_collectable')
|
||||
})
|
||||
tax_dict.update({"sales_tax": item.get("tax_collectable")})
|
||||
|
||||
return tax_dict
|
||||
|
||||
|
||||
def set_sales_tax(doc, method):
|
||||
TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
|
||||
TAXJAR_CALCULATE_TAX = frappe.db.get_single_value("TaxJar Settings", "taxjar_calculate_tax")
|
||||
@@ -164,7 +245,7 @@ def set_sales_tax(doc, method):
|
||||
if not TAXJAR_CALCULATE_TAX:
|
||||
return
|
||||
|
||||
if get_region(doc.company) != 'United States':
|
||||
if get_region(doc.company) != "United States":
|
||||
return
|
||||
|
||||
if not doc.items:
|
||||
@@ -197,22 +278,26 @@ def set_sales_tax(doc, method):
|
||||
doc.run_method("calculate_taxes_and_totals")
|
||||
break
|
||||
else:
|
||||
doc.append("taxes", {
|
||||
"charge_type": "Actual",
|
||||
"description": "Sales Tax",
|
||||
"account_head": TAX_ACCOUNT_HEAD,
|
||||
"tax_amount": tax_data.amount_to_collect
|
||||
})
|
||||
doc.append(
|
||||
"taxes",
|
||||
{
|
||||
"charge_type": "Actual",
|
||||
"description": "Sales Tax",
|
||||
"account_head": TAX_ACCOUNT_HEAD,
|
||||
"tax_amount": tax_data.amount_to_collect,
|
||||
},
|
||||
)
|
||||
# Assigning values to tax_collectable and taxable_amount fields in sales item table
|
||||
for item in tax_data.breakdown.line_items:
|
||||
doc.get('items')[cint(item.id)-1].tax_collectable = item.tax_collectable
|
||||
doc.get('items')[cint(item.id)-1].taxable_amount = item.taxable_amount
|
||||
doc.get("items")[cint(item.id) - 1].tax_collectable = item.tax_collectable
|
||||
doc.get("items")[cint(item.id) - 1].taxable_amount = item.taxable_amount
|
||||
|
||||
doc.run_method("calculate_taxes_and_totals")
|
||||
|
||||
|
||||
def check_for_nexus(doc, tax_dict):
|
||||
TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
|
||||
if not frappe.db.get_value('TaxJar Nexus', {'region_code': tax_dict["to_state"]}):
|
||||
if not frappe.db.get_value("TaxJar Nexus", {"region_code": tax_dict["to_state"]}):
|
||||
for item in doc.get("items"):
|
||||
item.tax_collectable = flt(0)
|
||||
item.taxable_amount = flt(0)
|
||||
@@ -222,13 +307,17 @@ def check_for_nexus(doc, tax_dict):
|
||||
doc.taxes.remove(tax)
|
||||
return
|
||||
|
||||
|
||||
def check_sales_tax_exemption(doc):
|
||||
# if the party is exempt from sales tax, then set all tax account heads to zero
|
||||
TAX_ACCOUNT_HEAD = frappe.db.get_single_value("TaxJar Settings", "tax_account_head")
|
||||
|
||||
sales_tax_exempted = hasattr(doc, "exempt_from_sales_tax") and doc.exempt_from_sales_tax \
|
||||
or frappe.db.has_column("Customer", "exempt_from_sales_tax") \
|
||||
sales_tax_exempted = (
|
||||
hasattr(doc, "exempt_from_sales_tax")
|
||||
and doc.exempt_from_sales_tax
|
||||
or frappe.db.has_column("Customer", "exempt_from_sales_tax")
|
||||
and frappe.db.get_value("Customer", doc.customer, "exempt_from_sales_tax")
|
||||
)
|
||||
|
||||
if sales_tax_exempted:
|
||||
for tax in doc.taxes:
|
||||
@@ -240,6 +329,7 @@ def check_sales_tax_exemption(doc):
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def validate_tax_request(tax_dict):
|
||||
"""Return the sales tax that should be collected for a given order."""
|
||||
|
||||
@@ -283,9 +373,12 @@ def get_shipping_address_details(doc):
|
||||
|
||||
def get_iso_3166_2_state_code(address):
|
||||
import pycountry
|
||||
|
||||
country_code = frappe.db.get_value("Country", address.get("country"), "code")
|
||||
|
||||
error_message = _("""{0} is not a valid state! Check for typos or enter the ISO code for your state.""").format(address.get("state"))
|
||||
error_message = _(
|
||||
"""{0} is not a valid state! Check for typos or enter the ISO code for your state."""
|
||||
).format(address.get("state"))
|
||||
state = address.get("state").upper().strip()
|
||||
|
||||
# The max length for ISO state codes is 3, excluding the country code
|
||||
@@ -306,7 +399,7 @@ def get_iso_3166_2_state_code(address):
|
||||
except LookupError:
|
||||
frappe.throw(_(error_message))
|
||||
else:
|
||||
return lookup_state.code.split('-')[1]
|
||||
return lookup_state.code.split("-")[1]
|
||||
|
||||
|
||||
def sanitize_error_response(response):
|
||||
@@ -317,7 +410,7 @@ def sanitize_error_response(response):
|
||||
"to zip": "Zipcode",
|
||||
"to city": "City",
|
||||
"to state": "State",
|
||||
"to country": "Country"
|
||||
"to country": "Country",
|
||||
}
|
||||
|
||||
for k, v in sanitized_responses.items():
|
||||
|
||||
Reference in New Issue
Block a user