Moved the order from the data models to the reports in the accounting application.

This commit is contained in:
依瑪貓 2020-07-21 19:38:01 +08:00
parent 090f3dc3a5
commit 5a48e07ef6
2 changed files with 116 additions and 95 deletions

View File

@ -79,7 +79,6 @@ class Account(models.Model):
class Meta:
db_table = "accounting_accounts"
ordering = ["code"]
class Transaction(models.Model):
@ -197,7 +196,6 @@ class Transaction(models.Model):
class Meta:
db_table = "accounting_transactions"
ordering = ["date", "ord"]
class Record(models.Model):
@ -325,7 +323,6 @@ class Record(models.Model):
class Meta:
db_table = "accounting_records"
ordering = ["is_credit", "ord"]
class RecordSummary(models.Model):

View File

@ -81,7 +81,9 @@ def cash(request, account_code, period_spec):
raise Http404()
# The accounting records
if current_account.code == "0":
records = list(Record.objects.filter(
records = list(
Record.objects
.filter(
Q(transaction__in=Transaction.objects.filter(
Q(date__gte=period.start),
Q(date__lte=period.end),
@ -92,7 +94,8 @@ def cash(request, account_code, period_spec):
~Q(account__code__startswith="11"),
~Q(account__code__startswith="12"),
~Q(account__code__startswith="21"),
~Q(account__code__startswith="22")))
~Q(account__code__startswith="22"))
.order_by("transaction__date", "is_credit", "ord"))
balance_before = Record.objects.filter(
Q(transaction__date__lt=period.start),
(Q(account__code__startswith="11") |
@ -104,13 +107,17 @@ def cash(request, account_code, period_spec):
When(is_credit=True, then=-1),
default=1) * F("amount")), 0))["balance"]
else:
records = list(Record.objects.filter(
records = list(
Record.objects
.filter(
Q(transaction__in=Transaction.objects.filter(
Q(date__gte=period.start),
Q(date__lte=period.end),
Q(record__account__code__startswith=
current_account.code))),
~Q(account__code__startswith=current_account.code)))
~Q(account__code__startswith=
current_account.code))
.order_by("transaction__date", "is_credit", "ord"))
balance_before = Record.objects.filter(
transaction__date__lt=period.start,
account__code__startswith=current_account.code)\
@ -304,10 +311,13 @@ def ledger(request, account_code, period_spec):
if current_account is None:
raise Http404()
# The accounting records
records = list(Record.objects.filter(
records = list(
Record.objects
.filter(
transaction__date__gte=period.start,
transaction__date__lte=period.end,
account__code__startswith=current_account.code))
account__code__startswith=current_account.code)
.order_by("transaction__date", "is_credit", "ord"))
if re.match("^[1-3]", current_account.code) is not None:
balance = Record.objects.filter(
transaction__date__lt=period.start,
@ -451,10 +461,11 @@ def journal(request, period_spec):
# The period
period = _get_period(period_spec)
# The accounting records
records = Record.objects.filter(
records = Record.objects\
.filter(
transaction__date__gte=period.start,
transaction__date__lte=period.end).order_by(
"transaction__date", "is_credit", "ord")
transaction__date__lte=period.end)\
.order_by("transaction__date", "is_credit", "ord")
# The brought-forward records
brought_forward_accounts = Account.objects.filter(
Q(code__startswith="1")
@ -538,7 +549,8 @@ def trial_balance(request, period_spec):
period = _get_period(period_spec)
# The accounts
nominal = list(
Account.objects.filter(
Account.objects
.filter(
Q(record__transaction__date__gte=period.start),
Q(record__transaction__date__lte=period.end),
~(Q(code__startswith="1")
@ -555,10 +567,12 @@ def trial_balance(request, period_spec):
default=None),
credit=Case(
When(balance__lt=0, then=-F("balance")),
default=None)))
default=None))
.order_by("code"))
real = list(
Account.objects
.filter(Q(record__transaction__date__lte=period.end),
.filter(
Q(record__transaction__date__lte=period.end),
(Q(code__startswith="1")
| Q(code__startswith="2")
| Q(code__startswith="3")),
@ -574,8 +588,10 @@ def trial_balance(request, period_spec):
default=None),
credit=Case(
When(balance__lt=0, then=-F("balance")),
default=None)))
balance = Record.objects.filter(
default=None))
.order_by("code"))
balance = Record.objects\
.filter(
(Q(transaction__date__lt=period.start)
& ~(Q(account__code__startswith="1")
| Q(account__code__startswith="2")
@ -643,7 +659,8 @@ def income_statement(request, period_spec):
period = _get_period(period_spec)
# The accounts
accounts = list(
Account.objects.filter(
Account.objects
.filter(
Q(record__transaction__date__gte=period.start),
Q(record__transaction__date__lte=period.end),
~(Q(code__startswith="1")
@ -653,7 +670,8 @@ def income_statement(request, period_spec):
balance=Sum(Case(
When(record__is_credit=True, then=1),
default=-1) * F("record__amount")))
.filter(balance__isnull=False))
.filter(balance__isnull=False)
.order_by("code"))
groups = list(Account.objects.filter(
code__in=[x.code[:2] for x in accounts]))
sections = list(Account.objects.filter(
@ -728,7 +746,8 @@ def balance_sheet(request, period_spec):
# The accounts
accounts = list(
Account.objects
.filter(Q(record__transaction__date__lte=period.end),
.filter(
Q(record__transaction__date__lte=period.end),
(Q(code__startswith="1")
| Q(code__startswith="2")
| Q(code__startswith="3")),
@ -737,7 +756,8 @@ def balance_sheet(request, period_spec):
balance=Sum(Case(
When(record__is_credit=True, then=-1),
default=1) * F("record__amount")))
.filter(balance__isnull=False))
.filter(balance__isnull=False)
.order_by("code"))
for account in accounts:
account.url = reverse(
"accounting:ledger", args=[account.code, period.spec])
@ -849,13 +869,17 @@ def _cash_accounts():
Returns:
list[Account]: The cash accounts.
"""
accounts = list(Account.objects.filter(
code__in=Record.objects.filter(
accounts = list(
Account.objects
.filter(
code__in=Record.objects
.filter(
Q(account__code__startswith="11")
| Q(account__code__startswith="12")
| Q(account__code__startswith="21")
| Q(account__code__startswith="22"))
.values("account__code")))
.values("account__code"))
.order_by("code"))
accounts.insert(0, Account(
code="0",
title=pgettext(