Coverage for accounting/pagination.py: 57%
32 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-03-29 14:03 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-03-29 14:03 -0600
1from django.db.models import Sum, F
2from rest_framework.response import Response
3from app.pagination import AppPagination
4from accounting.models import Movement, ProductCharge
7class AccountingPagination(AppPagination):
8 def get_paginated_response(self, data):
9 query_params = self.request.query_params
11 membership_pk = query_params.get("membership")
12 first_date = query_params.get("date__gte")
13 last_date = query_params.get("date__lte")
15 if membership_pk: 15 ↛ 16line 15 didn't jump to line 16, because the condition on line 15 was never true
16 movements = Movement.objects.filter(membership=membership_pk)
17 else:
18 movements = Movement.objects.all()
20 if first_date and last_date: 20 ↛ 21line 20 didn't jump to line 21
21 first_date_balance = (
22 movements.filter(date__lt=first_date)
23 .annotate(signed_amount=(F("amount") * F("num_type")))
24 .aggregate(total=Sum("signed_amount"))
25 .get("total")
26 )
27 last_date_balance = (
28 movements.filter(date__lte=last_date)
29 .annotate(signed_amount=(F("amount") * F("num_type")))
30 .aggregate(total=Sum("signed_amount"))
31 .get("total")
32 )
34 balance_first_date = {
35 "date": first_date,
36 "balance": first_date_balance or 0,
37 }
38 balance_last_date = {
39 "date": last_date,
40 "balance": last_date_balance or 0,
41 }
42 else:
43 balance_first_date = None
44 balance_last_date = None
46 return Response(
47 {
48 "count": self.page.paginator.count,
49 "page": self.page.number,
50 "per_page": self.get_page_size(self.request),
51 "balance_first_date": balance_first_date,
52 "balance_last_date": balance_last_date,
53 "results": data,
54 }
55 )
58class ProductChargePagination(AppPagination):
59 def get_paginated_response(self, data):
60 query_params = self.request.query_params
62 membership_pk = query_params.get("membership")
64 product_charges = ProductCharge.objects.all()
66 if membership_pk:
67 product_charges = product_charges.filter(membership=membership_pk)
69 total_amount = product_charges.aggregate(total_amount=Sum("amount")).get("total_amount") or 0
70 total_paid_amount = (
71 product_charges.aggregate(total_paid_amount=Sum("paid_amount")).get("total_paid_amount") or 0
72 )
73 total_invoiced_amount = (
74 product_charges.aggregate(total_invoiced_amount=Sum("invoiced_amount")).get("total_invoiced_amount") or 0
75 )
77 return Response(
78 {
79 "count": self.page.paginator.count,
80 "page": self.page.number,
81 "per_page": self.get_page_size(self.request),
82 "resume": {
83 "amount": total_amount,
84 "paid_amount": total_paid_amount,
85 "invoiced_amount": total_invoiced_amount,
86 },
87 "results": data,
88 }
89 )