Repository URL to install this package:
|
Version:
0.4.184 ▾
|
from lib_b2b.address import Address
from lib_b2b.errors import NotFoundError
from lib_b2b.util import ascii_safe_str
from py_aws_oracle_util import fetch
from os import environ
import logging
from aws_xray_sdk.core import xray_recorder
logger = logging.getLogger(__name__)
customer_table = environ['customer_table'] if 'customer_table' in environ else 'test.b2b.Customer'
class GloviaTaxCode:
NO_TAX_CODE = 'NONE'
@staticmethod
@xray_recorder.capture()
def fetch(zipcode):
statement = """select
C_ZIPCODE,
TAX_LOC_CODE
from C_ZIPTAX
where
C_ZIPCODE=:zipcode"""
results = fetch(
sql=statement,
parameters={'zipcode': zipcode[:5]},
config_path="/erp/tdb"
)
if results:
record = results[0]
return record
else:
raise NotFoundError(f"Unable to find tax location for {zipcode}.")
@classmethod
@xray_recorder.capture()
def for_(cls, customer_edi_id: str, ship_to: Address):
from .customer import GloviaCustomer
glovia_customer = GloviaCustomer.fetch(customer_edi_id)
tax_loc = cls.NO_TAX_CODE
if glovia_customer.sales_tax:
try:
gtl = GloviaTaxCode.fetch(ascii_safe_str(ship_to, 'postalCode'))
if gtl and 'TAX_LOC_CODE' in gtl:
tax_loc = gtl['TAX_LOC_CODE']
except NotFoundError:
tax_loc = 'NONE'
return tax_loc
@classmethod
def tax_rates_for(cls, customer_edi_id: str, ship_to: Address):
from .customer import GloviaCustomer
glovia_customer = GloviaCustomer.fetch(customer_edi_id)
tax_rates = []
if glovia_customer.sales_tax:
try:
tax_rate_query = """select zt.C_ZIPCODE, zt.TAX_LOC_CODE, jr.TAX_JUR_CODE, jr.TAX_RATE, tj.NAME
from C_ZIPTAX zt
inner join LOC_JUR lj on lj.TAX_LOC_CODE = zt.TAX_LOC_CODE
inner join JUR_RATE jr on jr.TAX_JUR_CODE = lj.TAX_JUR_CODE
and jr.TAX_TYPE_CODE = lj.TAX_TYPE_CODE
inner join TAX_JUR tj on tj.TAX_JUR_CODE = lj.TAX_JUR_CODE
where
zt.C_ZIPCODE=:zipcode
and lj.CCN = :ccn
and lj.TAX_TYPE_CODE='S'"""
results = fetch(
sql=tax_rate_query,
parameters={'zipcode': ship_to.postalCode5, 'ccn': glovia_customer.ccn},
config_path="/erp/tdb"
)
from lib_b2b.erp import ERPTaxRate
if results:
for result in results:
if result['TAX_RATE'] > 0:
_rate = result['TAX_RATE'] / 100
else:
_rate = result['TAX_RATE']
tax_rates.append(
ERPTaxRate(
rate=_rate,
tax_location_code=result['TAX_LOC_CODE'],
tax_rate_code=result['TAX_JUR_CODE'],
tax_rate_description=result['NAME']
)
)
except Exception as e:
logger.exception(e)
return tax_rates