Added the get_ledger_url utility to replace the common codes to retrieve the URL of a ledger.
This commit is contained in:
parent
6d1e705e4b
commit
74b695c089
@ -29,6 +29,7 @@ from accounting.models import Currency, BaseAccount, Account, Transaction, \
|
|||||||
from accounting.report.period import Period
|
from accounting.report.period import Period
|
||||||
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.option_link import OptionLink
|
from .utils.option_link import OptionLink
|
||||||
from .utils.base_page_params import BasePageParams
|
from .utils.base_page_params import BasePageParams
|
||||||
from .utils.period_choosers import BalanceSheetPeriodChooser
|
from .utils.period_choosers import BalanceSheetPeriodChooser
|
||||||
@ -144,24 +145,12 @@ class AccountCollector:
|
|||||||
Account.base_code == "3353")).all()
|
Account.base_code == "3353")).all()
|
||||||
account_by_id: dict[int, Account] \
|
account_by_id: dict[int, Account] \
|
||||||
= {x.id: x for x in self.__all_accounts}
|
= {x.id: x for x in self.__all_accounts}
|
||||||
|
|
||||||
def get_url(account: Account) -> str:
|
|
||||||
"""Returns the ledger URL of an account.
|
|
||||||
|
|
||||||
:param account: The account.
|
|
||||||
:return: The ledger URL of the account.
|
|
||||||
"""
|
|
||||||
if self.__period.is_default:
|
|
||||||
return url_for("accounting.report.ledger-default",
|
|
||||||
currency=self.__currency, account=account)
|
|
||||||
return url_for("accounting.report.ledger",
|
|
||||||
currency=self.__currency, account=account,
|
|
||||||
period=self.__period)
|
|
||||||
|
|
||||||
self.accounts: list[ReportAccount] \
|
self.accounts: list[ReportAccount] \
|
||||||
= [ReportAccount(account=account_by_id[x.id],
|
= [ReportAccount(account=account_by_id[x.id],
|
||||||
amount=x.balance,
|
amount=x.balance,
|
||||||
url=get_url(account_by_id[x.id]))
|
url=get_ledger_url(self.__currency,
|
||||||
|
account_by_id[x.id],
|
||||||
|
self.__period))
|
||||||
for x in account_balances]
|
for x in account_balances]
|
||||||
self.__add_accumulated()
|
self.__add_accumulated()
|
||||||
self.__add_current_period()
|
self.__add_current_period()
|
||||||
|
@ -29,6 +29,7 @@ from accounting.models import Currency, BaseAccount, Account, Transaction, \
|
|||||||
from accounting.report.period import Period
|
from accounting.report.period import Period
|
||||||
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.option_link import OptionLink
|
from .utils.option_link import OptionLink
|
||||||
from .utils.base_page_params import BasePageParams
|
from .utils.base_page_params import BasePageParams
|
||||||
from .utils.period_choosers import IncomeStatementPeriodChooser
|
from .utils.period_choosers import IncomeStatementPeriodChooser
|
||||||
@ -278,23 +279,11 @@ class IncomeStatement(BaseReport):
|
|||||||
accounts: dict[int, Account] \
|
accounts: dict[int, Account] \
|
||||||
= {x.id: x for x in Account.query
|
= {x.id: x for x in Account.query
|
||||||
.filter(Account.id.in_([x.id for x in balances])).all()}
|
.filter(Account.id.in_([x.id for x in balances])).all()}
|
||||||
|
|
||||||
def get_url(account: Account) -> str:
|
|
||||||
"""Returns the ledger URL of an account.
|
|
||||||
|
|
||||||
:param account: The account.
|
|
||||||
:return: The ledger URL of the account.
|
|
||||||
"""
|
|
||||||
if self.__period.is_default:
|
|
||||||
return url_for("accounting.report.ledger-default",
|
|
||||||
currency=self.__currency, account=account)
|
|
||||||
return url_for("accounting.report.ledger",
|
|
||||||
currency=self.__currency, account=account,
|
|
||||||
period=self.__period)
|
|
||||||
|
|
||||||
return [ReportAccount(account=accounts[x.id],
|
return [ReportAccount(account=accounts[x.id],
|
||||||
amount=x.balance,
|
amount=x.balance,
|
||||||
url=get_url(accounts[x.id]))
|
url=get_ledger_url(self.__currency,
|
||||||
|
accounts[x.id],
|
||||||
|
self.__period))
|
||||||
for x in balances]
|
for x in balances]
|
||||||
|
|
||||||
def csv(self) -> Response:
|
def csv(self) -> Response:
|
||||||
|
@ -31,6 +31,7 @@ from accounting.report.period import Period
|
|||||||
from accounting.utils.pagination import Pagination
|
from accounting.utils.pagination import Pagination
|
||||||
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.option_link import OptionLink
|
from .utils.option_link import OptionLink
|
||||||
from .utils.base_page_params import BasePageParams
|
from .utils.base_page_params import BasePageParams
|
||||||
from .utils.period_choosers import LedgerPeriodChooser
|
from .utils.period_choosers import LedgerPeriodChooser
|
||||||
@ -290,15 +291,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_ledger_url(x, self.account, self.period),
|
||||||
return url_for("accounting.report.ledger-default",
|
self.currency)
|
||||||
currency=currency, account=self.account)
|
|
||||||
return url_for("accounting.report.ledger",
|
|
||||||
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]:
|
||||||
@ -306,18 +301,12 @@ class PageParams(BasePageParams):
|
|||||||
|
|
||||||
:return: The account options.
|
:return: The account options.
|
||||||
"""
|
"""
|
||||||
def get_url(account: Account):
|
|
||||||
if self.period.is_default:
|
|
||||||
return url_for("accounting.report.ledger-default",
|
|
||||||
currency=self.currency, account=account)
|
|
||||||
return url_for("accounting.report.ledger",
|
|
||||||
currency=self.currency, account=account,
|
|
||||||
period=self.period)
|
|
||||||
|
|
||||||
in_use: sa.Select = sa.Select(JournalEntry.account_id)\
|
in_use: sa.Select = sa.Select(JournalEntry.account_id)\
|
||||||
.filter(JournalEntry.currency_code == self.currency.code)\
|
.filter(JournalEntry.currency_code == self.currency.code)\
|
||||||
.group_by(JournalEntry.account_id)
|
.group_by(JournalEntry.account_id)
|
||||||
return [OptionLink(str(x), get_url(x), x.id == self.account.id)
|
return [OptionLink(str(x), get_ledger_url(self.currency, x,
|
||||||
|
self.period),
|
||||||
|
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()]
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@ from accounting.models import Currency, Account, Transaction, JournalEntry
|
|||||||
from accounting.report.period import Period
|
from accounting.report.period import Period
|
||||||
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.option_link import OptionLink
|
from .utils.option_link import OptionLink
|
||||||
from .utils.base_page_params import BasePageParams
|
from .utils.base_page_params import BasePageParams
|
||||||
from .utils.period_choosers import TrialBalancePeriodChooser
|
from .utils.period_choosers import TrialBalancePeriodChooser
|
||||||
@ -200,23 +201,11 @@ class TrialBalance(BaseReport):
|
|||||||
accounts: dict[int, Account] \
|
accounts: dict[int, Account] \
|
||||||
= {x.id: x for x in Account.query
|
= {x.id: x for x in Account.query
|
||||||
.filter(Account.id.in_([x.id for x in balances])).all()}
|
.filter(Account.id.in_([x.id for x in balances])).all()}
|
||||||
|
|
||||||
def get_url(account: Account) -> str:
|
|
||||||
"""Returns the ledger URL of an account.
|
|
||||||
|
|
||||||
:param account: The account.
|
|
||||||
:return: The ledger URL of the account.
|
|
||||||
"""
|
|
||||||
if self.__period.is_default:
|
|
||||||
return url_for("accounting.report.ledger-default",
|
|
||||||
currency=self.__currency, account=account)
|
|
||||||
return url_for("accounting.report.ledger",
|
|
||||||
currency=self.__currency, account=account,
|
|
||||||
period=self.__period)
|
|
||||||
|
|
||||||
self.__accounts = [ReportAccount(account=accounts[x.id],
|
self.__accounts = [ReportAccount(account=accounts[x.id],
|
||||||
amount=x.balance,
|
amount=x.balance,
|
||||||
url=get_url(accounts[x.id]))
|
url=get_ledger_url(self.__currency,
|
||||||
|
accounts[x.id],
|
||||||
|
self.__period))
|
||||||
for x in balances]
|
for x in balances]
|
||||||
self.__total = Total(
|
self.__total = Total(
|
||||||
sum([x.debit for x in self.__accounts if x.debit is not None]),
|
sum([x.debit for x in self.__accounts if x.debit is not None]),
|
||||||
|
40
src/accounting/report/reports/utils/get_url.py
Normal file
40
src/accounting/report/reports/utils/get_url.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
# The Mia! Accounting Flask Project.
|
||||||
|
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/3/9
|
||||||
|
|
||||||
|
# Copyright (c) 2023 imacat.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
"""The utilities to get the ledger URL.
|
||||||
|
|
||||||
|
"""
|
||||||
|
from flask import url_for
|
||||||
|
|
||||||
|
from accounting.models import Currency, Account
|
||||||
|
from accounting.report.period import Period
|
||||||
|
|
||||||
|
|
||||||
|
def get_ledger_url(currency: Currency, account: Account, period: Period) \
|
||||||
|
-> str:
|
||||||
|
"""Returns the URL of a ledger.
|
||||||
|
|
||||||
|
:param currency: The currency.
|
||||||
|
:param account: The account.
|
||||||
|
:param period: The period.
|
||||||
|
:return: The URL of the ledger.
|
||||||
|
"""
|
||||||
|
if period.is_default:
|
||||||
|
return url_for("accounting.report.ledger-default",
|
||||||
|
currency=currency, account=account)
|
||||||
|
return url_for("accounting.report.ledger",
|
||||||
|
currency=currency, account=account,
|
||||||
|
period=period)
|
@ -30,6 +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
|
||||||
|
|
||||||
|
|
||||||
class PeriodChooser(ABC):
|
class PeriodChooser(ABC):
|
||||||
@ -131,12 +132,7 @@ class LedgerPeriodChooser(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_ledger_url(self.currency, self.account, period)
|
||||||
return url_for("accounting.report.ledger-default",
|
|
||||||
currency=self.currency, account=self.account)
|
|
||||||
return url_for("accounting.report.ledger",
|
|
||||||
currency=self.currency, account=self.account,
|
|
||||||
period=period)
|
|
||||||
|
|
||||||
|
|
||||||
class IncomeExpensesPeriodChooser(PeriodChooser):
|
class IncomeExpensesPeriodChooser(PeriodChooser):
|
||||||
|
@ -31,6 +31,7 @@ from accounting.locale import gettext
|
|||||||
from accounting.models import Currency, Account
|
from accounting.models import Currency, Account
|
||||||
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 .option_link import OptionLink
|
from .option_link import OptionLink
|
||||||
from .report_type import ReportType
|
from .report_type import ReportType
|
||||||
|
|
||||||
@ -98,13 +99,9 @@ class ReportChooser:
|
|||||||
|
|
||||||
:return: The ledger.
|
:return: The ledger.
|
||||||
"""
|
"""
|
||||||
url: str = url_for("accounting.report.ledger-default",
|
return OptionLink(gettext("Ledger"),
|
||||||
currency=self.__currency, account=self.__account) \
|
get_ledger_url(self.__currency, self.__account,
|
||||||
if self.__period.is_default \
|
self.__period),
|
||||||
else url_for("accounting.report.ledger",
|
|
||||||
currency=self.__currency, account=self.__account,
|
|
||||||
period=self.__period)
|
|
||||||
return OptionLink(gettext("Ledger"), url,
|
|
||||||
self.__active_report == ReportType.LEDGER,
|
self.__active_report == ReportType.LEDGER,
|
||||||
fa_icon="fa-solid fa-clipboard")
|
fa_icon="fa-solid fa-clipboard")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user