2020-06-30 05:41:36 +08:00
|
|
|
# The accounting application of the Mia project.
|
|
|
|
# by imacat <imacat@mail.imacat.idv.tw>, 2020/6/29
|
|
|
|
|
|
|
|
# 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 data models of the accounting application.
|
|
|
|
|
|
|
|
"""
|
2020-08-02 09:53:26 +08:00
|
|
|
from dirtyfields import DirtyFieldsMixin
|
2020-07-06 00:09:26 +08:00
|
|
|
from django.conf import settings
|
2020-06-29 19:41:52 +08:00
|
|
|
from django.db import models
|
2020-07-01 00:43:46 +08:00
|
|
|
from django.urls import reverse
|
2020-06-29 19:41:52 +08:00
|
|
|
|
2020-07-21 21:23:54 +08:00
|
|
|
from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr
|
2020-07-14 22:01:32 +08:00
|
|
|
|
2020-06-30 05:41:36 +08:00
|
|
|
|
2020-08-02 09:53:26 +08:00
|
|
|
class Account(DirtyFieldsMixin, models.Model):
|
2020-07-21 10:38:47 +08:00
|
|
|
"""An account."""
|
2020-07-06 00:09:26 +08:00
|
|
|
sn = models.PositiveIntegerField(primary_key=True)
|
2020-06-30 05:41:36 +08:00
|
|
|
parent = models.ForeignKey(
|
|
|
|
"self", on_delete=models.PROTECT, null=True, blank=True,
|
2020-08-03 00:02:51 +08:00
|
|
|
db_column="parent_sn", related_name="child_set")
|
2020-07-21 10:59:48 +08:00
|
|
|
code = models.CharField(max_length=5, unique=True)
|
2020-07-16 23:13:15 +08:00
|
|
|
title_zh_hant = models.CharField(
|
|
|
|
max_length=32, db_column="title_zhtw")
|
2020-06-30 05:41:36 +08:00
|
|
|
title_en = models.CharField(max_length=128, null=True, blank=True)
|
2020-07-16 23:13:15 +08:00
|
|
|
title_zh_hans = models.CharField(
|
|
|
|
max_length=32, null=True, blank=True, db_column="title_zhcn")
|
2020-06-30 05:41:36 +08:00
|
|
|
created_at = models.DateTimeField(
|
|
|
|
auto_now_add=True, db_column="created")
|
2020-07-06 00:09:26 +08:00
|
|
|
created_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
|
|
db_column="createdby",
|
2020-07-21 10:38:47 +08:00
|
|
|
related_name="created_accounting_accounts")
|
2020-06-30 05:41:36 +08:00
|
|
|
updated_at = models.DateTimeField(
|
|
|
|
auto_now_add=True, db_column="updated")
|
2020-07-06 00:09:26 +08:00
|
|
|
updated_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
|
|
db_column="updatedby",
|
2020-07-21 10:38:47 +08:00
|
|
|
related_name="updated_accounting_accounts")
|
2020-08-02 18:13:04 +08:00
|
|
|
CASH = "1111"
|
2020-08-02 18:20:36 +08:00
|
|
|
ACCUMULATED_BALANCE = "3351"
|
|
|
|
NET_CHANGE = "3353"
|
2020-06-30 05:41:36 +08:00
|
|
|
|
2020-08-02 01:04:47 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Account, self).__init__(*args, **kwargs)
|
|
|
|
self.url = None
|
2020-08-02 19:42:48 +08:00
|
|
|
self.debit_amount = None
|
|
|
|
self.credit_amount = None
|
|
|
|
self.amount = None
|
2020-08-03 00:02:51 +08:00
|
|
|
self.is_for_debit = None
|
|
|
|
self.is_for_credit = None
|
|
|
|
self.is_in_use = None
|
2020-08-02 01:04:47 +08:00
|
|
|
|
2020-06-30 05:41:36 +08:00
|
|
|
def __str__(self):
|
2020-07-21 10:38:47 +08:00
|
|
|
"""Returns the string representation of this account."""
|
2020-07-14 22:01:32 +08:00
|
|
|
return self.code.__str__() + " " + self.title
|
|
|
|
|
2020-08-02 01:04:47 +08:00
|
|
|
class Meta:
|
|
|
|
db_table = "accounting_accounts"
|
|
|
|
|
2020-07-14 22:01:32 +08:00
|
|
|
@property
|
|
|
|
def title(self):
|
2020-07-21 21:23:54 +08:00
|
|
|
return get_multi_lingual_attr(self, "title")
|
2020-07-14 22:01:32 +08:00
|
|
|
|
|
|
|
@title.setter
|
|
|
|
def title(self, value):
|
2020-07-21 21:23:54 +08:00
|
|
|
set_multi_lingual_attr(self, "title", value)
|
2020-06-30 05:41:36 +08:00
|
|
|
|
2020-08-03 00:02:51 +08:00
|
|
|
@property
|
|
|
|
def option_data(self):
|
|
|
|
return {
|
|
|
|
"code": self.code,
|
|
|
|
"title": self.title,
|
|
|
|
}
|
|
|
|
|
2020-06-30 05:41:36 +08:00
|
|
|
|
2020-08-02 09:53:26 +08:00
|
|
|
class Transaction(DirtyFieldsMixin, models.Model):
|
2020-06-30 05:41:36 +08:00
|
|
|
"""An accounting transaction."""
|
2020-07-06 00:09:26 +08:00
|
|
|
sn = models.PositiveIntegerField(primary_key=True)
|
|
|
|
date = models.DateField()
|
2020-06-30 05:41:36 +08:00
|
|
|
ord = models.PositiveSmallIntegerField(default=1)
|
2020-07-23 22:02:26 +08:00
|
|
|
notes = models.CharField(max_length=128, null=True, blank=True)
|
2020-06-30 05:41:36 +08:00
|
|
|
created_at = models.DateTimeField(
|
|
|
|
auto_now_add=True, db_column="created")
|
2020-07-06 00:09:26 +08:00
|
|
|
created_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
|
|
db_column="createdby",
|
|
|
|
related_name="created_accounting_transactions")
|
2020-06-30 05:41:36 +08:00
|
|
|
updated_at = models.DateTimeField(
|
|
|
|
auto_now_add=True, db_column="updated")
|
2020-07-06 00:09:26 +08:00
|
|
|
updated_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
|
|
db_column="updatedby",
|
|
|
|
related_name="updated_accounting_transactions")
|
2020-06-30 05:41:36 +08:00
|
|
|
|
2020-08-02 01:04:47 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Transaction, self).__init__(*args, **kwargs)
|
|
|
|
self._records = None
|
|
|
|
self._is_balanced = None
|
|
|
|
self._has_order_hole = None
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""Returns the string representation of this accounting
|
|
|
|
transaction."""
|
|
|
|
return self.date.__str__() + " #" + self.ord.__str__()
|
|
|
|
|
2020-08-02 10:35:35 +08:00
|
|
|
def get_absolute_url(self):
|
|
|
|
"""Returns the URL to view this transaction."""
|
|
|
|
if self.is_cash_expense:
|
|
|
|
return reverse(
|
|
|
|
"accounting:transactions.show", args=("expense", self))
|
|
|
|
elif self.is_cash_income:
|
|
|
|
return reverse(
|
|
|
|
"accounting:transactions.show", args=("income", self))
|
|
|
|
else:
|
|
|
|
return reverse(
|
|
|
|
"accounting:transactions.show", args=("transfer", self))
|
|
|
|
|
|
|
|
def is_dirty(self, **kwargs):
|
|
|
|
"""Returns whether the data of this transaction is changed and need
|
|
|
|
to be saved into the database.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the data of this transaction is changed and need
|
|
|
|
to be saved into the database, or False otherwise.
|
|
|
|
"""
|
|
|
|
if super(Transaction, self).is_dirty(**kwargs):
|
|
|
|
return True
|
|
|
|
if len([x for x in self.records if x.is_dirty(**kwargs)]) > 0:
|
|
|
|
return True
|
|
|
|
kept = [x.pk for x in self.records]
|
|
|
|
if len([x for x in self.record_set.all() if x.pk not in kept]) > 0:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-08-02 01:04:47 +08:00
|
|
|
class Meta:
|
|
|
|
db_table = "accounting_transactions"
|
2020-07-24 19:42:48 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def records(self):
|
|
|
|
"""The records of the transaction.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[Record]: The records.
|
|
|
|
"""
|
|
|
|
if self._records is None:
|
2020-07-27 22:36:28 +08:00
|
|
|
self._records = list(self.record_set.all())
|
2020-08-02 14:08:38 +08:00
|
|
|
self._records.sort(key=lambda x: (x.is_credit, x.ord))
|
2020-07-24 19:42:48 +08:00
|
|
|
return self._records
|
|
|
|
|
|
|
|
@records.setter
|
|
|
|
def records(self, value):
|
|
|
|
self._records = value
|
|
|
|
|
2020-06-30 20:59:24 +08:00
|
|
|
@property
|
|
|
|
def debit_records(self):
|
2020-07-27 22:36:28 +08:00
|
|
|
"""The debit records of this transaction.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[Record]: The records.
|
|
|
|
"""
|
2020-07-24 19:42:48 +08:00
|
|
|
return [x for x in self.records if not x.is_credit]
|
2020-06-30 20:59:24 +08:00
|
|
|
|
2020-07-23 22:02:26 +08:00
|
|
|
def debit_total(self):
|
|
|
|
"""The total amount of the debit records."""
|
2020-07-27 22:36:28 +08:00
|
|
|
return sum([x.amount for x in self.debit_records
|
|
|
|
if isinstance(x.amount, int)])
|
2020-07-23 22:02:26 +08:00
|
|
|
|
2020-06-30 20:59:24 +08:00
|
|
|
@property
|
|
|
|
def credit_records(self):
|
2020-07-27 22:36:28 +08:00
|
|
|
"""The credit records of this transaction.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[Record]: The records.
|
|
|
|
"""
|
2020-07-24 19:42:48 +08:00
|
|
|
return [x for x in self.records if x.is_credit]
|
2020-06-30 20:59:24 +08:00
|
|
|
|
2020-07-23 22:02:26 +08:00
|
|
|
def credit_total(self):
|
|
|
|
"""The total amount of the credit records."""
|
2020-07-27 22:36:28 +08:00
|
|
|
return sum([x.amount for x in self.credit_records
|
|
|
|
if isinstance(x.amount, int)])
|
2020-07-23 22:02:26 +08:00
|
|
|
|
2020-06-30 20:59:24 +08:00
|
|
|
@property
|
|
|
|
def is_balanced(self):
|
2020-07-02 09:45:11 +08:00
|
|
|
"""Whether the sum of the amounts of the debit records is the
|
|
|
|
same as the sum of the amounts of the credit records. """
|
2020-07-07 07:10:24 +08:00
|
|
|
if self._is_balanced is None:
|
|
|
|
debit_sum = sum([x.amount for x in self.debit_records])
|
|
|
|
credit_sum = sum([x.amount for x in self.credit_records])
|
|
|
|
self._is_balanced = debit_sum == credit_sum
|
|
|
|
return self._is_balanced
|
|
|
|
|
|
|
|
@is_balanced.setter
|
|
|
|
def is_balanced(self, value):
|
|
|
|
self._is_balanced = value
|
|
|
|
|
2020-07-07 07:12:26 +08:00
|
|
|
def has_many_same_day(self):
|
|
|
|
"""whether there are more than one transactions at this day,
|
|
|
|
so that the user can sort their orders. """
|
2020-07-23 22:33:33 +08:00
|
|
|
return Transaction.objects.filter(date=self.date).count() > 1
|
2020-06-30 20:59:24 +08:00
|
|
|
|
2020-07-06 23:58:47 +08:00
|
|
|
@property
|
|
|
|
def has_order_hole(self):
|
|
|
|
"""Whether the order of the transactions on this day is not
|
|
|
|
1, 2, 3, 4, 5..., and should be reordered. """
|
2020-07-07 07:10:24 +08:00
|
|
|
if self._has_order_hole is None:
|
|
|
|
orders = [x.ord for x in Transaction.objects.filter(
|
2020-07-07 19:33:34 +08:00
|
|
|
date=self.date)]
|
2020-07-13 19:54:27 +08:00
|
|
|
if len(orders) == 0:
|
|
|
|
self._has_order_hole = False
|
2020-07-07 19:33:34 +08:00
|
|
|
if max(orders) != len(orders):
|
|
|
|
self._has_order_hole = True
|
|
|
|
elif min(orders) != 1:
|
2020-07-07 07:10:24 +08:00
|
|
|
self._has_order_hole = True
|
|
|
|
elif len(orders) != len(set(orders)):
|
|
|
|
self._has_order_hole = True
|
|
|
|
else:
|
|
|
|
self._has_order_hole = False
|
|
|
|
return self._has_order_hole
|
|
|
|
|
|
|
|
@has_order_hole.setter
|
|
|
|
def has_order_hole(self, value):
|
|
|
|
self._has_order_hole = value
|
2020-07-06 23:58:47 +08:00
|
|
|
|
2020-06-30 20:59:24 +08:00
|
|
|
@property
|
|
|
|
def is_cash_income(self):
|
|
|
|
"""Whether this transaction is a cash income transaction."""
|
|
|
|
debit_records = self.debit_records
|
|
|
|
return (len(debit_records) == 1
|
2020-08-02 18:13:04 +08:00
|
|
|
and debit_records[0].account.code == Account.CASH
|
2020-06-30 20:59:24 +08:00
|
|
|
and debit_records[0].summary is None)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_cash_expense(self):
|
|
|
|
"""Whether this transaction is a cash expense transaction."""
|
|
|
|
credit_records = self.credit_records
|
|
|
|
return (len(credit_records) == 1
|
2020-08-02 18:13:04 +08:00
|
|
|
and credit_records[0].account.code == Account.CASH
|
2020-06-30 20:59:24 +08:00
|
|
|
and credit_records[0].summary is None)
|
|
|
|
|
2020-07-23 22:02:26 +08:00
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
"""The transaction type."""
|
|
|
|
if self.is_cash_expense:
|
|
|
|
return "expense"
|
|
|
|
elif self.is_cash_income:
|
|
|
|
return "income"
|
|
|
|
else:
|
|
|
|
return "transfer"
|
|
|
|
|
2020-06-30 05:41:36 +08:00
|
|
|
|
2020-08-02 09:53:26 +08:00
|
|
|
class Record(DirtyFieldsMixin, models.Model):
|
2020-06-30 05:41:36 +08:00
|
|
|
"""An accounting record."""
|
2020-07-06 00:09:26 +08:00
|
|
|
sn = models.PositiveIntegerField(primary_key=True)
|
2020-06-30 05:41:36 +08:00
|
|
|
transaction = models.ForeignKey(
|
|
|
|
Transaction, on_delete=models.CASCADE,
|
|
|
|
db_column="transaction_sn")
|
|
|
|
is_credit = models.BooleanField()
|
2020-07-28 00:33:02 +08:00
|
|
|
ord = models.PositiveSmallIntegerField()
|
2020-07-21 10:38:47 +08:00
|
|
|
account = models.ForeignKey(
|
2020-07-21 10:43:05 +08:00
|
|
|
Account, on_delete=models.PROTECT, db_column="account_sn")
|
2020-06-30 05:41:36 +08:00
|
|
|
summary = models.CharField(max_length=128, blank=True, null=True)
|
|
|
|
amount = models.PositiveIntegerField()
|
|
|
|
created_at = models.DateTimeField(
|
|
|
|
auto_now_add=True, db_column="created")
|
2020-07-06 00:09:26 +08:00
|
|
|
created_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
|
|
db_column="createdby",
|
|
|
|
related_name="created_accounting_records")
|
2020-06-30 05:41:36 +08:00
|
|
|
updated_at = models.DateTimeField(
|
|
|
|
auto_now_add=True, db_column="updated")
|
2020-07-06 00:09:26 +08:00
|
|
|
updated_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
|
|
db_column="updatedby",
|
|
|
|
related_name="updated_accounting_records")
|
2020-06-30 05:41:36 +08:00
|
|
|
|
2020-08-02 01:04:47 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Record, self).__init__(*args, **kwargs)
|
|
|
|
self._debit_amount = None
|
|
|
|
self._credit_amount = None
|
2020-08-02 18:00:06 +08:00
|
|
|
self.balance = None
|
2020-08-02 01:04:47 +08:00
|
|
|
self._is_balanced = None
|
|
|
|
self._has_order_hole = None
|
|
|
|
self._is_credit_card_paid = None
|
|
|
|
self._is_existing_equipment = None
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""Returns the string representation of this accounting
|
|
|
|
record."""
|
|
|
|
return "%s %s %s %s" % (
|
|
|
|
self.transaction.date,
|
|
|
|
self.account.title,
|
|
|
|
self.summary,
|
|
|
|
self.amount)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
db_table = "accounting_records"
|
2020-07-13 19:54:27 +08:00
|
|
|
|
2020-06-30 20:59:24 +08:00
|
|
|
@property
|
|
|
|
def debit_amount(self):
|
2020-08-02 17:57:16 +08:00
|
|
|
"""The debit amount of this accounting record."""
|
|
|
|
if self._debit_amount is None:
|
|
|
|
self._debit_amount = self.amount if not self.is_credit else None
|
|
|
|
return self._debit_amount
|
2020-06-30 20:59:24 +08:00
|
|
|
|
2020-07-13 19:54:27 +08:00
|
|
|
@debit_amount.setter
|
|
|
|
def debit_amount(self, value):
|
|
|
|
self._debit_amount = value
|
|
|
|
|
2020-06-30 20:59:24 +08:00
|
|
|
@property
|
|
|
|
def credit_amount(self):
|
2020-08-02 17:57:16 +08:00
|
|
|
"""The credit amount of this accounting record."""
|
|
|
|
if self._credit_amount is None:
|
|
|
|
self._credit_amount = self.amount if self.is_credit else None
|
|
|
|
return self._credit_amount
|
2020-06-30 20:59:24 +08:00
|
|
|
|
2020-07-13 19:54:27 +08:00
|
|
|
@credit_amount.setter
|
|
|
|
def credit_amount(self, value):
|
|
|
|
self._credit_amount = value
|
|
|
|
|
2020-07-16 07:45:16 +08:00
|
|
|
@property
|
|
|
|
def is_balanced(self):
|
|
|
|
"""Whether the transaction of this record is balanced. """
|
|
|
|
if self._is_balanced is None:
|
|
|
|
self._is_balanced = self.transaction.is_balanced
|
|
|
|
return self._is_balanced
|
|
|
|
|
|
|
|
@is_balanced.setter
|
|
|
|
def is_balanced(self, value):
|
|
|
|
self._is_balanced = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_order_hole(self):
|
|
|
|
"""Whether the order of the transactions on this day is not
|
|
|
|
1, 2, 3, 4, 5..., and should be reordered. """
|
|
|
|
if self._has_order_hole is None:
|
|
|
|
self._has_order_hole = self.transaction.has_order_hole
|
|
|
|
return self._has_order_hole
|
|
|
|
|
|
|
|
@has_order_hole.setter
|
|
|
|
def has_order_hole(self, value):
|
|
|
|
self._has_order_hole = value
|
|
|
|
|
2020-07-16 22:12:59 +08:00
|
|
|
@property
|
|
|
|
def is_credit_card_paid(self):
|
|
|
|
# TODO: To be done
|
|
|
|
if self._is_credit_card_paid is None:
|
|
|
|
self._is_credit_card_paid = True
|
|
|
|
return self._is_credit_card_paid
|
|
|
|
|
|
|
|
@is_credit_card_paid.setter
|
|
|
|
def is_credit_card_paid(self, value):
|
|
|
|
self._is_credit_card_paid = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_existing_equipment(self):
|
|
|
|
# TODO: To be done
|
|
|
|
if self._is_existing_equipment is None:
|
|
|
|
self._is_existing_equipment = False
|
|
|
|
return self._is_existing_equipment
|
|
|
|
|
|
|
|
@is_existing_equipment.setter
|
|
|
|
def is_existing_equipment(self, value):
|
|
|
|
self._is_existing_equipment = value
|