2020-07-14 10:02:16 +08:00
|
|
|
# The accounting application of the Mia project.
|
|
|
|
# by imacat <imacat@mail.imacat.idv.tw>, 2020/7/13
|
|
|
|
|
|
|
|
# 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 utilities of the accounting application.
|
|
|
|
|
|
|
|
"""
|
2020-07-28 22:48:42 +08:00
|
|
|
import re
|
2020-07-14 10:02:16 +08:00
|
|
|
|
2020-07-14 07:41:19 +08:00
|
|
|
from django.conf import settings
|
2020-07-23 09:57:29 +08:00
|
|
|
from django.db.models import Q, Sum, Case, When, F, Count, Max, Min
|
2020-07-14 07:41:19 +08:00
|
|
|
from django.urls import reverse
|
2020-07-23 09:40:21 +08:00
|
|
|
from django.utils import timezone
|
2020-08-01 23:56:41 +08:00
|
|
|
from django.utils.translation import pgettext
|
2020-07-14 07:41:19 +08:00
|
|
|
|
2020-08-01 23:56:41 +08:00
|
|
|
from .forms import TransactionForm, RecordForm
|
|
|
|
from .models import Account, Transaction, Record
|
2020-07-14 07:41:19 +08:00
|
|
|
from mia_core.period import Period
|
2020-07-28 22:48:42 +08:00
|
|
|
from mia_core.status import retrieve_status
|
2020-07-23 23:24:42 +08:00
|
|
|
from mia_core.utils import new_pk
|
2020-07-14 07:41:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ReportUrl:
|
|
|
|
"""The URL of the accounting reports.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
**kwargs: the keyword arguments:
|
|
|
|
period (Period): The currently-specified period.
|
2020-07-21 10:38:47 +08:00
|
|
|
cash (Account): The currently-specified account of the
|
2020-07-14 07:41:19 +08:00
|
|
|
cash account or cash summary.
|
2020-07-21 10:38:47 +08:00
|
|
|
ledger (Account): The currently-specified account of the
|
2020-07-14 07:41:19 +08:00
|
|
|
ledger or leger summary.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
cash (str): The URL of the cash account.
|
|
|
|
cash_summary (str): The URL of the cash summary.
|
|
|
|
ledger (str): The URL of the ledger.
|
|
|
|
ledger_summary (str): The URL of the ledger summary.
|
|
|
|
journal (str): The URL of the journal.
|
|
|
|
trial_balance (str): The URL of the trial balance.
|
|
|
|
income_statement (str): The URL of the income statement.
|
|
|
|
balance_sheet (str): The URL of the balance sheet.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
if "period" in kwargs:
|
|
|
|
self._period = kwargs["period"]
|
|
|
|
else:
|
2020-07-14 07:43:47 +08:00
|
|
|
self._period = Period()
|
2020-07-14 07:41:19 +08:00
|
|
|
if "cash" in kwargs:
|
2020-07-23 08:33:53 +08:00
|
|
|
self._cash = kwargs["cash"]
|
2020-07-14 07:41:19 +08:00
|
|
|
else:
|
2020-07-23 08:33:53 +08:00
|
|
|
self._cash = Account.objects.get(
|
2020-07-21 10:38:47 +08:00
|
|
|
code=settings.ACCOUNTING["DEFAULT_CASH_ACCOUNT"])
|
2020-07-14 07:41:19 +08:00
|
|
|
if "ledger" in kwargs:
|
2020-07-23 08:33:53 +08:00
|
|
|
self._ledger = kwargs["ledger"]
|
2020-07-14 07:41:19 +08:00
|
|
|
else:
|
2020-07-23 08:33:53 +08:00
|
|
|
self._ledger = Account.objects.get(
|
2020-07-21 10:38:47 +08:00
|
|
|
code=settings.ACCOUNTING["DEFAULT_LEDGER_ACCOUNT"])
|
2020-07-14 07:41:19 +08:00
|
|
|
|
|
|
|
def cash(self):
|
|
|
|
return reverse(
|
2020-07-23 08:33:53 +08:00
|
|
|
"accounting:cash", args=(self._cash, self._period))
|
2020-07-14 07:41:19 +08:00
|
|
|
|
|
|
|
def cash_summary(self):
|
2020-07-23 08:33:53 +08:00
|
|
|
return reverse("accounting:cash-summary", args=(self._cash,))
|
2020-07-14 07:41:19 +08:00
|
|
|
|
|
|
|
def ledger(self):
|
|
|
|
return reverse(
|
2020-07-23 08:33:53 +08:00
|
|
|
"accounting:ledger", args=(self._ledger, self._period))
|
2020-07-14 07:41:19 +08:00
|
|
|
|
|
|
|
def ledger_summary(self):
|
2020-07-23 08:33:53 +08:00
|
|
|
return reverse("accounting:ledger-summary", args=(self._ledger,))
|
2020-07-14 07:41:19 +08:00
|
|
|
|
|
|
|
def journal(self):
|
2020-07-23 08:33:53 +08:00
|
|
|
return reverse("accounting:journal", args=(self._period,))
|
2020-07-14 07:41:19 +08:00
|
|
|
|
|
|
|
def trial_balance(self):
|
2020-07-23 08:33:53 +08:00
|
|
|
return reverse("accounting:trial-balance", args=(self._period,))
|
2020-07-14 07:41:19 +08:00
|
|
|
|
|
|
|
def income_statement(self):
|
2020-07-23 08:33:53 +08:00
|
|
|
return reverse("accounting:income-statement", args=(self._period,))
|
2020-07-14 07:41:19 +08:00
|
|
|
|
|
|
|
def balance_sheet(self):
|
2020-07-23 08:33:53 +08:00
|
|
|
return reverse("accounting:balance-sheet", args=(self._period,))
|
2020-07-23 09:40:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Populator:
|
|
|
|
"""The helper to populate the accounting data.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user (User): The user in action.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
user (User): The user in action.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, user):
|
|
|
|
self.user = user
|
|
|
|
|
|
|
|
def add_accounts(self, accounts):
|
|
|
|
"""Adds accounts.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
accounts (tuple[tuple[any]]): Tuples of
|
|
|
|
(code, Traditional Chinese, English, Simplified Chinese)
|
|
|
|
of the accounts.
|
|
|
|
"""
|
|
|
|
for data in accounts:
|
|
|
|
code = data[0]
|
|
|
|
if isinstance(code, int):
|
|
|
|
code = str(code)
|
|
|
|
parent = None if len(code) == 1\
|
|
|
|
else Account.objects.get(code=code[:-1])
|
2020-07-23 23:24:42 +08:00
|
|
|
Account(pk=new_pk(Account), parent=parent, code=code,
|
2020-07-23 09:40:21 +08:00
|
|
|
title_zh_hant=data[1], title_en=data[2],
|
|
|
|
title_zh_hans=data[3],
|
|
|
|
created_by=self.user, updated_by=self.user).save()
|
|
|
|
|
|
|
|
def add_transfer_transaction(self, date, debit, credit):
|
|
|
|
"""Adds a transfer transaction.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
date (datetime.date|int): The date, or the number of days from
|
|
|
|
today.
|
|
|
|
debit (tuple[tuple[any]]): Tuples of (account, summary, amount)
|
|
|
|
of the debit records.
|
|
|
|
credit (tuple[tuple[any]]): Tuples of (account, summary, amount)
|
|
|
|
of the credit records.
|
|
|
|
"""
|
|
|
|
if isinstance(date, int):
|
|
|
|
date = timezone.localdate() + timezone.timedelta(days=date)
|
|
|
|
order = Transaction.objects.filter(date=date).count() + 1
|
2020-07-23 23:24:42 +08:00
|
|
|
transaction = Transaction(pk=new_pk(Transaction), date=date, ord=order,
|
2020-07-23 09:40:21 +08:00
|
|
|
created_by=self.user, updated_by=self.user)
|
|
|
|
transaction.save()
|
|
|
|
order = 1
|
|
|
|
for data in debit:
|
|
|
|
account = data[0]
|
|
|
|
if isinstance(account, str):
|
|
|
|
account = Account.objects.get(code=account)
|
|
|
|
elif isinstance(account, int):
|
|
|
|
account = Account.objects.get(code=str(account))
|
2020-07-23 23:24:42 +08:00
|
|
|
transaction.record_set.create(pk=new_pk(Record), is_credit=False,
|
2020-07-23 09:40:21 +08:00
|
|
|
ord=order, account=account,
|
|
|
|
summary=data[1], amount=data[2],
|
|
|
|
created_by=self.user,
|
|
|
|
updated_by=self.user)
|
|
|
|
order = order + 1
|
|
|
|
order = 1
|
|
|
|
for data in credit:
|
|
|
|
account = data[0]
|
|
|
|
if isinstance(account, str):
|
|
|
|
account = Account.objects.get(code=account)
|
|
|
|
elif isinstance(account, int):
|
|
|
|
account = Account.objects.get(code=str(account))
|
2020-07-23 23:24:42 +08:00
|
|
|
transaction.record_set.create(pk=new_pk(Record), is_credit=True,
|
2020-07-23 09:40:21 +08:00
|
|
|
ord=order, account=account,
|
|
|
|
summary=data[1], amount=data[2],
|
|
|
|
created_by=self.user,
|
|
|
|
updated_by=self.user)
|
|
|
|
order = order + 1
|
|
|
|
|
|
|
|
def add_income_transaction(self, date, credit):
|
|
|
|
"""Adds a cash income transaction.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
date (datetime.date|int): The date, or the number of days from
|
|
|
|
today.
|
|
|
|
credit (tuple[tuple[any]]): Tuples of (account, summary, amount) of
|
|
|
|
the credit records.
|
|
|
|
"""
|
|
|
|
amount = sum([x[2] for x in credit])
|
|
|
|
self.add_transfer_transaction(date, (("1111", None, amount),), credit)
|
|
|
|
|
|
|
|
def add_expense_transaction(self, date, debit):
|
|
|
|
"""Adds a cash income transaction.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
date (datetime.date|int): The date, or the number of days from
|
|
|
|
today.
|
|
|
|
debit (tuple[tuple[any]]): Tuples of (account, summary, amount) of
|
|
|
|
the debit records.
|
|
|
|
"""
|
|
|
|
amount = sum([x[2] for x in debit])
|
|
|
|
self.add_transfer_transaction(date, debit, (("1111", None, amount),))
|
2020-07-23 09:57:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_cash_accounts():
|
|
|
|
"""Returns the cash accounts.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[Account]: The cash accounts.
|
|
|
|
"""
|
|
|
|
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"))
|
|
|
|
.order_by("code"))
|
|
|
|
accounts.insert(0, Account(
|
|
|
|
code="0",
|
|
|
|
title=pgettext(
|
|
|
|
"Accounting|", "current assets and liabilities"),
|
|
|
|
))
|
|
|
|
return accounts
|
|
|
|
|
|
|
|
|
|
|
|
def get_ledger_accounts():
|
|
|
|
"""Returns the accounts for the ledger.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[Account]: The accounts for the ledger.
|
|
|
|
"""
|
|
|
|
# TODO: Te be replaced with the Django model queries
|
|
|
|
return list(Account.objects.raw("""SELECT s.*
|
|
|
|
FROM accounting_accounts AS s
|
|
|
|
WHERE s.code IN (SELECT s.code
|
|
|
|
FROM accounting_accounts AS s
|
|
|
|
INNER JOIN (SELECT s.code
|
|
|
|
FROM accounting_accounts AS s
|
|
|
|
INNER JOIN accounting_records AS r ON r.account_sn = s.sn
|
|
|
|
GROUP BY s.code) AS u
|
|
|
|
ON u.code LIKE s.code || '%'
|
|
|
|
GROUP BY s.code)
|
|
|
|
ORDER BY s.code"""))
|
|
|
|
|
|
|
|
|
|
|
|
def find_imbalanced(records):
|
|
|
|
""""Finds the records with imbalanced transactions, and sets their
|
|
|
|
is_balanced attribute.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
records (list[Record]): The accounting records.
|
|
|
|
"""
|
2020-07-23 23:15:33 +08:00
|
|
|
imbalanced = [x.pk for x in Transaction.objects
|
2020-07-23 09:57:29 +08:00
|
|
|
.annotate(
|
|
|
|
balance=Sum(Case(
|
|
|
|
When(record__is_credit=True, then=-1),
|
|
|
|
default=1) * F("record__amount")))
|
|
|
|
.filter(~Q(balance=0))]
|
|
|
|
for record in records:
|
2020-07-23 23:15:33 +08:00
|
|
|
record.is_balanced = record.transaction.pk not in imbalanced
|
2020-07-23 09:57:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
def find_order_holes(records):
|
|
|
|
""""Finds whether the order of the transactions on this day is not
|
|
|
|
1, 2, 3, 4, 5..., and should be reordered, and sets their
|
|
|
|
has_order_holes attributes.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
records (list[Record]): The accounting records.
|
|
|
|
"""
|
|
|
|
holes = [x["date"] for x in Transaction.objects
|
|
|
|
.values("date")
|
|
|
|
.annotate(count=Count("ord"),
|
|
|
|
max=Max("ord"),
|
|
|
|
min=Min("ord"))
|
|
|
|
.filter(~(Q(max=F("count")) & Q(min=1)))] +\
|
|
|
|
[x["date"] for x in Transaction.objects
|
|
|
|
.values("date", "ord")
|
2020-08-01 23:56:41 +08:00
|
|
|
.annotate(count=Count("pk"))
|
2020-07-23 09:57:29 +08:00
|
|
|
.filter(~Q(count=1))]
|
|
|
|
for record in records:
|
|
|
|
record.has_order_hole = record.transaction.date in holes
|
2020-07-28 22:48:42 +08:00
|
|
|
|
|
|
|
|
2020-07-29 00:02:34 +08:00
|
|
|
def fill_transaction_from_form(transaction, form):
|
|
|
|
"""Fills the transaction from the form.
|
2020-07-28 22:48:42 +08:00
|
|
|
|
|
|
|
Args:
|
|
|
|
transaction (Transaction): The transaction.
|
2020-07-29 00:02:34 +08:00
|
|
|
form (dict): The form
|
2020-07-28 22:48:42 +08:00
|
|
|
"""
|
|
|
|
if "date" in form:
|
|
|
|
transaction.date = form["date"]
|
|
|
|
if "notes" in form:
|
|
|
|
transaction.notes = form["notes"]
|
|
|
|
# The records
|
2020-07-29 09:22:56 +08:00
|
|
|
max_no = {
|
|
|
|
"debit": 0,
|
|
|
|
"credit": 0,
|
|
|
|
}
|
2020-07-28 22:48:42 +08:00
|
|
|
for key in form.keys():
|
2020-07-28 22:59:29 +08:00
|
|
|
m = re.match(
|
2020-08-01 23:56:41 +08:00
|
|
|
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)$",
|
2020-07-29 09:22:56 +08:00
|
|
|
key)
|
2020-07-28 22:48:42 +08:00
|
|
|
if m is not None:
|
2020-07-29 09:22:56 +08:00
|
|
|
rec_type = m.group(1)
|
|
|
|
no = int(m.group(2))
|
|
|
|
if max_no[rec_type] < no:
|
|
|
|
max_no[rec_type] = no
|
2020-07-28 22:48:42 +08:00
|
|
|
records = []
|
2020-07-29 09:22:56 +08:00
|
|
|
for rec_type in max_no.keys():
|
|
|
|
for i in range(max_no[rec_type]):
|
|
|
|
no = i + 1
|
|
|
|
record = Record(
|
|
|
|
ord=no,
|
|
|
|
is_credit=(rec_type == "credit"),
|
|
|
|
transaction=transaction)
|
2020-08-01 23:56:41 +08:00
|
|
|
if F"{rec_type}-{no}-id" in form:
|
|
|
|
record.pk = form[F"{rec_type}-{no}-id"]
|
2020-07-29 09:22:56 +08:00
|
|
|
if F"{rec_type}-{no}-account" in form:
|
2020-07-29 14:47:49 +08:00
|
|
|
record.account = Account(code=form[F"{rec_type}-{no}-account"])
|
2020-07-29 09:22:56 +08:00
|
|
|
if F"{rec_type}-{no}-summary" in form:
|
|
|
|
record.summary = form[F"{rec_type}-{no}-summary"]
|
|
|
|
if F"{rec_type}-{no}-amount" in form:
|
|
|
|
record.amount = form[F"{rec_type}-{no}-amount"]
|
|
|
|
records.append(record)
|
2020-07-28 22:48:42 +08:00
|
|
|
transaction.records = records
|
2020-07-29 00:02:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
def sort_form_transaction_records(form):
|
|
|
|
"""Sorts the records in the form by their specified order, so that the
|
|
|
|
form can be used to populate the data to return to the user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
form (dict): The POSTed form.
|
|
|
|
"""
|
|
|
|
# Collects the available record numbers
|
2020-07-29 09:40:51 +08:00
|
|
|
record_no = {
|
2020-07-29 09:37:15 +08:00
|
|
|
"debit": [],
|
|
|
|
"credit": [],
|
|
|
|
}
|
2020-07-29 00:02:34 +08:00
|
|
|
for key in form.keys():
|
|
|
|
m = re.match(
|
2020-08-01 23:56:41 +08:00
|
|
|
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)",
|
2020-07-29 09:37:15 +08:00
|
|
|
key)
|
|
|
|
if m is None:
|
|
|
|
continue
|
2020-07-29 09:40:51 +08:00
|
|
|
record_type = m.group(1)
|
2020-07-29 09:37:15 +08:00
|
|
|
no = int(m.group(2))
|
2020-07-29 09:40:51 +08:00
|
|
|
if no not in record_no[record_type]:
|
|
|
|
record_no[record_type].append(no)
|
2020-07-29 00:02:34 +08:00
|
|
|
# Sorts these record numbers by their specified orders
|
2020-07-29 09:40:51 +08:00
|
|
|
for record_type in record_no.keys():
|
2020-07-29 09:37:15 +08:00
|
|
|
orders = {}
|
2020-07-29 09:40:51 +08:00
|
|
|
for no in record_no[record_type]:
|
2020-07-29 09:37:15 +08:00
|
|
|
try:
|
2020-07-29 09:40:51 +08:00
|
|
|
orders[no] = int(form[F"{record_type}-{no}-ord"])
|
2020-07-29 09:37:15 +08:00
|
|
|
except KeyError:
|
|
|
|
orders[no] = 9999
|
|
|
|
except ValueError:
|
|
|
|
orders[no] = 9999
|
2020-07-29 09:40:51 +08:00
|
|
|
record_no[record_type].sort(key=lambda x: orders[x])
|
2020-07-29 09:37:15 +08:00
|
|
|
# Constructs the sorted new form
|
2020-07-29 00:02:34 +08:00
|
|
|
new_form = {}
|
2020-07-29 09:40:51 +08:00
|
|
|
for record_type in record_no.keys():
|
|
|
|
for i in range(len(record_no[record_type])):
|
|
|
|
old_no = record_no[record_type][i]
|
2020-07-29 09:37:15 +08:00
|
|
|
no = i + 1
|
2020-07-29 09:40:51 +08:00
|
|
|
new_form[F"{record_type}-{no}-ord"] = no
|
2020-08-01 23:56:41 +08:00
|
|
|
for attr in ["id", "account", "summary", "amount"]:
|
2020-07-29 09:40:51 +08:00
|
|
|
if F"{record_type}-{old_no}-{attr}" in form:
|
|
|
|
new_form[F"{record_type}-{no}-{attr}"]\
|
|
|
|
= form[F"{record_type}-{old_no}-{attr}"]
|
2020-07-29 00:02:34 +08:00
|
|
|
# Purges the old form and fills it with the new form
|
2020-07-29 09:38:34 +08:00
|
|
|
for x in [x for x in form.keys() if re.match(
|
2020-08-01 23:56:41 +08:00
|
|
|
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)",
|
2020-07-29 09:38:34 +08:00
|
|
|
x)]:
|
2020-07-29 00:02:34 +08:00
|
|
|
del form[x]
|
|
|
|
for key in new_form.keys():
|
|
|
|
form[key] = new_form[key]
|
2020-07-30 08:04:18 +08:00
|
|
|
|
|
|
|
|
2020-08-01 23:56:41 +08:00
|
|
|
def make_transaction_form_from_model(transaction, exists):
|
|
|
|
"""Converts a transaction data model to a transaction form.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
transaction (Transaction): The transaction data model.
|
|
|
|
exists (bool): Whether the current transaction exists.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
TransactionForm: The transaction form.
|
|
|
|
"""
|
|
|
|
transaction_form = TransactionForm(
|
|
|
|
{x: str(getattr(transaction, x)) for x in ["date", "notes"]
|
|
|
|
if getattr(transaction, x) is not None})
|
|
|
|
transaction_form.transaction = transaction if exists else None
|
|
|
|
for record in transaction.records:
|
|
|
|
data = {x: getattr(record, x)
|
|
|
|
for x in ["summary", "amount"]
|
|
|
|
if getattr(record, x) is not None}
|
|
|
|
data["id"] = record.pk
|
|
|
|
try:
|
|
|
|
data["account"] = record.account.code
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
record_form = RecordForm(data)
|
|
|
|
record_form.transaction = transaction_form.transaction
|
|
|
|
record_form.is_credit = record.is_credit
|
|
|
|
if record.is_credit:
|
|
|
|
transaction_form.credit_records.append(record_form)
|
|
|
|
else:
|
|
|
|
transaction_form.debit_records.append(record_form)
|
|
|
|
return transaction_form
|
|
|
|
|
|
|
|
|
|
|
|
def make_transaction_form_from_post(post, txn_type, transaction):
|
|
|
|
"""Converts the POSTed data to a transaction form.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
post (dict[str]): The POSTed data.
|
|
|
|
txn_type (str): The transaction type.
|
|
|
|
transaction (Transaction|None): The current transaction, or None
|
|
|
|
if there is no current transaction.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
TransactionForm: The transaction form.
|
|
|
|
"""
|
|
|
|
transaction_form = TransactionForm(
|
|
|
|
{x: post[x] for x in ("date", "notes") if x in post})
|
|
|
|
transaction_form.transaction = transaction
|
|
|
|
transaction_form.txn_type = txn_type
|
|
|
|
# The records
|
|
|
|
max_no = {
|
|
|
|
"debit": 0,
|
|
|
|
"credit": 0,
|
|
|
|
}
|
|
|
|
for key in post.keys():
|
|
|
|
m = re.match(
|
|
|
|
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)$",
|
|
|
|
key)
|
|
|
|
if m is not None:
|
|
|
|
rec_type = m.group(1)
|
|
|
|
no = int(m.group(2))
|
|
|
|
if max_no[rec_type] < no:
|
|
|
|
max_no[rec_type] = no
|
|
|
|
if max_no["debit"] == 0:
|
|
|
|
max_no["debit"] = 1
|
|
|
|
if max_no["credit"] == 0:
|
|
|
|
max_no["credit"] = 1
|
|
|
|
for rec_type in max_no.keys():
|
|
|
|
records = []
|
|
|
|
is_credit = (rec_type == "credit")
|
|
|
|
for i in range(max_no[rec_type]):
|
|
|
|
no = i + 1
|
|
|
|
record = RecordForm(
|
|
|
|
{x: post[F"{rec_type}-{no}-{x}"]
|
|
|
|
for x in ["id", "account", "summary", "amount"]
|
|
|
|
if F"{rec_type}-{no}-{x}" in post})
|
|
|
|
record.transaction = transaction_form.transaction
|
|
|
|
record.is_credit = is_credit
|
|
|
|
records.append(record)
|
|
|
|
if rec_type == "debit":
|
|
|
|
transaction_form.debit_records = records
|
|
|
|
else:
|
|
|
|
transaction_form.credit_records = records
|
|
|
|
return transaction_form
|
|
|
|
|
|
|
|
|
|
|
|
def make_transaction_form_from_status(request, txn_type, transaction):
|
|
|
|
"""Converts the previously-stored status to a transaction form.
|
2020-07-30 08:04:18 +08:00
|
|
|
|
|
|
|
Args:
|
2020-08-01 23:56:41 +08:00
|
|
|
request (HttpRequest): The request.
|
|
|
|
txn_type (str): The transaction type.
|
|
|
|
transaction (Transaction|None): The current transaction, or None
|
|
|
|
if there is no current transaction.
|
2020-07-30 08:04:18 +08:00
|
|
|
|
2020-08-01 23:56:41 +08:00
|
|
|
Returns:
|
|
|
|
TransactionForm: The transaction form, or None if there is no
|
|
|
|
previously-stored status.
|
2020-07-30 08:04:18 +08:00
|
|
|
"""
|
2020-08-01 23:56:41 +08:00
|
|
|
status = retrieve_status(request)
|
|
|
|
if status is None:
|
|
|
|
return None
|
|
|
|
if "form" not in status:
|
|
|
|
return
|
|
|
|
return make_transaction_form_from_post(
|
|
|
|
status["form"], txn_type, transaction)
|