Added the get_income_expenses_url utility to replace the common codes to retrieve the URL of an income and expenses log.

This commit is contained in:
依瑪貓 2023-03-09 11:14:58 +08:00
parent 380256eda7
commit 1c740b9bbc
4 changed files with 39 additions and 34 deletions

View File

@ -33,6 +33,7 @@ from accounting.utils.pagination import Pagination
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_income_expenses_url
from .utils.option_link import OptionLink from .utils.option_link import OptionLink
from .utils.period_choosers import IncomeExpensesPeriodChooser from .utils.period_choosers import IncomeExpensesPeriodChooser
from .utils.report_chooser import ReportChooser from .utils.report_chooser import ReportChooser
@ -322,15 +323,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_expenses_url(x, self.account, self.period),
return url_for("accounting.report.income-expenses-default", self.currency)
currency=currency, account=self.account)
return url_for("accounting.report.income-expenses",
currency=currency, account=self.account,
period=self.period)
return self._get_currency_options(get_url, self.currency)
@property @property
def account_options(self) -> list[OptionLink]: def account_options(self) -> list[OptionLink]:
@ -338,18 +333,12 @@ class PageParams(BasePageParams):
:return: The account options. :return: The account options.
""" """
def get_url(account: IncomeExpensesAccount):
if self.period.is_default:
return url_for("accounting.report.income-expenses-default",
currency=self.currency, account=account)
return url_for("accounting.report.income-expenses",
currency=self.currency, account=account,
period=self.period)
current_al: IncomeExpensesAccount \ current_al: IncomeExpensesAccount \
= IncomeExpensesAccount.current_assets_and_liabilities() = IncomeExpensesAccount.current_assets_and_liabilities()
options: list[OptionLink] \ options: list[OptionLink] \
= [OptionLink(str(current_al), get_url(current_al), = [OptionLink(str(current_al),
get_income_expenses_url(self.currency, current_al,
self.period),
self.account.id == 0)] self.account.id == 0)]
in_use: sa.Select = sa.Select(JournalEntry.account_id)\ in_use: sa.Select = sa.Select(JournalEntry.account_id)\
.join(Account)\ .join(Account)\
@ -359,7 +348,11 @@ class PageParams(BasePageParams):
Account.base_code.startswith("21"), Account.base_code.startswith("21"),
Account.base_code.startswith("22")))\ Account.base_code.startswith("22")))\
.group_by(JournalEntry.account_id) .group_by(JournalEntry.account_id)
options.extend([OptionLink(str(x), get_url(IncomeExpensesAccount(x)), options.extend([OptionLink(str(x),
get_income_expenses_url(
self.currency,
IncomeExpensesAccount(x),
self.period),
x.id == self.account.id) x.id == self.account.id)
for x in Account.query.filter(Account.id.in_(in_use)) for x in Account.query.filter(Account.id.in_(in_use))
.order_by(Account.base_code, Account.no).all()]) .order_by(Account.base_code, Account.no).all()])

View File

@ -20,6 +20,7 @@
from flask import url_for from flask import url_for
from accounting.models import Currency, Account from accounting.models import Currency, Account
from accounting.report.income_expense_account import IncomeExpensesAccount
from accounting.report.period import Period from accounting.report.period import Period
@ -38,3 +39,20 @@ def get_ledger_url(currency: Currency, account: Account, period: Period) \
return url_for("accounting.report.ledger", return url_for("accounting.report.ledger",
currency=currency, account=account, currency=currency, account=account,
period=period) period=period)
def get_income_expenses_url(currency: Currency, account: IncomeExpensesAccount,
period: Period) -> str:
"""Returns the URL of an income and expenses log.
:param currency: The currency.
:param account: The account.
:param period: The period.
:return: The URL of the income and expenses log.
"""
if period.is_default:
return url_for("accounting.report.income-expenses-default",
currency=currency, account=account)
return url_for("accounting.report.income-expenses",
currency=currency, account=account,
period=period)

View File

@ -30,7 +30,7 @@ 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 from .get_url import get_ledger_url, get_income_expenses_url
class PeriodChooser(ABC): class PeriodChooser(ABC):
@ -149,12 +149,7 @@ class IncomeExpensesPeriodChooser(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_expenses_url(self.currency, self.account, period)
return url_for("accounting.report.income-expenses-default",
currency=self.currency, account=self.account)
return url_for("accounting.report.income-expenses",
currency=self.currency, account=self.account,
period=period)
class TrialBalancePeriodChooser(PeriodChooser): class TrialBalancePeriodChooser(PeriodChooser):

View File

@ -29,9 +29,10 @@ from flask_babel import LazyString
from accounting import db from accounting import db
from accounting.locale import gettext from accounting.locale import gettext
from accounting.models import Currency, Account from accounting.models import Currency, Account
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 from .get_url import get_ledger_url, get_income_expenses_url
from .option_link import OptionLink from .option_link import OptionLink
from .report_type import ReportType from .report_type import ReportType
@ -114,13 +115,11 @@ class ReportChooser:
account: Account = self.__account account: Account = self.__account
if not re.match(r"[12][12]", account.base_code): if not re.match(r"[12][12]", account.base_code):
account: Account = Account.cash() account: Account = Account.cash()
url: str = url_for("accounting.report.income-expenses-default", return OptionLink(gettext("Income and Expenses Log"),
currency=self.__currency, account=account) \ get_income_expenses_url(
if self.__period.is_default \ self.__currency,
else url_for("accounting.report.income-expenses", IncomeExpensesAccount(account),
currency=self.__currency, account=account, self.__period),
period=self.__period)
return OptionLink(gettext("Income and Expenses Log"), url,
self.__active_report == ReportType.INCOME_EXPENSES, self.__active_report == ReportType.INCOME_EXPENSES,
fa_icon="fa-solid fa-money-bill-wave") fa_icon="fa-solid fa-money-bill-wave")