简体   繁体   English

对Django Oscar中的所有产品征收18%税的最佳方法是什么?

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

I want to apply 18% tax to all products on Django Oscar. 我想对Django Oscar上的所有产品征收18%的税。 What is the best and simple way to achieve this? 实现此目的的最佳和简单方法是什么?

I have followed this documentation. 我已遵循文档。

checkout/tax.py 结帐/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 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

Now, the order total is supposed to have included 18% tax but I only see this is applied to cart total and not order total. 现在,订单总额应该包含18%的税,但是我只看到这适用于购物车总额,而不是订单总额。 And even in product detail view tax should show up as 18%. 即使在产品详细信息视图中,税收也应占18%。

You have missed a step that is mentioned in the documentation - where the order total is recalculated after applying the tax: 您错过了文档中提到的步骤-应用税后重新计算订单总额:

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

And even in product detail view tax should show up as 18%. 即使在产品详细信息视图中,税收也应占18%。

That is not going to happen automatically - the logic above is only applied when placing an order. 这不会自动发生-上面的逻辑仅在下订单时适用。 If you're using the DeferredTax strategy then the tax is not known at the point of adding a product to the basket, so it cannot be displayed. 如果您使用DeferredTax策略,则在将产品添加到购物篮时尚不清楚该税,因此无法显示该税。 You would need to add some display logic to the detail view to indicate what the likely taxes are. 您需要在明细视图中添加一些显示逻辑,以指示可能的税金。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM