Added the subject selector for the cash account in the accounting application.

This commit is contained in:
依瑪貓 2020-07-14 21:27:30 +08:00
parent c42120d1a7
commit e00cf7656b
2 changed files with 50 additions and 15 deletions

View File

@ -25,7 +25,7 @@ First written: 2020/7/1
{% load accounting %} {% load accounting %}
{% block settings %} {% block settings %}
{% blocktrans asvar title with subject=subject.title_zhtw period=period.description context "Accounting|" %}Cash Account for {{ subject }} in {{ period }}{% endblocktrans %} {% blocktrans asvar title with subject=current_subject.title_zhtw period=period.description context "Accounting|" %}Cash Account for {{ subject }} in {{ period }}{% endblocktrans %}
{% setvar "title" title %} {% setvar "title" title %}
{% setvar "use_period_chooser" True %} {% setvar "use_period_chooser" True %}
{% endblock %} {% endblock %}
@ -66,6 +66,22 @@ First written: 2020/7/1
{% trans "Cash Account" context "Accounting|" as current_report_title %} {% trans "Cash Account" context "Accounting|" as current_report_title %}
{% include "accounting/include/report-chooser.html" %} {% include "accounting/include/report-chooser.html" %}
{% endwith %} {% endwith %}
<div class="btn-group">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="d-none d-md-inline">{{ current_subject.title_zhtw }}</span>
<span class="d-md-none">{% trans "Subject" context "Accounting|" as text %}{{ text|force_escape }}</span>
</button>
<div class="dropdown-menu subject-picker">
<div class="dropdown-header">{% trans "Shortcuts" context "Accounting|Subject|" as text %}{{ text|force_escape }}</div>
{% for subject in shortcut_subjects %}
<a class="dropdown-item {% if subject.code == current_subject.code %}{% endif %}>" href="{% url "accounting:cash" subject.code period.spec %}">{{ subject.title_zhtw|title }}</a>
{% endfor %}
<div class="dropdown-header">{% trans "All" context "Accounting|Subject|" as text %}{{ text|force_escape }}</div>
{% for subject in all_sibjects %}
<a class="dropdown-item {% if subject.code == current_subject.code %}{% endif %}>" href="{% url "accounting:cash" subject.code period.spec %}">{{ subject.title_zhtw|title }}</a>
{% endfor %}
</div>
</div>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#period-modal"> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#period-modal">
<i class="far fa-calendar-alt"></i> <i class="far fa-calendar-alt"></i>
<span class="d-none d-md-inline">{{ period.description }}</span> <span class="d-none d-md-inline">{{ period.description }}</span>

View File

@ -21,7 +21,7 @@
from datetime import timedelta from datetime import timedelta
from django.db import connection from django.db import connection
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render from django.shortcuts import render
from django.urls import reverse from django.urls import reverse
from django.utils import dateformat from django.utils import dateformat
@ -75,11 +75,30 @@ def cash(request, subject_code, period_spec):
data_end = last_txn.date if last_txn is not None else None data_end = last_txn.date if last_txn is not None else None
period = Period( period = Period(
period_spec, data_start, data_end, get_language()) period_spec, data_start, data_end, get_language())
subjects = list(Subject.objects.raw("""SELECT s.*
FROM accounting_subjects AS s
WHERE s.code IN (SELECT s1.code
FROM accounting_subjects AS s1
INNER JOIN accounting_records AS r1 ON s1.sn=r1.subject_sn
WHERE s1.code LIKE '11%'
OR s1.code LIKE '12%'
OR s1.code LIKE '21%'
OR s1.code LIKE '22%'
GROUP BY s1.code)
ORDER BY s.code"""))
subjects.insert(0, Subject(
code="0",
title_zhtw=pgettext(
"Accounting|", "current assets and liabilities"),
))
current_subject = None
for subject in subjects:
if subject.code == subject_code:
current_subject = subject
if current_subject is None:
raise Http404()
# The SQL query # The SQL query
if subject_code == "0": if current_subject.code == "0":
subject = Subject(code="0")
subject.title_zhtw = pgettext(
"Accounting|", "Current Assets And Liabilities")
select_records = """SELECT r.* select_records = """SELECT r.*
FROM accounting_records AS r FROM accounting_records AS r
INNER JOIN (SELECT INNER JOIN (SELECT
@ -119,8 +138,6 @@ ORDER BY
select_balance_before, select_balance_before,
[data_start, period.start - timedelta(days=1)]) [data_start, period.start - timedelta(days=1)])
else: else:
subject = Subject.objects.filter(
code=subject_code).first()
select_records = """SELECT r.* select_records = """SELECT r.*
FROM accounting_records AS r FROM accounting_records AS r
INNER JOIN (SELECT INNER JOIN (SELECT
@ -148,8 +165,8 @@ ORDER BY
select_records, select_records,
[period.start, [period.start,
period.end, period.end,
subject.code + "%", current_subject.code + "%",
subject.code + "%"]) current_subject.code + "%"])
select_balance_before = """SELECT select_balance_before = """SELECT
SUM(CASE WHEN is_credit THEN 1 ELSE -1 END * amount) AS amount SUM(CASE WHEN is_credit THEN 1 ELSE -1 END * amount) AS amount
FROM (%s) AS b""" % select_records FROM (%s) AS b""" % select_records
@ -157,8 +174,8 @@ ORDER BY
select_balance_before, select_balance_before,
[data_start, [data_start,
period.start - timedelta(days=1), period.start - timedelta(days=1),
subject.code + "%", current_subject.code + "%",
subject.code + "%"]) current_subject.code + "%"])
# The list data # The list data
records = list(Record.objects.raw( records = list(Record.objects.raw(
sql_records.sql, sql_records.sql,
@ -177,7 +194,7 @@ ORDER BY
record.balance = balance record.balance = balance
record_sum = Record( record_sum = Record(
transaction=Transaction(date=records[-1].transaction.date), transaction=Transaction(date=records[-1].transaction.date),
subject=subject, subject=current_subject,
summary=pgettext("Accounting|", "Total"), summary=pgettext("Accounting|", "Total"),
balance=balance balance=balance
) )
@ -196,7 +213,9 @@ ORDER BY
return render(request, "accounting/cash.html", { return render(request, "accounting/cash.html", {
"records": pagination.records, "records": pagination.records,
"pagination": pagination, "pagination": pagination,
"subject": subject, "current_subject": current_subject,
"period": period, "period": period,
"reports": ReportUrl(cash=subject, period=period) "reports": ReportUrl(cash=current_subject, period=period),
"shortcut_subjects": [x for x in subjects if x.code in settings.ACCOUNTING["CASH_SHORTCUT_SUBJECTS"]],
"all_sibjects": [x for x in subjects if x.code not in settings.ACCOUNTING["CASH_SHORTCUT_SUBJECTS"]],
}) })