Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Remove unused gross_price_range, more tests for calculating price

Anna Siwik87a136d3c8ea
diff --git a/docs/architecture/money.rst b/docs/architecture/money.rst
index 9dfcbe54ad..86415f80f0 100644
--- a/docs/architecture/money.rst
+++ b/docs/architecture/money.rst
@@ -22,12 +22,12 @@ In Saleor's codebase, money amounts exist either as `Money` or `TaxedMoney` inst
Products and shipping methods prices are stored using `MoneyField` and are assumed to be gross amounts. Those amounts are then naively converted to `TaxedMoney` objects using the `TaxedAmount(net=item.price, gross=item.price)` approach with conversion happening before resolving item's final price (before taxes and discounts).
.. note::
-
+
In future Saleor will support setting for store administrators to change this approach to use net values instead if this fits their business more.
TaxedMoneyRange
---------------
-Sometimes a product may be available under more than single price due to it's variants defining custom prices different from base brice. For such situations `Product` defines additional `get_price_range` and `get_gross_price_range` methods that return `TaxedMoneyRange` object defining minim and maximum prices on its `start` and `stop` attributes. This object is then used by the UI to differentatie between displaying price as "10 USD" or "from 10 USD" in case of products where prices differ between variants.
+Sometimes a product may be available under more than single price due to it's variants defining custom prices different from base brice. For such situations `Product` defines additional `get_price_range` method that return `TaxedMoneyRange` object defining minimum and maximum prices on its `start` and `stop` attributes. This object is then used by the UI to differentiate between displaying price as "10 USD" or "from 10 USD" in case of products where prices differ between variants.
-TaxedMoneyRange is also used in for displaying other money ranges, like min and maximum cost and margin on product's variant in dashboard.
\ No newline at end of file
+TaxedMoneyRange is also used in for displaying other money ranges, like min and maximum cost and margin on product's variant in dashboard.
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index bacb2579d5..8a9cb33342 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -183,7 +183,6 @@ def product_details(request, pk):
availability = get_availability(product)
sale_price = availability.price_range
purchase_cost, gross_margin = get_product_costs_data(product)
- gross_price_range = product.get_gross_price_range()
# no_variants is True for product types that doesn't require variant.
# In this case we're using the first variant under the hood to allow stock
@@ -192,10 +191,9 @@ def product_details(request, pk):
only_variant = variants.first() if no_variants else None
ctx = {
'product': product, 'sale_price': sale_price, 'variants': variants,
- 'gross_price_range': gross_price_range, 'images': images,
- 'no_variants': no_variants, 'only_variant': only_variant,
- 'purchase_cost': purchase_cost, 'gross_margin': gross_margin,
- 'is_empty': not variants.exists()}
+ 'images': images, 'no_variants': no_variants,
+ 'only_variant': only_variant, 'purchase_cost': purchase_cost,
+ 'gross_margin': gross_margin, 'is_empty': not variants.exists()}
return TemplateResponse(request, 'dashboard/product/detail.html', ctx)
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 1271cc1e45..006f52d4de 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -177,19 +177,10 @@ class Product(SeoModel):
for variant in self]
return TaxedMoneyRange(min(prices), max(prices))
tax_rate = self.tax_rate or self.product_type.tax_rate
- price = apply_tax_to_price(taxes, tax_rate, self.price)
discounted_price = calculate_discounted_price(
- self, price, discounts)
- return TaxedMoneyRange(start=discounted_price, stop=discounted_price)
-
- def get_gross_price_range(self, discounts=None):
- grosses = [
- self.get_price_per_item(variant, discounts=discounts)
- for variant in self]
- if not grosses:
- return None
- grosses = sorted(grosses, key=lambda x: x.tax)
- return TaxedMoneyRange(min(grosses), max(grosses))
+ self, self.price, discounts)
+ taxed_price = apply_tax_to_price(taxes, tax_rate, discounted_price)
+ return TaxedMoneyRange(start=taxed_price, stop=taxed_price)
class ProductVariant(models.Model):
diff --git a/tests/test_product.py b/tests/test_product.py
index 64152830ce..d28096993a 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -4,7 +4,7 @@ from unittest.mock import patch
import pytest
from django.urls import reverse
-from prices import Money, TaxedMoney
+from prices import Money, TaxedMoney, TaxedMoneyRange
from saleor.cart import CartStatus, utils
from saleor.cart.models import Cart
@@ -455,3 +455,64 @@ def test_get_price_per_item(
assert price == TaxedMoney(
net=Money(product_net, 'USD'), gross=Money(product_gross, 'USD'))
+
+
+def test_product_get_price_per_item_variant_has_no_price(
+ product_type, default_category, taxes):
+ product = Product.objects.create(
+ product_type=product_type,
+ category=default_category,
+ price=Money('10.00', 'USD'))
+ variant = product.variants.create()
+
+ price = variant.get_price_per_item(taxes=taxes)
+
+ assert price == TaxedMoney(
+ net=Money('10.00', 'USD'), gross=Money('12.30', 'USD'))
+
+
+def test_product_get_price_per_item_variant_with_price(
+ product_type, default_category, taxes):
+ product = Product.objects.create(
+ product_type=product_type,
+ category=default_category,
+ price=Money('10.00', 'USD'))
+ variant = product.variants.create(price_override=Money('20.00', 'USD'))
+
+ price = variant.get_price_per_item(taxes=taxes)
+
+ assert price == TaxedMoney(
+ net=Money('20.00', 'USD'), gross=Money('24.60', 'USD'))
+
+
+def test_product_get_price_range_with_variants(
+ product_type, default_category, taxes):
+ product = Product.objects.create(
+ product_type=product_type,
+ category=default_category,
+ price=Money('15.00', 'USD'))
+ product.variants.create(sku='1')
+ product.variants.create(sku='2', price_override=Money('20.00', 'USD'))
+ product.variants.create(sku='3', price_override=Money('11.00', 'USD'))
+
+ price = product.get_price_range(taxes=taxes)
+
+ start = TaxedMoney(
+ net=Money('11.00', 'USD'), gross=Money('13.53', 'USD'))
+ stop = TaxedMoney(
+ net=Money('20.00', 'USD'), gross=Money('24.60', 'USD'))
+ assert price == TaxedMoneyRange(start=start, stop=stop)
+
+
+def test_product_get_price_range_no_variants(
+ product_type, default_category, taxes):
+ product = Product.objects.create(
+ product_type=product_type,
+ category=default_category,
+ price=Money('10.00', 'USD'))
+
+ price = product.get_price_range(taxes=taxes)
+
+ expected_price = TaxedMoney(
+ net=Money('10.00', 'USD'), gross=Money('12.30', 'USD'))
+ assert price == TaxedMoneyRange(start=expected_price, stop=expected_price)