Added the short_amount template filter and revised the accounting_amount template filter, removing the trailing decimal digits in the accounting application.

This commit is contained in:
依瑪貓 2020-08-22 08:58:41 +08:00
parent 567a610e90
commit c29cc0f090
9 changed files with 95 additions and 59 deletions

View File

@ -22,7 +22,6 @@ First written: 2020/7/15
{% endcomment %} {% endcomment %}
{% load static %} {% load static %}
{% load i18n %} {% load i18n %}
{% load humanize %}
{% load mia_core %} {% load mia_core %}
{% load accounting %} {% load accounting %}
@ -122,16 +121,16 @@ First written: 2020/7/15
{{ month.label }} {{ month.label }}
<div> <div>
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ month.credit|accounting_amount }} {{ month.credit|short_amount }}
</span> </span>
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
{{ month.debit|accounting_amount }} {{ month.debit|short_amount }}
</span> </span>
<span class="badge {% if month.balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill"> <span class="badge {% if month.balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill">
{{ month.balance|intcomma:False }} {{ month.balance|short_amount }}
</span> </span>
<span class="badge {% if month.cumulative_balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill"> <span class="badge {% if month.cumulative_balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill">
{{ month.cumulative_balance|intcomma:False }} {{ month.cumulative_balance|short_amount }}
</span> </span>
</div> </div>
</a> </a>
@ -140,16 +139,16 @@ First written: 2020/7/15
{{ month.label }} {{ month.label }}
<div> <div>
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ month.credit|accounting_amount }} {{ month.credit|short_amount }}
</span> </span>
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
{{ month.debit|accounting_amount }} {{ month.debit|short_amount }}
</span> </span>
<span class="badge {% if month.balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill"> <span class="badge {% if month.balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill">
{{ month.balance|intcomma:False }} {{ month.balance|short_amount }}
</span> </span>
<span class="badge {% if month.cumulative_balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill"> <span class="badge {% if month.cumulative_balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill">
{{ month.cumulative_balance|intcomma:False }} {{ month.cumulative_balance|short_amount }}
</span> </span>
</div> </div>
</div> </div>

View File

