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

View File

@ -20,6 +20,7 @@
from flask import url_for
from accounting.models import Currency, Account
from accounting.report.income_expense_account import IncomeExpensesAccount
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",
currency=currency, account=account,
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, \
LastMonth, SinceLastMonth, ThisYear, LastYear, Today, Yesterday, \
TemplatePeriod
from .get_url import get_ledger_url
from .get_url import get_ledger_url, get_income_expenses_url
class PeriodChooser(ABC):
@ -149,12 +149,7 @@ class IncomeExpensesPeriodChooser(PeriodChooser):
super().__init__(None if first is None else first.date)
def _url_for(self, period: Period) -> str:
if period.is_default:
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)
return get_income_expenses_url(self.currency, self.account, period)
class TrialBalancePeriodChooser(PeriodChooser):

View File

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