Skip to content

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

Add gross margin per variant in product details

michal60875c16e26f
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 15c13704f4..4fb7623373 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -157,8 +157,7 @@ def product_detail(request, pk):
images = product.images.all()
availability = get_availability(product)
sale_price = availability.price_range
- purchase_cost, gross_margin, gross_margin_percent = (
- get_purchase_cost_and_gross_margins(product))
+ purchase_cost, gross_margin = get_purchase_cost_and_gross_margins(product)
gross_price_range = product.get_gross_price_range()
# no_variants is True for product classes that doesn't require variant.
@@ -175,8 +174,7 @@ def product_detail(request, pk):
'gross_price_range': gross_price_range, 'images': images,
'no_variants': no_variants, 'only_variant': only_variant,
'stock': stock, 'purchase_cost': purchase_cost,
- 'gross_margin': gross_margin,
- 'gross_margin_percent': gross_margin_percent}
+ 'gross_margin': gross_margin}
return TemplateResponse(request, 'dashboard/product/detail.html', ctx)
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 133220f51f..a4c3ae1ca1 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -327,6 +327,21 @@ class ProductVariant(models.Model, Item):
if stock:
return stock.cost_price
+ def get_costs_data(self):
+ costs = []
+ margins = []
+ for stock in self.stock.all():
+ if stock.cost_price:
+ cost = stock.cost_price
+ costs.append(cost)
+ price = self.get_price_per_item()
+ margin = price - cost
+ percent = round((margin.gross / price.gross) * 100, 0)
+ margins.append(percent)
+ costs = sorted(costs, key=lambda x: x.gross)
+ margins = sorted(margins)
+ return {'costs': costs, 'margins': margins}
+
@python_2_unicode_compatible
class StockLocation(models.Model):
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index f50a5b9c8a..92ce2c383b 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -319,41 +319,32 @@ def get_variant_availability_status(variant):
def get_purchase_cost_and_gross_margins(product):
zero_price = Price(0, 0, currency=DEFAULT_CURRENCY)
zero_price_range = PriceRange(zero_price, zero_price)
- purchase_costs_range = gross_margin = zero_price_range
- gross_margin_percent = (0, 0)
+ purchase_costs_range = zero_price_range
+ gross_margin = (0, 0)
if not product.variants.exists():
- return purchase_costs_range, gross_margin, gross_margin_percent
+ return purchase_costs_range, gross_margin
variants = product.variants.all()
- costs, margins, percents = get_cost_data_from_variants(variants)
+ costs, margins = get_cost_data_from_variants(variants)
if costs:
purchase_costs_range = PriceRange(min(costs), max(costs))
- gross_margin = PriceRange(min(margins), max(margins))
- gross_margin_percent = (percents[0], percents[-1])
- return purchase_costs_range, gross_margin, gross_margin_percent
+ gross_margin = (margins[0], margins[-1])
+ return purchase_costs_range, gross_margin
-def sort_cost_data(costs, margins, percents):
+def sort_cost_data(costs, margins):
costs = sorted(costs, key=lambda x: x.gross)
- margins = sorted(margins, key=lambda x: x.gross)
- percents = sorted(percents)
- return costs, margins, percents
+ margins = sorted(margins)
+ return costs, margins
def get_cost_data_from_variants(variants):
costs = []
margins = []
- percents = []
for variant in variants:
- for stock in variant.stock.all():
- if stock.cost_price:
- cost = stock.cost_price
- costs.append(cost)
- price = variant.get_price_per_item()
- margin = price - cost
- margins.append(margin)
- percent = round((margin.gross / cost.gross) * 100, 0)
- percents.append(percent)
- return sort_cost_data(costs, margins, percents)
+ costs_data = variant.get_costs_data()
+ costs += costs_data['costs']
+ margins += costs_data['margins']
+ return sort_cost_data(costs, margins)
diff --git a/templates/dashboard/product/detail.html b/templates/dashboard/product/detail.html
index 54cd4d5b86..da58dc20bd 100644
--- a/templates/dashboard/product/detail.html
+++ b/templates/dashboard/product/detail.html
@@ -62,21 +62,7 @@
</div>
<div class="col s7 m6">
{% if gross_margin %}
- {% with gross_margin.min_price as min_price %}
- {% with gross_margin.max_price as max_price %}
- <p class="text-padding right-align">{% gross min_price %} - {% gross max_price %}</p>
- {% endwith %}
- {% endwith %}
- {% else %}
- <p class="text-padding right-align">-</p>
- {% endif %}
- </div>
- <div class="col s5 m6">
- <p class="text-padding">{% trans "Gross margin percent" context "Product field" %}</p>
- </div>
- <div class="col s7 m6">
- {% if gross_margin_percent %}
- <p class="text-padding right-align">{{ gross_margin_percent.0 }}% - {{ gross_margin_percent.1 }}%</p>
+ <p class="text-padding right-align">{{ gross_margin.0 }}% - {{ gross_margin.1 }}%</p>
{% else %}
<p class="text-padding right-align">-</p>
{% endif %}
@@ -115,6 +101,7 @@
<th>{% trans "Name" %}</th>
<th>{% trans "Status" %}</th>
<th class="right-align">{% trans "Price" %}</th>
+ <th class="right-align">{% trans "Gross margin" %}</th>
{% if perms.product.edit_product %}
<th></th>
{% endif %}
@@ -127,6 +114,16 @@
<td>{{ variant }}</td>
<td class="wide">{% render_variant_availability_status variant %}</td>
<td class="right-align">{% gross variant.get_price_per_item %}</td>
+ <td class="right-align">
+ {% if variant.get_costs_data.margins|length > 1 %}
+ {{ variant.get_costs_data.margins|first }}% - {{ variant.get_costs_data.margins|last }}%</td>
+ {% else %}
+ {% if variant.get_costs_data.margins %}
+ {{ variant.get_costs_data.margins|first }}%
+ {% else %}
+ 0%
+ {% endif %}
+ {% endif %}
{% if perms.product.edit_product %}
<td><a href="{% url 'dashboard:variant-update' product_pk=product.pk variant_pk=variant.pk %}" class="btn btn-flat">{% trans "Edit" %}</a></td>
{% endif %}