2020-06-30 20:59:24 +08:00
|
|
|
# The accounting application of the Mia project.
|
|
|
|
# by imacat <imacat@mail.imacat.idv.tw>, 2020/6/30
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
"""The view controllers of the accounting application.
|
|
|
|
|
|
|
|
"""
|
2020-07-08 10:52:03 +08:00
|
|
|
from datetime import date
|
2020-06-30 20:59:24 +08:00
|
|
|
|
2020-07-06 00:10:38 +08:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
2020-06-29 19:41:52 +08:00
|
|
|
|
2020-06-30 20:59:24 +08:00
|
|
|
from django.urls import reverse
|
2020-07-08 00:14:36 +08:00
|
|
|
from django.utils import dateformat
|
2020-07-06 22:26:29 +08:00
|
|
|
from django.utils.decorators import method_decorator
|
2020-07-08 00:14:36 +08:00
|
|
|
from django.utils.timezone import localdate
|
2020-07-08 11:07:00 +08:00
|
|
|
from django.utils.translation import get_language, pgettext
|
2020-06-30 20:59:24 +08:00
|
|
|
from django.views import generic
|
2020-07-06 21:36:31 +08:00
|
|
|
from django.views.decorators.http import require_GET
|
2020-06-30 20:59:24 +08:00
|
|
|
|
2020-07-08 11:07:00 +08:00
|
|
|
from accounting.models import Record, Transaction, Subject
|
2020-07-08 10:52:03 +08:00
|
|
|
from mia_core.period import Period
|
2020-06-30 20:59:24 +08:00
|
|
|
from mia import settings
|
2020-07-06 22:26:29 +08:00
|
|
|
from mia_core.digest_auth import digest_login_required
|
2020-07-07 20:54:35 +08:00
|
|
|
from mia_core.utils import UrlBuilder, Pagination, \
|
2020-07-07 20:56:01 +08:00
|
|
|
PageNoOutOfRangeException
|
2020-06-30 20:59:24 +08:00
|
|
|
|
|
|
|
|
2020-07-06 21:36:31 +08:00
|
|
|
@require_GET
|
2020-07-06 22:26:29 +08:00
|
|
|
@digest_login_required
|
2020-06-30 20:59:24 +08:00
|
|
|
def home(request):
|
|
|
|
"""The accounting home page.
|
|
|
|
|
|
|
|
Returns:
|
2020-07-02 00:50:27 +08:00
|
|
|
HttpResponseRedirect: The redirection to the default
|
|
|
|
accounting report.
|
2020-06-30 20:59:24 +08:00
|
|
|
"""
|
|
|
|
return HttpResponseRedirect(reverse("accounting:cash.home"))
|
|
|
|
|
|
|
|
|
2020-07-06 21:36:31 +08:00
|
|
|
@require_GET
|
2020-07-06 22:26:29 +08:00
|
|
|
@digest_login_required
|
2020-06-30 20:59:24 +08:00
|
|
|
def cash_home(request):
|
|
|
|
"""The accounting cash report home page.
|
|
|
|
|
|
|
|
Returns:
|
2020-07-02 00:50:27 +08:00
|
|
|
HttpResponseRedirect: The redirection to the default subject
|
|
|
|
and month.
|
2020-06-30 20:59:24 +08:00
|
|
|
"""
|
|
|
|
subject_code = settings.ACCOUNTING["DEFAULT_CASH_SUBJECT"]
|
2020-07-08 00:14:36 +08:00
|
|
|
period_spec = dateformat.format(localdate(), "Y-m")
|
2020-06-30 20:59:24 +08:00
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse("accounting:cash", args=(subject_code, period_spec)))
|
|
|
|
|
|
|
|
|
2020-07-06 22:26:29 +08:00
|
|
|
@method_decorator(digest_login_required, name='dispatch')
|
2020-07-01 23:39:32 +08:00
|
|
|
class BaseReportView(generic.ListView):
|
|
|
|
"""A base account report.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
page_no (int): The specified page number
|
|
|
|
page_size (int): The specified page size
|
2020-07-08 10:52:03 +08:00
|
|
|
period (Period): The template period helper
|
2020-07-08 11:07:00 +08:00
|
|
|
subject (Subject): The currently-specified subject
|
2020-07-01 23:39:32 +08:00
|
|
|
"""
|
|
|
|
page_no = None
|
|
|
|
page_size = None
|
2020-07-07 21:04:07 +08:00
|
|
|
pagination = None
|
2020-07-08 10:52:03 +08:00
|
|
|
period = None
|
2020-07-08 11:07:00 +08:00
|
|
|
subject = None
|
2020-07-01 23:39:32 +08:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
"""Adds object_list to the context.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The request.
|
|
|
|
args (list): The remaining arguments.
|
|
|
|
kwargs (dict): The keyword arguments.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The response
|
|
|
|
"""
|
2020-07-06 00:10:38 +08:00
|
|
|
if request.user.is_anonymous:
|
|
|
|
return HttpResponse(status=401)
|
2020-07-01 23:39:32 +08:00
|
|
|
try:
|
|
|
|
self.page_size = int(request.GET["page-size"])
|
|
|
|
if self.page_size < 1:
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
str(UrlBuilder(request.get_full_path())
|
|
|
|
.del_param("page-size")))
|
|
|
|
except KeyError:
|
|
|
|
self.page_size = None
|
|
|
|
except ValueError:
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
str(UrlBuilder(request.get_full_path())
|
|
|
|
.del_param("page-size")))
|
|
|
|
try:
|
|
|
|
self.page_no = int(request.GET["page"])
|
|
|
|
if self.page_no < 1:
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
str(UrlBuilder(request.get_full_path())
|
|
|
|
.del_param("page")))
|
|
|
|
except KeyError:
|
|
|
|
self.page_no = None
|
|
|
|
except ValueError:
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
str(UrlBuilder(request.get_full_path())
|
|
|
|
.del_param("page")))
|
|
|
|
try:
|
2020-07-02 09:45:11 +08:00
|
|
|
r = super(BaseReportView, self) \
|
|
|
|
.get(request, *args, **kwargs)
|
2020-07-07 20:56:01 +08:00
|
|
|
except PageNoOutOfRangeException:
|
2020-07-01 23:39:32 +08:00
|
|
|
return HttpResponseRedirect(
|
|
|
|
str(UrlBuilder(request.get_full_path())
|
|
|
|
.del_param("page")))
|
2020-07-07 19:33:34 +08:00
|
|
|
return r
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2020-07-07 21:04:07 +08:00
|
|
|
data = super(BaseReportView, self).get_context_data(**kwargs)
|
2020-07-08 10:52:03 +08:00
|
|
|
data["period"] = self.period
|
2020-07-08 11:07:00 +08:00
|
|
|
data["subject"] = self.subject
|
2020-07-08 23:40:36 +08:00
|
|
|
data["pagination"] = self.pagination
|
2020-07-07 21:04:07 +08:00
|
|
|
return data
|
2020-07-01 23:39:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CashReportView(BaseReportView):
|
2020-06-30 20:59:24 +08:00
|
|
|
"""The accounting cash report."""
|
|
|
|
http_method_names = ["get"]
|
|
|
|
template_name = "accounting/cash.html"
|
|
|
|
context_object_name = "records"
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-06-30 23:49:21 +08:00
|
|
|
"""Return the accounting records for the cash report.
|
|
|
|
|
|
|
|
Returns:
|
2020-07-02 00:50:27 +08:00
|
|
|
List[Record]: The accounting records for the cash report
|
2020-06-30 23:49:21 +08:00
|
|
|
"""
|
2020-07-08 11:00:14 +08:00
|
|
|
first_txn = Transaction.objects.order_by("date").first()
|
|
|
|
data_start = first_txn.date if first_txn is not None else None
|
|
|
|
last_txn = Transaction.objects.order_by("-date").first()
|
|
|
|
data_end = last_txn.date if last_txn is not None else None
|
2020-07-08 10:52:03 +08:00
|
|
|
self.period = Period(
|
2020-07-08 11:00:14 +08:00
|
|
|
get_language(), data_start, data_end,
|
2020-07-08 10:52:03 +08:00
|
|
|
self.kwargs["period_spec"])
|
2020-06-30 20:59:24 +08:00
|
|
|
if self.kwargs["subject_code"] == "0":
|
2020-07-08 11:07:00 +08:00
|
|
|
self.subject = Subject(code="0")
|
|
|
|
self.subject.title_zhtw = pgettext(
|
|
|
|
"Accounting|", "Current assets and liabilities")
|
2020-06-30 20:59:24 +08:00
|
|
|
records = Record.objects.raw(
|
|
|
|
"""SELECT r.*
|
|
|
|
FROM accounting_records AS r
|
2020-06-30 23:49:21 +08:00
|
|
|
INNER JOIN (SELECT
|
|
|
|
t1.sn AS sn,
|
|
|
|
t1.date AS date,
|
|
|
|
t1.ord AS ord
|
|
|
|
FROM accounting_records AS r1
|
|
|
|
LEFT JOIN accounting_transactions AS t1
|
|
|
|
ON r1.transaction_sn=t1.sn
|
|
|
|
LEFT JOIN accounting_subjects AS s1
|
|
|
|
ON r1.subject_sn = s1.sn
|
|
|
|
WHERE (s1.code LIKE '11%%'
|
|
|
|
OR s1.code LIKE '12%%'
|
|
|
|
OR s1.code LIKE '21%%'
|
|
|
|
OR s1.code LIKE '22%%')
|
|
|
|
AND t1.date >= %s
|
2020-07-02 09:45:11 +08:00
|
|
|
AND t1.date <= %s
|
2020-06-30 23:49:21 +08:00
|
|
|
GROUP BY t1.sn) AS t
|
|
|
|
ON r.transaction_sn=t.sn
|
|
|
|
LEFT JOIN accounting_subjects AS s ON r.subject_sn = s.sn
|
2020-06-30 20:59:24 +08:00
|
|
|
WHERE s.code NOT LIKE '11%%'
|
|
|
|
AND s.code NOT LIKE '12%%'
|
|
|
|
AND s.code NOT LIKE '21%%'
|
|
|
|
AND s.code NOT LIKE '22%%'
|
2020-06-30 23:49:21 +08:00
|
|
|
ORDER BY
|
|
|
|
t.date,
|
|
|
|
t.ord,
|
|
|
|
CASE WHEN is_credit THEN 1 ELSE 2 END,
|
|
|
|
r.ord""",
|
2020-07-08 10:52:03 +08:00
|
|
|
[self.period.start, self.period.end])
|
2020-06-30 20:59:24 +08:00
|
|
|
else:
|
2020-07-08 11:07:00 +08:00
|
|
|
self.subject = Subject.objects.filter(
|
|
|
|
code=self.kwargs["subject_code"]).first()
|
2020-06-30 20:59:24 +08:00
|
|
|
records = Record.objects.raw(
|
|
|
|
"""SELECT r.*
|
|
|
|
FROM accounting_records AS r
|
2020-06-30 23:49:21 +08:00
|
|
|
INNER JOIN (SELECT
|
|
|
|
t1.sn AS sn,
|
|
|
|
t1.date AS date,
|
|
|
|
t1.ord AS ord
|
|
|
|
FROM accounting_records AS r1
|
|
|
|
LEFT JOIN accounting_transactions AS t1
|
|
|
|
ON r1.transaction_sn=t1.sn
|
|
|
|
LEFT JOIN accounting_subjects AS s1
|
|
|
|
ON r1.subject_sn = s1.sn
|
|
|
|
WHERE t1.date >= %s
|
2020-07-02 09:45:11 +08:00
|
|
|
AND t1.date <= %s
|
2020-06-30 23:49:21 +08:00
|
|
|
AND s1.code LIKE %s
|
|
|
|
GROUP BY t1.sn) AS t
|
|
|
|
ON r.transaction_sn=t.sn
|
|
|
|
LEFT JOIN accounting_subjects AS s ON r.subject_sn = s.sn
|
2020-06-30 20:59:24 +08:00
|
|
|
WHERE s.code NOT LIKE %s
|
2020-06-30 23:49:21 +08:00
|
|
|
ORDER BY
|
|
|
|
t.date,
|
|
|
|
t.ord,
|
|
|
|
CASE WHEN is_credit THEN 1 ELSE 2 END,
|
|
|
|
r.ord""",
|
2020-07-08 10:52:03 +08:00
|
|
|
[self.period.start,
|
|
|
|
self.period.end,
|
2020-07-08 11:07:00 +08:00
|
|
|
self.subject.code + "%",
|
|
|
|
self.subject.code + "%"])
|
2020-07-07 21:04:07 +08:00
|
|
|
self.pagination = Pagination(
|
2020-07-07 22:49:37 +08:00
|
|
|
self.request.get_full_path(), records,
|
|
|
|
self.page_no, self.page_size, True)
|
2020-07-07 21:04:07 +08:00
|
|
|
return self.pagination.records
|