Revised the view of the ledger and replaced the raw SQL query with the Django model query in the accounting transaction.
This commit is contained in:
parent
39428d1d35
commit
ff9146c8b1
@ -22,6 +22,7 @@ import re
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
|
from django.db.models import Sum
|
||||||
from django.http import HttpResponseRedirect, Http404
|
from django.http import HttpResponseRedirect, Http404
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
@ -406,28 +407,21 @@ def ledger(request, subject_code, period_spec):
|
|||||||
if current_subject is None:
|
if current_subject is None:
|
||||||
raise Http404()
|
raise Http404()
|
||||||
# The accounting records
|
# The accounting records
|
||||||
select_records = """SELECT r.*
|
records = list(Record.objects.filter(
|
||||||
FROM accounting_records AS r
|
transaction__date__gte=period.start,
|
||||||
INNER JOIN accounting_transactions AS t ON r.transaction_sn = t.sn
|
transaction__date__lte=period.end,
|
||||||
INNER JOIN accounting_subjects AS s ON r.subject_sn = s.sn
|
subject__code__startswith=current_subject.code))
|
||||||
WHERE t.date >= %s AND t.date <= %s AND s.code LIKE %s
|
|
||||||
ORDER BY t.date, t.ord,
|
|
||||||
CASE WHEN r.is_credit THEN 1 ELSE 2 END, r.ord"""
|
|
||||||
records = list(Record.objects.raw(
|
|
||||||
select_records,
|
|
||||||
[period.start, period.end, current_subject.code + "%"]))
|
|
||||||
if re.match("^[1-3]", current_subject.code) is not None:
|
if re.match("^[1-3]", current_subject.code) is not None:
|
||||||
select_balance_before = f"""SELECT
|
debit = Record.objects.filter(
|
||||||
SUM(CASE WHEN is_credit THEN -1 ELSE 1 END * amount)
|
transaction__date__lt=period.start,
|
||||||
FROM ({select_records})"""
|
subject__code__startswith=current_subject.code,
|
||||||
with connection.cursor() as cursor:
|
is_credit=False).aggregate(sum=Sum("amount"))
|
||||||
cursor.execute(
|
credit = Record.objects.filter(
|
||||||
select_balance_before,
|
transaction__date__lt=period.start,
|
||||||
[data_start,
|
subject__code__startswith=current_subject.code,
|
||||||
period.start - timedelta(days=1),
|
is_credit=True).aggregate(sum=Sum("amount"))
|
||||||
current_subject.code + "%"])
|
balance = (0 if debit["sum"] is None else debit["sum"]) \
|
||||||
row = cursor.fetchone()
|
- (0 if credit["sum"] is None else credit["sum"])
|
||||||
balance = 0 if row[0] is None else row[0]
|
|
||||||
record_brought_forward = Record(
|
record_brought_forward = Record(
|
||||||
transaction=Transaction(
|
transaction=Transaction(
|
||||||
date=records[-1].transaction.date),
|
date=records[-1].transaction.date),
|
||||||
|
Loading…
Reference in New Issue
Block a user