Replaced the SQL statement in the _find_imbalanced() utility function in the accounting application.

This commit is contained in:
依瑪貓 2020-07-19 19:07:51 +08:00
parent 1e742dc375
commit dc17058d31

View File

@ -110,12 +110,12 @@ def _find_imbalanced(records):
Args: Args:
records (list[Record]): The accounting records. records (list[Record]): The accounting records.
""" """
with connection.cursor() as cursor: imbalanced = [x.sn for x in Transaction.objects
cursor.execute("""SELECT transaction_sn .annotate(
FROM accounting_records balance=Sum(Case(
GROUP BY transaction_sn When(record__is_credit=True, then=-1),
HAVING SUM(CASE WHEN is_credit THEN -1 ELSE 1 END * amount) != 0""") default=1) * F("record__amount")))
imbalanced = [x[0] for x in cursor.fetchall()] .filter(~Q(balance=0))]
for record in records: for record in records:
record.is_balanced = record.transaction.sn not in imbalanced record.is_balanced = record.transaction.sn not in imbalanced