Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add counting get_costs_data to Stock
michald6af4e821669
diff --git a/saleor/product/models.py b/saleor/product/models.pyindex a4c3ae1ca1..93257ab29d 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -331,13 +331,9 @@ class ProductVariant(models.Model, Item):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_data = stock.get_costs_data()+ costs += costs_data['costs']+ margins += costs_data['margins']costs = sorted(costs, key=lambda x: x.gross)margins = sorted(margins)return {'costs': costs, 'margins': margins}@@ -407,6 +403,21 @@ class Stock(models.Model):def quantity_available(self):return max(self.quantity - self.quantity_allocated, 0)+ def get_costs_data(self):+ costs = []+ margins = []+ if self.cost_price:+ cost = self.cost_price+ costs.append(cost)+ price = self.variant.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)+ print margins+ return {'costs': costs, 'margins': margins}+@python_2_unicode_compatibleclass ProductAttribute(models.Model):diff --git a/templates/dashboard/product/_stock_table.html b/templates/dashboard/product/_stock_table.htmlindex e247640a21..3a9b868f4e 100644--- a/templates/dashboard/product/_stock_table.html+++ b/templates/dashboard/product/_stock_table.html@@ -8,6 +8,7 @@<th>{% trans "Quantity" %}</th><th>{% trans "Allocated" %}</th><th class="right-align">{% trans "Cost" %}</th>+ <th class="right-align">{% trans "Gross margin" %}</th></tr></thead><tbody>@@ -17,6 +18,9 @@<td>{{ stock_item.quantity }}</td><td>{{ stock_item.quantity_allocated }}</td><td class="right-align">{% if stock_item.cost_price %}{% gross stock_item.cost_price %}{% else %}-{% endif %}</td>+ <td class="right-align">+ {{ stock_item.get_costs_data.margins|first }}%+ </td></tr>{% empty %}<tr>diff --git a/templates/dashboard/product/detail.html b/templates/dashboard/product/detail.htmlindex da58dc20bd..0b565ea538 100644--- a/templates/dashboard/product/detail.html+++ b/templates/dashboard/product/detail.html@@ -115,15 +115,18 @@<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 }}%+ {% with variant.get_costs_data.margins as margins %}+ {% if margins|length > 1 %}+ {{ margins|first }}% - {{ margins|last }}%{% else %}- 0%+ {% if variant.get_costs_data.margins %}+ {{ margins|first }}%+ {% else %}+ 0%+ {% endif %}{% endif %}- {% endif %}+ {% endwith %}+ </td>{% 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 %}diff --git a/templates/dashboard/product/product_variant/detail.html b/templates/dashboard/product/product_variant/detail.htmlindex cc858aaf45..e518f46eb9 100644--- a/templates/dashboard/product/product_variant/detail.html+++ b/templates/dashboard/product/product_variant/detail.html@@ -56,10 +56,38 @@<p class="text-padding right-align">{% gross variant.get_price %}</p></div><div class="col s5 m6">- <p class="text-padding">{% trans "Cost price" context "Dashboard variant details view" %}</p>+ <p class="text-padding">{% trans "Cost price range" context "Dashboard variant details view" %}</p></div><div class="col s7 m6">- <p class="text-padding right-align">{% if variant.get_cost_price %}{% gross variant.get_cost_price %}{% else %}-{% endif %}</p>+ <p class="text-padding right-align">+ {% with variant.get_costs_data.costs as costs %}+ {% if costs|length > 1 %}+ {% gross costs|first %} - {% gross costs|last %}+ {% elif costs|length == 1 %}+ {% gross costs|first %}+ {% else %}+ -+ {% endif %}+ {% endwith %}+ </p>+ </div>+ <div class="col s5 m6">+ <p class="text-padding">{% trans "Gross margin" context "Dashboard variant details view" %}</p>+ </div>+ <div class="col s7 m6">+ <p class="text-padding right-align">+ {% with variant.get_costs_data.margins as margins %}+ {% if margins|length > 1 %}+ {{ margins|first }}% - {{ margins|last }}%+ {% else %}+ {% if variant.get_costs_data.margins %}+ {{ margins|first }}%+ {% else %}+ 0%+ {% endif %}+ {% endif %}+ {% endwith %}+ </p></div></div></div>