2020-07-21 08:20:57 +08:00
|
|
|
# The accounting application of the Mia project.
|
|
|
|
# by imacat <imacat@mail.imacat.idv.tw>, 2020/6/30
|
|
|
|
|
|
|
|
# Copyright (c) 2020 imacat.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""The view controllers of the accounting application.
|
|
|
|
|
|
|
|
"""
|
|
|
|
import re
|
|
|
|
|
2020-07-23 14:26:05 +08:00
|
|
|
from django.conf import settings
|
2020-08-02 11:01:36 +08:00
|
|
|
from django.db import transaction
|
|
|
|
from django.db.models import Sum, Case, When, F, Q, Max
|
|
|
|
from django.db.models.functions import TruncMonth, Coalesce, Now
|
2020-07-21 08:20:57 +08:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.urls import reverse
|
2020-08-01 07:49:56 +08:00
|
|
|
from django.utils import timezone
|
2020-07-30 00:10:11 +08:00
|
|
|
from django.utils.decorators import method_decorator
|
2020-07-28 08:22:42 +08:00
|
|
|
from django.utils.translation import pgettext, gettext_noop
|
|
|
|
from django.views.decorators.http import require_GET, require_POST
|
2020-07-30 00:10:11 +08:00
|
|
|
from django.views.generic import RedirectView
|
2020-07-21 08:20:57 +08:00
|
|
|
|
2020-07-30 00:12:13 +08:00
|
|
|
from mia_core.digest_auth import login_required
|
2020-07-29 15:07:47 +08:00
|
|
|
from mia_core.period import Period
|
|
|
|
from mia_core.status import success_redirect, error_redirect
|
2020-07-31 00:14:12 +08:00
|
|
|
from mia_core.utils import Pagination, get_multi_lingual_search, UrlBuilder, \
|
2020-08-02 11:01:36 +08:00
|
|
|
strip_form, new_pk
|
2020-07-23 14:26:05 +08:00
|
|
|
from .models import Record, Transaction, Account, RecordSummary
|
|
|
|
from .utils import ReportUrl, get_cash_accounts, get_ledger_accounts, \
|
2020-08-02 03:06:31 +08:00
|
|
|
find_imbalanced, find_order_holes, fill_txn_from_post, \
|
|
|
|
sort_post_txn_records, make_txn_form_from_status, \
|
|
|
|
make_txn_form_from_model, make_txn_form_from_post
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
@method_decorator(require_GET, name="dispatch")
|
2020-07-30 00:12:13 +08:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2020-07-30 00:10:11 +08:00
|
|
|
class CashDefaultView(RedirectView):
|
|
|
|
"""The default cash account."""
|
|
|
|
query_string = True
|
|
|
|
pattern_name = "accounting:cash"
|
2020-07-23 14:17:05 +08:00
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
kwargs["account"] = Account.objects.get(
|
|
|
|
code=settings.ACCOUNTING["DEFAULT_CASH_ACCOUNT"])
|
|
|
|
kwargs["period"] = Period.default_spec()
|
|
|
|
return super().get_redirect_url(*args, **kwargs)
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
@require_GET
|
2020-07-30 00:12:13 +08:00
|
|
|
@login_required
|
2020-07-23 08:33:53 +08:00
|
|
|
def cash(request, account, period):
|
2020-07-21 08:20:57 +08:00
|
|
|
"""The cash account.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest) The request.
|
2020-07-23 08:33:53 +08:00
|
|
|
account (Account): The account.
|
2020-07-21 22:05:39 +08:00
|
|
|
period (Period): The period.
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponse: The response.
|
|
|
|
"""
|
|
|
|
# The accounting records
|
2020-07-23 08:33:53 +08:00
|
|
|
if account.code == "0":
|
2020-07-21 19:38:01 +08:00
|
|
|
records = list(
|
|
|
|
Record.objects
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(
|
2020-07-21 20:57:59 +08:00
|
|
|
Q(transaction__in=Transaction.objects.filter(
|
|
|
|
Q(date__gte=period.start),
|
|
|
|
Q(date__lte=period.end),
|
|
|
|
(Q(record__account__code__startswith="11") |
|
|
|
|
Q(record__account__code__startswith="12") |
|
|
|
|
Q(record__account__code__startswith="21") |
|
|
|
|
Q(record__account__code__startswith="22")))),
|
|
|
|
~Q(account__code__startswith="11"),
|
|
|
|
~Q(account__code__startswith="12"),
|
|
|
|
~Q(account__code__startswith="21"),
|
|
|
|
~Q(account__code__startswith="22"))
|
2020-08-02 00:08:53 +08:00
|
|
|
.order_by("transaction__date", "transaction__ord",
|
|
|
|
"is_credit", "ord"))
|
2020-07-30 00:10:11 +08:00
|
|
|
balance_before = Record.objects \
|
2020-07-21 19:41:53 +08:00
|
|
|
.filter(
|
2020-08-02 00:08:53 +08:00
|
|
|
Q(transaction__date__lt=period.start),
|
|
|
|
(Q(account__code__startswith="11") |
|
|
|
|
Q(account__code__startswith="12") |
|
|
|
|
Q(account__code__startswith="21") |
|
|
|
|
Q(account__code__startswith="21"))) \
|
2020-07-21 08:20:57 +08:00
|
|
|
.aggregate(
|
2020-08-02 00:08:53 +08:00
|
|
|
balance=Coalesce(Sum(Case(
|
|
|
|
When(is_credit=True, then=-1),
|
|
|
|
default=1) * F("amount")), 0))["balance"]
|
2020-07-21 08:20:57 +08:00
|
|
|
else:
|
2020-07-21 19:38:01 +08:00
|
|
|
records = list(
|
|
|
|
Record.objects
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(
|
2020-07-21 20:57:59 +08:00
|
|
|
Q(transaction__in=Transaction.objects.filter(
|
|
|
|
Q(date__gte=period.start),
|
|
|
|
Q(date__lte=period.end),
|
2020-07-23 08:33:53 +08:00
|
|
|
Q(record__account__code__startswith=account.code))),
|
|
|
|
~Q(account__code__startswith=account.code))
|
2020-08-02 00:08:53 +08:00
|
|
|
.order_by("transaction__date", "transaction__ord",
|
|
|
|
"is_credit", "ord"))
|
2020-07-30 00:10:11 +08:00
|
|
|
balance_before = Record.objects \
|
2020-07-21 19:41:53 +08:00
|
|
|
.filter(
|
2020-08-02 00:08:53 +08:00
|
|
|
transaction__date__lt=period.start,
|
|
|
|
account__code__startswith=account.code) \
|
2020-07-21 08:20:57 +08:00
|
|
|
.aggregate(
|
2020-08-02 00:08:53 +08:00
|
|
|
balance=Coalesce(Sum(Case(When(
|
|
|
|
is_credit=True, then=-1),
|
|
|
|
default=1) * F("amount")), 0))["balance"]
|
2020-07-21 08:20:57 +08:00
|
|
|
balance = balance_before
|
|
|
|
for record in records:
|
|
|
|
sign = 1 if record.is_credit else -1
|
|
|
|
balance = balance + sign * record.amount
|
|
|
|
record.balance = balance
|
|
|
|
record_sum = Record(
|
2020-08-02 00:08:53 +08:00
|
|
|
transaction=(Transaction(date=records[-1].transaction.date)
|
|
|
|
if len(records) > 0
|
|
|
|
else Transaction(date=timezone.localdate())),
|
2020-07-23 08:33:53 +08:00
|
|
|
account=account,
|
2020-07-21 08:20:57 +08:00
|
|
|
summary=pgettext("Accounting|", "Total"),
|
|
|
|
)
|
2020-08-02 17:55:07 +08:00
|
|
|
record_sum.balance = balance
|
2020-07-21 08:20:57 +08:00
|
|
|
record_sum.credit_amount = sum([
|
|
|
|
x.amount for x in records if x.is_credit])
|
|
|
|
record_sum.debit_amount = sum([
|
|
|
|
x.amount for x in records if not x.is_credit])
|
2020-08-02 17:55:07 +08:00
|
|
|
record_balance_before = Record(
|
2020-07-21 08:20:57 +08:00
|
|
|
transaction=Transaction(date=period.start),
|
2020-08-02 18:20:36 +08:00
|
|
|
account=Account.objects.get(code=Account.ACCUMULATED_BALANCE),
|
2020-07-21 08:20:57 +08:00
|
|
|
is_credit=balance_before >= 0,
|
|
|
|
amount=abs(balance_before),
|
2020-08-02 17:55:07 +08:00
|
|
|
)
|
|
|
|
record_balance_before.balance = balance_before
|
|
|
|
records.insert(0, record_balance_before)
|
2020-07-21 08:20:57 +08:00
|
|
|
records.append(record_sum)
|
|
|
|
pagination = Pagination(request, records, True)
|
|
|
|
records = pagination.items
|
2020-07-23 09:57:29 +08:00
|
|
|
find_imbalanced(records)
|
|
|
|
find_order_holes(records)
|
|
|
|
accounts = get_cash_accounts()
|
2020-07-21 10:38:47 +08:00
|
|
|
shortcut_accounts = settings.ACCOUNTING["CASH_SHORTCUT_ACCOUNTS"]
|
2020-07-21 08:20:57 +08:00
|
|
|
return render(request, "accounting/cash.html", {
|
|
|
|
"item_list": records,
|
|
|
|
"pagination": pagination,
|
2020-07-23 09:37:29 +08:00
|
|
|
"account": account,
|
2020-07-21 08:20:57 +08:00
|
|
|
"period": period,
|
2020-07-23 08:33:53 +08:00
|
|
|
"reports": ReportUrl(cash=account, period=period),
|
2020-07-21 10:38:47 +08:00
|
|
|
"shortcut_accounts": [x for x in accounts
|
|
|
|
if x.code in shortcut_accounts],
|
|
|
|
"all_accounts": [x for x in accounts
|
|
|
|
if x.code not in shortcut_accounts],
|
2020-07-21 08:20:57 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
@method_decorator(require_GET, name="dispatch")
|
2020-07-30 00:12:13 +08:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2020-07-30 00:10:11 +08:00
|
|
|
class CashSummaryDefaultView(RedirectView):
|
|
|
|
"""The default cash account summary."""
|
|
|
|
query_string = True
|
|
|
|
pattern_name = "accounting:cash-summary"
|
2020-07-21 08:20:57 +08:00
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
kwargs["account"] = Account.objects.get(
|
|
|
|
code=settings.ACCOUNTING["DEFAULT_CASH_ACCOUNT"])
|
|
|
|
return super().get_redirect_url(*args, **kwargs)
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
@require_GET
|
2020-07-30 00:12:13 +08:00
|
|
|
@login_required
|
2020-07-23 08:33:53 +08:00
|
|
|
def cash_summary(request, account):
|
2020-07-21 08:20:57 +08:00
|
|
|
"""The cash account summary.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest) The request.
|
2020-07-23 08:33:53 +08:00
|
|
|
account (Account): The account.
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponse: The response.
|
|
|
|
"""
|
2020-07-21 10:38:47 +08:00
|
|
|
# The account
|
2020-07-23 09:57:29 +08:00
|
|
|
accounts = get_cash_accounts()
|
2020-07-21 08:20:57 +08:00
|
|
|
# The month summaries
|
2020-07-23 08:33:53 +08:00
|
|
|
if account.code == "0":
|
2020-07-21 19:41:53 +08:00
|
|
|
months = [RecordSummary(**x) for x in Record.objects
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(
|
|
|
|
Q(transaction__in=Transaction.objects.filter(
|
|
|
|
Q(record__account__code__startswith="11") |
|
|
|
|
Q(record__account__code__startswith="12") |
|
|
|
|
Q(record__account__code__startswith="21") |
|
|
|
|
Q(record__account__code__startswith="22"))),
|
|
|
|
~Q(account__code__startswith="11"),
|
|
|
|
~Q(account__code__startswith="12"),
|
|
|
|
~Q(account__code__startswith="21"),
|
|
|
|
~Q(account__code__startswith="22"))
|
|
|
|
.annotate(month=TruncMonth("transaction__date"))
|
|
|
|
.values("month")
|
|
|
|
.order_by("month")
|
|
|
|
.annotate(
|
|
|
|
debit=Coalesce(
|
|
|
|
Sum(Case(When(is_credit=False, then=F("amount")))),
|
|
|
|
0),
|
|
|
|
credit=Coalesce(
|
|
|
|
Sum(Case(When(is_credit=True, then=F("amount")))),
|
|
|
|
0),
|
|
|
|
balance=Sum(Case(
|
|
|
|
When(is_credit=False, then=-F("amount")),
|
|
|
|
default=F("amount"))))]
|
2020-07-21 08:20:57 +08:00
|
|
|
else:
|
2020-07-21 19:41:53 +08:00
|
|
|
months = [RecordSummary(**x) for x in Record.objects
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(
|
|
|
|
Q(transaction__in=Transaction.objects.filter(
|
|
|
|
record__account__code__startswith=account.code)),
|
|
|
|
~Q(account__code__startswith=account.code))
|
|
|
|
.annotate(month=TruncMonth("transaction__date"))
|
|
|
|
.values("month")
|
|
|
|
.order_by("month")
|
|
|
|
.annotate(
|
|
|
|
debit=Coalesce(
|
|
|
|
Sum(Case(When(is_credit=False, then=F("amount")))),
|
|
|
|
0),
|
|
|
|
credit=Coalesce(
|
|
|
|
Sum(Case(When(is_credit=True, then=F("amount")))),
|
|
|
|
0),
|
|
|
|
balance=Sum(Case(
|
|
|
|
When(is_credit=False, then=-F("amount")),
|
|
|
|
default=F("amount"))))]
|
2020-07-21 08:20:57 +08:00
|
|
|
cumulative_balance = 0
|
|
|
|
for month in months:
|
|
|
|
cumulative_balance = cumulative_balance + month.balance
|
|
|
|
month.cumulative_balance = cumulative_balance
|
|
|
|
months.append(RecordSummary(
|
|
|
|
label=pgettext("Accounting|", "Total"),
|
|
|
|
credit=sum([x.credit for x in months]),
|
|
|
|
debit=sum([x.debit for x in months]),
|
|
|
|
balance=sum([x.balance for x in months]),
|
|
|
|
cumulative_balance=cumulative_balance,
|
|
|
|
))
|
|
|
|
pagination = Pagination(request, months, True)
|
2020-07-21 10:38:47 +08:00
|
|
|
shortcut_accounts = settings.ACCOUNTING["CASH_SHORTCUT_ACCOUNTS"]
|
2020-07-21 08:20:57 +08:00
|
|
|
return render(request, "accounting/cash-summary.html", {
|
|
|
|
"item_list": pagination.items,
|
|
|
|
"pagination": pagination,
|
2020-07-23 09:37:29 +08:00
|
|
|
"account": account,
|
2020-07-23 08:33:53 +08:00
|
|
|
"reports": ReportUrl(cash=account),
|
2020-07-21 10:38:47 +08:00
|
|
|
"shortcut_accounts": [x for x in accounts if
|
|
|
|
x.code in shortcut_accounts],
|
|
|
|
"all_accounts": [x for x in accounts if
|
|
|
|
x.code not in shortcut_accounts],
|
2020-07-21 08:20:57 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
@method_decorator(require_GET, name="dispatch")
|
2020-07-30 00:12:13 +08:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2020-07-30 00:10:11 +08:00
|
|
|
class LedgerDefaultView(RedirectView):
|
|
|
|
"""The default ledger."""
|
|
|
|
query_string = True
|
|
|
|
pattern_name = "accounting:ledger"
|
2020-07-21 08:20:57 +08:00
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
kwargs["account"] = Account.objects.get(
|
|
|
|
code=settings.ACCOUNTING["DEFAULT_LEDGER_ACCOUNT"])
|
|
|
|
kwargs["period"] = Period.default_spec()
|
|
|
|
return super().get_redirect_url(*args, **kwargs)
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
@require_GET
|
2020-07-30 00:12:13 +08:00
|
|
|
@login_required
|
2020-07-23 08:33:53 +08:00
|
|
|
def ledger(request, account, period):
|
2020-07-21 08:20:57 +08:00
|
|
|
"""The ledger.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest) The request.
|
2020-07-23 08:33:53 +08:00
|
|
|
account (Account): The account.
|
2020-07-21 22:05:39 +08:00
|
|
|
period (Period): The period.
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponse: The response.
|
|
|
|
"""
|
|
|
|
# The accounting records
|
2020-07-21 19:38:01 +08:00
|
|
|
records = list(
|
|
|
|
Record.objects
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(
|
2020-07-21 20:57:59 +08:00
|
|
|
transaction__date__gte=period.start,
|
|
|
|
transaction__date__lte=period.end,
|
2020-07-23 08:33:53 +08:00
|
|
|
account__code__startswith=account.code)
|
2020-08-02 00:08:53 +08:00
|
|
|
.order_by("transaction__date", "transaction__ord", "is_credit",
|
|
|
|
"ord"))
|
2020-07-23 08:33:53 +08:00
|
|
|
if re.match("^[1-3]", account.code) is not None:
|
2020-07-30 00:10:11 +08:00
|
|
|
balance = Record.objects \
|
2020-07-21 19:41:53 +08:00
|
|
|
.filter(
|
2020-08-02 00:08:53 +08:00
|
|
|
transaction__date__lt=period.start,
|
|
|
|
account__code__startswith=account.code) \
|
2020-07-21 08:20:57 +08:00
|
|
|
.aggregate(
|
2020-08-02 00:08:53 +08:00
|
|
|
balance=Coalesce(Sum(Case(When(
|
|
|
|
is_credit=True, then=-1),
|
|
|
|
default=1) * F("amount")), 0))["balance"]
|
2020-07-21 08:20:57 +08:00
|
|
|
record_brought_forward = Record(
|
|
|
|
transaction=Transaction(date=period.start),
|
2020-07-23 08:33:53 +08:00
|
|
|
account=account,
|
2020-07-21 08:20:57 +08:00
|
|
|
summary=pgettext("Accounting|", "Brought Forward"),
|
|
|
|
is_credit=balance < 0,
|
|
|
|
amount=abs(balance),
|
|
|
|
)
|
2020-08-02 17:55:07 +08:00
|
|
|
record_brought_forward.balance = balance
|
2020-07-21 08:20:57 +08:00
|
|
|
else:
|
|
|
|
balance = 0
|
|
|
|
record_brought_forward = None
|
|
|
|
for record in records:
|
|
|
|
balance = balance + \
|
|
|
|
(-1 if record.is_credit else 1) * record.amount
|
|
|
|
record.balance = balance
|
|
|
|
if record_brought_forward is not None:
|
|
|
|
records.insert(0, record_brought_forward)
|
|
|
|
pagination = Pagination(request, records, True)
|
|
|
|
records = pagination.items
|
2020-07-23 09:57:29 +08:00
|
|
|
find_imbalanced(records)
|
|
|
|
find_order_holes(records)
|
2020-07-21 08:20:57 +08:00
|
|
|
return render(request, "accounting/ledger.html", {
|
|
|
|
"item_list": records,
|
|
|
|
"pagination": pagination,
|
2020-07-23 09:37:29 +08:00
|
|
|
"account": account,
|
2020-07-21 08:20:57 +08:00
|
|
|
"period": period,
|
2020-07-23 08:33:53 +08:00
|
|
|
"reports": ReportUrl(ledger=account, period=period),
|
2020-07-23 09:57:29 +08:00
|
|
|
"accounts": get_ledger_accounts(),
|
2020-07-21 08:20:57 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
@method_decorator(require_GET, name="dispatch")
|
2020-07-30 00:12:13 +08:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2020-07-30 00:10:11 +08:00
|
|
|
class LedgerSummaryDefaultView(RedirectView):
|
|
|
|
"""The default ledger summary."""
|
|
|
|
query_string = True
|
|
|
|
pattern_name = "accounting:ledger-summary"
|
2020-07-21 08:20:57 +08:00
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
kwargs["account"] = Account.objects.get(
|
|
|
|
code=settings.ACCOUNTING["DEFAULT_LEDGER_ACCOUNT"])
|
|
|
|
return super().get_redirect_url(*args, **kwargs)
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
@require_GET
|
2020-07-30 00:12:13 +08:00
|
|
|
@login_required
|
2020-07-23 08:33:53 +08:00
|
|
|
def ledger_summary(request, account):
|
2020-07-21 08:20:57 +08:00
|
|
|
"""The ledger summary report.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest) The request.
|
2020-07-23 08:33:53 +08:00
|
|
|
account (Account): The account.
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponse: The response.
|
|
|
|
"""
|
|
|
|
# The month summaries
|
2020-07-21 20:57:59 +08:00
|
|
|
months = [RecordSummary(**x) for x in Record.objects
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(account__code__startswith=account.code)
|
|
|
|
.annotate(month=TruncMonth("transaction__date"))
|
|
|
|
.values("month")
|
|
|
|
.order_by("month")
|
|
|
|
.annotate(
|
|
|
|
debit=Coalesce(
|
|
|
|
Sum(Case(When(is_credit=False, then=F("amount")))),
|
|
|
|
0),
|
|
|
|
credit=Coalesce(
|
|
|
|
Sum(Case(When(is_credit=True, then=F("amount")))),
|
|
|
|
0),
|
|
|
|
balance=Sum(Case(
|
|
|
|
When(is_credit=False, then=F("amount")),
|
|
|
|
default=-F("amount"))))]
|
2020-07-21 08:20:57 +08:00
|
|
|
cumulative_balance = 0
|
|
|
|
for month in months:
|
|
|
|
cumulative_balance = cumulative_balance + month.balance
|
|
|
|
month.cumulative_balance = cumulative_balance
|
|
|
|
months.append(RecordSummary(
|
|
|
|
label=pgettext("Accounting|", "Total"),
|
|
|
|
credit=sum([x.credit for x in months]),
|
|
|
|
debit=sum([x.debit for x in months]),
|
|
|
|
balance=sum([x.balance for x in months]),
|
|
|
|
cumulative_balance=cumulative_balance,
|
|
|
|
))
|
|
|
|
pagination = Pagination(request, months, True)
|
|
|
|
return render(request, "accounting/ledger-summary.html", {
|
|
|
|
"item_list": pagination.items,
|
|
|
|
"pagination": pagination,
|
2020-07-23 09:37:29 +08:00
|
|
|
"account": account,
|
2020-07-23 08:33:53 +08:00
|
|
|
"reports": ReportUrl(ledger=account),
|
2020-07-23 09:57:29 +08:00
|
|
|
"accounts": get_ledger_accounts(),
|
2020-07-21 08:20:57 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
@method_decorator(require_GET, name="dispatch")
|
2020-07-30 00:12:13 +08:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2020-07-30 00:10:11 +08:00
|
|
|
class JournalDefaultView(RedirectView):
|
|
|
|
"""The default journal."""
|
|
|
|
query_string = True
|
|
|
|
pattern_name = "accounting:journal"
|
2020-07-21 08:20:57 +08:00
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
kwargs["period"] = Period.default_spec()
|
|
|
|
return super().get_redirect_url(*args, **kwargs)
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
@require_GET
|
2020-07-30 00:12:13 +08:00
|
|
|
@login_required
|
2020-07-21 22:05:39 +08:00
|
|
|
def journal(request, period):
|
2020-07-21 08:20:57 +08:00
|
|
|
"""The journal.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest) The request.
|
2020-07-21 22:05:39 +08:00
|
|
|
period (Period): The period.
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponse: The response.
|
|
|
|
"""
|
|
|
|
# The accounting records
|
2020-07-30 00:10:11 +08:00
|
|
|
records = Record.objects \
|
2020-07-21 19:38:01 +08:00
|
|
|
.filter(
|
2020-08-02 00:08:53 +08:00
|
|
|
transaction__date__gte=period.start,
|
|
|
|
transaction__date__lte=period.end) \
|
2020-07-22 08:25:43 +08:00
|
|
|
.order_by("transaction__date", "transaction__ord", "is_credit", "ord")
|
2020-07-21 08:20:57 +08:00
|
|
|
# The brought-forward records
|
2020-07-30 00:10:11 +08:00
|
|
|
brought_forward_accounts = Account.objects \
|
2020-07-21 19:41:53 +08:00
|
|
|
.filter(
|
2020-08-02 00:08:53 +08:00
|
|
|
Q(code__startswith="1")
|
|
|
|
| Q(code__startswith="2")
|
|
|
|
| Q(code__startswith="3")) \
|
2020-07-21 08:20:57 +08:00
|
|
|
.annotate(balance=Sum(
|
2020-08-02 00:08:53 +08:00
|
|
|
Case(
|
|
|
|
When(record__is_credit=True, then=-1),
|
|
|
|
default=1
|
|
|
|
) * F("record__amount"),
|
|
|
|
filter=Q(record__transaction__date__lt=period.start))) \
|
2020-07-22 10:34:53 +08:00
|
|
|
.filter(~Q(balance=0))
|
2020-07-21 08:20:57 +08:00
|
|
|
debit_records = [Record(
|
|
|
|
transaction=Transaction(date=period.start),
|
2020-07-21 10:38:47 +08:00
|
|
|
account=x,
|
2020-07-21 08:20:57 +08:00
|
|
|
is_credit=False,
|
|
|
|
amount=x.balance
|
2020-07-21 10:38:47 +08:00
|
|
|
) for x in brought_forward_accounts if x.balance > 0]
|
2020-07-21 08:20:57 +08:00
|
|
|
credit_records = [Record(
|
|
|
|
transaction=Transaction(date=period.start),
|
2020-07-21 10:38:47 +08:00
|
|
|
account=x,
|
2020-07-21 08:20:57 +08:00
|
|
|
is_credit=True,
|
|
|
|
amount=-x.balance
|
2020-07-21 10:38:47 +08:00
|
|
|
) for x in brought_forward_accounts if x.balance < 0]
|
2020-07-21 08:20:57 +08:00
|
|
|
sum_debits = sum([x.amount for x in debit_records])
|
|
|
|
sum_credits = sum([x.amount for x in credit_records])
|
|
|
|
if sum_debits < sum_credits:
|
|
|
|
debit_records.append(Record(
|
|
|
|
transaction=Transaction(date=period.start),
|
2020-08-02 18:20:36 +08:00
|
|
|
account=Account.objects.get(code=Account.ACCUMULATED_BALANCE),
|
2020-07-21 08:20:57 +08:00
|
|
|
is_credit=False,
|
|
|
|
amount=sum_credits - sum_debits
|
|
|
|
))
|
|
|
|
elif sum_debits > sum_credits:
|
|
|
|
credit_records.append(Record(
|
|
|
|
transaction=Transaction(date=period.start),
|
2020-08-02 18:20:36 +08:00
|
|
|
account=Account.objects.get(code=Account.ACCUMULATED_BALANCE),
|
2020-07-21 08:20:57 +08:00
|
|
|
is_credit=True,
|
|
|
|
amount=sum_debits - sum_credits
|
|
|
|
))
|
2020-08-02 00:08:53 +08:00
|
|
|
records = list(debit_records) + list(credit_records) + list(records)
|
2020-07-21 08:20:57 +08:00
|
|
|
pagination = Pagination(request, records, True)
|
|
|
|
return render(request, "accounting/journal.html", {
|
|
|
|
"item_list": pagination.items,
|
|
|
|
"pagination": pagination,
|
2020-07-21 10:38:47 +08:00
|
|
|
"reports": ReportUrl(period=period),
|
2020-07-21 08:20:57 +08:00
|
|
|
"period": period,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
@method_decorator(require_GET, name="dispatch")
|
2020-07-30 00:12:13 +08:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2020-07-30 00:10:11 +08:00
|
|
|
class TrialBalanceDefaultView(RedirectView):
|
|
|
|
"""The default trial balance."""
|
|
|
|
query_string = True
|
|
|
|
pattern_name = "accounting:trial-balance"
|
2020-07-21 08:20:57 +08:00
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
kwargs["period"] = Period.default_spec()
|
|
|
|
return super().get_redirect_url(*args, **kwargs)
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
@require_GET
|
2020-07-30 00:12:13 +08:00
|
|
|
@login_required
|
2020-07-21 22:05:39 +08:00
|
|
|
def trial_balance(request, period):
|
2020-07-21 08:20:57 +08:00
|
|
|
"""The trial balance.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest) The request.
|
2020-07-21 22:05:39 +08:00
|
|
|
period (Period): The period.
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponse: The response.
|
|
|
|
"""
|
|
|
|
# The accounts
|
|
|
|
nominal = list(
|
2020-07-21 19:38:01 +08:00
|
|
|
Account.objects
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(
|
2020-07-21 20:57:59 +08:00
|
|
|
Q(record__transaction__date__gte=period.start),
|
|
|
|
Q(record__transaction__date__lte=period.end),
|
|
|
|
~(Q(code__startswith="1")
|
|
|
|
| Q(code__startswith="2")
|
|
|
|
| Q(code__startswith="3")))
|
2020-08-02 00:08:53 +08:00
|
|
|
.annotate(
|
2020-07-21 20:57:59 +08:00
|
|
|
balance=Sum(Case(
|
|
|
|
When(record__is_credit=True, then=-1),
|
|
|
|
default=1) * F("record__amount")))
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(balance__isnull=False)
|
|
|
|
.annotate(
|
2020-07-21 20:57:59 +08:00
|
|
|
debit=Case(
|
|
|
|
When(balance__gt=0, then=F("balance")),
|
|
|
|
default=None),
|
|
|
|
credit=Case(
|
|
|
|
When(balance__lt=0, then=-F("balance")),
|
|
|
|
default=None))
|
2020-08-02 00:08:53 +08:00
|
|
|
.order_by("code"))
|
2020-07-21 08:20:57 +08:00
|
|
|
real = list(
|
2020-07-21 10:38:47 +08:00
|
|
|
Account.objects
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(
|
2020-07-21 20:57:59 +08:00
|
|
|
Q(record__transaction__date__lte=period.end),
|
|
|
|
(Q(code__startswith="1")
|
|
|
|
| Q(code__startswith="2")
|
|
|
|
| Q(code__startswith="3")),
|
2020-08-02 18:20:36 +08:00
|
|
|
~Q(code=Account.ACCUMULATED_BALANCE))
|
2020-08-02 00:08:53 +08:00
|
|
|
.annotate(
|
2020-07-21 20:57:59 +08:00
|
|
|
balance=Sum(Case(
|
|
|
|
When(record__is_credit=True, then=-1),
|
|
|
|
default=1) * F("record__amount")))
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(balance__isnull=False)
|
|
|
|
.annotate(
|
2020-07-21 20:57:59 +08:00
|
|
|
debit=Case(
|
|
|
|
When(balance__gt=0, then=F("balance")),
|
|
|
|
default=None),
|
|
|
|
credit=Case(
|
|
|
|
When(balance__lt=0, then=-F("balance")),
|
|
|
|
default=None))
|
2020-08-02 00:08:53 +08:00
|
|
|
.order_by("code"))
|
2020-07-30 00:10:11 +08:00
|
|
|
balance = Record.objects \
|
2020-07-21 19:38:01 +08:00
|
|
|
.filter(
|
2020-08-02 00:08:53 +08:00
|
|
|
(Q(transaction__date__lt=period.start)
|
|
|
|
& ~(Q(account__code__startswith="1")
|
|
|
|
| Q(account__code__startswith="2")
|
|
|
|
| Q(account__code__startswith="3")))
|
|
|
|
| (Q(transaction__date__lte=period.end)
|
2020-08-02 18:20:36 +08:00
|
|
|
& Q(account__code=Account.ACCUMULATED_BALANCE))) \
|
2020-07-21 08:20:57 +08:00
|
|
|
.aggregate(
|
2020-08-02 00:08:53 +08:00
|
|
|
balance=Sum(Case(
|
|
|
|
When(is_credit=True, then=-1),
|
|
|
|
default=1) * F("amount")))["balance"]
|
2020-07-21 08:20:57 +08:00
|
|
|
if balance is not None and balance != 0:
|
2020-08-02 18:20:36 +08:00
|
|
|
brought_forward = Account.objects.get(
|
|
|
|
code=Account.ACCUMULATED_BALANCE)
|
2020-07-21 08:20:57 +08:00
|
|
|
if balance > 0:
|
|
|
|
brought_forward.debit = balance
|
|
|
|
brought_forward.credit = 0
|
|
|
|
else:
|
|
|
|
brought_forward.debit = None
|
|
|
|
brought_forward.credit = -balance
|
|
|
|
real.append(brought_forward)
|
|
|
|
accounts = nominal + real
|
|
|
|
accounts.sort(key=lambda x: x.code)
|
2020-07-21 10:38:47 +08:00
|
|
|
total_account = Account()
|
2020-07-21 08:20:57 +08:00
|
|
|
total_account.title = pgettext("Accounting|", "Total")
|
|
|
|
total_account.debit = sum([x.debit for x in accounts
|
|
|
|
if x.debit is not None])
|
|
|
|
total_account.credit = sum([x.credit for x in accounts
|
|
|
|
if x.credit is not None])
|
|
|
|
return render(request, "accounting/trial-balance.html", {
|
|
|
|
"item_list": accounts,
|
|
|
|
"total_item": total_account,
|
|
|
|
"reports": ReportUrl(period=period),
|
|
|
|
"period": period,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
@method_decorator(require_GET, name="dispatch")
|
2020-07-30 00:12:13 +08:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2020-07-30 00:10:11 +08:00
|
|
|
class IncomeStatementDefaultView(RedirectView):
|
|
|
|
"""The default income statement."""
|
|
|
|
query_string = True
|
|
|
|
pattern_name = "accounting:income-statement"
|
2020-07-21 08:20:57 +08:00
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
kwargs["period"] = Period.default_spec()
|
|
|
|
return super().get_redirect_url(*args, **kwargs)
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
@require_GET
|
2020-07-30 00:12:13 +08:00
|
|
|
@login_required
|
2020-07-21 22:05:39 +08:00
|
|
|
def income_statement(request, period):
|
2020-07-21 08:20:57 +08:00
|
|
|
"""The income statement.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest) The request.
|
2020-07-21 22:05:39 +08:00
|
|
|
period (Period): The period.
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponse: The response.
|
|
|
|
"""
|
|
|
|
# The accounts
|
|
|
|
accounts = list(
|
2020-07-21 19:38:01 +08:00
|
|
|
Account.objects
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(
|
2020-07-21 20:57:59 +08:00
|
|
|
Q(record__transaction__date__gte=period.start),
|
|
|
|
Q(record__transaction__date__lte=period.end),
|
|
|
|
~(Q(code__startswith="1")
|
|
|
|
| Q(code__startswith="2")
|
|
|
|
| Q(code__startswith="3")))
|
2020-08-02 00:08:53 +08:00
|
|
|
.annotate(
|
2020-07-21 20:57:59 +08:00
|
|
|
balance=Sum(Case(
|
|
|
|
When(record__is_credit=True, then=1),
|
|
|
|
default=-1) * F("record__amount")))
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(balance__isnull=False)
|
|
|
|
.order_by("code"))
|
2020-07-21 10:38:47 +08:00
|
|
|
groups = list(Account.objects.filter(
|
2020-07-21 08:20:57 +08:00
|
|
|
code__in=[x.code[:2] for x in accounts]))
|
2020-07-21 10:38:47 +08:00
|
|
|
sections = list(Account.objects.filter(
|
2020-07-21 08:20:57 +08:00
|
|
|
Q(code="4") | Q(code="5") | Q(code="6")
|
|
|
|
| Q(code="7") | Q(code="8") | Q(code="9")).order_by("code"))
|
|
|
|
cumulative_accounts = {
|
2020-07-21 10:38:47 +08:00
|
|
|
"5": Account(title=pgettext("Accounting|", "Gross Income")),
|
|
|
|
"6": Account(title=pgettext("Accounting|", "Operating Income")),
|
|
|
|
"7": Account(title=pgettext("Accounting|", "Before Tax Income")),
|
|
|
|
"8": Account(title=pgettext("Accounting|", "After Tax Income")),
|
2020-08-02 18:20:36 +08:00
|
|
|
"9": Account.objects.get(code=Account.NET_CHANGE),
|
2020-07-21 08:20:57 +08:00
|
|
|
}
|
|
|
|
cumulative_total = 0
|
|
|
|
for section in sections:
|
|
|
|
section.groups = [x for x in groups
|
|
|
|
if x.code[:1] == section.code]
|
|
|
|
for group in section.groups:
|
|
|
|
group.details = [x for x in accounts
|
|
|
|
if x.code[:2] == group.code]
|
|
|
|
group.balance = None
|
|
|
|
group.total = sum([x.balance
|
|
|
|
for x in group.details])
|
|
|
|
section.balance = None
|
|
|
|
section.total = sum([x.total for x in section.groups])
|
|
|
|
cumulative_total = cumulative_total + section.total
|
|
|
|
if section.code in cumulative_accounts:
|
2020-07-30 00:10:11 +08:00
|
|
|
section.cumulative_total \
|
2020-07-21 08:20:57 +08:00
|
|
|
= cumulative_accounts[section.code]
|
|
|
|
section.cumulative_total.balance = None
|
|
|
|
section.cumulative_total.total = cumulative_total
|
|
|
|
else:
|
|
|
|
section.cumulative_total = None
|
|
|
|
section.has_next = True
|
|
|
|
sections[-1].has_next = False
|
|
|
|
return render(request, "accounting/income-statement.html", {
|
|
|
|
"item_list": sections,
|
|
|
|
"reports": ReportUrl(period=period),
|
|
|
|
"period": period,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
@method_decorator(require_GET, name="dispatch")
|
2020-07-30 00:12:13 +08:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2020-07-30 00:10:11 +08:00
|
|
|
class BalanceSheetDefaultView(RedirectView):
|
|
|
|
"""The default balance sheet."""
|
|
|
|
query_string = True
|
|
|
|
pattern_name = "accounting:balance-sheet"
|
2020-07-21 08:20:57 +08:00
|
|
|
|
2020-07-30 00:10:11 +08:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
kwargs["period"] = Period.default_spec()
|
|
|
|
return super().get_redirect_url(*args, **kwargs)
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
@require_GET
|
2020-07-30 00:12:13 +08:00
|
|
|
@login_required
|
2020-07-21 22:05:39 +08:00
|
|
|
def balance_sheet(request, period):
|
2020-07-21 08:20:57 +08:00
|
|
|
"""The balance sheet.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest) The request.
|
2020-07-21 22:05:39 +08:00
|
|
|
period (Period): The period.
|
2020-07-21 08:20:57 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponse: The response.
|
|
|
|
"""
|
|
|
|
# The accounts
|
|
|
|
accounts = list(
|
2020-07-21 10:38:47 +08:00
|
|
|
Account.objects
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(
|
2020-07-21 20:57:59 +08:00
|
|
|
Q(record__transaction__date__lte=period.end),
|
|
|
|
(Q(code__startswith="1")
|
|
|
|
| Q(code__startswith="2")
|
|
|
|
| Q(code__startswith="3")),
|
2020-08-02 18:20:36 +08:00
|
|
|
~Q(code=Account.ACCUMULATED_BALANCE))
|
2020-08-02 00:08:53 +08:00
|
|
|
.annotate(
|
2020-07-21 20:57:59 +08:00
|
|
|
balance=Sum(Case(
|
|
|
|
When(record__is_credit=True, then=-1),
|
|
|
|
default=1) * F("record__amount")))
|
2020-08-02 00:08:53 +08:00
|
|
|
.filter(balance__isnull=False)
|
|
|
|
.order_by("code"))
|
2020-07-21 13:38:18 +08:00
|
|
|
for account in accounts:
|
2020-07-23 08:33:53 +08:00
|
|
|
account.url = reverse("accounting:ledger", args=(account, period))
|
2020-07-30 00:10:11 +08:00
|
|
|
balance = Record.objects \
|
2020-07-21 08:20:57 +08:00
|
|
|
.filter(
|
2020-08-02 00:08:53 +08:00
|
|
|
Q(transaction__date__lt=period.start)
|
|
|
|
& ~((Q(account__code__startswith="1")
|
|
|
|
| Q(account__code__startswith="2")
|
|
|
|
| Q(account__code__startswith="3"))
|
2020-08-02 18:20:36 +08:00
|
|
|
& ~Q(account__code=Account.ACCUMULATED_BALANCE))) \
|
2020-07-21 08:20:57 +08:00
|
|
|
.aggregate(
|
2020-08-02 00:08:53 +08:00
|
|
|
balance=Sum(Case(
|
|
|
|
When(is_credit=True, then=-1),
|
|
|
|
default=1) * F("amount")))["balance"]
|
2020-07-21 08:20:57 +08:00
|
|
|
if balance is not None and balance != 0:
|
2020-08-02 18:20:36 +08:00
|
|
|
brought_forward = Account.objects.get(
|
|
|
|
code=Account.ACCUMULATED_BALANCE)
|
2020-07-22 10:26:04 +08:00
|
|
|
brought_forward.balance = balance
|
2020-07-21 13:38:18 +08:00
|
|
|
brought_forward.url = reverse(
|
2020-07-21 22:05:39 +08:00
|
|
|
"accounting:income-statement", args=(period,))
|
2020-07-21 08:20:57 +08:00
|
|
|
accounts.append(brought_forward)
|
2020-07-30 00:10:11 +08:00
|
|
|
balance = Record.objects \
|
2020-07-21 08:20:57 +08:00
|
|
|
.filter(
|
2020-08-02 00:08:53 +08:00
|
|
|
Q(transaction__date__gte=period.start)
|
|
|
|
& Q(transaction__date__lte=period.end)
|
|
|
|
& ~((Q(account__code__startswith="1")
|
|
|
|
| Q(account__code__startswith="2")
|
|
|
|
| Q(account__code__startswith="3"))
|
2020-08-02 18:20:36 +08:00
|
|
|
& ~Q(account__code=Account.ACCUMULATED_BALANCE))) \
|
2020-07-21 08:20:57 +08:00
|
|
|
.aggregate(
|
2020-08-02 00:08:53 +08:00
|
|
|
balance=Sum(Case(
|
|
|
|
When(is_credit=True, then=-1),
|
|
|
|
default=1) * F("amount")))["balance"]
|
2020-07-21 08:20:57 +08:00
|
|
|
if balance is not None and balance != 0:
|
2020-08-02 18:20:36 +08:00
|
|
|
net_change = Account.objects.get(code=Account.NET_CHANGE)
|
|
|
|
net_change.balance = balance
|
|
|
|
net_change.url = reverse(
|
2020-07-21 22:05:39 +08:00
|
|
|
"accounting:income-statement", args=(period,))
|
2020-08-02 18:20:36 +08:00
|
|
|
accounts.append(net_change)
|
2020-07-22 10:26:04 +08:00
|
|
|
for account in [x for x in accounts if x.code[0] in "23"]:
|
|
|
|
account.balance = -account.balance
|
2020-07-21 10:38:47 +08:00
|
|
|
groups = list(Account.objects.filter(
|
2020-07-21 08:20:57 +08:00
|
|
|
code__in=[x.code[:2] for x in accounts]))
|
2020-07-21 10:38:47 +08:00
|
|
|
sections = list(Account.objects.filter(
|
2020-07-21 08:20:57 +08:00
|
|
|
Q(code="1") | Q(code="2") | Q(code="3")).order_by("code"))
|
|
|
|
for section in sections:
|
|
|
|
section.groups = [x for x in groups
|
|
|
|
if x.code[:1] == section.code]
|
|
|
|
for group in section.groups:
|
|
|
|
group.details = [x for x in accounts
|
|
|
|
if x.code[:2] == group.code]
|
|
|
|
group.balance = sum([x.balance
|
|
|
|
for x in group.details])
|
|
|
|
section.balance = sum([x.balance for x in section.groups])
|
|
|
|
by_code = {x.code: x for x in sections}
|
|
|
|
return render(request, "accounting/balance-sheet.html", {
|
2020-07-21 11:58:50 +08:00
|
|
|
"assets": by_code["1"],
|
|
|
|
"liabilities": by_code["2"],
|
|
|
|
"owners_equity": by_code["3"],
|
2020-07-21 08:20:57 +08:00
|
|
|
"reports": ReportUrl(period=period),
|
|
|
|
"period": period,
|
|
|
|
})
|
2020-07-21 08:26:23 +08:00
|
|
|
|
|
|
|
|
2020-07-21 10:04:29 +08:00
|
|
|
@require_GET
|
2020-07-30 00:12:13 +08:00
|
|
|
@login_required
|
2020-07-21 10:04:29 +08:00
|
|
|
def search(request):
|
|
|
|
"""The search.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest) The request.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponse: The response.
|
|
|
|
"""
|
|
|
|
# The accounting records
|
|
|
|
query = request.GET.get("q")
|
|
|
|
if query is None:
|
|
|
|
records = []
|
|
|
|
else:
|
|
|
|
records = Record.objects.filter(
|
2020-07-21 10:38:47 +08:00
|
|
|
get_multi_lingual_search("account__title", query)
|
|
|
|
| Q(account__code__icontains=query)
|
2020-07-21 10:04:29 +08:00
|
|
|
| Q(summary__icontains=query)
|
|
|
|
| Q(transaction__note__icontains=query))
|
|
|
|
pagination = Pagination(request, records, True)
|
|
|
|
return render(request, "accounting/search.html", {
|
|
|
|
"item_list": pagination.items,
|
|
|
|
"pagination": pagination,
|
2020-07-22 10:27:20 +08:00
|
|
|
"reports": ReportUrl(),
|
2020-07-21 10:04:29 +08:00
|
|
|
})
|
2020-07-23 22:02:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
@require_GET
|
2020-07-30 00:12:13 +08:00
|
|
|
@login_required
|
2020-08-02 03:08:26 +08:00
|
|
|
def txn_show(request, txn_type, txn):
|
2020-07-23 22:02:26 +08:00
|
|
|
"""The view of an accounting transaction.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The request.
|
2020-07-29 15:06:23 +08:00
|
|
|
txn_type (str): The transaction type.
|
2020-08-02 03:00:23 +08:00
|
|
|
txn (Transaction): The transaction.
|
2020-07-23 22:02:26 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponse: The response.
|
|
|
|
"""
|
2020-07-29 15:06:23 +08:00
|
|
|
return render(request, F"accounting/transactions/{txn_type}/view.html", {
|
2020-08-02 03:00:23 +08:00
|
|
|
"item": txn,
|
2020-07-23 22:02:26 +08:00
|
|
|
})
|
2020-07-27 22:36:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
@require_GET
|
2020-07-30 00:12:13 +08:00
|
|
|
@login_required
|
2020-08-02 03:08:26 +08:00
|
|
|
def txn_edit(request, txn_type, txn=None):
|
2020-07-28 03:10:45 +08:00
|
|
|
"""The view to edit an accounting transaction.
|
2020-07-27 22:36:28 +08:00
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The request.
|
2020-07-29 15:06:23 +08:00
|
|
|
txn_type (str): The transaction type.
|
2020-08-02 03:00:23 +08:00
|
|
|
txn (Transaction): The transaction.
|
2020-07-27 22:36:28 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponse: The response.
|
|
|
|
"""
|
2020-08-02 03:06:31 +08:00
|
|
|
form = make_txn_form_from_status(request, txn_type, txn)
|
2020-08-01 23:56:41 +08:00
|
|
|
if form is None:
|
2020-08-02 03:00:23 +08:00
|
|
|
if txn is None:
|
|
|
|
txn = Transaction(date=timezone.localdate())
|
|
|
|
if len(txn.debit_records) == 0:
|
|
|
|
txn.records.append(Record(ord=1, is_credit=False))
|
|
|
|
if len(txn.credit_records) == 0:
|
|
|
|
txn.records.append(Record(ord=1, is_credit=True))
|
2020-08-02 14:29:45 +08:00
|
|
|
form = make_txn_form_from_model(txn_type, txn)
|
2020-07-29 15:06:23 +08:00
|
|
|
return render(request, F"accounting/transactions/{txn_type}/form.html", {
|
2020-08-01 23:56:41 +08:00
|
|
|
"item": form,
|
2020-07-27 22:36:28 +08:00
|
|
|
})
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
@require_POST
|
2020-07-30 00:12:13 +08:00
|
|
|
@login_required
|
2020-08-02 03:08:26 +08:00
|
|
|
def txn_store(request, txn_type, txn=None):
|
2020-07-28 08:22:42 +08:00
|
|
|
"""The view to store an accounting transaction.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The request.
|
2020-07-29 15:06:23 +08:00
|
|
|
txn_type (str): The transaction type.
|
2020-08-02 03:00:23 +08:00
|
|
|
txn (Transaction): The transaction.
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponse: The response.
|
|
|
|
"""
|
2020-08-01 23:56:41 +08:00
|
|
|
post = request.POST.dict()
|
|
|
|
strip_form(post)
|
2020-08-02 03:06:31 +08:00
|
|
|
sort_post_txn_records(post)
|
|
|
|
form = make_txn_form_from_post(post, txn_type, txn)
|
2020-08-01 23:56:41 +08:00
|
|
|
if not form.is_valid():
|
2020-08-02 03:00:23 +08:00
|
|
|
if txn is None:
|
2020-07-29 15:06:23 +08:00
|
|
|
url = reverse("accounting:transactions.create", args=(txn_type,))
|
2020-07-29 15:04:31 +08:00
|
|
|
else:
|
2020-08-02 10:01:17 +08:00
|
|
|
url = reverse("accounting:transactions.edit", args=(txn_type, txn))
|
|
|
|
url = str(UrlBuilder(url).set("r", request.GET.get("r")))
|
|
|
|
return error_redirect(request, url, post)
|
2020-08-02 03:00:23 +08:00
|
|
|
if txn is None:
|
|
|
|
txn = Transaction()
|
2020-08-02 14:29:45 +08:00
|
|
|
fill_txn_from_post(txn_type, txn, post)
|
2020-08-02 10:01:17 +08:00
|
|
|
if not txn.is_dirty():
|
|
|
|
url = reverse("accounting:transactions.show", args=(txn_type, txn))
|
|
|
|
url = str(UrlBuilder(url).set("r", request.GET.get("r")))
|
|
|
|
message = gettext_noop("This transaction was not modified.")
|
|
|
|
return success_redirect(request, url, message)
|
2020-08-02 11:01:36 +08:00
|
|
|
with transaction.atomic():
|
|
|
|
user = request.user
|
|
|
|
if txn.pk is None:
|
|
|
|
max_ord = Transaction.objects\
|
|
|
|
.filter(date=txn.date)\
|
|
|
|
.annotate(max=Max("ord"))\
|
|
|
|
.first()
|
|
|
|
txn.pk = new_pk(Transaction)
|
|
|
|
txn.ord = 1 if max_ord is None else max_ord.max + 1
|
|
|
|
txn.created_at = Now()
|
|
|
|
txn.created_by = user
|
|
|
|
txn.updated_at = Now()
|
|
|
|
txn.updated_by = user
|
|
|
|
txn.save()
|
|
|
|
existing = [x.pk for x in txn.records if x.pk is not None]
|
|
|
|
for record in [x for x in txn.record_set.all()
|
|
|
|
if x.pk not in existing]:
|
|
|
|
record.delete()
|
|
|
|
for record in txn.records:
|
|
|
|
if record.pk is None:
|
|
|
|
record.pk = new_pk(Record)
|
|
|
|
record.created_at = Now()
|
|
|
|
record.created_by = user
|
|
|
|
record.updated_at = Now()
|
|
|
|
record.updated_by = user
|
|
|
|
record.save()
|
2020-08-02 10:01:17 +08:00
|
|
|
url = reverse("accounting:transactions.show", args=(txn_type, txn))
|
|
|
|
url = str(UrlBuilder(url).set("r", request.GET.get("r")))
|
|
|
|
message = gettext_noop("This transaction was saved successfully.")
|
|
|
|
return success_redirect(request, url, message)
|