Added the account_options() view to return the account options in the accounting application.
This commit is contained in:
parent
ce1d640866
commit
cbac2ba61e
@ -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."""
|
||||
|
@ -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/<str:account_code>",
|
||||
mia_core_views.todo, name="accounts.show"),
|
||||
path("accounts/<str:account_code>/edit",
|
||||
@ -103,6 +105,4 @@ urlpatterns = [
|
||||
mia_core_views.todo, name="accounts.update"),
|
||||
path("accounts/<str:account_code>/delete",
|
||||
mia_core_views.todo, name="accounts.delete"),
|
||||
path("accounts/options",
|
||||
mia_core_views.todo, name="accounts.options"),
|
||||
]
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user