Implemented the find_existing_equipments() utility, and applied it in the ledger in the accounting application.

This commit is contained in:
依瑪貓 2020-08-07 01:06:57 +08:00
parent c78d0f613d
commit 35e6b533cf
3 changed files with 32 additions and 12 deletions

View File

@ -326,6 +326,7 @@ class Record(DirtyFieldsMixin, models.Model):
self._is_payable = None
self._is_existing_equipment = None
self.is_payable = False
self.is_existing_equipment = False
def __str__(self):
"""Returns the string representation of this accounting
@ -383,14 +384,3 @@ class Record(DirtyFieldsMixin, models.Model):
@has_order_hole.setter
def has_order_hole(self, value):
self._has_order_hole = 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

View File

@ -352,6 +352,35 @@ def find_payable_records(account, records):
x.is_payable = True
def find_existing_equipments(account, records):
"""Finds and sets the equipments that still exist.
Args:
account (Account): The current ledger account.
records (list[Record]): The accounting records.
"""
if "EQUIPMENT_ACCOUNTS" not in settings.ACCOUNTING:
return
if not isinstance(settings.ACCOUNTING["EQUIPMENT_ACCOUNTS"], list):
return
if account.code not in settings.ACCOUNTING["EQUIPMENT_ACCOUNTS"]:
return
rows = Record.objects\
.filter(
account__code__in=settings.ACCOUNTING["EQUIPMENT_ACCOUNTS"],
summary__isnull=False)\
.values("account__code", "summary")\
.annotate(
balance=Sum(Case(When(is_credit=True, then=1), default=-1)
* F("amount")))\
.filter(~Q(balance=0))
keys = ["%s-%s" % (x["account__code"], x["summary"]) for x in rows]
for x in [x for x in records
if x.pk is not None
and F"{x.account.code}-{x.summary}" in keys]:
x.is_existing_equipment = True
def get_summary_categories():
"""Finds and returns the summary categories and their corresponding account
hints.

View File

@ -45,7 +45,7 @@ from .utils import ReportUrl, get_cash_accounts, get_ledger_accounts, \
find_imbalanced, find_order_holes, fill_txn_from_post, \
sort_post_txn_records, make_txn_form_from_status, \
make_txn_form_from_model, make_txn_form_from_post, MonthlySummary, \
get_summary_categories, find_payable_records
get_summary_categories, find_payable_records, find_existing_equipments
@method_decorator(require_GET, name="dispatch")
@ -341,6 +341,7 @@ def ledger(request, account, period):
find_imbalanced(records)
find_order_holes(records)
find_payable_records(account, records)
find_existing_equipments(account, records)
return render(request, "accounting/ledger.html", {
"item_list": records,
"pagination": pagination,