unbreak refund order function, while here show on order list and detail pages if an order is refunded, also fix tickettype detail stats so they dont include refunded orders

This commit is contained in:
Thomas Steen Rasmussen 2021-08-01 17:45:37 +02:00
parent 929cde010e
commit 792fc2bfc0
3 changed files with 18 additions and 7 deletions

View file

@ -14,6 +14,7 @@ from django.utils.dateparse import parse_datetime
from django.utils.translation import ugettext_lazy as _
from unidecode import unidecode
from tickets.models import ShopTicket
from utils.models import CreatedUpdatedModel, UUIDModel
from utils.slugs import unique_slugify
@ -290,16 +291,17 @@ class Order(CreatedUpdatedModel):
else:
self.refunded = True
# delete any tickets related to this order
if self.shoptickets.all():
tickets = ShopTicket.objects.filter(opr__order=self)
if tickets.exists():
msg = "Order %s marked as refunded, deleting %s tickets..." % (
self.pk,
self.shoptickets.count(),
tickets.count(),
)
if request:
messages.success(request, msg)
else:
print(msg)
self.shoptickets.all().delete()
tickets.delete()
else:
msg = "Order %s marked as refunded, no tickets to delete" % self.pk
if request:
@ -399,7 +401,10 @@ class ProductCategory(CreatedUpdatedModel, UUIDModel):
class ProductStatsManager(models.Manager):
def with_ticket_stats(self):
return (
self.filter(orderproductrelation__order__paid=True)
self.filter(
orderproductrelation__order__paid=True,
orderproductrelation__order__refunded=False,
)
.annotate(total_units_sold=Sum("orderproductrelation__quantity"))
.annotate(profit=F("price") - F("cost"))
.annotate(total_income=F("price") * F("total_units_sold"))

View file

@ -117,7 +117,11 @@ Details for Order #{{ order.id }} | {{ block.super }}
</div>
{% if order.paid %}
<div class="panel-footer">
<h4>Your order is paid in full.</h4>
{% if order.refunded %}
<h4>This order has been refunded and tickets related to it are deleted and no longer valid.</h4>
{% else %}
<h4>This order is paid in full.</h4>
{% endif %}
</div>
{% endif %}
</div>

View file

@ -21,6 +21,7 @@ Orders | {{ block.super }}
<th>Total amount</th>
<th>Open?</th>
<th>Paid?</th>
<th>Refunded?</th>
<th>Delivered?</th>
<th>Invoice</th>
<th>Actions</th>
@ -35,6 +36,7 @@ Orders | {{ block.super }}
<td>{{ order.total|currency }}</td>
<td class="text-center">{{ order.open|truefalseicon }}</td>
<td class="text-center">{{ order.paid|truefalseicon }}</td>
<td class="text-center">{{ order.refunded|truefalseicon }}</td>
<td class="text-center">{{ order.ticket_generated_status }}</td>
<td>
{% if order.paid %}