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-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-16 23:03:37 +08:00
|
|
|
from mia_core.templatetags.mia_core import smart_month
|
2020-07-16 23:13:15 +08:00
|
|
|
from mia_core.utils import get_multi_lingual_attr
|
2020-07-14 22:01:32 +08:00
|
|
|
|
2020-06-30 05:41:36 +08:00
|
|
|
|
2020-07-21 10:38:47 +08:00
|
|
|
class Account(models.Model):
|
|
|
|
"""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,
|
|
|
|
db_column="parent_sn")
|
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-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
|
|
|
|
|
|
|
|
_title = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def title(self):
|
|
|
|
if self._title is None:
|
2020-07-16 23:13:15 +08:00
|
|
|
self._title = get_multi_lingual_attr(self, "title")
|
2020-07-14 22:01:32 +08:00
|
|
|
return self._title
|
|
|
|
|
|
|
|
@title.setter
|
|
|
|
def title(self, value):
|
|
|
|
self._title = value
|
2020-06-30 05:41:36 +08:00
|
|
|
|
2020-07-21 13:38:18 +08:00
|
|
|
_url = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
return self._url
|
|
|
|
|
|
|
|
@url.setter
|
|
|
|
def url(self, value):
|
|
|
|
self._url = value
|
|
|
|
|
2020-06-30 05:41:36 +08:00
|
|
|
class Meta:
|
2020-07-21 10:43:05 +08:00
|
|
|
db_table = "accounting_accounts"
|
2020-06-30 05:41:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Transaction(models.Model):
|
|
|
|
"""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)
|
|
|
|
note = models.CharField(max_length=128, null=True, blank=True)
|
|
|
|
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-06-30 20:59:24 +08:00
|
|
|
@property
|
|
|
|
def debit_records(self):
|
|
|
|
"""The debit records of this transaction."""
|
|
|
|
return [x for x in self.record_set.all() if not x.is_credit]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def credit_records(self):
|
|
|
|
"""The credit records of this transaction."""
|
|
|
|
return [x for x in self.record_set.all() if x.is_credit]
|
|
|
|
|
2020-07-07 07:10:24 +08:00
|
|
|
_is_balanced = None
|
|
|
|
|
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
|
|
|
@property
|
|
|
|
def has_many_same_day(self):
|
|
|
|
"""whether there are more than one transactions at this day,
|
|
|
|
so that the user can sort their orders. """
|
|
|
|
return Transaction.objects.filter(
|
|
|
|
date=self.date).count() > 1
|
2020-06-30 20:59:24 +08:00
|
|
|
|
2020-07-07 19:32:19 +08:00
|
|
|
_has_order_hole = None
|
|
|
|
|
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-07-21 10:38:47 +08:00
|
|
|
and debit_records[0].account.code == "1111"
|
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-07-21 10:38:47 +08:00
|
|
|
and credit_records[0].account.code == "1111"
|
2020-06-30 20:59:24 +08:00
|
|
|
and credit_records[0].summary is None)
|
|
|
|
|
2020-07-01 00:43:46 +08:00
|
|
|
def get_absolute_url(self):
|
|
|
|
"""Returns the URL to view this transaction."""
|
|
|
|
if self.is_cash_expense:
|
2020-07-02 09:45:11 +08:00
|
|
|
return reverse(
|
2020-07-09 10:46:19 +08:00
|
|
|
"accounting:transactions.view",
|
|
|
|
args=("expense", self.sn))
|
2020-07-01 00:43:46 +08:00
|
|
|
elif self.is_cash_income:
|
2020-07-02 09:45:11 +08:00
|
|
|
return reverse(
|
2020-07-09 10:46:19 +08:00
|
|
|
"accounting:transactions.view",
|
|
|
|
args=("income", self.sn))
|
2020-07-01 00:43:46 +08:00
|
|
|
else:
|
2020-07-02 09:45:11 +08:00
|
|
|
return reverse(
|
2020-07-09 10:46:19 +08:00
|
|
|
"accounting:transactions.view",
|
|
|
|
args=("transfer", self.sn))
|
2020-07-01 00:43:46 +08:00
|
|
|
|
2020-06-30 05:41:36 +08:00
|
|
|
def __str__(self):
|
|
|
|
"""Returns the string representation of this accounting
|
|
|
|
transaction."""
|
|
|
|
return self.date.__str__() + " #" + self.ord.__str__()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
db_table = "accounting_transactions"
|
|
|
|
|
|
|
|
|
|
|
|
class Record(models.Model):
|
|
|
|
"""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()
|
|
|
|
ord = models.PositiveSmallIntegerField(default=1)
|
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-07-13 19:54:27 +08:00
|
|
|
_debit_amount = None
|
|
|
|
|
2020-06-30 20:59:24 +08:00
|
|
|
@property
|
|
|
|
def debit_amount(self):
|
|
|
|
"""The debit amount of this accounting record"""
|
2020-07-13 19:54:27 +08:00
|
|
|
if self._debit_amount is not None:
|
|
|
|
return self._debit_amount
|
2020-06-30 20:59:24 +08:00
|
|
|
return self.amount if not self.is_credit else None
|
|
|
|
|
2020-07-13 19:54:27 +08:00
|
|
|
@debit_amount.setter
|
|
|
|
def debit_amount(self, value):
|
|
|
|
self._debit_amount = value
|
|
|
|
|
|
|
|
_credit_amount = None
|
|
|
|
|
2020-06-30 20:59:24 +08:00
|
|
|
@property
|
|
|
|
def credit_amount(self):
|
|
|
|
"""The credit amount of this accounting record"""
|
2020-07-13 19:54:27 +08:00
|
|
|
if self._credit_amount is not None:
|
|
|
|
return self._credit_amount
|
2020-06-30 20:59:24 +08:00
|
|
|
return self.amount if self.is_credit else None
|
|
|
|
|
2020-07-13 19:54:27 +08:00
|
|
|
@credit_amount.setter
|
|
|
|
def credit_amount(self, value):
|
|
|
|
self._credit_amount = value
|
|
|
|
|
|
|
|
_balance = None
|
|
|
|
|
2020-06-30 20:59:24 +08:00
|
|
|
@property
|
2020-07-13 19:54:27 +08:00
|
|
|
def balance(self):
|
|
|
|
return self._balance
|
2020-06-30 20:59:24 +08:00
|
|
|
|
2020-07-13 19:54:27 +08:00
|
|
|
@balance.setter
|
|
|
|
def balance(self, value):
|
|
|
|
self._balance = value
|
2020-06-30 20:59:24 +08:00
|
|
|
|
2020-07-16 07:45:16 +08:00
|
|
|
_is_balanced = None
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
_has_order_hole = None
|
|
|
|
|
|
|
|
@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
|
|
|
_is_credit_card_paid = None
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
_is_existing_equipment = None
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
2020-06-30 05:41:36 +08:00
|
|
|
def __str__(self):
|
|
|
|
"""Returns the string representation of this accounting
|
|
|
|
record."""
|
2020-07-02 09:45:11 +08:00
|
|
|
return "%s %s %s %s" % (
|
|
|
|
self.transaction.date,
|
2020-07-21 10:38:47 +08:00
|
|
|
self.account.title,
|
2020-07-02 09:45:11 +08:00
|
|
|
self.summary,
|
|
|
|
self.amount)
|
2020-06-30 05:41:36 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
db_table = "accounting_records"
|
2020-07-16 00:28:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RecordSummary(models.Model):
|
|
|
|
"""A summary record."""
|
|
|
|
month = models.DateField(primary_key=True)
|
2020-07-18 07:52:16 +08:00
|
|
|
credit = models.PositiveIntegerField()
|
|
|
|
debit = models.PositiveIntegerField()
|
2020-07-17 00:12:30 +08:00
|
|
|
balance = models.IntegerField()
|
2020-07-16 00:28:50 +08:00
|
|
|
|
|
|
|
_label = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def label(self):
|
|
|
|
if self._label is None:
|
|
|
|
self._label = smart_month(self.month)
|
|
|
|
return self._label
|
|
|
|
|
|
|
|
@label.setter
|
|
|
|
def label(self, value):
|
|
|
|
self._label = value
|
|
|
|
|
|
|
|
_cumulative_balance = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def cumulative_balance(self):
|
|
|
|
return self._cumulative_balance
|
|
|
|
|
|
|
|
@cumulative_balance.setter
|
|
|
|
def cumulative_balance(self, value):
|
|
|
|
self._cumulative_balance = value
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
db_table = None
|
|
|
|
managed = False
|