Example #1
0
    def calculate_checkout_line_total(
        self,
        checkout_info: "CheckoutInfo",
        lines: Iterable["CheckoutLineInfo"],
        checkout_line_info: "CheckoutLineInfo",
        address: Optional["Address"],
        discounts: Iterable["DiscountInfo"],
        previous_value: CheckoutTaxedPricesData,
    ) -> CheckoutTaxedPricesData:
        if self._skip_plugin(previous_value):
            return previous_value

        if not checkout_line_info.product.charge_taxes:
            return previous_value

        if not _validate_checkout(checkout_info, lines):
            return previous_value

        taxes_data = get_checkout_tax_data(checkout_info, lines, discounts,
                                           self.config)
        if not taxes_data or "Errors found" in taxes_data["Status"]:
            return previous_value
        process_checkout_metadata(taxes_data, checkout_info.checkout)
        return self._calculate_checkout_line_total_price(
            taxes_data, lines, checkout_line_info.line.id, previous_value)
Example #2
0
    def calculate_checkout_shipping(
        self,
        checkout_info: "CheckoutInfo",
        lines: List["CheckoutLineInfo"],
        address: Optional["Address"],
        discounts: List["DiscountInfo"],
        previous_value: TaxedMoney,
    ) -> TaxedMoney:
        if not charge_taxes_on_shipping():
            return previous_value

        if self._skip_plugin(previous_value):
            return previous_value

        if not _validate_checkout(checkout_info, lines):
            return previous_value

        taxes_data = get_checkout_tax_data(checkout_info, lines, discounts,
                                           self.config)
        if not taxes_data or "error" in taxes_data:
            return previous_value
        process_checkout_metadata(taxes_data, checkout_info.checkout)

        tax_lines = taxes_data.get("TransactionTaxes", [])
        if not tax_lines:
            return previous_value

        currency = checkout_info.checkout.currency
        return self._calculate_checkout_shipping(currency, tax_lines,
                                                 previous_value)
Example #3
0
    def calculate_checkout_line_total(
        self,
        checkout_line: "CheckoutLine",
        discounts: Iterable[DiscountInfo],
        previous_value: TaxedMoney,
    ) -> TaxedMoney:
        if self._skip_plugin(previous_value):
            return previous_value

        base_total = previous_value
        if not checkout_line.variant.product.charge_taxes:
            return base_total

        checkout = checkout_line.checkout

        if not _validate_checkout(checkout, [checkout_line]):
            return base_total

        taxes_data = get_checkout_tax_data(checkout, discounts, self.config)

        if not taxes_data or "Error" in taxes_data["Status"]:
            return base_total

        tax_meta = json.dumps(taxes_data["TransactionTaxes"])
        process_checkout_metadata(tax_meta, checkout)

        line_tax_total = Decimal(0)

        for line in taxes_data.get("TransactionTaxes", []):
            if line.get("InvoiceLine") == checkout_line.id:
                line_tax_total += Decimal(line.get("TaxAmount", 0.0))

        if not line_tax_total > 0:
            return base_total

        currency = checkout.currency
        tax = Decimal(line_tax_total)
        line_net = Decimal(base_total.net.amount)
        line_gross = Money(amount=line_net + tax, currency=currency)
        line_net = Money(amount=line_net, currency=currency)

        return quantize_price(TaxedMoney(net=line_net, gross=line_gross),
                              currency)
Example #4
0
    def _get_checkout_tax_data(
        self,
        checkout_info: "CheckoutInfo",
        lines_info: Iterable["CheckoutLineInfo"],
        discounts: Iterable[DiscountInfo],
        previous_value: Decimal,
    ):
        if self._skip_plugin(previous_value):
            return None

        valid = _validate_checkout(checkout_info, lines_info)
        if not valid:
            return None

        taxes_data = get_checkout_tax_data(checkout_info, lines_info,
                                           discounts, self.config)
        if not taxes_data or "error" in taxes_data:
            return None

        return taxes_data
