Merge pull request #878 from ae-utbm/fix-invoices

fix: InvoiceQuerySet.annotate_total() (but this time good)
This commit is contained in:
thomas girod 2024-10-13 11:57:59 +02:00 committed by GitHub
commit 143713fac1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 7 deletions

View File

@ -158,10 +158,11 @@ def test_user_invoice_with_multiple_items():
item_recipe = Recipe(InvoiceItem, invoice=foreign_key(Recipe(Invoice, user=user))) item_recipe = Recipe(InvoiceItem, invoice=foreign_key(Recipe(Invoice, user=user)))
item_recipe.make(_quantity=3, quantity=1, product_unit_price=5) item_recipe.make(_quantity=3, quantity=1, product_unit_price=5)
item_recipe.make(_quantity=1, quantity=1, product_unit_price=5) item_recipe.make(_quantity=1, quantity=1, product_unit_price=5)
item_recipe.make(_quantity=2, quantity=1, product_unit_price=iter([5, 8]))
res = list( res = list(
Invoice.objects.filter(user=user) Invoice.objects.filter(user=user)
.annotate_total() .annotate_total()
.order_by("-total") .order_by("-total")
.values_list("total", flat=True) .values_list("total", flat=True)
) )
assert res == [15, 5] assert res == [15, 13, 5]

View File

@ -21,9 +21,11 @@
# Place - Suite 330, Boston, MA 02111-1307, USA. # Place - Suite 330, Boston, MA 02111-1307, USA.
# #
# #
import itertools
# This file contains all the views that concern the user model # This file contains all the views that concern the user model
from datetime import date, timedelta from datetime import date, timedelta
from operator import itemgetter
from smtplib import SMTPException from smtplib import SMTPException
from django.conf import settings from django.conf import settings
@ -665,9 +667,15 @@ class UserAccountView(UserAccountBase):
kwargs["refilling_month"] = self.expense_by_month( kwargs["refilling_month"] = self.expense_by_month(
Refilling.objects.filter(customer=self.object.customer) Refilling.objects.filter(customer=self.object.customer)
) )
kwargs["invoices_month"] = self.expense_by_month( kwargs["invoices_month"] = [
Invoice.objects.filter(user=self.object) # the django ORM removes the `group by` clause in this query,
) # so a little of post-processing is needed
{"grouped_date": key, "total": sum(i["total"] for i in group)}
for key, group in itertools.groupby(
self.expense_by_month(Invoice.objects.filter(user=self.object)),
key=itemgetter("grouped_date"),
)
]
kwargs["etickets"] = self.object.customer.buyings.exclude(product__eticket=None) kwargs["etickets"] = self.object.customer.buyings.exclude(product__eticket=None)
return kwargs return kwargs

View File

@ -173,9 +173,8 @@ class InvoiceQueryset(models.QuerySet):
return self.annotate( return self.annotate(
total=Subquery( total=Subquery(
InvoiceItem.objects.filter(invoice_id=OuterRef("pk")) InvoiceItem.objects.filter(invoice_id=OuterRef("pk"))
.annotate(item_amount=F("product_unit_price") * F("quantity")) .values("invoice_id")
.values("item_amount") .annotate(total=Sum(F("product_unit_price") * F("quantity")))
.annotate(total=Sum("item_amount"))
.values("total") .values("total")
) )
) )