From c29cc0f0908dfc37576820f617df979433b973d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sat, 22 Aug 2020 08:58:41 +0800 Subject: [PATCH] Added the short_amount template filter and revised the accounting_amount template filter, removing the trailing decimal digits in the accounting application. --- .../accounting/report-cash-summary.html | 17 +++--- .../templates/accounting/report-cash.html | 13 ++-- .../accounting/report-income-statement.html | 7 +-- .../templates/accounting/report-journal.html | 9 ++- .../accounting/report-ledger-summary.html | 21 ++++--- .../templates/accounting/report-ledger.html | 13 ++-- .../accounting/report-trial-balance.html | 9 ++- accounting/templates/accounting/search.html | 5 +- accounting/templatetags/accounting.py | 60 ++++++++++++++++--- 9 files changed, 95 insertions(+), 59 deletions(-) diff --git a/accounting/templates/accounting/report-cash-summary.html b/accounting/templates/accounting/report-cash-summary.html index fdeaed4..a3e9517 100644 --- a/accounting/templates/accounting/report-cash-summary.html +++ b/accounting/templates/accounting/report-cash-summary.html @@ -22,7 +22,6 @@ First written: 2020/7/15 {% endcomment %} {% load static %} {% load i18n %} -{% load humanize %} {% load mia_core %} {% load accounting %} @@ -122,16 +121,16 @@ First written: 2020/7/15 {{ month.label }}
- {{ month.credit|accounting_amount }} + {{ month.credit|short_amount }} - {{ month.debit|accounting_amount }} + {{ month.debit|short_amount }} - {{ month.balance|intcomma:False }} + {{ month.balance|short_amount }} - {{ month.cumulative_balance|intcomma:False }} + {{ month.cumulative_balance|short_amount }}
@@ -140,16 +139,16 @@ First written: 2020/7/15 {{ month.label }}
- {{ month.credit|accounting_amount }} + {{ month.credit|short_amount }} - {{ month.debit|accounting_amount }} + {{ month.debit|short_amount }} - {{ month.balance|intcomma:False }} + {{ month.balance|short_amount }} - {{ month.cumulative_balance|intcomma:False }} + {{ month.cumulative_balance|short_amount }}
diff --git a/accounting/templates/accounting/report-cash.html b/accounting/templates/accounting/report-cash.html index fc8f48f..9aae4c8 100644 --- a/accounting/templates/accounting/report-cash.html +++ b/accounting/templates/accounting/report-cash.html @@ -22,7 +22,6 @@ First written: 2020/7/1 {% endcomment %} {% load static %} {% load i18n %} -{% load humanize %} {% load mia_core %} {% load accounting %} @@ -160,16 +159,16 @@ First written: 2020/7/1
{% if record.credit_amount is not None %} - {{ record.credit_amount|intcomma:False }} + {{ record.credit_amount|short_amount }} {% endif %} {% if record.debit_amount is not None %} - -{{ record.debit_amount|intcomma:False }} + -{{ record.debit_amount|short_amount }} {% endif %} - {{ record.balance|intcomma:False }} + {{ record.balance|short_amount }}
@@ -183,16 +182,16 @@ First written: 2020/7/1
{% if record.credit_amount is not None %} - {{ record.credit_amount|intcomma:False }} + {{ record.credit_amount|short_amount }} {% endif %} {% if record.debit_amount is not None %} - -{{ record.debit_amount|intcomma:False }} + -{{ record.debit_amount|short_amount }} {% endif %} - {{ record.balance|intcomma:False }} + {{ record.balance|short_amount }}
{% endif %} diff --git a/accounting/templates/accounting/report-income-statement.html b/accounting/templates/accounting/report-income-statement.html index 8b23c12..29753c7 100644 --- a/accounting/templates/accounting/report-income-statement.html +++ b/accounting/templates/accounting/report-income-statement.html @@ -22,7 +22,6 @@ First written: 2020/7/19 {% endcomment %} {% load static %} {% load i18n %} -{% load humanize %} {% load mia_core %} {% load accounting %} @@ -169,7 +168,7 @@ First written: 2020/7/19 {{ account.title|title_case }}
- {{ account.amount|accounting_amount }} + {{ account.amount|short_amount }}
@@ -179,7 +178,7 @@ First written: 2020/7/19 {{ _("Total")|force_escape }}
- {{ group.amount|accounting_amount }} + {{ group.amount|short_amount }}
@@ -197,7 +196,7 @@ First written: 2020/7/19 {{ section.cumulative_total.title|title_case }}
- {{ section.cumulative_total.amount|accounting_amount }} + {{ section.cumulative_total.amount|short_amount }}
diff --git a/accounting/templates/accounting/report-journal.html b/accounting/templates/accounting/report-journal.html index d42c028..e644a35 100644 --- a/accounting/templates/accounting/report-journal.html +++ b/accounting/templates/accounting/report-journal.html @@ -22,7 +22,6 @@ First written: 2020/7/17 {% endcomment %} {% load static %} {% load i18n %} -{% load humanize %} {% load mia_core %} {% load accounting %} @@ -140,12 +139,12 @@ First written: 2020/7/17
{% if record.debit_amount is not None %} - {{ record.debit_amount|intcomma:False }} + {{ record.debit_amount|short_amount }} {% endif %} {% if record.credit_amount is not None %} - {{ record.credit_amount|intcomma:False }} + {{ record.credit_amount|short_amount }} {% endif %}
@@ -170,12 +169,12 @@ First written: 2020/7/17
{% if record.debit_amount is not None %} - {{ record.debit_amount|intcomma:False }} + {{ record.debit_amount|short_amount }} {% endif %} {% if record.credit_amount is not None %} - {{ record.credit_amount|intcomma:False }} + {{ record.credit_amount|short_amount }} {% endif %}
diff --git a/accounting/templates/accounting/report-ledger-summary.html b/accounting/templates/accounting/report-ledger-summary.html index 897b6d0..4c8a300 100644 --- a/accounting/templates/accounting/report-ledger-summary.html +++ b/accounting/templates/accounting/report-ledger-summary.html @@ -22,7 +22,6 @@ First written: 2020/7/16 {% endcomment %} {% load static %} {% load i18n %} -{% load humanize %} {% load mia_core %} {% load accounting %} @@ -115,22 +114,22 @@ First written: 2020/7/16 {{ month.label }}
- {{ month.debit|accounting_amount }} + {{ month.debit|short_amount }} - {{ month.credit|accounting_amount }} + {{ month.credit|short_amount }} - {{ month.balance|intcomma:False }} + {{ month.balance|short_amount }} - {{ month.cumulative_balance|intcomma:False }} + {{ month.cumulative_balance|short_amount }} - {{ month.balance|intcomma:False }} + {{ month.balance|short_amount }} - {{ month.cumulative_balance|intcomma:False }} + {{ month.cumulative_balance|short_amount }}
@@ -139,16 +138,16 @@ First written: 2020/7/16 {{ month.label }}
- {{ month.debit|accounting_amount }} + {{ month.debit|short_amount }} - {{ month.credit|accounting_amount }} + {{ month.credit|short_amount }} - {{ month.balance|intcomma:False }} + {{ month.balance|short_amount }} - {{ month.cumulative_balance|intcomma:False }} + {{ month.cumulative_balance|short_amount }}
diff --git a/accounting/templates/accounting/report-ledger.html b/accounting/templates/accounting/report-ledger.html index 7dbaaa0..91b6e9e 100644 --- a/accounting/templates/accounting/report-ledger.html +++ b/accounting/templates/accounting/report-ledger.html @@ -22,7 +22,6 @@ First written: 2020/7/16 {% endcomment %} {% load static %} {% load i18n %} -{% load humanize %} {% load mia_core %} {% load accounting %} @@ -170,16 +169,16 @@ First written: 2020/7/16
{% if record.debit_amount is not None %} - {{ record.debit_amount|intcomma:False }} + {{ record.debit_amount|short_amount }} {% endif %} {% if record.credit_amount is not None %} - {{ record.credit_amount|intcomma:False }} + {{ record.credit_amount|short_amount }} {% endif %} - {{ record.balance|intcomma:False }} + {{ record.balance|short_amount }}
@@ -195,16 +194,16 @@ First written: 2020/7/16
{% if record.debit_amount is not None %} - {{ record.debit_amount|intcomma:False }} + {{ record.debit_amount|short_amount }} {% endif %} {% if record.credit_amount is not None %} - {{ record.credit_amount|intcomma:False }} + {{ record.credit_amount|short_amount }} {% endif %} - {{ record.balance|intcomma:False }} + {{ record.balance|short_amount }}
diff --git a/accounting/templates/accounting/report-trial-balance.html b/accounting/templates/accounting/report-trial-balance.html index 1ed6e2f..2181bb2 100644 --- a/accounting/templates/accounting/report-trial-balance.html +++ b/accounting/templates/accounting/report-trial-balance.html @@ -22,7 +22,6 @@ First written: 2020/7/19 {% endcomment %} {% load static %} {% load i18n %} -{% load humanize %} {% load mia_core %} {% load accounting %} @@ -131,12 +130,12 @@ First written: 2020/7/19
{% if account.debit_amount is not None %} - {{ account.debit_amount|intcomma:False }} + {{ account.debit_amount|short_amount }} {% endif %} {% if account.credit_amount is not None %} - {{ account.credit_amount|intcomma:False }} + {{ account.credit_amount|short_amount }} {% endif %}
@@ -147,10 +146,10 @@ First written: 2020/7/19 {{ _("Total")|force_escape }}
- {{ total_item.debit_amount|intcomma:False }} + {{ total_item.debit_amount|short_amount }} - {{ total_item.credit_amount|intcomma:False }} + {{ total_item.credit_amount|short_amount }}
diff --git a/accounting/templates/accounting/search.html b/accounting/templates/accounting/search.html index 1cc4e5a..6625f5c 100644 --- a/accounting/templates/accounting/search.html +++ b/accounting/templates/accounting/search.html @@ -22,7 +22,6 @@ First written: 2020/7/21 {% endcomment %} {% load static %} {% load i18n %} -{% load humanize %} {% load mia_core %} {% load accounting %} @@ -136,12 +135,12 @@ First written: 2020/7/21
{% if record.debit_amount is not None %} - {{ record.debit_amount|intcomma:False }} + {{ record.debit_amount|short_amount }} {% endif %} {% if record.credit_amount is not None %} - {{ record.credit_amount|intcomma:False }} + {{ record.credit_amount|short_amount }} {% endif %}
diff --git a/accounting/templatetags/accounting.py b/accounting/templatetags/accounting.py index 8251a1d..649b7f0 100644 --- a/accounting/templatetags/accounting.py +++ b/accounting/templatetags/accounting.py @@ -32,20 +32,64 @@ from mia_core.period import Period register = template.Library() -@register.filter -def accounting_amount(value: Union[str, Decimal]) -> str: - if value is None: - return "" - if value == 0: - return "-" - s = str(abs(value)) +def _format_positive_amount(value: Union[str, Decimal]) -> str: + """Formats a positive amount, groups every 3 digits by commas. + + Args: + value: The amount. + + Returns: + ReportUrl: The formatted amount. + """ + s = str(value) while True: m = re.match("^([1-9][0-9]*)([0-9]{3}.*)", s) if m is None: break s = m.group(1) + "," + m.group(2) + s = re.sub(r"^(.*\.[0-9]*?)0+$", r"\1", s) + s = re.sub(r"^(.*)\.$", r"\1", s) + return s + + +@register.filter +def accounting_amount(value: Union[str, Decimal]) -> str: + """Formats an amount with the accounting notation, grouping every 3 digits + by commas, and marking negative numbers with brackets instead of signs. + + Args: + value: The amount. + + Returns: + ReportUrl: The formatted amount. + """ + if value is None: + return "" + if value == 0: + return "-" + s = _format_positive_amount(abs(value)) if value < 0: - s = "(%s)" % s + s = F"({s})" + return s + + +@register.filter +def short_amount(value: Union[str, Decimal]) -> str: + """Formats an amount, groups every 3 digits by commas. + + Args: + value: The amount. + + Returns: + ReportUrl: The formatted amount. + """ + if value is None: + return "" + if value == 0: + return "-" + s = _format_positive_amount(abs(value)) + if value < 0: + s = "-" + s return s