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()])