簡體   English   中英

對Django Oscar中的所有產品征收18%稅的最佳方法是什么?

[英]What is the best way to apply 18% tax to all products in Django Oscar?

我想對Django Oscar上的所有產品征收18%的稅。 實現此目的的最佳和簡單方法是什么?

我已遵循文檔。

結帳/tax.py

from decimal import Decimal as D


def apply_to(submission):
    # Assume 7% sales tax on sales to New Jersey  You could instead use an
    # external service like Avalara to look up the appropriates taxes.
    STATE_TAX_RATES = {
        'NJ': D('0.07')
    }
    shipping_address = submission['shipping_address']
    rate = D('0.18')
    for line in submission['basket'].all_lines():
        line_tax = calculate_tax(
            line.line_price_excl_tax_incl_discounts, rate)
        unit_tax = (line_tax / line.quantity).quantize(D('0.01'))
        line.purchase_info.price.tax = unit_tax

    # Note, we change the submission in place - we don't need to
    # return anything from this function
    shipping_charge = submission['shipping_charge']
    if shipping_charge is not None:
        shipping_charge.tax = calculate_tax(
            shipping_charge.excl_tax, rate)


def calculate_tax(price, rate):
    tax = price * rate
    print(tax)
    return tax.quantize(D('0.01'))

checkout / session.py

from . import tax

    def build_submission(self, **kwargs):
        """
        Return a dict of data that contains everything required for an order
        submission.  This includes payment details (if any).

        This can be the right place to perform tax lookups and apply them to
        the basket.
        """
        # Pop the basket if there is one, because we pass it as a positional
        # argument to methods below
        basket = kwargs.pop('basket', self.request.basket)
        shipping_address = self.get_shipping_address(basket)
        shipping_method = self.get_shipping_method(
            basket, shipping_address)
        billing_address = self.get_billing_address(shipping_address)
        if not shipping_method:
            total = shipping_charge = None
        else:
            shipping_charge = shipping_method.calculate(basket)
            total = self.get_order_totals(
                basket, shipping_charge=shipping_charge, **kwargs)
        submission = {
            'user': self.request.user,
            'basket': basket,
            'shipping_address': shipping_address,
            'shipping_method': shipping_method,
            'shipping_charge': shipping_charge,
            'billing_address': billing_address,
            'order_total': total,
            'order_kwargs': {},
            'payment_kwargs': {}}

        # If there is a billing address, add it to the payment kwargs as calls
        # to payment gateways generally require the billing address. Note, that
        # it normally makes sense to pass the form instance that captures the
        # billing address information. That way, if payment fails, you can
        # render bound forms in the template to make re-submission easier.
        if billing_address:
            submission['payment_kwargs']['billing_address'] = billing_address

        # Allow overrides to be passed in
        submission.update(kwargs)

        # Set guest email after overrides as we need to update the order_kwargs
        # entry.
        user = submission['user']
        if (not user.is_authenticated
                and 'guest_email' not in submission['order_kwargs']):
            email = self.checkout_session.get_guest_email()
            submission['order_kwargs']['guest_email'] = email

        tax.apply_to(submission)
        return submission

現在,訂單總額應該包含18%的稅,但是我只看到這適用於購物車總額,而不是訂單總額。 即使在產品詳細信息視圖中,稅收也應占18%。

您錯過了文檔中提到的步驟-應用稅后重新計算訂單總額:

tax.apply_to(submission)

# Recalculate order total to ensure we have a tax-inclusive total
submission['order_total'] = self.get_order_totals(
    submission['basket'],
    submission['shipping_charge']
)

return submission

即使在產品詳細信息視圖中,稅收也應占18%。

這不會自動發生-上面的邏輯僅在下訂單時適用。 如果您使用DeferredTax策略,則在將產品添加到購物籃時尚不清楚該稅,因此無法顯示該稅。 您需要在明細視圖中添加一些顯示邏輯,以指示可能的稅金。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM