Added the get_income_statement_url utility to replace the common codes to retrieve the URL of an income statement.

This commit is contained in:
依瑪貓 2023-03-09 11:26:35 +08:00
parent 1c740b9bbc
commit b62f31d385
5 changed files with 36 additions and 33 deletions

View File

@ -30,7 +30,7 @@ from accounting.report.period import Period
from .utils.base_page_params import BasePageParams from .utils.base_page_params import BasePageParams
from .utils.base_report import BaseReport from .utils.base_report import BaseReport
from .utils.csv_export import BaseCSVRow, csv_download, period_spec from .utils.csv_export import BaseCSVRow, csv_download, period_spec
from .utils.get_url import get_ledger_url from .utils.get_url import get_ledger_url, get_income_statement_url
from .utils.option_link import OptionLink from .utils.option_link import OptionLink
from .utils.period_choosers import BalanceSheetPeriodChooser from .utils.period_choosers import BalanceSheetPeriodChooser
from .utils.report_chooser import ReportChooser from .utils.report_chooser import ReportChooser
@ -165,11 +165,10 @@ class AccountCollector:
:return: None. :return: None.
""" """
amount: Decimal | None = self.__query_accumulated() self.__add_owner_s_equity(
url: str = url_for("accounting.report.income-statement", Account.ACCUMULATED_CHANGE_CODE,
currency=self.__currency, self.__query_accumulated(),
period=self.__period.before) get_income_statement_url(self.__currency, self.__period.before))
self.__add_owner_s_equity(Account.ACCUMULATED_CHANGE_CODE, amount, url)
def __query_accumulated(self) -> Decimal | None: def __query_accumulated(self) -> Decimal | None:
"""Queries and returns the accumulated profit or loss. """Queries and returns the accumulated profit or loss.
@ -188,10 +187,10 @@ class AccountCollector:
:return: None. :return: None.
""" """
amount: Decimal | None = self.__query_currency_period() self.__add_owner_s_equity(
url: str = url_for("accounting.report.income-statement", Account.NET_CHANGE_CODE,
currency=self.__currency, period=self.__period) self.__query_currency_period(),
self.__add_owner_s_equity(Account.NET_CHANGE_CODE, amount, url) get_income_statement_url(self.__currency, self.__period))
def __query_currency_period(self) -> Decimal | None: def __query_currency_period(self) -> Decimal | None:
"""Queries and returns the net income or loss for current period. """Queries and returns the net income or loss for current period.

View File

@ -20,7 +20,7 @@
from decimal import Decimal from decimal import Decimal
import sqlalchemy as sa import sqlalchemy as sa
from flask import url_for, render_template, Response from flask import render_template, Response
from accounting import db from accounting import db
from accounting.locale import gettext from accounting.locale import gettext
@ -30,7 +30,7 @@ from accounting.report.period import Period
from .utils.base_page_params import BasePageParams from .utils.base_page_params import BasePageParams
from .utils.base_report import BaseReport from .utils.base_report import BaseReport
from .utils.csv_export import BaseCSVRow, csv_download, period_spec from .utils.csv_export import BaseCSVRow, csv_download, period_spec
from .utils.get_url import get_ledger_url from .utils.get_url import get_ledger_url, get_income_statement_url
from .utils.option_link import OptionLink from .utils.option_link import OptionLink
from .utils.period_choosers import IncomeStatementPeriodChooser from .utils.period_choosers import IncomeStatementPeriodChooser
from .utils.report_chooser import ReportChooser from .utils.report_chooser import ReportChooser
@ -187,14 +187,9 @@ class PageParams(BasePageParams):
:return: The currency options. :return: The currency options.
""" """
def get_url(currency: Currency): return self._get_currency_options(
if self.period.is_default: lambda x: get_income_statement_url(x, self.period),
return url_for("accounting.report.income-statement-default", self.currency)
currency=currency)
return url_for("accounting.report.income-statement",
currency=currency, period=self.period)
return self._get_currency_options(get_url, self.currency)
class IncomeStatement(BaseReport): class IncomeStatement(BaseReport):

View File

@ -56,3 +56,17 @@ def get_income_expenses_url(currency: Currency, account: IncomeExpensesAccount,
return url_for("accounting.report.income-expenses", return url_for("accounting.report.income-expenses",
currency=currency, account=account, currency=currency, account=account,
period=period) period=period)
def get_income_statement_url(currency: Currency, period: Period) -> str:
"""Returns the URL of an income statement.
:param currency: The currency.
:param period: The period.
:return: The URL of the income statement.
"""
if period.is_default:
return url_for("accounting.report.income-statement-default",
currency=currency)
return url_for("accounting.report.income-statement",
currency=currency, period=period)

View File

@ -30,7 +30,8 @@ from accounting.report.income_expense_account import IncomeExpensesAccount
from accounting.report.period import YearPeriod, Period, ThisMonth, \ from accounting.report.period import YearPeriod, Period, ThisMonth, \
LastMonth, SinceLastMonth, ThisYear, LastYear, Today, Yesterday, \ LastMonth, SinceLastMonth, ThisYear, LastYear, Today, Yesterday, \
TemplatePeriod TemplatePeriod
from .get_url import get_ledger_url, get_income_expenses_url from .get_url import get_ledger_url, get_income_expenses_url, \
get_income_statement_url
class PeriodChooser(ABC): class PeriodChooser(ABC):
@ -183,11 +184,7 @@ class IncomeStatementPeriodChooser(PeriodChooser):
super().__init__(None if first is None else first.date) super().__init__(None if first is None else first.date)
def _url_for(self, period: Period) -> str: def _url_for(self, period: Period) -> str:
if period.is_default: return get_income_statement_url(self.currency, period)
return url_for("accounting.report.income-statement-default",
currency=self.currency)
return url_for("accounting.report.income-statement",
currency=self.currency, period=period)
class BalanceSheetPeriodChooser(PeriodChooser): class BalanceSheetPeriodChooser(PeriodChooser):

View File

@ -32,7 +32,8 @@ from accounting.models import Currency, Account
from accounting.report.income_expense_account import IncomeExpensesAccount from accounting.report.income_expense_account import IncomeExpensesAccount
from accounting.report.period import Period from accounting.report.period import Period
from accounting.template_globals import default_currency_code from accounting.template_globals import default_currency_code
from .get_url import get_ledger_url, get_income_expenses_url from .get_url import get_ledger_url, get_income_expenses_url, \
get_income_statement_url
from .option_link import OptionLink from .option_link import OptionLink
from .report_type import ReportType from .report_type import ReportType
@ -144,12 +145,9 @@ class ReportChooser:
:return: The income statement. :return: The income statement.
""" """
url: str = url_for("accounting.report.income-statement-default", return OptionLink(gettext("Income Statement"),
currency=self.__currency) \ get_income_statement_url(self.__currency,
if self.__period.is_default \ self.__period),
else url_for("accounting.report.income-statement",
currency=self.__currency, period=self.__period)
return OptionLink(gettext("Income Statement"), url,
self.__active_report == ReportType.INCOME_STATEMENT, self.__active_report == ReportType.INCOME_STATEMENT,
fa_icon="fa-solid fa-file-invoice-dollar") fa_icon="fa-solid fa-file-invoice-dollar")