From 99fb99b160dcfed68f0a1768a3483e8e3f2ae8df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Mon, 27 Jul 2020 22:36:28 +0800 Subject: [PATCH] Added the view and the templates for the forms of the transactions in the accounting application. --- accounting/models.py | 20 +- .../accounting/transactions/expense/edit.html | 151 +++++++++++++ .../accounting/transactions/income/edit.html | 151 +++++++++++++ .../transactions/transfer/edit.html | 213 ++++++++++++++++++ accounting/urls.py | 4 +- accounting/views.py | 23 ++ mia_core/templatetags/mia_core.py | 21 ++ 7 files changed, 577 insertions(+), 6 deletions(-) create mode 100644 accounting/templates/accounting/transactions/expense/edit.html create mode 100644 accounting/templates/accounting/transactions/income/edit.html create mode 100644 accounting/templates/accounting/transactions/transfer/edit.html diff --git a/accounting/models.py b/accounting/models.py index 5ec789d..eec35bd 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -106,7 +106,7 @@ class Transaction(models.Model): list[Record]: The records. """ if self._records is None: - self._records = self.record_set.all() + self._records = list(self.record_set.all()) return self._records @records.setter @@ -115,23 +115,33 @@ class Transaction(models.Model): @property def debit_records(self): - """The debit records of this transaction.""" + """The debit records of this transaction. + + Returns: + list[Record]: The records. + """ return [x for x in self.records if not x.is_credit] @property def debit_total(self): """The total amount of the debit records.""" - return sum([x.amount for x in self.debit_records]) + return sum([x.amount for x in self.debit_records + if isinstance(x.amount, int)]) @property def credit_records(self): - """The credit records of this transaction.""" + """The credit records of this transaction. + + Returns: + list[Record]: The records. + """ return [x for x in self.records if x.is_credit] @property def credit_total(self): """The total amount of the credit records.""" - return sum([x.amount for x in self.credit_records]) + return sum([x.amount for x in self.credit_records + if isinstance(x.amount, int)]) _is_balanced = None diff --git a/accounting/templates/accounting/transactions/expense/edit.html b/accounting/templates/accounting/transactions/expense/edit.html new file mode 100644 index 0000000..27911a9 --- /dev/null +++ b/accounting/templates/accounting/transactions/expense/edit.html @@ -0,0 +1,151 @@ +{% extends "base.html" %} +{% comment %} +The Mia Accounting Application +edit.html: The template for the form of a cash-expense transaction + + Copyright (c) 2020 imacat. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +Author: imacat@mail.imacat.idv.tw (imacat) +First written: 2020/7/23 +{% endcomment %} +{% load static %} +{% load i18n %} +{% load mia_core %} +{% load accounting %} + +{% block settings %} + {% trans "Cash Expense Transaction" context "Accounting|" as title %} + {% setvar "title" title %} +{% endblock %} + +{% block content %} + +
+ + + {% trans "Back" context "Navigation|" as text %}{{ text|force_escape }} + +
+ +
+ {% csrf_token %} + {# TODO: To be done #} + + + + + +
+
+ +
+
+ +
{{ errors.date|default:"" }}
+
+
+ +
+
+
    + {% for x in item.debit_records %} +
  • +
    +
    + {% if x.pk is not None %} + + {% endif %} + + +
    {{ errors.debit.account|index:forloop.counter }}
    +
    +
    +
    +
    + +
    {{ errors.debit.summary|index:forloop.counter }}
    +
    +
    + +
    {{ errors.debit.amount|index:forloop.counter }}
    +
    +
    +
    +
    +
    +
    + + +
    +
    + + +
    +
    +
  • + {% endfor %} +
+
    +
  • + +
  • +
  • +
    + {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} + {{ item.debit_total }} +
    +
  • +
+
+
+ +
+
+ +
+
+ +
{{ errors.notes|default:"" }}
+
+
+ +
+
+ +
+
+
+ +{% endblock %} diff --git a/accounting/templates/accounting/transactions/income/edit.html b/accounting/templates/accounting/transactions/income/edit.html new file mode 100644 index 0000000..31d09a6 --- /dev/null +++ b/accounting/templates/accounting/transactions/income/edit.html @@ -0,0 +1,151 @@ +{% extends "base.html" %} +{% comment %} +The Mia Accounting Application +edit.html: The template for the form of a cash-income transaction + + Copyright (c) 2020 imacat. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +Author: imacat@mail.imacat.idv.tw (imacat) +First written: 2020/7/23 +{% endcomment %} +{% load static %} +{% load i18n %} +{% load mia_core %} +{% load accounting %} + +{% block settings %} + {% trans "Cash Income Transaction" context "Accounting|" as title %} + {% setvar "title" title %} +{% endblock %} + +{% block content %} + +
+ + + {% trans "Back" context "Navigation|" as text %}{{ text|force_escape }} + +
+ +
+ {% csrf_token %} + {# TODO: To be done #} + + + + + +
+
+ +
+
+ +
{{ errors.date|default:"" }}
+
+
+ +
+
+
    + {% for x in item.credit_records %} +
  • +
    +
    + {% if x.pk is not None %} + + {% endif %} + + +
    {{ errors.credit.account|index:forloop.counter }}
    +
    +
    +
    +
    + +
    {{ errors.credit.summary|index:forloop.counter }}
    +
    +
    + +
    {{ errors.credit.amount|index:forloop.counter }}
    +
    +
    +
    +
    +
    +
    + + +
    +
    + + +
    +
    +
  • + {% endfor %} +
+
    +
  • + +
  • +
  • +
    + {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} + {{ item.credit_total }} +
    +
  • +
+
+
+ +
+
+ +
+
+ +
{{ errors.notes|default:"" }}
+
+
+ +
+
+ +
+
+
+ +{% endblock %} diff --git a/accounting/templates/accounting/transactions/transfer/edit.html b/accounting/templates/accounting/transactions/transfer/edit.html new file mode 100644 index 0000000..a538721 --- /dev/null +++ b/accounting/templates/accounting/transactions/transfer/edit.html @@ -0,0 +1,213 @@ +{% extends "base.html" %} +{% comment %} +The Mia Accounting Application +edit.html: The template for the form of a transfer transaction + + Copyright (c) 2020 imacat. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +Author: imacat@mail.imacat.idv.tw (imacat) +First written: 2020/7/23 +{% endcomment %} +{% load static %} +{% load i18n %} +{% load mia_core %} +{% load accounting %} + +{% block settings %} + {% trans "Transfer Transaction" context "Accounting|" as title %} + {% setvar "title" title %} +{% endblock %} + +{% block content %} + +
+ + + {% trans "Back" context "Navigation|" as text %}{{ text|force_escape }} + +
+ +
+ {% csrf_token %} + {# TODO: To be done #} + + + + + + + + +
+
+ +
+
+ +
{{ errors.date|default:"" }}
+
+
+ +
+
+

{% trans "Debit" context "Accounting|" as text %}{{ text|force_escape }}

+ +
    + {% for x in item.debit_records %} +
  • +
    +
    +
    + {% if x.pk is not None %} + + {% endif %} + + +
    {{ errors.debit.account|index:forloop.counter }}
    +
    +
    +
    +
    + +
    {{ errors.debit.summary|index:forloop.counter }}
    +
    +
    + +
    {{ errors.debit.amount|index:forloop.counter }}
    +
    +
    +
    +
    +
    + + +
    +
    +
  • + {% endfor %} +
+
    +
  • + +
  • +
  • +
    + {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} + {{ item.debit_total }} +
    +
    {{ errors.balance }}
    +
  • +
+
+ +
+

{% trans "Credit" context "Accounting|" as text %}{{ text|force_escape }}

+ +
    + {% for x in item.credit_records %} +
  • +
    +
    +
    + {% if x.pk is not None %} + + {% endif %} + + +
    {{ errors.credit.account|index:forloop.counter }}
    +
    +
    +
    +
    + +
    {{ errors.credit.summary|index:forloop.counter }}
    +
    +
    + +
    {{ errors.credit.amount|index:forloop.counter }}
    +
    +
    +
    +
    +
    + + +
    +
    +
  • + {% endfor %} +
+
    +
  • + +
  • +
  • +
    + {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} + {{ item.credit_total }} +
    +
    {{ errors.balance }}
    +
  • +
+
+
+ +
+
+ +
+
+ +
{{ errors.notes|default:"" }}
+
+
+ +
+
+ +
+
+
+ +{% endblock %} diff --git a/accounting/urls.py b/accounting/urls.py index 9d382e5..67b1c82 100644 --- a/accounting/urls.py +++ b/accounting/urls.py @@ -68,7 +68,7 @@ urlpatterns = [ path("search", views.search, name="search"), path("transactions//create", - mia_core_views.todo, name="transactions.create"), + views.transaction_create, name="transactions.create"), path("transactions//store", mia_core_views.todo, name="transactions.store"), path("transactions//", @@ -95,4 +95,6 @@ urlpatterns = [ mia_core_views.todo, name="accounts.update"), path("accounts//delete", mia_core_views.todo, name="accounts.delete"), + path("accounts/options", + mia_core_views.todo, name="accounts.options"), ] diff --git a/accounting/views.py b/accounting/views.py index 0e61ac4..6c4c3c3 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -859,3 +859,26 @@ def transaction_show(request, type, transaction): "transaction_type": type, "item": transaction, }) + + +@require_GET +@digest_login_required +def transaction_create(request, type): + """The view to create an accounting transaction. + + Args: + request (HttpRequest): The request. + type (str): The transaction type. + + Returns: + HttpResponse: The response. + """ + transaction = Transaction() + if len(transaction.debit_records) == 0: + transaction.records.append(Record(ord=1, is_credit=False)) + if len(transaction.credit_records) == 0: + transaction.records.append(Record(ord=1, is_credit=True)) + return render(request, F"accounting/transactions/{type}/edit.html", { + "transaction_type": type, + "item": transaction, + }) diff --git a/mia_core/templatetags/mia_core.py b/mia_core/templatetags/mia_core.py index 10d8eb8..d501389 100644 --- a/mia_core/templatetags/mia_core.py +++ b/mia_core/templatetags/mia_core.py @@ -153,3 +153,24 @@ def smart_month(value): if value.year == year and value.month == month: return gettext("Last Month") return defaultfilters.date(value, "Y/n") + + +@register.filter() +def index(value, arg): + """Returns the arg-th element of the value list or tuple. + + Args: + value (list|tuple): The list or tuple. + arg (int): The index. + + Returns: + any: The arg-th element of the value + """ + if not (isinstance(value, list) or isinstance(value, tuple)): + return None + if not isinstance(arg, int): + return None + if arg >= len(value): + return None + return value[arg] +