2020-08-01 23:56:41 +08:00
|
|
|
# The core application of the Mia project.
|
|
|
|
# by imacat <imacat@mail.imacat.idv.tw>, 2020/7/31
|
|
|
|
|
|
|
|
# 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 forms of the Mia core application.
|
|
|
|
|
|
|
|
"""
|
2020-08-16 22:38:35 +08:00
|
|
|
import datetime
|
2020-08-01 23:56:41 +08:00
|
|
|
import re
|
2020-08-21 10:00:59 +08:00
|
|
|
from decimal import Decimal
|
2020-08-16 22:38:35 +08:00
|
|
|
from typing import Optional, List, Dict
|
2020-08-01 23:56:41 +08:00
|
|
|
|
|
|
|
from django import forms
|
2020-08-19 13:43:27 +08:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2020-08-09 11:52:12 +08:00
|
|
|
from django.core.validators import RegexValidator
|
2020-08-19 13:38:05 +08:00
|
|
|
from django.db.models import Q, Max, Model
|
2020-08-09 16:22:51 +08:00
|
|
|
from django.db.models.functions import Length
|
2020-08-06 00:41:29 +08:00
|
|
|
from django.utils.translation import gettext as _
|
2020-08-01 23:56:41 +08:00
|
|
|
|
2020-08-16 22:38:35 +08:00
|
|
|
from .models import Account, Record, Transaction
|
2020-08-09 21:03:37 +08:00
|
|
|
from .validators import validate_record_account_code, validate_record_id
|
2020-08-01 23:56:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RecordForm(forms.Form):
|
|
|
|
"""An accounting record form.
|
|
|
|
|
|
|
|
Attributes:
|
2020-08-12 20:42:31 +08:00
|
|
|
txn_form (TransactionForm): The parent transaction form.
|
2020-08-01 23:56:41 +08:00
|
|
|
is_credit (bool): Whether this is a credit record.
|
|
|
|
"""
|
|
|
|
id = forms.IntegerField(
|
|
|
|
required=False,
|
|
|
|
error_messages={
|
2020-08-08 16:26:08 +08:00
|
|
|
"invalid": _("This accounting record is not valid."),
|
2020-08-01 23:56:41 +08:00
|
|
|
},
|
|
|
|
validators=[validate_record_id])
|
|
|
|
account = forms.CharField(
|
|
|
|
error_messages={
|
2020-08-06 00:41:29 +08:00
|
|
|
"required": _("Please select the account."),
|
2020-08-01 23:56:41 +08:00
|
|
|
},
|
|
|
|
validators=[validate_record_account_code])
|
|
|
|
summary = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
max_length=128,
|
|
|
|
error_messages={
|
2020-08-06 01:47:01 +08:00
|
|
|
"max_length": _("This summary is too long (max. 128 characters)."),
|
2020-08-01 23:56:41 +08:00
|
|
|
})
|
2020-08-21 10:00:59 +08:00
|
|
|
amount = forms.DecimalField(
|
|
|
|
max_digits=18,
|
|
|
|
decimal_places=2,
|
|
|
|
min_value=0.01,
|
2020-08-01 23:56:41 +08:00
|
|
|
error_messages={
|
2020-08-06 00:41:29 +08:00
|
|
|
"required": _("Please fill in the amount."),
|
|
|
|
"invalid": _("Please fill in a number."),
|
2020-08-21 10:00:59 +08:00
|
|
|
"min_value": _("The amount must be more than 0."),
|
2020-08-01 23:56:41 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2020-08-12 20:42:31 +08:00
|
|
|
self.txn_form = None
|
2020-08-01 23:56:41 +08:00
|
|
|
self.is_credit = None
|
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def account_title(self) -> Optional[str]:
|
2020-08-01 23:56:41 +08:00
|
|
|
"""Returns the title of the specified account, if any.
|
|
|
|
|
|
|
|
Returns:
|
2020-08-13 07:25:35 +08:00
|
|
|
The title of the specified account, or None if the specified
|
|
|
|
account is not available.
|
2020-08-01 23:56:41 +08:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return Account.objects.get(code=self["account"].value()).title
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
except Account.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
"""Validates the form globally.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-01 23:56:41 +08:00
|
|
|
"""
|
|
|
|
errors = []
|
2020-08-08 10:57:32 +08:00
|
|
|
if "id" in self.errors:
|
|
|
|
errors = errors + self.errors["id"].as_data()
|
2020-08-02 12:01:20 +08:00
|
|
|
validators = [self._validate_transaction,
|
|
|
|
self._validate_account_type,
|
|
|
|
self._validate_is_credit]
|
2020-08-01 23:56:41 +08:00
|
|
|
for validator in validators:
|
|
|
|
try:
|
|
|
|
validator()
|
|
|
|
except forms.ValidationError as e:
|
|
|
|
errors.append(e)
|
|
|
|
if errors:
|
|
|
|
raise forms.ValidationError(errors)
|
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def _validate_transaction(self) -> None:
|
2020-08-01 23:56:41 +08:00
|
|
|
"""Validates whether the transaction matches the transaction form.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-01 23:56:41 +08:00
|
|
|
"""
|
|
|
|
if "id" in self.errors:
|
|
|
|
return
|
2020-08-12 20:42:31 +08:00
|
|
|
if self.txn_form.transaction is None:
|
2020-08-01 23:56:41 +08:00
|
|
|
if "id" in self.data:
|
2020-08-08 10:57:32 +08:00
|
|
|
raise forms.ValidationError(
|
2020-08-06 00:41:29 +08:00
|
|
|
_("This record is not for this transaction."),
|
2020-08-01 23:56:41 +08:00
|
|
|
code="not_belong")
|
|
|
|
else:
|
|
|
|
if "id" in self.data:
|
|
|
|
record = Record.objects.get(pk=self.data["id"])
|
2020-08-12 20:42:31 +08:00
|
|
|
if record.transaction.pk != self.txn_form.transaction.pk:
|
2020-08-08 10:57:32 +08:00
|
|
|
raise forms.ValidationError(
|
2020-08-06 00:41:29 +08:00
|
|
|
_("This record is not for this transaction."),
|
2020-08-01 23:56:41 +08:00
|
|
|
code="not_belong")
|
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def _validate_account_type(self) -> None:
|
2020-08-01 23:56:41 +08:00
|
|
|
"""Validates whether the account is a correct debit or credit account.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-01 23:56:41 +08:00
|
|
|
"""
|
|
|
|
if "account" in self.errors:
|
|
|
|
return
|
|
|
|
if self.is_credit:
|
|
|
|
if not re.match("^([123489]|7[1234])", self.data["account"]):
|
|
|
|
error = forms.ValidationError(
|
2020-08-06 00:41:29 +08:00
|
|
|
_("This account is not for credit records."),
|
2020-08-01 23:56:41 +08:00
|
|
|
code="not_credit")
|
|
|
|
self.add_error("account", error)
|
|
|
|
raise error
|
|
|
|
else:
|
|
|
|
if not re.match("^([1235689]|7[5678])", self.data["account"]):
|
|
|
|
error = forms.ValidationError(
|
2020-08-06 00:41:29 +08:00
|
|
|
_("This account is not for debit records."),
|
2020-08-01 23:56:41 +08:00
|
|
|
code="not_debit")
|
|
|
|
self.add_error("account", error)
|
|
|
|
raise error
|
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def _validate_is_credit(self) -> None:
|
2020-08-02 12:01:20 +08:00
|
|
|
"""Validates whether debit and credit records are submitted correctly
|
|
|
|
as corresponding debit and credit records.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-02 12:01:20 +08:00
|
|
|
"""
|
|
|
|
if "id" in self.errors:
|
|
|
|
return
|
|
|
|
if "id" not in self.data:
|
|
|
|
return
|
|
|
|
record = Record.objects.get(pk=self.data["id"])
|
|
|
|
if record.is_credit != self.is_credit:
|
|
|
|
if self.is_credit:
|
2020-08-08 10:57:32 +08:00
|
|
|
raise forms.ValidationError(
|
2020-08-08 16:26:08 +08:00
|
|
|
_("This accounting record is not a credit record."),
|
2020-08-02 12:01:20 +08:00
|
|
|
code="not_credit")
|
|
|
|
else:
|
2020-08-08 10:57:32 +08:00
|
|
|
raise forms.ValidationError(
|
2020-08-08 16:26:08 +08:00
|
|
|
_("This accounting record is not a debit record."),
|
2020-08-02 12:01:20 +08:00
|
|
|
code="not_debit")
|
|
|
|
|
2020-08-01 23:56:41 +08:00
|
|
|
|
|
|
|
class TransactionForm(forms.Form):
|
|
|
|
"""A transaction form.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
txn_type (str): The transaction type.
|
|
|
|
transaction (Transaction|None): The current transaction or None
|
|
|
|
debit_records (list[RecordForm]): The debit records.
|
|
|
|
credit_records (list[RecordForm]): The credit records.
|
|
|
|
"""
|
|
|
|
date = forms.DateField(
|
|
|
|
required=True,
|
|
|
|
error_messages={
|
2020-08-06 01:47:01 +08:00
|
|
|
"required": _("Please fill in the date."),
|
2020-08-06 00:41:29 +08:00
|
|
|
"invalid": _("This date is not valid.")
|
2020-08-01 23:56:41 +08:00
|
|
|
})
|
|
|
|
notes = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
max_length=128,
|
|
|
|
error_messages={
|
2020-08-06 01:47:01 +08:00
|
|
|
"max_length": _("These notes are too long (max. 128 characters).")
|
2020-08-01 23:56:41 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2020-08-12 20:42:31 +08:00
|
|
|
# Populates the belonging record forms
|
2020-08-01 23:56:41 +08:00
|
|
|
self.debit_records = []
|
|
|
|
self.credit_records = []
|
2020-08-12 20:42:31 +08:00
|
|
|
if len(args) > 0 and isinstance(args[0], dict):
|
|
|
|
by_rec_id = {}
|
|
|
|
for key in args[0].keys():
|
|
|
|
m = re.match(
|
|
|
|
("^((debit|credit)-([1-9][0-9]*))-"
|
|
|
|
"(id|ord|account|summary|amount)$"),
|
|
|
|
key)
|
|
|
|
if m is None:
|
|
|
|
continue
|
|
|
|
rec_id = m.group(1)
|
|
|
|
column = m.group(4)
|
|
|
|
if rec_id not in by_rec_id:
|
|
|
|
by_rec_id[rec_id] = {
|
|
|
|
"is_credit": m.group(2) == "credit",
|
|
|
|
"no": int(m.group(3)),
|
|
|
|
"data": {},
|
|
|
|
}
|
|
|
|
by_rec_id[rec_id]["data"][column] = args[0][key]
|
|
|
|
debit_data_list = [x for x in by_rec_id.values()
|
|
|
|
if not x["is_credit"]]
|
|
|
|
debit_data_list.sort(key=lambda x: x["no"])
|
|
|
|
for x in debit_data_list:
|
|
|
|
record_form = RecordForm(x["data"])
|
|
|
|
record_form.txn_form = self
|
|
|
|
record_form.is_credit = False
|
|
|
|
self.debit_records.append(record_form)
|
|
|
|
credit_data_list = [x for x in by_rec_id.values()
|
|
|
|
if x["is_credit"]]
|
|
|
|
credit_data_list.sort(key=lambda x: x["no"])
|
|
|
|
for x in credit_data_list:
|
|
|
|
record_form = RecordForm(x["data"])
|
|
|
|
record_form.txn_form = self
|
|
|
|
record_form.is_credit = True
|
|
|
|
self.credit_records.append(record_form)
|
|
|
|
self.txn_type = None
|
|
|
|
self.transaction = None
|
2020-08-01 23:56:41 +08:00
|
|
|
|
2020-08-19 13:38:05 +08:00
|
|
|
@staticmethod
|
|
|
|
def from_post(post: Dict[str, str], txn_type: str, txn: Model):
|
2020-08-19 13:46:46 +08:00
|
|
|
"""Constructs a transaction form from the POST data.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
post: The post data.
|
|
|
|
txn_type: The transaction type.
|
|
|
|
txn: The transaction data model.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The transaction form.
|
|
|
|
"""
|
2020-08-19 13:38:05 +08:00
|
|
|
TransactionForm._sort_post_txn_records(post)
|
|
|
|
form = TransactionForm(post)
|
|
|
|
form.txn_type = txn_type
|
|
|
|
form.transaction = txn
|
|
|
|
return form
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _sort_post_txn_records(post: Dict[str, str]) -> None:
|
|
|
|
"""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:
|
|
|
|
post: The POSTed form.
|
|
|
|
"""
|
|
|
|
# Collects the available record numbers
|
|
|
|
record_no = {
|
|
|
|
"debit": [],
|
|
|
|
"credit": [],
|
|
|
|
}
|
|
|
|
for key in post.keys():
|
|
|
|
m = re.match(
|
|
|
|
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)",
|
|
|
|
key)
|
|
|
|
if m is None:
|
|
|
|
continue
|
|
|
|
record_type = m.group(1)
|
|
|
|
no = int(m.group(2))
|
|
|
|
if no not in record_no[record_type]:
|
|
|
|
record_no[record_type].append(no)
|
|
|
|
# Sorts these record numbers by their specified orders
|
|
|
|
for record_type in record_no.keys():
|
|
|
|
orders = {}
|
|
|
|
for no in record_no[record_type]:
|
|
|
|
try:
|
|
|
|
orders[no] = int(post[F"{record_type}-{no}-ord"])
|
|
|
|
except KeyError:
|
|
|
|
orders[no] = 9999
|
|
|
|
except ValueError:
|
|
|
|
orders[no] = 9999
|
|
|
|
record_no[record_type].sort(key=lambda n: orders[n])
|
|
|
|
# Constructs the sorted new form
|
|
|
|
new_post = {}
|
|
|
|
for record_type in record_no.keys():
|
|
|
|
for i in range(len(record_no[record_type])):
|
|
|
|
old_no = record_no[record_type][i]
|
|
|
|
no = i + 1
|
|
|
|
new_post[F"{record_type}-{no}-ord"] = str(no)
|
|
|
|
for attr in ["id", "account", "summary", "amount"]:
|
|
|
|
if F"{record_type}-{old_no}-{attr}" in post:
|
|
|
|
new_post[F"{record_type}-{no}-{attr}"] \
|
|
|
|
= post[F"{record_type}-{old_no}-{attr}"]
|
|
|
|
# Purges the old form and fills it with the new form
|
|
|
|
for x in [x for x in post.keys() if re.match(
|
|
|
|
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)",
|
|
|
|
x)]:
|
|
|
|
del post[x]
|
|
|
|
for key in new_post.keys():
|
|
|
|
post[key] = new_post[key]
|
|
|
|
|
2020-08-19 13:43:27 +08:00
|
|
|
@staticmethod
|
|
|
|
def from_model(txn: Transaction, txn_type: str):
|
2020-08-19 13:47:33 +08:00
|
|
|
"""Constructs a transaction form from the transaction data model.
|
2020-08-19 13:43:27 +08:00
|
|
|
|
|
|
|
Args:
|
|
|
|
txn: The transaction data model.
|
2020-08-19 13:47:33 +08:00
|
|
|
txn_type: The transaction type.
|
2020-08-19 13:43:27 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
The transaction form.
|
|
|
|
"""
|
|
|
|
form = TransactionForm(
|
|
|
|
{x: str(getattr(txn, x)) for x in ["date", "notes"]
|
|
|
|
if getattr(txn, x) is not None})
|
|
|
|
form.transaction = txn if txn.pk is not None else None
|
|
|
|
form.txn_type = txn_type
|
|
|
|
records = []
|
|
|
|
if txn_type != "income":
|
|
|
|
records = records + txn.debit_records
|
|
|
|
if txn_type != "expense":
|
|
|
|
records = records + txn.credit_records
|
|
|
|
for record in records:
|
|
|
|
data = {x: getattr(record, x)
|
|
|
|
for x in ["summary", "amount"]
|
|
|
|
if getattr(record, x) is not None}
|
|
|
|
if record.pk is not None:
|
|
|
|
data["id"] = record.pk
|
|
|
|
try:
|
|
|
|
data["account"] = record.account.code
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
pass
|
|
|
|
record_form = RecordForm(data)
|
|
|
|
record_form.txn_form = form
|
|
|
|
record_form.is_credit = record.is_credit
|
|
|
|
if record.is_credit:
|
|
|
|
form.credit_records.append(record_form)
|
|
|
|
else:
|
|
|
|
form.debit_records.append(record_form)
|
|
|
|
return form
|
|
|
|
|
2020-08-01 23:56:41 +08:00
|
|
|
def clean(self):
|
|
|
|
"""Validates the form globally.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-01 23:56:41 +08:00
|
|
|
"""
|
2020-08-08 11:55:25 +08:00
|
|
|
errors = []
|
|
|
|
validators = [self._validate_has_debit_records,
|
|
|
|
self._validate_has_credit_records,
|
|
|
|
self._validate_balance]
|
|
|
|
for validator in validators:
|
|
|
|
try:
|
|
|
|
validator()
|
|
|
|
except forms.ValidationError as e:
|
|
|
|
errors.append(e)
|
|
|
|
if errors:
|
|
|
|
raise forms.ValidationError(errors)
|
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def _validate_has_debit_records(self) -> None:
|
2020-08-08 11:55:25 +08:00
|
|
|
"""Validates whether there is any debit record.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-08 11:55:25 +08:00
|
|
|
"""
|
|
|
|
if self.txn_type == "income":
|
|
|
|
return
|
|
|
|
if len(self.debit_records) > 0:
|
|
|
|
return
|
|
|
|
if self.txn_type == "transfer":
|
|
|
|
raise forms.ValidationError(
|
2020-08-08 16:26:08 +08:00
|
|
|
_("Please fill in debit accounting records."),
|
2020-08-08 11:55:25 +08:00
|
|
|
code="has_debit_records")
|
|
|
|
raise forms.ValidationError(
|
|
|
|
_("Please fill in accounting records."),
|
|
|
|
code="has_debit_records")
|
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def _validate_has_credit_records(self) -> None:
|
2020-08-08 11:55:25 +08:00
|
|
|
"""Validates whether there is any credit record.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-08 11:55:25 +08:00
|
|
|
"""
|
|
|
|
if self.txn_type == "expense":
|
|
|
|
return
|
|
|
|
if len(self.credit_records) > 0:
|
|
|
|
return
|
|
|
|
if self.txn_type == "transfer":
|
|
|
|
raise forms.ValidationError(
|
2020-08-08 16:26:08 +08:00
|
|
|
_("Please fill in credit accounting records."),
|
2020-08-08 11:55:25 +08:00
|
|
|
code="has_debit_records")
|
|
|
|
raise forms.ValidationError(
|
|
|
|
_("Please fill in accounting records."),
|
|
|
|
code="has_debit_records")
|
2020-08-01 23:56:41 +08:00
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def _validate_balance(self) -> None:
|
2020-08-01 23:56:41 +08:00
|
|
|
"""Validates whether the total amount of debit and credit records are
|
|
|
|
consistent.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-01 23:56:41 +08:00
|
|
|
"""
|
|
|
|
if self.txn_type != "transfer":
|
|
|
|
return
|
|
|
|
if self.debit_total() == self.credit_total():
|
|
|
|
return
|
2020-08-06 00:41:29 +08:00
|
|
|
raise forms.ValidationError(
|
2020-08-08 16:26:08 +08:00
|
|
|
_("The total of the debit and credit amounts are inconsistent."),
|
2020-08-01 23:56:41 +08:00
|
|
|
code="balance")
|
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def is_valid(self) -> bool:
|
|
|
|
if not super().is_valid():
|
2020-08-01 23:56:41 +08:00
|
|
|
return False
|
|
|
|
for x in self.debit_records + self.credit_records:
|
|
|
|
if not x.is_valid():
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def balance_error(self) -> Optional[str]:
|
2020-08-01 23:56:41 +08:00
|
|
|
"""Returns the error message when the transaction is imbalanced.
|
|
|
|
|
|
|
|
Returns:
|
2020-08-13 07:25:35 +08:00
|
|
|
The error message when the transaction is imbalanced, or None
|
|
|
|
otherwise.
|
2020-08-01 23:56:41 +08:00
|
|
|
"""
|
|
|
|
errors = [x for x in self.non_field_errors().data
|
|
|
|
if x.code == "balance"]
|
|
|
|
if errors:
|
|
|
|
return errors[0].message
|
|
|
|
return None
|
|
|
|
|
2020-08-21 10:00:59 +08:00
|
|
|
def debit_total(self) -> Decimal:
|
2020-08-01 23:56:41 +08:00
|
|
|
"""Returns the total amount of the debit records.
|
|
|
|
|
|
|
|
Returns:
|
2020-08-13 07:25:35 +08:00
|
|
|
The total amount of the credit records.
|
2020-08-01 23:56:41 +08:00
|
|
|
"""
|
2020-08-21 10:00:59 +08:00
|
|
|
return sum([Decimal(x.data["amount"]) for x in self.debit_records
|
2020-08-12 11:59:50 +08:00
|
|
|
if "amount" in x.data and "amount" not in x.errors])
|
2020-08-01 23:56:41 +08:00
|
|
|
|
2020-08-21 10:00:59 +08:00
|
|
|
def credit_total(self) -> Decimal:
|
2020-08-01 23:56:41 +08:00
|
|
|
"""Returns the total amount of the credit records.
|
|
|
|
|
|
|
|
Returns:
|
2020-08-13 07:25:35 +08:00
|
|
|
The total amount of the credit records.
|
2020-08-01 23:56:41 +08:00
|
|
|
"""
|
2020-08-21 10:00:59 +08:00
|
|
|
return sum([Decimal(x.data["amount"]) for x in self.credit_records
|
2020-08-12 11:59:50 +08:00
|
|
|
if "amount" in x.data and "amount" not in x.errors])
|
2020-08-09 11:52:12 +08:00
|
|
|
|
|
|
|
|
2020-08-16 22:38:35 +08:00
|
|
|
class TransactionSortForm(forms.Form):
|
|
|
|
"""A form to sort the transactions in a same day."""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.date = None
|
|
|
|
self.txn_list: Optional[List[Transaction]] = None
|
|
|
|
self.txn_orders: List[TransactionSortForm.Order] = []
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_post(date: datetime.date, post: Dict[str, str]):
|
|
|
|
form = TransactionSortForm({})
|
|
|
|
form.date = date
|
|
|
|
post_orders: List[TransactionSortForm.Order] = []
|
|
|
|
for txn in Transaction.objects.filter(date=date).all():
|
|
|
|
key = F"transaction-{txn.pk}-ord"
|
|
|
|
if key not in post:
|
|
|
|
post_orders.append(form.Order(txn, 9999))
|
|
|
|
elif not re.match("^[0-9]+$", post[key]):
|
|
|
|
post_orders.append(form.Order(txn, 9999))
|
|
|
|
else:
|
|
|
|
post_orders.append(form.Order(txn, int(post[key])))
|
|
|
|
post_orders.sort(key=lambda x: (x.order, x.txn.ord))
|
|
|
|
form.txn_orders = []
|
|
|
|
for i in range(len(post_orders)):
|
|
|
|
form.txn_orders.append(form.Order(post_orders[i].txn, i + 1))
|
|
|
|
form.txn_list = [x.txn for x in form.txn_orders]
|
|
|
|
return form
|
|
|
|
|
|
|
|
class Order:
|
|
|
|
"""A transaction order"""
|
|
|
|
def __init__(self, txn: Transaction, order: int):
|
|
|
|
self.txn = txn
|
|
|
|
self.order = order
|
|
|
|
|
|
|
|
|
2020-08-09 11:52:12 +08:00
|
|
|
class AccountForm(forms.Form):
|
|
|
|
"""An account form."""
|
|
|
|
code = forms.CharField(
|
|
|
|
error_messages={
|
|
|
|
"required": _("Please fill in the code."),
|
|
|
|
"invalid": _("Please fill in a number."),
|
|
|
|
"max_length": _("This code is too long (max. 5)."),
|
|
|
|
"min_value": _("This code is too long (max. 5)."),
|
|
|
|
}, validators=[
|
|
|
|
RegexValidator(
|
|
|
|
regex="^[1-9]+$",
|
2020-08-09 12:17:07 +08:00
|
|
|
message=_("You can only use numbers 1-9 in the code.")),
|
2020-08-09 11:52:12 +08:00
|
|
|
])
|
|
|
|
title = forms.CharField(
|
|
|
|
max_length=128,
|
|
|
|
error_messages={
|
|
|
|
"required": _("Please fill in the title."),
|
|
|
|
"max_length": _("This title is too long (max. 128)."),
|
|
|
|
})
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2020-08-12 15:39:07 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2020-08-09 16:22:51 +08:00
|
|
|
self.account = None
|
2020-08-09 11:52:12 +08:00
|
|
|
|
|
|
|
@property
|
2020-08-13 07:25:35 +08:00
|
|
|
def parent(self) -> Optional[Account]:
|
|
|
|
"""The parent account, or None if this is the topmost account."""
|
2020-08-09 11:52:12 +08:00
|
|
|
code = self["code"].value()
|
|
|
|
if code is None or len(code) < 2:
|
|
|
|
return None
|
|
|
|
return Account.objects.get(code=code[:-1])
|
2020-08-09 16:22:51 +08:00
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
"""Validates the form globally.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-09 16:22:51 +08:00
|
|
|
"""
|
|
|
|
errors = []
|
|
|
|
validators = [self._validate_code_not_under_myself,
|
|
|
|
self._validate_code_unique,
|
|
|
|
self._validate_code_parent_exists,
|
|
|
|
self._validate_code_descendant_code_size]
|
|
|
|
for validator in validators:
|
|
|
|
try:
|
|
|
|
validator()
|
|
|
|
except forms.ValidationError as e:
|
|
|
|
errors.append(e)
|
|
|
|
if errors:
|
|
|
|
raise forms.ValidationError(errors)
|
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def _validate_code_not_under_myself(self) -> None:
|
2020-08-09 16:22:51 +08:00
|
|
|
"""Validates whether the code is under itself.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-09 16:22:51 +08:00
|
|
|
"""
|
|
|
|
if self.account is None:
|
|
|
|
return
|
|
|
|
if "code" not in self.data:
|
|
|
|
return
|
|
|
|
if self.data["code"] == self.account.code:
|
|
|
|
return
|
|
|
|
if not self.data["code"].startswith(self.account.code):
|
|
|
|
return
|
|
|
|
error = forms.ValidationError(
|
|
|
|
_("You cannot set the code under itself."),
|
|
|
|
code="not_under_myself")
|
|
|
|
self.add_error("code", error)
|
|
|
|
raise error
|
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def _validate_code_unique(self) -> None:
|
2020-08-09 16:22:51 +08:00
|
|
|
"""Validates whether the code is unique.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-09 16:22:51 +08:00
|
|
|
"""
|
|
|
|
if "code" not in self.data:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
if self.account is None:
|
|
|
|
Account.objects.get(code=self.data["code"])
|
|
|
|
else:
|
|
|
|
Account.objects.get(Q(code=self.data["code"]),
|
|
|
|
~Q(pk=self.account.pk))
|
|
|
|
except Account.DoesNotExist:
|
|
|
|
return
|
|
|
|
error = forms.ValidationError(_("This code is already in use."),
|
|
|
|
code="code_unique")
|
|
|
|
self.add_error("code", error)
|
|
|
|
raise error
|
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def _validate_code_parent_exists(self) -> None:
|
2020-08-09 16:22:51 +08:00
|
|
|
"""Validates whether the parent account exists.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-09 16:22:51 +08:00
|
|
|
"""
|
|
|
|
if "code" not in self.data:
|
|
|
|
return
|
|
|
|
if len(self.data["code"]) < 2:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
Account.objects.get(code=self.data["code"][:-1])
|
|
|
|
except Account.DoesNotExist:
|
|
|
|
error = forms.ValidationError(
|
|
|
|
_("The parent account of this code does not exist."),
|
|
|
|
code="code_unique")
|
|
|
|
self.add_error("code", error)
|
|
|
|
raise error
|
|
|
|
return
|
|
|
|
|
2020-08-13 07:25:35 +08:00
|
|
|
def _validate_code_descendant_code_size(self) -> None:
|
2020-08-09 16:22:51 +08:00
|
|
|
"""Validates whether the codes of the descendants will be too long.
|
|
|
|
|
|
|
|
Raises:
|
2020-08-09 21:20:58 +08:00
|
|
|
forms.ValidationError: When the validation fails.
|
2020-08-09 16:22:51 +08:00
|
|
|
"""
|
|
|
|
if "code" not in self.data:
|
|
|
|
return
|
|
|
|
if self.account is None:
|
|
|
|
return
|
|
|
|
cur_max_len = Account.objects\
|
|
|
|
.filter(Q(code__startswith=self.account.code),
|
|
|
|
~Q(pk=self.account.pk))\
|
|
|
|
.aggregate(max_len=Max(Length("code")))["max_len"]
|
|
|
|
if cur_max_len is None:
|
2020-08-13 07:25:35 +08:00
|
|
|
return
|
2020-08-09 16:22:51 +08:00
|
|
|
new_max_len = cur_max_len - len(self.account.code)\
|
2020-08-13 07:25:35 +08:00
|
|
|
+ len(self.data["code"])
|
2020-08-09 16:22:51 +08:00
|
|
|
if new_max_len <= 5:
|
|
|
|
return
|
|
|
|
error = forms.ValidationError(
|
|
|
|
_("The descendant account codes will be too long (max. 5)."),
|
|
|
|
code="descendant_code_size")
|
|
|
|
self.add_error("code", error)
|
|
|
|
raise error
|