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 %}
{% load static %}
{% load i18n %}
{% load humanize %}
{% load mia_core %}
{% load accounting %}
@ -122,16 +121,16 @@ First written: 2020/7/15
{{ month.label }}
<div>
<span class="badge badge-success badge-pill">
{{ month.credit|accounting_amount }}
{{ month.credit|short_amount }}
</span>
<span class="badge badge-warning badge-pill">
{{ month.debit|accounting_amount }}
{{ month.debit|short_amount }}
</span>
<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 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>
</div>
</a>
@ -140,16 +139,16 @@ First written: 2020/7/15
{{ month.label }}
<div>
<span class="badge badge-success badge-pill">
{{ month.credit|accounting_amount }}
{{ month.credit|short_amount }}
</span>
<span class="badge badge-warning badge-pill">
{{ month.debit|accounting_amount }}
{{ month.debit|short_amount }}
</span>
<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 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>
</div>
</div>

View File

@ -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
<div>
{% if record.credit_amount is not None %}
<span class="badge badge-success badge-pill">
{{ record.credit_amount|intcomma:False }}
{{ record.credit_amount|short_amount }}
</span>
{% endif %}
{% if record.debit_amount is not None %}
<span class="badge badge-warning badge-pill">
-{{ record.debit_amount|intcomma:False }}
-{{ record.debit_amount|short_amount }}
</span>
{% endif %}
<span class="badge {% if record.balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill">
{{ record.balance|intcomma:False }}
{{ record.balance|short_amount }}
</span>
</div>
</a>
@ -183,16 +182,16 @@ First written: 2020/7/1
<div>
{% if record.credit_amount is not None %}
<span class="badge badge-success badge-pill">
{{ record.credit_amount|intcomma:False }}
{{ record.credit_amount|short_amount }}
</span>
{% endif %}
{% if record.debit_amount is not None %}
<span class="badge badge-warning badge-pill">
-{{ record.debit_amount|intcomma:False }}
-{{ record.debit_amount|short_amount }}
</span>
{% endif %}
<span class="badge {% if record.balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill">
{{ record.balance|intcomma:False }}
{{ record.balance|short_amount }}
</span>
</div>
{% endif %}

View File

@ -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 }}
<div class="float-right">
<span class="badge {% if account.amount < 0 %} badge-warning {% else %} badge-secondary {% endif %} badge-pill">
{{ account.amount|accounting_amount }}
{{ account.amount|short_amount }}
</span>
</div>
</a>
@ -179,7 +178,7 @@ First written: 2020/7/19
{{ _("Total")|force_escape }}
<div class="float-right">
<span class="badge {% if group.amount < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill">
{{ group.amount|accounting_amount }}
{{ group.amount|short_amount }}
</span>
</div>
</li>
@ -197,7 +196,7 @@ First written: 2020/7/19
{{ section.cumulative_total.title|title_case }}
<div class="float-right">
<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>
</div>
</li>

View File

@ -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
<div>
{% if record.debit_amount is not None %}
<span class="badge badge-success badge-pill">
{{ record.debit_amount|intcomma:False }}
{{ record.debit_amount|short_amount }}
</span>
{% endif %}
{% if record.credit_amount is not None %}
<span class="badge badge-warning badge-pill">
{{ record.credit_amount|intcomma:False }}
{{ record.credit_amount|short_amount }}
</span>
{% endif %}
</div>
@ -170,12 +169,12 @@ First written: 2020/7/17
<div>
{% if record.debit_amount is not None %}
<span class="badge badge-success badge-pill">
{{ record.debit_amount|intcomma:False }}
{{ record.debit_amount|short_amount }}
</span>
{% endif %}
{% if record.credit_amount is not None %}
<span class="badge badge-warning badge-pill">
{{ record.credit_amount|intcomma:False }}
{{ record.credit_amount|short_amount }}
</span>
{% endif %}
</div>

View File

@ -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 }}
<div>
<span class="badge badge-success badge-pill">
{{ month.debit|accounting_amount }}
{{ month.debit|short_amount }}
</span>
<span class="badge badge-warning badge-pill">
{{ month.credit|accounting_amount }}
{{ month.credit|short_amount }}
</span>
<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 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 class="badge {% if month.balance < 0 %} badge-danger {% else %} badge-info {% endif %} badge-pill">
{{ month.balance|intcomma:False }}
{{ month.balance|short_amount }}
</span>
<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>
</div>
</a>
@ -139,16 +138,16 @@ First written: 2020/7/16
{{ month.label }}
<div>
<span class="badge badge-success badge-pill">
{{ month.debit|accounting_amount }}
{{ month.debit|short_amount }}
</span>
<span class="badge badge-warning badge-pill">
{{ month.credit|accounting_amount }}
{{ month.credit|short_amount }}
</span>
<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 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>
</div>
</div>

View File

@ -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
<div>
{% if record.debit_amount is not None %}
<span class="badge badge-success badge-pill">
{{ record.debit_amount|intcomma:False }}
{{ record.debit_amount|short_amount }}
</span>
{% endif %}
{% if record.credit_amount is not None %}
<span class="badge badge-warning badge-pill">
{{ record.credit_amount|intcomma:False }}
{{ record.credit_amount|short_amount }}
</span>
{% endif %}
<span class="badge {% if record.balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill">
{{ record.balance|intcomma:False }}
{{ record.balance|short_amount }}
</span>
</div>
</div>
@ -195,16 +194,16 @@ First written: 2020/7/16
<div>
{% if record.debit_amount is not None %}
<span class="badge badge-success badge-pill">
{{ record.debit_amount|intcomma:False }}
{{ record.debit_amount|short_amount }}
</span>
{% endif %}
{% if record.credit_amount is not None %}
<span class="badge badge-warning badge-pill">
{{ record.credit_amount|intcomma:False }}
{{ record.credit_amount|short_amount }}
</span>
{% endif %}
<span class="badge {% if record.balance < 0 %} badge-danger {% else %} badge-primary {% endif %} badge-pill">
{{ record.balance|intcomma:False }}
{{ record.balance|short_amount }}
</span>
</div>
</div>

View File

@ -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
<div>
{% if account.debit_amount is not None %}
<span class="badge badge-success badge-pill">
{{ account.debit_amount|intcomma:False }}
{{ account.debit_amount|short_amount }}
</span>
{% endif %}
{% if account.credit_amount is not None %}
<span class="badge badge-warning badge-pill">
{{ account.credit_amount|intcomma:False }}
{{ account.credit_amount|short_amount }}
</span>
{% endif %}
</div>
@ -147,10 +146,10 @@ First written: 2020/7/19
{{ _("Total")|force_escape }}
<div>
<span class="badge badge-success badge-pill">
{{ total_item.debit_amount|intcomma:False }}
{{ total_item.debit_amount|short_amount }}
</span>
<span class="badge badge-warning badge-pill">
{{ total_item.credit_amount|intcomma:False }}
{{ total_item.credit_amount|short_amount }}
</span>
</div>
</li>

View File

@ -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
<div>
{% if record.debit_amount is not None %}
<span class="badge badge-success badge-pill">
{{ record.debit_amount|intcomma:False }}
{{ record.debit_amount|short_amount }}
</span>
{% endif %}
{% if record.credit_amount is not None %}
<span class="badge badge-warning badge-pill">
{{ record.credit_amount|intcomma:False }}
{{ record.credit_amount|short_amount }}
</span>
{% endif %}
</div>

View File

@ -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