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

View File

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