Example #5
0
    def calculate_checkout_total(
        self,
        checkout: "Checkout",
        lines: Iterable["CheckoutLine"],
        discounts: Iterable[DiscountInfo],
        previous_value: TaxedMoney,
    ) -> TaxedMoney:
        if self._skip_plugin(previous_value):
            logger.debug("Skip Plugin in Calculate Checkout Total")
            return previous_value
        checkout_total = previous_value

        if not _validate_checkout(checkout, lines):
            logger.debug("Checkout Invalid in Calculate Checkout Total")
            return checkout_total

        response = get_checkout_tax_data(checkout, discounts, self.config)
        if not response or "Errors found" in response["Status"]:
            return checkout_total

        if len(response["TransactionTaxes"]) == 0:
            raise TaxError("ATE did not return TransactionTaxes")

        currency = checkout.currency

        tax = Money(Decimal(response.get("TotalTaxAmount", 0.0)), currency)
        net = checkout_total.net
        total_gross = net + tax
        taxed_total = quantize_price(TaxedMoney(net=net, gross=total_gross),
                                     currency)
        total = self._append_prices_of_not_taxed_lines(
            taxed_total,
            lines,
            discounts,
        )

        voucher_value = checkout.discount
        if voucher_value:
            total -= voucher_value

        return max(total, zero_taxed_money(total.currency))
    def calculate_checkout_line_total(
        self,
        checkout_info: "CheckoutInfo",
        lines: Iterable["CheckoutLineInfo"],
        checkout_line_info: "CheckoutLineInfo",
        address: Optional["Address"],
        discounts: Iterable["DiscountInfo"],
        previous_value: TaxedMoney,
    ) -> TaxedMoney:
        if self._skip_plugin(previous_value):
            return previous_value

        if not checkout_line_info.product.charge_taxes:
            return previous_value

        checkout = checkout_info.checkout

        if not _validate_checkout(checkout_info, lines):
            return previous_value

        response = get_checkout_tax_data(checkout_info, lines, discounts,
                                         self.config)
        if not response or "Error" in response["Status"]:
            return previous_value

        tax = Decimal(response.get("TotalTaxAmount", "0.00"))
        tax_lines = response.get("TransactionTaxes", [])
        if not tax_lines:
            return previous_value

        tax_meta = json.dumps(tax_lines)
        process_checkout_metadata(tax_meta, checkout)

        currency = checkout.currency
        net = Decimal(previous_value.net.amount)

        line_net = Money(amount=net, currency=currency)
        line_gross = Money(amount=net + tax, currency=currency)

        return quantize_price(TaxedMoney(net=line_net, gross=line_gross),
                              currency)
    def calculate_checkout_total(
        self,
        checkout_info: "CheckoutInfo",
        lines: Iterable["CheckoutLineInfo"],
        address: Optional["Address"],
        discounts: Iterable[DiscountInfo],
        previous_value: TaxedMoney,
    ) -> TaxedMoney:
        if self._skip_plugin(previous_value):
            logger.debug("Skip Plugin in Calculate Checkout Total")
            return previous_value
        checkout_total = previous_value

        if not _validate_checkout(checkout_info, lines):
            logger.debug("Checkout Invalid in Calculate Checkout Total")
            return checkout_total

        checkout = checkout_info.checkout
        response = get_checkout_tax_data(checkout_info, lines, discounts,
                                         self.config)
        if not response or "Errors found" in response["Status"]:
            return checkout_total

        currency = checkout.currency
        tax = Money(Decimal(response.get("TotalTaxAmount", 0.0)), currency)
        net = checkout_total.net
        gross = net + tax
        taxed_total = quantize_price(TaxedMoney(net=net, gross=gross),
                                     currency)
        total = self._append_prices_of_not_taxed_lines(
            taxed_total,
            lines,
            checkout_info.channel,
            discounts,
        )

        voucher_value = checkout.discount
        if voucher_value:
            total -= voucher_value

        return max(total, zero_taxed_money(total.currency))