Added the short_value template filter that strips the excess trailing decimal zeros in the accounting form in the accounting application.
This commit is contained in:
		@@ -19,6 +19,7 @@ form-record-non-transfer.html: The template for a record in the non-transfer tra
 | 
			
		||||
Author: imacat@mail.imacat.idv.tw (imacat)
 | 
			
		||||
First written: 2020/8/5
 | 
			
		||||
{% endcomment %}
 | 
			
		||||
{% load accounting %}
 | 
			
		||||
 | 
			
		||||
<li id="{{ record_type }}-{{ no }}" class="list-group-item {% if record.non_field_errors %} list-group-item-danger {% endif %} draggable-record {{ record_type }}-record" data-no="{{ no }}">
 | 
			
		||||
  <div id="{{ record_type }}-{{ no }}-error">{% if record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}</div>
 | 
			
		||||
@@ -37,7 +38,7 @@ First written: 2020/8/5
 | 
			
		||||
          </div>
 | 
			
		||||
          <div class="col-sm-4">
 | 
			
		||||
            <label for="{{ record_type }}-{{ no }}-amount" class="record-label">{{ _("Amount:")|force_escape }}</label>
 | 
			
		||||
            <input id="{{ record_type }}-{{ no }}-amount" class="form-control record-amount {{ record_type }}-to-sum {% if record.amount.errors %} is-invalid {% endif %}" type="number" step="0.01" min="0.01" name="{{ record_type }}-{{ no }}-amount" value="{{ record.amount.value|default:"" }}" required="required" data-type="{{ record_type }}" />
 | 
			
		||||
            <input id="{{ record_type }}-{{ no }}-amount" class="form-control record-amount {{ record_type }}-to-sum {% if record.amount.errors %} is-invalid {% endif %}" type="number" step="0.01" min="0.01" name="{{ record_type }}-{{ no }}-amount" value="{{ record.amount.value|short_value }}" required="required" data-type="{{ record_type }}" />
 | 
			
		||||
            <div id="{{ record_type }}-{{ no }}-amount-error" class="invalid-feedback">{{ record.amount.errors.0|default:"" }}</div>
 | 
			
		||||
          </div>
 | 
			
		||||
        </div>
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,7 @@ form-record-transfer.html: The template for a record in the transfer transaction
 | 
			
		||||
Author: imacat@mail.imacat.idv.tw (imacat)
 | 
			
		||||
First written: 2020/8/5
 | 
			
		||||
{% endcomment %}
 | 
			
		||||
{% load accounting %}
 | 
			
		||||
 | 
			
		||||
<li id="{{ record_type }}-{{ no }}" class="list-group-item {% if record.non_field_errors %} list-group-item-danger {% endif %} draggable-record {{ record_type }}-record" data-no="{{ no }}">
 | 
			
		||||
  <div id="{{ record_type }}-{{ no }}-error">{% if record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}</div>
 | 
			
		||||
@@ -36,7 +37,7 @@ First written: 2020/8/5
 | 
			
		||||
        </div>
 | 
			
		||||
        <div class="col-lg-4">
 | 
			
		||||
          <label for="{{ record_type }}-{{ no }}-amount" class="record-label">{{ _("Amount:")|force_escape }}</label>
 | 
			
		||||
          <input id="{{ record_type }}-{{ no }}-amount" class="form-control record-amount {{ record_type }}-to-sum {% if record.amount.errors %} is-invalid {% endif %}" type="number" step="0.01" min="0.01" name="{{ record_type }}-{{ no }}-amount" value="{{ record.amount.value|default:"" }}" required="required" data-type="{{ record_type }}" />
 | 
			
		||||
          <input id="{{ record_type }}-{{ no }}-amount" class="form-control record-amount {{ record_type }}-to-sum {% if record.amount.errors %} is-invalid {% endif %}" type="number" step="0.01" min="0.01" name="{{ record_type }}-{{ no }}-amount" value="{{ record.amount.value|short_value }}" required="required" data-type="{{ record_type }}" />
 | 
			
		||||
          <div id="{{ record_type }}-{{ no }}-amount-error" class="invalid-feedback">{{ record.amount.errors.0|default:"" }}</div>
 | 
			
		||||
        </div>
 | 
			
		||||
      </div>
 | 
			
		||||
 
 | 
			
		||||
@@ -32,6 +32,21 @@ from mia_core.period import Period
 | 
			
		||||
register = template.Library()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _strip_decimal_zeros(value: Decimal) -> str:
 | 
			
		||||
    """Formats a decimal value, stripping excess decimal zeros.
 | 
			
		||||
 | 
			
		||||
    Args:
 | 
			
		||||
        value: The value.
 | 
			
		||||
 | 
			
		||||
    Returns:
 | 
			
		||||
        str: The value with excess decimal zeros stripped.
 | 
			
		||||
    """
 | 
			
		||||
    s = str(value)
 | 
			
		||||
    s = re.sub(r"^(.*\.[0-9]*?)0+$", r"\1", s)
 | 
			
		||||
    s = re.sub(r"^(.*)\.$", r"\1", s)
 | 
			
		||||
    return s
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _format_positive_amount(value: Decimal) -> str:
 | 
			
		||||
    """Formats a positive amount, groups every 3 digits by commas.
 | 
			
		||||
 | 
			
		||||
@@ -41,14 +56,12 @@ def _format_positive_amount(value: Decimal) -> str:
 | 
			
		||||
    Returns:
 | 
			
		||||
        str: The amount in the desired format.
 | 
			
		||||
    """
 | 
			
		||||
    s = str(value)
 | 
			
		||||
    s = _strip_decimal_zeros(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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -93,6 +106,21 @@ def short_amount(value: Optional[Decimal]) -> str:
 | 
			
		||||
    return s
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@register.filter
 | 
			
		||||
def short_value(value: Optional[Decimal]) -> str:
 | 
			
		||||
    """Formats a decimal value, stripping excess decimal zeros.
 | 
			
		||||
 | 
			
		||||
    Args:
 | 
			
		||||
        value: The value.
 | 
			
		||||
 | 
			
		||||
    Returns:
 | 
			
		||||
        str: The value with excess decimal zeroes stripped.
 | 
			
		||||
    """
 | 
			
		||||
    if value is None:
 | 
			
		||||
        return ""
 | 
			
		||||
    return _strip_decimal_zeros(value)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@register.simple_tag(takes_context=True)
 | 
			
		||||
def report_url(context: RequestContext,
 | 
			
		||||
               cash_account: Optional[Account],
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user