Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Recalculate order prices with proper taxes
Anna Siwik867694d1d057
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex f28ff970da..e00a5b7f62 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -384,6 +384,7 @@ def create_payment(order):transaction_id=str(fake.random_int(1, 100000)),currency=settings.DEFAULT_CURRENCY,total=order.total.gross.amount,+ tax=order.total.tax.amount,delivery=order.shipping_price.gross.amount,customer_ip_address=fake.ipv4(),billing_first_name=order.billing_address.first_name,diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex 6d7d2cf643..0440f3437b 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -21,7 +21,8 @@ from ...order.emails import (send_fulfillment_confirmation, send_fulfillment_update,send_order_confirmation)from ...order.models import Fulfillment, FulfillmentLine, Order, OrderNote-from ...order.utils import get_taxes_for_order, update_order_status+from ...order.utils import (+ get_taxes_for_order, update_order_prices, update_order_status)from ...shipping.models import ShippingMethodCountryfrom ..views import staff_member_requiredfrom .filters import OrderFilter@@ -330,11 +331,13 @@ def add_variant_to_order(request, order_pk):@permission_required('order.edit_order')def address_view(request, order_pk, address_type):order = get_object_or_404(Order, pk=order_pk)+ update_prices = Falseif address_type == 'shipping':address = order.shipping_addresssuccess_msg = pgettext_lazy('Dashboard message','Updated shipping address')+ update_prices = Trueelse:address = order.billing_addresssuccess_msg = pgettext_lazy(@@ -345,6 +348,8 @@ def address_view(request, order_pk, address_type):updated_address = form.save()if not address:save_address_in_order(order, updated_address, address_type)+ if update_prices:+ update_order_prices(order, request.discounts)if not order.is_draft():order.history.create(content=success_msg, user=request.user)messages.success(request, success_msg)@@ -361,6 +366,7 @@ def order_customer_edit(request, order_pk):status = 200if form.is_valid():form.save()+ update_order_prices(order, request.discounts)user_email = form.cleaned_data.get('user_email')user = form.cleaned_data.get('user')if user_email:@@ -392,6 +398,7 @@ def order_customer_remove(request, order_pk):form = OrderRemoveCustomerForm(request.POST or None, instance=order)if form.is_valid():form.save()+ update_order_prices(order, request.discounts)msg = pgettext_lazy('Dashboard message','Customer removed from an order')diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex 1d6eacc315..6ad5e3d997 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -626,11 +626,6 @@ def ajax_available_variants_list(request):Response format is that of a Select2 JS widget."""- def get_variant_label(variant, discounts):- return '%s, %s, %s' % (- variant.sku, variant.display_product(),- prices_i18n.amount(variant.get_price(discounts).gross))-available_products = Product.objects.available_products().prefetch_related('category','product_type__product_attributes')@@ -638,17 +633,17 @@ def ajax_available_variants_list(request):product__in=available_products).prefetch_related('product__category','product__product_type__product_attributes')+search_query = request.GET.get('q', '')if search_query:queryset = queryset.filter(Q(sku__icontains=search_query) |Q(name__icontains=search_query) |Q(product__name__icontains=search_query))- discounts = request.discounts+variants = [- {'id': variant.id, 'text': get_variant_label(variant, discounts)}- for variant in queryset- ]+ {'id': variant.id, 'text': variant.get_ajax_label(request.discounts)}+ for variant in queryset]return JsonResponse({'results': variants})diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 6cc8c00dbd..65f155ee25 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -74,6 +74,24 @@ def recalculate_order(order, **kwargs):order.save()+def update_order_prices(order, discounts):+ """Update prices in order with given discounts and proper taxes."""+ taxes = get_taxes_for_order(order)++ for line in order:+ if line.variant:+ line.unit_price = line.variant.get_price(discounts, taxes)+ line.tax_rate = get_tax_rate_by_name(+ line.variant.product.tax_rate, taxes)+ line.save()++ if order.shipping_method:+ order.shipping_price = order.shipping_method.get_total_price(taxes)+ order.save()++ recalculate_order(order)++def cancel_order(order, restock):"""Cancel order and associated fulfillments.diff --git a/saleor/order/views.py b/saleor/order/views.pyindex 84046860e6..a63c203d66 100644--- a/saleor/order/views.py+++ b/saleor/order/views.py@@ -50,7 +50,7 @@ def details(request, token):def payment(request, token):orders = Order.objects.confirmed().filter(billing_address__isnull=False)- orders = Order.objects.prefetch_related('lines__variant')+ orders = orders.prefetch_related('lines__variant')orders = orders.select_related('billing_address', 'shipping_address', 'user')order = get_object_or_404(orders, token=token)diff --git a/saleor/product/models.py b/saleor/product/models.pyindex 7160bd40f9..9657a767c8 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -11,9 +11,10 @@ from django.utils.encoding import smart_textfrom django.utils.text import slugifyfrom django.utils.translation import pgettext_lazyfrom django_prices.models import MoneyField+from django_prices.templatetags import prices_i18nfrom mptt.managers import TreeManagerfrom mptt.models import MPTTModel-from prices import TaxedMoney, TaxedMoneyRange+from prices import TaxedMoneyRangefrom text_unidecode import unidecodefrom versatileimagefield.fields import PPOIField, VersatileImageField@@ -240,6 +241,11 @@ class ProductVariant(models.Model):def get_first_image(self):return self.product.get_first_image()+ def get_ajax_label(self, discounts=None):+ price = self.get_price(discounts).gross+ return '%s, %s, %s' % (+ self.sku, self.display_product(), prices_i18n.amount(price))+class ProductAttribute(models.Model):slug = models.SlugField(max_length=50, unique=True)diff --git a/saleor/product/views.py b/saleor/product/views.pyindex 0cbe285c4d..f0abe158a7 100644--- a/saleor/product/views.py+++ b/saleor/product/views.py@@ -68,6 +68,7 @@ def product_details(request, slug, product_id, form=None):# show_variant_picker determines if variant picker is used or select inputshow_variant_picker = all([v.attributes for v in product.variants.all()])json_ld_data = product_json_ld(product, product_attributes)+ display_gross_prices = request.site.settings.display_gross_pricesreturn TemplateResponse(request, 'product/details.html', {'is_visible': is_visible,@@ -80,7 +81,8 @@ def product_details(request, slug, product_id, form=None):'variant_picker_data': json.dumps(variant_picker_data, default=serialize_decimal),'json_ld_product_data': json.dumps(- json_ld_data, default=serialize_decimal)})+ json_ld_data, default=serialize_decimal),+ 'display_gross_prices': display_gross_prices})def product_add_to_cart(request, slug, product_id):diff --git a/templates/dashboard/order/pdf/invoice.html b/templates/dashboard/order/pdf/invoice.htmlindex 834fbd3cb8..df12d07214 100644--- a/templates/dashboard/order/pdf/invoice.html+++ b/templates/dashboard/order/pdf/invoice.html@@ -67,7 +67,7 @@{% trans "Tax rate" context "Shipment order table header" %}</th><th class="right-align">- {% trans "Price gross" context "Shipment order table header" %}+ {% trans "Gross price" context "Shipment order table header" %}</th><th class="right-align">{% trans "Quantity" context "Shipment order table header" %}diff --git a/templates/product/details.html b/templates/product/details.htmlindex 9718ae13a0..16d3471933 100644--- a/templates/product/details.html+++ b/templates/product/details.html@@ -142,7 +142,7 @@{% endif %}<small>{% tax_rate request.taxes product.tax_rate as tax_rate %}- {% if request.site.settings.display_gross_prices %}+ {% if display_gross_prices %}{% blocktrans trimmed with tax_rate=tax_rate context "Product details page vat rate value" %}including {{ tax_rate }}% VAT{% endblocktrans %}@@ -160,7 +160,7 @@{% if show_variant_picker %}{% csrf_token %}<div id="variant-picker" data-variant-picker-data="{{ variant_picker_data }}"- data-action="{% url 'product:add-to-cart' product_id=product.pk slug=product.get_slug %}" data-display-gross-price="{{ request.site.settings.display_gross_prices }}"></div>+ data-action="{% url 'product:add-to-cart' product_id=product.pk slug=product.get_slug %}" data-display-gross-price="{{ display_gross_prices }}"></div>{% else %}<form id="product-form" role="form" class="product-form clearfix" method="post" action="{% url 'product:add-to-cart' product_id=product.pk slug=product.get_slug %}" novalidate>{% csrf_token %}