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-08-09 13:19:20 +08:00
|
|
|
from django.db import models, transaction
|
2020-08-12 22:26:54 +08:00
|
|
|
from django.db.models import Q, Max
|
2020-07-01 00:43:46 +08:00
|
|
|
from django.urls import reverse
|
2020-06-29 19:41:52 +08:00
|
|
|
|
2020-08-09 16:22:51 +08:00
|
|
|
from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr, \
|
|
|
|
new_pk
|
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-08-04 01:59:51 +08:00
|
|
|
id = models.PositiveIntegerField(primary_key=True)
|
2020-06-30 05:41:36 +08:00
|
|
|
parent = models.ForeignKey(
|
2020-08-09 16:34:47 +08:00
|
|
|
"self", on_delete=models.PROTECT, null=True,
|
2020-08-04 01:59:51 +08:00
|
|
|
related_name="child_set")
|
2020-07-21 10:59:48 +08:00
|
|
|
code = models.CharField(max_length=5, unique=True)
|
2020-08-09 16:34:47 +08:00
|
|
|
title_zh_hant = models.CharField(max_length=32)
|
|
|
|
title_en = models.CharField(max_length=128, null=True)
|
|
|
|
title_zh_hans = models.CharField(max_length=32, null=True)
|
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
2020-07-06 00:09:26 +08:00
|
|
|
created_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
2020-07-21 10:38:47 +08:00
|
|
|
related_name="created_accounting_accounts")
|
2020-08-09 16:34:47 +08:00
|
|
|
updated_at = models.DateTimeField(auto_now=True)
|
2020-07-06 00:09:26 +08:00
|
|
|
updated_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
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):
|
2020-08-12 15:39:07 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2020-08-02 01:04:47 +08:00
|
|
|
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
|
2020-08-08 15:18:39 +08:00
|
|
|
self._is_in_use = None
|
2020-08-07 10:09:41 +08:00
|
|
|
self._is_parent_and_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-09 16:22:51 +08:00
|
|
|
def save(self, current_user=None, force_insert=False, force_update=False,
|
|
|
|
using=None, update_fields=None):
|
|
|
|
self.parent = None if len(self.code) == 1\
|
|
|
|
else Account.objects.get(code=self.code[:-1])
|
|
|
|
if self.pk is None:
|
|
|
|
self.pk = new_pk(Account)
|
2020-08-09 18:21:34 +08:00
|
|
|
if current_user is not None:
|
|
|
|
self.created_by = current_user
|
|
|
|
if current_user is not None:
|
2020-08-09 16:59:12 +08:00
|
|
|
self.updated_by = current_user
|
2020-08-09 16:22:51 +08:00
|
|
|
with transaction.atomic():
|
2020-08-12 22:29:03 +08:00
|
|
|
super().save(force_insert=force_insert, force_update=force_update,
|
|
|
|
using=using, update_fields=update_fields)
|
2020-08-09 16:22:51 +08:00
|
|
|
|
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-08-07 10:09:41 +08:00
|
|
|
@property
|
|
|
|
def is_parent_and_in_use(self):
|
|
|
|
"""Whether this is a parent account and is in use.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if this is a parent account and is in use, or false
|
|
|
|
otherwise
|
|
|
|
"""
|
|
|
|
if self._is_parent_and_in_use is None:
|
|
|
|
self._is_parent_and_in_use = self.child_set.count() > 0\
|
|
|
|
and self.record_set.count() > 0
|
|
|
|
return self._is_parent_and_in_use
|
|
|
|
|
|
|
|
@is_parent_and_in_use.setter
|
|
|
|
def is_parent_and_in_use(self, value):
|
|
|
|
self._is_parent_and_in_use = value
|
|
|
|
|
2020-08-08 15:18:39 +08:00
|
|
|
@property
|
|
|
|
def is_in_use(self):
|
|
|
|
"""Whether this account is in use.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if this account is in use, or false otherwise.
|
|
|
|
"""
|
|
|
|
if self._is_in_use is None:
|
|
|
|
self._is_in_use = self.child_set.count() > 0\
|
|
|
|
or self.record_set.count() > 0
|
|
|
|
return self._is_in_use
|
|
|
|
|
|
|
|
@is_in_use.setter
|
|
|
|
def is_in_use(self, value):
|
|
|
|
self._is_in_use = value
|
|
|
|
|
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-08-04 01:59:51 +08:00
|
|
|
id = models.PositiveIntegerField(primary_key=True)
|
2020-07-06 00:09:26 +08:00
|
|
|
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-08-09 16:34:47 +08:00
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
2020-07-06 00:09:26 +08:00
|
|
|
created_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
|
|
related_name="created_accounting_transactions")
|
2020-08-09 16:34:47 +08:00
|
|
|
updated_at = models.DateTimeField(auto_now=True)
|
2020-07-06 00:09:26 +08:00
|
|
|
updated_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
|
|
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):
|
2020-08-12 15:39:07 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2020-08-02 01:04:47 +08:00
|
|
|
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(
|
2020-08-08 15:43:21 +08:00
|
|
|
"accounting:transactions.detail", args=("expense", self))
|
2020-08-02 10:35:35 +08:00
|
|
|
elif self.is_cash_income:
|
|
|
|
return reverse(
|
2020-08-08 15:43:21 +08:00
|
|
|
"accounting:transactions.detail", args=("income", self))
|
2020-08-02 10:35:35 +08:00
|
|
|
else:
|
|
|
|
return reverse(
|
2020-08-08 15:43:21 +08:00
|
|
|
"accounting:transactions.detail", args=("transfer", self))
|
2020-08-02 10:35:35 +08:00
|
|
|
|
2020-08-12 13:52:44 +08:00
|
|
|
def is_dirty(self, check_relationship=False, check_m2m=None):
|
2020-08-02 10:35:35 +08:00
|
|
|
"""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.
|
|
|
|
"""
|
2020-08-12 22:29:03 +08:00
|
|
|
if super().is_dirty(check_relationship=check_relationship,
|
|
|
|
check_m2m=check_m2m):
|
2020-08-02 10:35:35 +08:00
|
|
|
return True
|
2020-08-12 13:52:44 +08:00
|
|
|
if len([x for x in self.records
|
|
|
|
if x.is_dirty(check_relationship=check_relationship,
|
|
|
|
check_m2m=check_m2m)]) > 0:
|
2020-08-02 10:35:35 +08:00
|
|
|
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-12 22:26:54 +08:00
|
|
|
def save(self, current_user=None, old_date=None, force_insert=False,
|
|
|
|
force_update=False, using=None, update_fields=None):
|
|
|
|
# When the date is changed, the orders of the transactions in the same
|
|
|
|
# day need to be reordered
|
|
|
|
txn_to_sort = []
|
|
|
|
if self.date != old_date:
|
|
|
|
if old_date is not None:
|
|
|
|
txn_same_day = list(
|
|
|
|
Transaction.objects
|
|
|
|
.filter(Q(date=old_date), ~Q(pk=self.pk))
|
|
|
|
.order_by("ord"))
|
|
|
|
for i in range(len(txn_same_day)):
|
|
|
|
if txn_same_day[i].ord != i + 1:
|
|
|
|
txn_to_sort.append([txn_same_day[i], i + 1])
|
|
|
|
max_ord = Transaction.objects\
|
|
|
|
.filter(date=self.date)\
|
|
|
|
.aggregate(max=Max("ord"))["max"]
|
|
|
|
self.ord = 1 if max_ord is None else max_ord + 1
|
|
|
|
# Collects the records to be deleted
|
|
|
|
to_keep = [x.pk for x in self.records if x.pk is not None]
|
|
|
|
to_delete = [x for x in self.record_set.all() if x.pk not in to_keep]
|
|
|
|
# Applies the created by and updated by
|
|
|
|
if self.pk is None:
|
|
|
|
self.pk = new_pk(Transaction)
|
|
|
|
if current_user is not None:
|
|
|
|
self.created_by = current_user
|
|
|
|
if current_user is not None:
|
|
|
|
self.updated_by = current_user
|
|
|
|
to_save = [x for x in self.records if x.is_dirty()]
|
|
|
|
for record in to_save:
|
|
|
|
if record.pk is None:
|
|
|
|
record.pk = new_pk(Record)
|
|
|
|
if current_user is not None:
|
|
|
|
record.created_by = current_user
|
|
|
|
if current_user is not None:
|
|
|
|
record.updated_by = current_user
|
|
|
|
# Runs the update
|
|
|
|
with transaction.atomic():
|
|
|
|
super().save(force_insert=force_insert, force_update=force_update,
|
|
|
|
using=using, update_fields=update_fields)
|
|
|
|
for record in to_delete:
|
|
|
|
record.delete()
|
|
|
|
for record in to_save:
|
|
|
|
record.save(force_insert=force_insert,
|
|
|
|
force_update=force_update,
|
|
|
|
using=using, update_fields=update_fields)
|
|
|
|
for x in txn_to_sort:
|
|
|
|
Transaction.objects.filter(pk=x[0].pk).update(ord=x[1])
|
|
|
|
|
2020-08-09 13:19:20 +08:00
|
|
|
def delete(self, using=None, keep_parents=False):
|
|
|
|
txn_same_day = list(
|
|
|
|
Transaction.objects
|
|
|
|
.filter(Q(date=self.date), ~Q(pk=self.pk))
|
|
|
|
.order_by("ord"))
|
|
|
|
txn_to_sort = []
|
|
|
|
for i in range(len(txn_same_day)):
|
2020-08-12 22:41:28 +08:00
|
|
|
if txn_same_day[i].ord != i + 1:
|
|
|
|
txn_to_sort.append([txn_same_day[i], i + 1])
|
2020-08-09 13:19:20 +08:00
|
|
|
with transaction.atomic():
|
|
|
|
for record in self.record_set.all():
|
|
|
|
record.delete()
|
2020-08-12 22:29:03 +08:00
|
|
|
super().delete(using=using, keep_parents=keep_parents)
|
2020-08-12 22:41:28 +08:00
|
|
|
for x in txn_to_sort:
|
|
|
|
Transaction.objects.filter(pk=x[0].pk).update(ord=x[1])
|
2020-08-09 13:19:20 +08:00
|
|
|
|
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-08-06 23:51:20 +08:00
|
|
|
@property
|
|
|
|
def debit_summaries(self):
|
|
|
|
"""The summaries of the debit records.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[str]: The summaries of the debit records.
|
|
|
|
"""
|
|
|
|
return [x.account.title if x.summary is None else x.summary
|
|
|
|
for x in self.debit_records]
|
|
|
|
|
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-08-06 23:51:20 +08:00
|
|
|
@property
|
|
|
|
def credit_summaries(self):
|
|
|
|
"""The summaries of the credit records.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[str]: The summaries of the credit records.
|
|
|
|
"""
|
|
|
|
return [x.account.title if x.summary is None else x.summary
|
|
|
|
for x in self.credit_records]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def amount(self):
|
|
|
|
"""The amount of this transaction.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int: The amount of this transaction.
|
|
|
|
"""
|
|
|
|
return self.debit_total()
|
|
|
|
|
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-08-04 01:59:51 +08:00
|
|
|
id = models.PositiveIntegerField(primary_key=True)
|
2020-06-30 05:41:36 +08:00
|
|
|
transaction = models.ForeignKey(
|
2020-08-04 01:59:51 +08:00
|
|
|
Transaction, on_delete=models.CASCADE)
|
2020-06-30 05:41:36 +08:00
|
|
|
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-08-04 01:59:51 +08:00
|
|
|
Account, on_delete=models.PROTECT)
|
2020-06-30 05:41:36 +08:00
|
|
|
summary = models.CharField(max_length=128, blank=True, null=True)
|
|
|
|
amount = models.PositiveIntegerField()
|
2020-08-09 16:34:47 +08:00
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
2020-07-06 00:09:26 +08:00
|
|
|
created_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
|
|
related_name="created_accounting_records")
|
2020-08-09 16:34:47 +08:00
|
|
|
updated_at = models.DateTimeField(auto_now=True)
|
2020-07-06 00:09:26 +08:00
|
|
|
updated_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
|
|
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):
|
2020-08-12 15:39:07 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2020-08-02 01:04:47 +08:00
|
|
|
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
|
2020-08-07 00:58:33 +08:00
|
|
|
self._is_payable = None
|
2020-08-02 01:04:47 +08:00
|
|
|
self._is_existing_equipment = None
|
2020-08-07 00:58:33 +08:00
|
|
|
self.is_payable = False
|
2020-08-07 01:06:57 +08:00
|
|
|
self.is_existing_equipment = False
|
2020-08-02 01:04:47 +08:00
|
|
|
|
|
|
|
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
|