calculate profit in the database, skip the margin calculation for now
This commit is contained in:
parent
ec816a5c11
commit
0e0374b577
|
@ -14,24 +14,28 @@
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Product</th>
|
<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">Revenue</th>
|
||||||
<th class="text-right">Cost</th>
|
<th class="text-right">Cost</th>
|
||||||
<th class="text-right">Profit</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>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for p in product_list %}
|
{% for p in product_list %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ p.name }}</td>
|
<td>{{ p.name }}</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-center">{{ p.order_set.count }}</td>
|
<td class="text-center">{{ p.order_set.count }}</td>
|
||||||
<td class="text-center">{{ p.total_units_sold }}</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.total_revenue|floatformat:"2" }} DKK</td>
|
||||||
<td class="text-right">{{ p.cost|floatformat:"2" }} DKK</td>
|
<td class="text-right">{{ p.total_cost|floatformat:"2" }} DKK</td>
|
||||||
<td class="text-right">{{ p.profit|floatformat:"2" }} DKK</td>
|
<td class="text-right">{{ p.total_profit|floatformat:"2" }} DKK</td>
|
||||||
<td class="text-right">{{ p.margin|floatformat:"2" }}%</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
|
@ -7,7 +7,7 @@ from django.contrib import messages
|
||||||
from django.contrib.postgres.fields import DateTimeRangeField
|
from django.contrib.postgres.fields import DateTimeRangeField
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import models
|
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.urls import reverse_lazy
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.dateparse import parse_datetime
|
from django.utils.dateparse import parse_datetime
|
||||||
|
@ -396,7 +396,13 @@ class ProductCategory(CreatedUpdatedModel, UUIDModel):
|
||||||
|
|
||||||
class ProductStatsManager(models.Manager):
|
class ProductStatsManager(models.Manager):
|
||||||
def with_ticket_stats(self):
|
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):
|
class Product(CreatedUpdatedModel, UUIDModel):
|
||||||
|
@ -512,20 +518,6 @@ class Product(CreatedUpdatedModel, UUIDModel):
|
||||||
# If there is no stock defined the product is generally available.
|
# If there is no stock defined the product is generally available.
|
||||||
return True
|
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):
|
class OrderProductRelation(CreatedUpdatedModel):
|
||||||
order = models.ForeignKey("shop.Order", on_delete=models.PROTECT)
|
order = models.ForeignKey("shop.Order", on_delete=models.PROTECT)
|
||||||
|
|
Loading…
Reference in a new issue