@ -22,7 +22,6 @@ First written: 2020/7/1
{% endcomment %} {% endcomment %}
{% load static %} {% load static %}
{% load i18n %} {% load i18n %}
{% load humanize %}
{% load mia_core %} {% load mia_core %}
{% load accounting %} {% load accounting %}
@ -160,16 +159,16 @@ First written: 2020/7/1
<div> <div>
{% if record.credit_amount is not None %} {% if record.credit_amount is not None %}
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ record.credit_amount|intcomma:False }} {{ record.credit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
{% if record.debit_amount is not None %} {% if record.debit_amount is not None %}
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
-{{ record.debit_amount|intcomma:False }} -{{ record.debit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
<span class="badge {% if record.balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill"> <span class="badge {% if record.balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill">
{{ record.balance|intcomma:False }} {{ record.balance|short_amount }}
</span> </span>
</div> </div>
</a> </a>
@ -183,16 +182,16 @@ First written: 2020/7/1
<div> <div>
{% if record.credit_amount is not None %} {% if record.credit_amount is not None %}
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ record.credit_amount|intcomma:False }} {{ record.credit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
{% if record.debit_amount is not None %} {% if record.debit_amount is not None %}
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
-{{ record.debit_amount|intcomma:False }} -{{ record.debit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
<span class="badge {% if record.balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill"> <span class="badge {% if record.balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill">
{{ record.balance|intcomma:False }} {{ record.balance|short_amount }}
</span> </span>
</div> </div>
{% endif %} {% endif %}

View File

@ -22,7 +22,6 @@ First written: 2020/7/19
{% endcomment %} {% endcomment %}
{% load static %} {% load static %}
{% load i18n %} {% load i18n %}
{% load humanize %}
{% load mia_core %} {% load mia_core %}
{% load accounting %} {% load accounting %}
@ -169,7 +168,7 @@ First written: 2020/7/19
{{ account.title|title_case }} {{ account.title|title_case }}
<div class="float-right"> <div class="float-right">
<span class="badge {% if account.amount < 0 %} badge-warning {% else %} badge-secondary {% endif %} badge-pill"> <span class="badge {% if account.amount < 0 %} badge-warning {% else %} badge-secondary {% endif %} badge-pill">
{{ account.amount|accounting_amount }} {{ account.amount|short_amount }}
</span> </span>
</div> </div>
</a> </a>
@ -179,7 +178,7 @@ First written: 2020/7/19
{{ _("Total")|force_escape }} {{ _("Total")|force_escape }}
<div class="float-right"> <div class="float-right">
<span class="badge {% if group.amount < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill"> <span class="badge {% if group.amount < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill">
{{ group.amount|accounting_amount }} {{ group.amount|short_amount }}
</span> </span>
</div> </div>
</li> </li>
@ -197,7 +196,7 @@ First written: 2020/7/19
{{ section.cumulative_total.title|title_case }} {{ section.cumulative_total.title|title_case }}
<div class="float-right"> <div class="float-right">
<span class="badge {% if section.cumulative_total.amount < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill"> <span class="badge {% if section.cumulative_total.amount < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill">
{{ section.cumulative_total.amount|accounting_amount }} {{ section.cumulative_total.amount|short_amount }}
</span> </span>
</div> </div>
</li> </li>

View File

@ -22,7 +22,6 @@ First written: 2020/7/17
{% endcomment %} {% endcomment %}
{% load static %} {% load static %}
{% load i18n %} {% load i18n %}
{% load humanize %}
{% load mia_core %} {% load mia_core %}
{% load accounting %} {% load accounting %}
@ -140,12 +139,12 @@ First written: 2020/7/17
<div> <div>
{% if record.debit_amount is not None %} {% if record.debit_amount is not None %}
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ record.debit_amount|intcomma:False }} {{ record.debit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
{% if record.credit_amount is not None %} {% if record.credit_amount is not None %}
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
{{ record.credit_amount|intcomma:False }} {{ record.credit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
</div> </div>
@ -170,12 +169,12 @@ First written: 2020/7/17
<div> <div>
{% if record.debit_amount is not None %} {% if record.debit_amount is not None %}
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ record.debit_amount|intcomma:False }} {{ record.debit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
{% if record.credit_amount is not None %} {% if record.credit_amount is not None %}
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
{{ record.credit_amount|intcomma:False }} {{ record.credit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
</div> </div>

View File

@ -22,7 +22,6 @@ First written: 2020/7/16
{% endcomment %} {% endcomment %}
{% load static %} {% load static %}
{% load i18n %} {% load i18n %}
{% load humanize %}
{% load mia_core %} {% load mia_core %}
{% load accounting %} {% load accounting %}
@ -115,22 +114,22 @@ First written: 2020/7/16
{{ month.label }} {{ month.label }}
<div> <div>
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ month.debit|accounting_amount }} {{ month.debit|short_amount }}
</span> </span>
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
{{ month.credit|accounting_amount }} {{ month.credit|short_amount }}
</span> </span>
<span class="badge {% if month.balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill"> <span class="badge {% if month.balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill">
{{ month.balance|intcomma:False }} {{ month.balance|short_amount }}
</span> </span>
<span class="badge {% if month.cumulative_balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill"> <span class="badge {% if month.cumulative_balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill">
{{ month.cumulative_balance|intcomma:False }} {{ month.cumulative_balance|short_amount }}
</span> </span>
<span class="badge {% if month.balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill"> <span class="badge {% if month.balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill">
{{ month.balance|intcomma:False }} {{ month.balance|short_amount }}
</span> </span>
<span class="badge {% if month.cumulative_balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill"> <span class="badge {% if month.cumulative_balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill">
{{ month.cumulative_balance|intcomma:False }} {{ month.cumulative_balance|short_amount }}
</span> </span>
</div> </div>
</a> </a>
@ -139,16 +138,16 @@ First written: 2020/7/16
{{ month.label }} {{ month.label }}
<div> <div>
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ month.debit|accounting_amount }} {{ month.debit|short_amount }}
</span> </span>
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
{{ month.credit|accounting_amount }} {{ month.credit|short_amount }}
</span> </span>
<span class="badge {% if month.balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill"> <span class="badge {% if month.balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill">
{{ month.balance|intcomma:False }} {{ month.balance|short_amount }}
</span> </span>
<span class="badge {% if month.cumulative_balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill"> <span class="badge {% if month.cumulative_balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill">
{{ month.cumulative_balance|intcomma:False }} {{ month.cumulative_balance|short_amount }}
</span> </span>
</div> </div>
</div> </div>

View File

@ -22,7 +22,6 @@ First written: 2020/7/16
{% endcomment %} {% endcomment %}
{% load static %} {% load static %}
{% load i18n %} {% load i18n %}
{% load humanize %}
{% load mia_core %} {% load mia_core %}
{% load accounting %} {% load accounting %}
@ -170,16 +169,16 @@ First written: 2020/7/16
<div> <div>
{% if record.debit_amount is not None %} {% if record.debit_amount is not None %}
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ record.debit_amount|intcomma:False }} {{ record.debit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
{% if record.credit_amount is not None %} {% if record.credit_amount is not None %}
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
{{ record.credit_amount|intcomma:False }} {{ record.credit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
<span class="badge {% if record.balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill"> <span class="badge {% if record.balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill">
{{ record.balance|intcomma:False }} {{ record.balance|short_amount }}
</span> </span>
</div> </div>
</div> </div>
@ -195,16 +194,16 @@ First written: 2020/7/16
<div> <div>
{% if record.debit_amount is not None %} {% if record.debit_amount is not None %}
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ record.debit_amount|intcomma:False }} {{ record.debit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
{% if record.credit_amount is not None %} {% if record.credit_amount is not None %}
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
{{ record.credit_amount|intcomma:False }} {{ record.credit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
<span class="badge {% if record.balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill"> <span class="badge {% if record.balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill">
{{ record.balance|intcomma:False }} {{ record.balance|short_amount }}
</span> </span>
</div> </div>
</div> </div>

View File

@ -22,7 +22,6 @@ First written: 2020/7/19
{% endcomment %} {% endcomment %}
{% load static %} {% load static %}
{% load i18n %} {% load i18n %}
{% load humanize %}
{% load mia_core %} {% load mia_core %}
{% load accounting %} {% load accounting %}
@ -131,12 +130,12 @@ First written: 2020/7/19
<div> <div>
{% if account.debit_amount is not None %} {% if account.debit_amount is not None %}
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ account.debit_amount|intcomma:False }} {{ account.debit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
{% if account.credit_amount is not None %} {% if account.credit_amount is not None %}
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
{{ account.credit_amount|intcomma:False }} {{ account.credit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
</div> </div>
@ -147,10 +146,10 @@ First written: 2020/7/19
{{ _("Total")|force_escape }} {{ _("Total")|force_escape }}
<div> <div>
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ total_item.debit_amount|intcomma:False }} {{ total_item.debit_amount|short_amount }}
</span> </span>
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
{{ total_item.credit_amount|intcomma:False }} {{ total_item.credit_amount|short_amount }}
</span> </span>
</div> </div>
</li> </li>

View File

@ -22,7 +22,6 @@ First written: 2020/7/21
{% endcomment %} {% endcomment %}
{% load static %} {% load static %}
{% load i18n %} {% load i18n %}
{% load humanize %}
{% load mia_core %} {% load mia_core %}
{% load accounting %} {% load accounting %}
@ -136,12 +135,12 @@ First written: 2020/7/21
<div> <div>
{% if record.debit_amount is not None %} {% if record.debit_amount is not None %}
<span class="badge badge-success badge-pill"> <span class="badge badge-success badge-pill">
{{ record.debit_amount|intcomma:False }} {{ record.debit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
{% if record.credit_amount is not None %} {% if record.credit_amount is not None %}
<span class="badge badge-warning badge-pill"> <span class="badge badge-warning badge-pill">
{{ record.credit_amount|intcomma:False }} {{ record.credit_amount|short_amount }}
</span> </span>
{% endif %} {% endif %}
</div> </div>

View File

@ -32,20 +32,64 @@ from mia_core.period import Period
register = template.Library() register = template.Library()
@register.filter def _format_positive_amount(value: Union[str, Decimal]) -> str:
def accounting_amount(value: Union[str, Decimal]) -> str: """Formats a positive amount, groups every 3 digits by commas.
if value is None:
return "" Args:
if value == 0: value: The amount.
return "-"
s = str(abs(value)) Returns:
ReportUrl: The formatted amount.
"""
s = str(value)
while True: while True:
m = re.match("^([1-9][0-9]*)([0-9]{3}.*)", s) m = re.match("^([1-9][0-9]*)([0-9]{3}.*)", s)
if m is None: if m is None:
break break
s = m.group(1) + "," + m.group(2) 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: 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 return s