From cbac2ba61e30a11b9c02353164a976c47953bef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Mon, 3 Aug 2020 00:02:51 +0800 Subject: [PATCH] Added the account_options() view to return the account options in the accounting application. --- accounting/models.py | 12 +++++++++++- accounting/urls.py | 4 ++-- accounting/views.py | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/accounting/models.py b/accounting/models.py index 828995d..4fb1b9c 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -32,7 +32,7 @@ class Account(DirtyFieldsMixin, models.Model): sn = models.PositiveIntegerField(primary_key=True) parent = models.ForeignKey( "self", on_delete=models.PROTECT, null=True, blank=True, - db_column="parent_sn") + db_column="parent_sn", related_name="child_set") code = models.CharField(max_length=5, unique=True) title_zh_hant = models.CharField( max_length=32, db_column="title_zhtw") @@ -61,6 +61,9 @@ class Account(DirtyFieldsMixin, models.Model): self.debit_amount = None self.credit_amount = None self.amount = None + self.is_for_debit = None + self.is_for_credit = None + self.is_in_use = None def __str__(self): """Returns the string representation of this account.""" @@ -77,6 +80,13 @@ class Account(DirtyFieldsMixin, models.Model): def title(self, value): set_multi_lingual_attr(self, "title", value) + @property + def option_data(self): + return { + "code": self.code, + "title": self.title, + } + class Transaction(DirtyFieldsMixin, models.Model): """An accounting transaction.""" diff --git a/accounting/urls.py b/accounting/urls.py index cfdd68f..c96049e 100644 --- a/accounting/urls.py +++ b/accounting/urls.py @@ -95,6 +95,8 @@ urlpatterns = [ mia_core_views.todo, name="accounts.create"), path("accounts/store", mia_core_views.todo, name="accounts.store"), + path("accounts/options", + views.account_options, name="accounts.options"), path("accounts/", mia_core_views.todo, name="accounts.show"), path("accounts//edit", @@ -103,6 +105,4 @@ urlpatterns = [ mia_core_views.todo, name="accounts.update"), path("accounts//delete", mia_core_views.todo, name="accounts.delete"), - path("accounts/options", - mia_core_views.todo, name="accounts.options"), ] diff --git a/accounting/views.py b/accounting/views.py index 98b1311..f5763d1 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -22,8 +22,9 @@ import re from django.conf import settings from django.db import transaction -from django.db.models import Sum, Case, When, F, Q, Max +from django.db.models import Sum, Case, When, F, Q, Max, Count, BooleanField from django.db.models.functions import TruncMonth, Coalesce, Now +from django.http import JsonResponse from django.shortcuts import render from django.urls import reverse from django.utils import timezone @@ -900,3 +901,41 @@ def txn_store(request, txn_type, txn=None): url = str(UrlBuilder(url).set("r", request.GET.get("r"))) message = gettext_noop("This transaction was saved successfully.") return success_redirect(request, url, message) + + +@require_GET +@login_required +def account_options(request): + """The view to return the account options. + + Args: + request (HttpRequest): The request. + + Returns: + JsonResponse: The response. + """ + accounts = Account.objects\ + .annotate(children_count=Count("child_set"))\ + .filter(children_count=0)\ + .annotate(record_count=Count("record"))\ + .annotate(is_in_use=Case( + When(record_count__gt=0, then=True), + default=False, + output_field=BooleanField())) + for x in accounts: + x.is_for_debit = re.match("^([1235689]|7[5678])", x.code) is not None + x.is_for_credit = re.match("^([123489]|7[1234])", x.code) is not None + data = { + "header_in_use": pgettext("Accounting|", "---Accounts In Use---"), + "debit_in_use": [x.option_data for x in accounts + if x.is_for_debit and x.is_in_use], + "credit_in_use": [x.option_data for x in accounts + if x.is_for_credit and x.is_in_use], + "header_not_in_use": pgettext( + "Accounting|", "---Accounts Not In Use---"), + "debit_not_in_use": [x.option_data for x in accounts + if x.is_for_debit and not x.is_in_use], + "credit_not_in_use": [x.option_data for x in accounts + if x.is_for_credit and not x.is_in_use], + } + return JsonResponse(data)