calculate profit in the database, skip the margin calculation for now

This commit is contained in:
Thomas Steen Rasmussen 2021-07-22 00:07:30 +02:00
parent ec816a5c11
commit 0e0374b577
2 changed files with 19 additions and 23 deletions

View file

@ -14,24 +14,28 @@
<thead>
<tr>
<th>Product</th>
<th class="text-center">Orders</th>
<th class="text-center">Units Sold</th>
<th class="text-right">Revenue</th>
<th class="text-right">Cost</th>
<th class="text-right">Profit</th>
<th class="text-right">Margin</th>
<th class="text-center">Orders</th>
<th class="text-center">Units Sold</th>
<th class="text-right">Total Revenue</th>
<th class="text-right">Total Cost</th>
<th class="text-right">Total Profit</th>
</tr>
</thead>
<tbody>
{% for p in product_list %}
<tr>
<td>{{ p.name }}</td>
<td class="text-right">{{ p.price|floatformat:"2" }}&nbsp;DKK</td>
<td class="text-right">{{ p.cost|floatformat:"2" }}&nbsp;DKK</td>
<td class="text-right">{{ p.profit|floatformat:"2" }}&nbsp;DKK</td>
<td class="text-center">{{ p.order_set.count }}</td>
<td class="text-center">{{ p.total_units_sold }}</td>
<td class="text-right">{{ p.price|floatformat:"2" }} DKK</td>
<td class="text-right">{{ p.cost|floatformat:"2" }} DKK</td>
<td class="text-right">{{ p.profit|floatformat:"2" }} DKK</td>
<td class="text-right">{{ p.margin|floatformat:"2" }}%</td>
<td class="text-right">{{ p.total_revenue|floatformat:"2" }}&nbsp;DKK</td>
<td class="text-right">{{ p.total_cost|floatformat:"2" }}&nbsp;DKK</td>
<td class="text-right">{{ p.total_profit|floatformat:"2" }}&nbsp;DKK</td>
</tr>
{% endfor %}
</tbody>

View file

@ -7,7 +7,7 @@ from django.contrib import messages
from django.contrib.postgres.fields import DateTimeRangeField
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.aggregates import Sum
from django.db.models import F, Sum
from django.urls import reverse_lazy
from django.utils import timezone
from django.utils.dateparse import parse_datetime
@ -396,7 +396,13 @@ class ProductCategory(CreatedUpdatedModel, UUIDModel):
class ProductStatsManager(models.Manager):
def with_ticket_stats(self):
return self.annotate(total_units_sold=Sum("orderproductrelation__quantity"))
return (
self.annotate(total_units_sold=Sum("orderproductrelation__quantity"))
.annotate(profit=F("price") - F("cost"))
.annotate(total_revenue=F("price") * F("total_units_sold"))
.annotate(total_cost=F("cost") * F("total_units_sold"))
.annotate(total_profit=F("profit") * F("total_units_sold"))
)
class Product(CreatedUpdatedModel, UUIDModel):
@ -512,20 +518,6 @@ class Product(CreatedUpdatedModel, UUIDModel):
# If there is no stock defined the product is generally available.
return True
@property
def profit(self):
try:
return self.price - self.cost
except ValueError:
return 0
@property
def margin(self):
try:
return (self.profit / self.price) * 100
except ValueError:
return 0
class OrderProductRelation(CreatedUpdatedModel):
order = models.ForeignKey("shop.Order", on_delete=models.PROTECT)