From 2c3bd4334555b494d1e2f3248d70e2e44cbf8467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sun, 2 Aug 2020 22:38:16 +0800 Subject: [PATCH] Fixed the URL of the 3351 brought-forward subject in the balance sheet to the income statement before the current period in the accounting application. --- .../templates/accounting/balance-sheet.html | 1 - accounting/views.py | 2 +- mia_core/period.py | 67 +++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/accounting/templates/accounting/balance-sheet.html b/accounting/templates/accounting/balance-sheet.html index 9413f4f..923c98e 100644 --- a/accounting/templates/accounting/balance-sheet.html +++ b/accounting/templates/accounting/balance-sheet.html @@ -293,7 +293,6 @@ First written: 2020/7/20 {% for item in group.details %}
  • - {# TODO: Link to the income statement for account #3351 #} {{ item.title|title }}
    diff --git a/accounting/views.py b/accounting/views.py index eea0263..98b1311 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -719,7 +719,7 @@ def balance_sheet(request, period): code=Account.ACCUMULATED_BALANCE) brought_forward.amount = balance brought_forward.url = reverse( - "accounting:income-statement", args=(period,)) + "accounting:income-statement", args=(period.period_before(),)) accounts.append(brought_forward) balance = Record.objects \ .filter( diff --git a/mia_core/period.py b/mia_core/period.py index e939b51..febd073 100644 --- a/mia_core/period.py +++ b/mia_core/period.py @@ -358,6 +358,24 @@ class Period: else self._data_end return dateformat.format(day, "Y-m-d") + def period_before(self): + """Returns the specification of the period before the current period. + + Returns: + str|None: The specification of the period before the current + period, or None if there is no data before the current period. + """ + if self._data_start is None: + return None + if self.start <= self._data_start: + return None + previous_day = self.start - datetime.timedelta(days=1) + if re.match("^[0-9]{4}$", self.spec): + return "-" + str(previous_day.year) + if re.match("^[0-9]{4}-[0-9]{2}$", self.spec): + return dateformat.format(previous_day, "-Y-m") + return dateformat.format(previous_day, "-Y-m-d") + def month_picker_params(self): """Returns the parameters for the month-picker, as a JSON text string. @@ -436,6 +454,21 @@ class Period: self.description = gettext( "Since %s") % self.get_month_text(year, month) return + # Until a specific month + m = re.match("^-([0-9]{4})-([0-9]{2})$", spec) + if m is not None: + year = int(m.group(1)) + month = int(m.group(2)) + try: + until_month = datetime.date(year, month, 1) + except ValueError: + self.invalid_period() + return + self.start = datetime.date(2000, 1, 1) + self.end = self.get_month_last_day(until_month) + self.description = gettext( + "Until %s") % self.get_month_text(year, month) + return # A specific year m = re.match("^([0-9]{4})$", spec) if m is not None: @@ -454,6 +487,25 @@ class Period: else: self.description = str(year) return + # Until a specific year + m = re.match("^-([0-9]{4})$", spec) + if m is not None: + year = int(m.group(1)) + try: + self.end = datetime.date(year, 12, 31) + except ValueError: + self.invalid_period() + return + self.start = datetime.date(2000, 1, 1) + today = timezone.localdate() + if year == today.year: + year_text = gettext("This Year") + elif year == today.year - 1: + year_text = gettext("Last Year") + else: + year_text = str(year) + self.description = gettext("Until %s") % year_text + return # All time if spec == "-": self.start = datetime.date(2000, 1, 1) @@ -524,6 +576,21 @@ class Period: self.spec = dateformat.format(self.start, "Y-m-d") self.description = self.get_date_text(self.start) return + # Until a specific day + m = re.match("^-([0-9]{4})-([0-9]{2})-([0-9]{2})$", spec) + if m is not None: + try: + self.end = datetime.date( + int(m.group(1)), + int(m.group(2)), + int(m.group(3))) + except ValueError: + self.invalid_period() + return + self.start = datetime.date(2000, 1, 1) + self.description = gettext( + "Until %s") % self.get_date_text(self.end) + return # Wrong period format self.invalid_period()