diff --git a/accounting/forms.py b/accounting/forms.py index 81d725c..86e1ffa 100644 --- a/accounting/forms.py +++ b/accounting/forms.py @@ -294,7 +294,7 @@ class TransactionForm(forms.Form): int: The total amount of the credit records. """ return sum([int(x.data["amount"]) for x in self.debit_records - if "amount" not in x.errors]) + if "amount" in x.data and "amount" not in x.errors]) def credit_total(self): """Returns the total amount of the credit records. @@ -303,7 +303,7 @@ class TransactionForm(forms.Form): int: The total amount of the credit records. """ return sum([int(x.data["amount"]) for x in self.credit_records - if "amount" not in x.errors]) + if "amount" in x.data and "amount" not in x.errors]) class AccountForm(forms.Form): diff --git a/accounting/templates/accounting/include/record_form-non-transfer.html b/accounting/templates/accounting/include/record_form-non-transfer.html index 27997e7..0c6cec4 100644 --- a/accounting/templates/accounting/include/record_form-non-transfer.html +++ b/accounting/templates/accounting/include/record_form-non-transfer.html @@ -20,8 +20,8 @@ Author: imacat@mail.imacat.idv.tw (imacat) First written: 2020/8/5 {% endcomment %} -
  • -
    {% if should_validate and record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}
    +
  • +
    {% if record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}
    @@ -29,7 +29,7 @@ First written: 2020/8/5 {% endif %} - {% if record.account is not None %} {% else %} @@ -37,17 +37,17 @@ First written: 2020/8/5 {% endif %} -
    {% if should_validate %}{{ record.account.errors.0|default:"" }}{% endif %}
    +
    {{ record.account.errors.0|default:"" }}
    - -
    {% if should_validate %}{{ record.summary.errors.0|default:"" }}{% endif %}
    + +
    {{ record.summary.errors.0|default:"" }}
    - -
    {% if should_validate %}{{ record.amount.errors.0|default:"" }}{% endif %}
    + +
    {{ record.amount.errors.0|default:"" }}
    diff --git a/accounting/templates/accounting/include/record_form-transfer.html b/accounting/templates/accounting/include/record_form-transfer.html index 083e3b1..c315883 100644 --- a/accounting/templates/accounting/include/record_form-transfer.html +++ b/accounting/templates/accounting/include/record_form-transfer.html @@ -20,8 +20,8 @@ Author: imacat@mail.imacat.idv.tw (imacat) First written: 2020/8/5 {% endcomment %} -
  • -
    {% if should_validate and record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}
    +
  • +
    {% if record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}
    @@ -30,7 +30,7 @@ First written: 2020/8/5 {% endif %} - {% if record.account is not None %} {% else %} @@ -38,17 +38,17 @@ First written: 2020/8/5 {% endif %} -
    {% if should_validate %}{{ record.account.errors.0|default:"" }}{% endif %}
    +
    {{ record.account.errors.0|default:"" }}
    - -
    {% if should_validate %}{{ record.summary.errors.0|default:"" }}{% endif %}
    + +
    {{ record.summary.errors.0|default:"" }}
    - -
    {% if should_validate %}{{ record.amount.errors.0|default:"" }}{% endif %}
    + +
    {{ record.amount.errors.0|default:"" }}
    diff --git a/accounting/templates/accounting/transaction_form-expense.html b/accounting/templates/accounting/transaction_form-expense.html index 1fef60b..df78244 100644 --- a/accounting/templates/accounting/transaction_form-expense.html +++ b/accounting/templates/accounting/transaction_form-expense.html @@ -62,8 +62,9 @@ First written: 2020/7/23
    - -
    {% if should_validate %}{{ form.date.errors.0|default:"" }}{% endif %}
    + {% now "Y-m-d" as today %} + +
    {{ form.date.errors.0|default:"" }}
    @@ -97,8 +98,8 @@ First written: 2020/7/23
    - -
    {% if should_validate %}{{ form.notes.errors.0|default:"" }}{% endif %}
    + +
    {{ form.notes.errors.0|default:"" }}
    diff --git a/accounting/templates/accounting/transaction_form-income.html b/accounting/templates/accounting/transaction_form-income.html index 82ba7ae..a380b7a 100644 --- a/accounting/templates/accounting/transaction_form-income.html +++ b/accounting/templates/accounting/transaction_form-income.html @@ -62,8 +62,8 @@ First written: 2020/7/23
    - -
    {% if should_validate %}{{ form.date.errors.0|default:"" }}{% endif %}
    + +
    {{ form.date.errors.0|default:"" }}
    @@ -97,8 +97,8 @@ First written: 2020/7/23
    - -
    {% if should_validate %}{{ form.notes.errors.0|default:"" }}{% endif %}
    + +
    {{ form.notes.errors.0|default:"" }}
    diff --git a/accounting/templates/accounting/transaction_form-transfer.html b/accounting/templates/accounting/transaction_form-transfer.html index 22c4066..31d743c 100644 --- a/accounting/templates/accounting/transaction_form-transfer.html +++ b/accounting/templates/accounting/transaction_form-transfer.html @@ -62,8 +62,8 @@ First written: 2020/7/23
    - -
    {% if should_validate %}{{ form.date.errors.0|default:"" }}{% endif %}
    + +
    {{ form.date.errors.0|default:"" }}
    @@ -85,11 +85,11 @@ First written: 2020/7/23
  • -
    +
    {{ _("Total")|force_escape }} {{ form.debit_total }}
    -
    {% if should_validate %}{{ form.balance_error|default:"" }}{% endif %}
    +
    {{ form.balance_error|default:"" }}
  • @@ -111,11 +111,11 @@ First written: 2020/7/23
  • -
    +
    {{ _("Total")|force_escape }} {{ form.credit_total }}
    -
    {% if should_validate %}{{ form.balance_error|default:"" }}{% endif %}
    +
    {{ form.balance_error|default:"" }}
  • @@ -126,8 +126,8 @@ First written: 2020/7/23
    - -
    {% if should_validate %}{{ form.notes.errors.0|default:"" }}{% endif %}
    + +
    {{ form.notes.errors.0|default:"" }}
    diff --git a/accounting/views.py b/accounting/views.py index 9536b30..068dd80 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -42,7 +42,7 @@ from mia_core.period import Period from mia_core.utils import Pagination, get_multi_lingual_search, UrlBuilder, \ strip_post, new_pk, PaginationException from mia_core.views import DeleteView -from .forms import AccountForm +from .forms import AccountForm, TransactionForm, RecordForm from .models import Record, Transaction, Account from .utils import get_cash_accounts, get_ledger_accounts, \ find_imbalanced, find_order_holes, fill_txn_from_post, \ @@ -827,16 +827,13 @@ def txn_form(request, txn_type, txn=None): HttpResponse: The response. """ form = make_txn_form_from_status(request, txn_type, txn) - should_validate = True if form is None: if txn is None: - txn = Transaction(date=timezone.localdate()) - should_validate = False - if len(txn.debit_records) == 0: - txn.records.append(Record(ord=1, is_credit=False)) - if len(txn.credit_records) == 0: - txn.records.append(Record(ord=1, is_credit=True)) - form = make_txn_form_from_model(txn_type, txn) + form = TransactionForm() + form.debit_records.append(RecordForm()) + form.credit_records.append(RecordForm()) + else: + form = make_txn_form_from_model(txn_type, txn) new_record_context = {"record": Record(), "record_type": "TTT", "no": "NNN", @@ -852,7 +849,6 @@ def txn_form(request, txn_type, txn=None): return render(request, F"accounting/transaction_form-{txn_type}.html", { "form": form, "summary_categories": get_summary_categories, - "should_validate": should_validate, "new_record_template": new_record_template, })