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,18 +81,21 @@ 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(
Q(transaction__in=Transaction.objects.filter( Record.objects
Q(date__gte=period.start), .filter(
Q(date__lte=period.end), Q(transaction__in=Transaction.objects.filter(
(Q(record__account__code__startswith="11") | Q(date__gte=period.start),
Q(record__account__code__startswith="12") | Q(date__lte=period.end),
Q(record__account__code__startswith="21") | (Q(record__account__code__startswith="11") |
Q(record__account__code__startswith="22")))), Q(record__account__code__startswith="12") |
~Q(account__code__startswith="11"), Q(record__account__code__startswith="21") |
~Q(account__code__startswith="12"), Q(record__account__code__startswith="22")))),
~Q(account__code__startswith="21"), ~Q(account__code__startswith="11"),
~Q(account__code__startswith="22"))) ~Q(account__code__startswith="12"),
~Q(account__code__startswith="21"),
~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(
Q(transaction__in=Transaction.objects.filter( Record.objects
Q(date__gte=period.start), .filter(
Q(date__lte=period.end), Q(transaction__in=Transaction.objects.filter(
Q(record__account__code__startswith= Q(date__gte=period.start),
current_account.code))), Q(date__lte=period.end),
~Q(account__code__startswith=current_account.code))) Q(record__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(
transaction__date__gte=period.start, Record.objects
transaction__date__lte=period.end, .filter(
account__code__startswith=current_account.code)) transaction__date__gte=period.start,
transaction__date__lte=period.end,
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\
transaction__date__gte=period.start, .filter(
transaction__date__lte=period.end).order_by( transaction__date__gte=period.start,
"transaction__date", "is_credit", "ord") transaction__date__lte=period.end)\
.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,54 +549,59 @@ 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
Q(record__transaction__date__gte=period.start), .filter(
Q(record__transaction__date__lte=period.end), Q(record__transaction__date__gte=period.start),
~(Q(code__startswith="1") Q(record__transaction__date__lte=period.end),
| Q(code__startswith="2") ~(Q(code__startswith="1")
| Q(code__startswith="3"))) | Q(code__startswith="2")
| Q(code__startswith="3")))
.annotate( .annotate(
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)
.annotate( .annotate(
debit=Case( debit=Case(
When(balance__gt=0, then=F("balance")), When(balance__gt=0, then=F("balance")),
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(code__startswith="1") Q(record__transaction__date__lte=period.end),
| Q(code__startswith="2") (Q(code__startswith="1")
| Q(code__startswith="3")), | Q(code__startswith="2")
~Q(code="3351")) | Q(code__startswith="3")),
~Q(code="3351"))
.annotate( .annotate(
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)
.annotate( .annotate(
debit=Case( debit=Case(
When(balance__gt=0, then=F("balance")), When(balance__gt=0, then=F("balance")),
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"))
(Q(transaction__date__lt=period.start) balance = Record.objects\
& ~(Q(account__code__startswith="1") .filter(
| Q(account__code__startswith="2") (Q(transaction__date__lt=period.start)
| Q(account__code__startswith="3"))) & ~(Q(account__code__startswith="1")
| (Q(transaction__date__lte=period.end) | Q(account__code__startswith="2")
& Q(account__code="3351")))\ | Q(account__code__startswith="3")))
| (Q(transaction__date__lte=period.end)
& Q(account__code="3351")))\
.aggregate( .aggregate(
balance=Sum(Case( balance=Sum(Case(
When(is_credit=True, then=-1), When(is_credit=True, then=-1),
default=1) * F("amount")))["balance"] default=1) * F("amount")))["balance"]
if balance is not None and balance != 0: if balance is not None and balance != 0:
brought_forward = Account.objects.filter(code="3351").first() brought_forward = Account.objects.filter(code="3351").first()
if balance > 0: if balance > 0:
@ -643,17 +659,19 @@ 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
Q(record__transaction__date__gte=period.start), .filter(
Q(record__transaction__date__lte=period.end), Q(record__transaction__date__gte=period.start),
~(Q(code__startswith="1") Q(record__transaction__date__lte=period.end),
| Q(code__startswith="2") ~(Q(code__startswith="1")
| Q(code__startswith="3"))) | Q(code__startswith="2")
| Q(code__startswith="3")))
.annotate( .annotate(
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,16 +746,18 @@ 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(code__startswith="1") Q(record__transaction__date__lte=period.end),
| Q(code__startswith="2") (Q(code__startswith="1")
| Q(code__startswith="3")), | Q(code__startswith="2")
~Q(code="3351")) | Q(code__startswith="3")),
~Q(code="3351"))
.annotate( .annotate(
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
Q(account__code__startswith="11") .filter(
| Q(account__code__startswith="12") code__in=Record.objects
| Q(account__code__startswith="21") .filter(
| Q(account__code__startswith="22")) Q(account__code__startswith="11")
.values("account__code"))) | Q(account__code__startswith="12")
| Q(account__code__startswith="21")
| Q(account__code__startswith="22"))
.values("account__code"))
.order_by("code"))
accounts.insert(0, Account( accounts.insert(0, Account(
code="0", code="0",
title=pgettext( title=pgettext(