[英]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.