Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
lib-py-b2b / tax_line.py
Size: Mime:
from decimal import Decimal
from lib_b2b.address import Address
from lib_b2b.persistent import Persistable
import logging
from os import environ

logger = logging.getLogger(__name__)


class TaxLine(Persistable):

    def __init__(self, amount: Decimal, rate: Decimal, tax_type: str):
        self.amount = amount
        self.rate = rate
        self.tax_type = tax_type

    @staticmethod
    def from_dict(data: dict):
        return TaxLine(
            amount=Decimal(str(data['amount'])),
            rate=Decimal(str(data['rate'])),
            tax_type=data.get('tax_type', data.get('type'))
        )

    def as_dict(self) -> dict:
        return vars(self)

    @staticmethod
    def create_for(line, customer, ship_to: Address) -> ['TaxLine']:
        """
        If there is a line amount, the integration type is standard,
        and the customer is taxable, create tax lines based on the ERP data
        :param line: the order line or additional charge for which we want to calculate taxes
        :type line: OrderLine or AdditionalCharge
        :param customer: the customer
        :type customer: str or Customer
        :param ship_to: the address the item is shipping to
        :type ship_to: Address
        :return: a list of tax lines
        :rtype: [TaxLine]
        """
        if customer and ship_to and line.amount:
            _customer_edi_id = customer if isinstance(customer, str) else customer.get('customer_edi_id')
            tax_lines = []
            try:
                from .util import as_decimal
                from .profile import Profile
                profile = Profile.profile_for(_customer_edi_id)
                from . import config as cfg

                if cfg.IntegrationType(profile.integration_type) is cfg.IntegrationType.STANDARD:
                    from lib_b2b.erp import ERP
                    erp = ERP.for_(_customer_edi_id)
                    from lib_b2b.erp import ERPTaxRate
                    tax_rates: [ERPTaxRate] = erp.find_tax_rates(customer_edi_id=_customer_edi_id,
                                                                 ship_to=ship_to)
                    for rate in tax_rates:
                        _rate: ERPTaxRate = rate
                        tax_lines.append(
                            TaxLine(
                                amount=as_decimal(_rate.rate * as_decimal(line.amount, 'Tax Line Amount'),
                                                  'Tax Line Amount * Rate'),
                                rate=_rate.rate,
                                tax_type=_rate.tax_rate_description
                            )
                        )
                return tax_lines
            except Exception as e:
                logger.warning(f"Unable to calculate tax lines for line [{str(line)}]. This will be caught "
                               f"in the validation step as well. So, producing a warning for now.", exc_info=True)
                return tax_lines