Replaced the SQL in the _find_order_holes() utility function in the accounting application.
This commit is contained in:
		| @@ -20,8 +20,7 @@ | |||||||
| """ | """ | ||||||
| import re | import re | ||||||
|  |  | ||||||
| from django.db import connection | from django.db.models import Sum, Case, When, F, Q, Count, Max | ||||||
| from django.db.models import Sum, Case, When, F, Q |  | ||||||
| from django.db.models.functions import TruncMonth, Coalesce | from django.db.models.functions import TruncMonth, Coalesce | ||||||
| from django.http import HttpResponseRedirect, Http404 | from django.http import HttpResponseRedirect, Http404 | ||||||
| from django.shortcuts import render | from django.shortcuts import render | ||||||
| @@ -128,14 +127,14 @@ def _find_order_holes(records): | |||||||
|     Args: |     Args: | ||||||
|         records (list[Record]): The accounting records. |         records (list[Record]): The accounting records. | ||||||
|     """ |     """ | ||||||
|     with connection.cursor() as cursor: |     holes = [x["date"] for x in Transaction.objects | ||||||
|         cursor.execute(""" |         .values("date") | ||||||
|   SELECT date FROM accounting_transactions |         .annotate(count=Count("ord"), max=Max("ord")) | ||||||
|   GROUP BY date HAVING COUNT(*)!=MAX(ord) |         .filter(Q(count=F("max")))]\ | ||||||
|   UNION |              + [x["date"] for x in Transaction.objects | ||||||
|   SELECT date FROM accounting_transactions |         .values("date", "ord") | ||||||
|   GROUP BY date, ord HAVING COUNT(*) > 1""") |         .annotate(count=Count("sn")) | ||||||
|         holes = [x[0] for x in cursor.fetchall()] |         .filter(~Q(count=1))] | ||||||
|     for record in records: |     for record in records: | ||||||
|         record.has_order_hole = record.transaction.date in holes |         record.has_order_hole = record.transaction.date in holes | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user