Renamed RecordSummary to MonthlySummary, changed it from a Django data model to a simple data class, and moved it from accounting.models to accounting.utils in the accounting application.

This commit is contained in:
依瑪貓 2020-08-03 22:48:43 +08:00
parent 66f57b7b0d
commit 1d7acef3e1
3 changed files with 46 additions and 43 deletions

View File

@ -23,7 +23,6 @@ from django.conf import settings
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from mia_core.templatetags.mia_core import smart_month
from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr
@ -376,30 +375,3 @@ class Record(DirtyFieldsMixin, models.Model):
@is_existing_equipment.setter @is_existing_equipment.setter
def is_existing_equipment(self, value): def is_existing_equipment(self, value):
self._is_existing_equipment = value self._is_existing_equipment = value
class RecordSummary(DirtyFieldsMixin, models.Model):
"""A summary record."""
month = models.DateField(primary_key=True)
credit = models.PositiveIntegerField()
debit = models.PositiveIntegerField()
balance = models.IntegerField()
def __init__(self, *args, **kwargs):
super(RecordSummary, self).__init__(*args, **kwargs)
self._label = None
self.cumulative_balance = None
class Meta:
db_table = None
managed = False
@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

View File

@ -29,11 +29,44 @@ from django.utils.translation import pgettext
from .forms import TransactionForm, RecordForm from .forms import TransactionForm, RecordForm
from .models import Account, Transaction, Record from .models import Account, Transaction, Record
from mia_core.templatetags.mia_core import smart_month
from mia_core.period import Period from mia_core.period import Period
from mia_core.status import retrieve_status from mia_core.status import retrieve_status
from mia_core.utils import new_pk from mia_core.utils import new_pk
class MonthlySummary:
"""A summary record.
Args:
month (datetime.date): The month.
label (str): The text label.
credit (int): The credit amount.
debit (int): The debit amount.
balance (int): The balance.
cumulative_balance (int): The cumulative balance.
Attributes:
month (datetime.date): The month.
label (str): The text label.
credit (int): The credit amount.
debit (int): The debit amount.
balance (int): The balance.
cumulative_balance (int): The cumulative balance.
"""
def __init__(self, month=None, label=None, credit=None, debit=None,
balance=None, cumulative_balance=None):
self.month = month
self.label = label
self.credit = credit
self.debit = debit
self.balance = balance
self.cumulative_balance = cumulative_balance
if self.label is None and self.month is not None:
self.label = smart_month(self.month)
class ReportUrl: class ReportUrl:
"""The URL of the accounting reports. """The URL of the accounting reports.

View File

@ -38,11 +38,11 @@ from mia_core.period import Period
from mia_core.status import success_redirect, error_redirect from mia_core.status import success_redirect, error_redirect
from mia_core.utils import Pagination, get_multi_lingual_search, UrlBuilder, \ from mia_core.utils import Pagination, get_multi_lingual_search, UrlBuilder, \
strip_form, new_pk strip_form, new_pk
from .models import Record, Transaction, Account, RecordSummary from .models import Record, Transaction, Account
from .utils import ReportUrl, get_cash_accounts, get_ledger_accounts, \ from .utils import ReportUrl, get_cash_accounts, get_ledger_accounts, \
find_imbalanced, find_order_holes, fill_txn_from_post, \ find_imbalanced, find_order_holes, fill_txn_from_post, \
sort_post_txn_records, make_txn_form_from_status, \ sort_post_txn_records, make_txn_form_from_status, \
make_txn_form_from_model, make_txn_form_from_post make_txn_form_from_model, make_txn_form_from_post, MonthlySummary
@method_decorator(require_GET, name="dispatch") @method_decorator(require_GET, name="dispatch")
@ -194,7 +194,7 @@ def cash_summary(request, account):
accounts = get_cash_accounts() accounts = get_cash_accounts()
# The month summaries # The month summaries
if account.code == "0": if account.code == "0":
months = [RecordSummary(**x) for x in Record.objects months = [MonthlySummary(**x) for x in Record.objects
.filter( .filter(
Q(transaction__in=Transaction.objects.filter( Q(transaction__in=Transaction.objects.filter(
Q(record__account__code__startswith="11") | Q(record__account__code__startswith="11") |
@ -219,7 +219,7 @@ def cash_summary(request, account):
When(is_credit=False, then=-F("amount")), When(is_credit=False, then=-F("amount")),
default=F("amount"))))] default=F("amount"))))]
else: else:
months = [RecordSummary(**x) for x in Record.objects months = [MonthlySummary(**x) for x in Record.objects
.filter( .filter(
Q(transaction__in=Transaction.objects.filter( Q(transaction__in=Transaction.objects.filter(
record__account__code__startswith=account.code)), record__account__code__startswith=account.code)),
@ -241,14 +241,13 @@ def cash_summary(request, account):
for month in months: for month in months:
cumulative_balance = cumulative_balance + month.balance cumulative_balance = cumulative_balance + month.balance
month.cumulative_balance = cumulative_balance month.cumulative_balance = cumulative_balance
total = RecordSummary( months.append(MonthlySummary(
label=pgettext("Accounting|", "Total"),
credit=sum([x.credit for x in months]), credit=sum([x.credit for x in months]),
debit=sum([x.debit for x in months]), debit=sum([x.debit for x in months]),
balance=sum([x.balance for x in months]), balance=sum([x.balance for x in months]),
) cumulative_balance=cumulative_balance,
total.cumulative_balance = cumulative_balance ))
total.label = pgettext("Accounting|", "Total")
months.append(total)
pagination = Pagination(request, months, True) pagination = Pagination(request, months, True)
shortcut_accounts = settings.ACCOUNTING["CASH_SHORTCUT_ACCOUNTS"] shortcut_accounts = settings.ACCOUNTING["CASH_SHORTCUT_ACCOUNTS"]
return render(request, "accounting/cash-summary.html", { return render(request, "accounting/cash-summary.html", {
@ -365,7 +364,7 @@ def ledger_summary(request, account):
HttpResponse: The response. HttpResponse: The response.
""" """
# The month summaries # The month summaries
months = [RecordSummary(**x) for x in Record.objects months = [MonthlySummary(**x) for x in Record.objects
.filter(account__code__startswith=account.code) .filter(account__code__startswith=account.code)
.annotate(month=TruncMonth("transaction__date")) .annotate(month=TruncMonth("transaction__date"))
.values("month") .values("month")
@ -384,14 +383,13 @@ def ledger_summary(request, account):
for month in months: for month in months:
cumulative_balance = cumulative_balance + month.balance cumulative_balance = cumulative_balance + month.balance
month.cumulative_balance = cumulative_balance month.cumulative_balance = cumulative_balance
total = RecordSummary( months.append(MonthlySummary(
label=pgettext("Accounting|", "Total"),
credit=sum([x.credit for x in months]), credit=sum([x.credit for x in months]),
debit=sum([x.debit for x in months]), debit=sum([x.debit for x in months]),
balance=sum([x.balance for x in months]), balance=sum([x.balance for x in months]),
) cumulative_balance=cumulative_balance,
total.cumulative_balance = cumulative_balance ))
total.label = pgettext("Accounting|", "Total")
months.append(total)
pagination = Pagination(request, months, True) pagination = Pagination(request, months, True)
return render(request, "accounting/ledger-summary.html", { return render(request, "accounting/ledger-summary.html", {
"item_list": pagination.items, "item_list": pagination.items,