diff --git a/src/accounting/report/reports/balance_sheet.py b/src/accounting/report/reports/balance_sheet.py index 565eb90..6d3c5e2 100644 --- a/src/accounting/report/reports/balance_sheet.py +++ b/src/accounting/report/reports/balance_sheet.py @@ -31,7 +31,7 @@ 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.option_link import OptionLink -from .utils.period_choosers import BalanceSheetPeriodChooser +from .utils.period_choosers import PeriodChooser from .utils.report_chooser import ReportChooser from .utils.report_type import ReportType from .utils.urls import ledger_url, balance_sheet_url, income_statement_url @@ -317,8 +317,8 @@ class PageParams(BasePageParams): """The liabilities.""" self.owner_s_equity: Section = owner_s_equity """The owner's equity.""" - self.period_chooser: BalanceSheetPeriodChooser \ - = BalanceSheetPeriodChooser(currency) + self.period_chooser: PeriodChooser = PeriodChooser( + lambda x: balance_sheet_url(currency, x)) """The period chooser.""" @property diff --git a/src/accounting/report/reports/income_expenses.py b/src/accounting/report/reports/income_expenses.py index 88f0b3d..2e1006c 100644 --- a/src/accounting/report/reports/income_expenses.py +++ b/src/accounting/report/reports/income_expenses.py @@ -34,7 +34,7 @@ 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.option_link import OptionLink -from .utils.period_choosers import IncomeExpensesPeriodChooser +from .utils.period_choosers import PeriodChooser from .utils.report_chooser import ReportChooser from .utils.report_type import ReportType from .utils.urls import income_expenses_url @@ -289,8 +289,8 @@ class PageParams(BasePageParams): """The report entries.""" self.total: ReportEntry | None = total """The total entry.""" - self.period_chooser: IncomeExpensesPeriodChooser \ - = IncomeExpensesPeriodChooser(currency, account) + self.period_chooser: PeriodChooser = PeriodChooser( + lambda x: income_expenses_url(currency, account, x)) """The period chooser.""" @property diff --git a/src/accounting/report/reports/income_statement.py b/src/accounting/report/reports/income_statement.py index 43122ea..fa640d9 100644 --- a/src/accounting/report/reports/income_statement.py +++ b/src/accounting/report/reports/income_statement.py @@ -31,7 +31,7 @@ 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.option_link import OptionLink -from .utils.period_choosers import IncomeStatementPeriodChooser +from .utils.period_choosers import PeriodChooser from .utils.report_chooser import ReportChooser from .utils.report_type import ReportType from .utils.urls import ledger_url, income_statement_url @@ -160,8 +160,8 @@ class PageParams(BasePageParams): """True if there is any data, or False otherwise.""" self.sections: list[Section] = sections """The sections in the income statement.""" - self.period_chooser: IncomeStatementPeriodChooser \ - = IncomeStatementPeriodChooser(currency) + self.period_chooser: PeriodChooser = PeriodChooser( + lambda x: income_statement_url(currency, x)) """The period chooser.""" @property diff --git a/src/accounting/report/reports/journal.py b/src/accounting/report/reports/journal.py index 0080b0e..5d26fa9 100644 --- a/src/accounting/report/reports/journal.py +++ b/src/accounting/report/reports/journal.py @@ -31,9 +31,10 @@ 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.period_choosers import JournalPeriodChooser +from .utils.period_choosers import PeriodChooser from .utils.report_chooser import ReportChooser from .utils.report_type import ReportType +from .utils.urls import journal_url class ReportEntry: @@ -122,8 +123,8 @@ class PageParams(BasePageParams): """The pagination.""" self.entries: list[JournalEntry] = entries """The entries.""" - self.period_chooser: JournalPeriodChooser \ - = JournalPeriodChooser() + self.period_chooser: PeriodChooser = PeriodChooser( + lambda x: journal_url(x)) """The period chooser.""" @property diff --git a/src/accounting/report/reports/ledger.py b/src/accounting/report/reports/ledger.py index 1d3a7b5..f40a9da 100644 --- a/src/accounting/report/reports/ledger.py +++ b/src/accounting/report/reports/ledger.py @@ -33,7 +33,7 @@ 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.option_link import OptionLink -from .utils.period_choosers import LedgerPeriodChooser +from .utils.period_choosers import PeriodChooser from .utils.report_chooser import ReportChooser from .utils.report_type import ReportType from .utils.urls import ledger_url @@ -264,8 +264,8 @@ class PageParams(BasePageParams): """The entries.""" self.total: ReportEntry | None = total """The total entry.""" - self.period_chooser: LedgerPeriodChooser \ - = LedgerPeriodChooser(currency, account) + self.period_chooser: PeriodChooser = PeriodChooser( + lambda x: ledger_url(currency, account, x)) """The period chooser.""" @property diff --git a/src/accounting/report/reports/trial_balance.py b/src/accounting/report/reports/trial_balance.py index e99336e..d566f12 100644 --- a/src/accounting/report/reports/trial_balance.py +++ b/src/accounting/report/reports/trial_balance.py @@ -30,7 +30,7 @@ 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.option_link import OptionLink -from .utils.period_choosers import TrialBalancePeriodChooser +from .utils.period_choosers import PeriodChooser from .utils.report_chooser import ReportChooser from .utils.report_type import ReportType from .utils.urls import ledger_url, trial_balance_url @@ -121,8 +121,8 @@ class PageParams(BasePageParams): """The accounts in the trial balance.""" self.total: Total = total """The total of the trial balance.""" - self.period_chooser: TrialBalancePeriodChooser \ - = TrialBalancePeriodChooser(currency) + self.period_chooser: PeriodChooser = PeriodChooser( + lambda x: trial_balance_url(currency, x)) """The period chooser.""" @property diff --git a/src/accounting/report/reports/utils/period_choosers.py b/src/accounting/report/reports/utils/period_choosers.py index e483b22..ca17932 100644 --- a/src/accounting/report/reports/utils/period_choosers.py +++ b/src/accounting/report/reports/utils/period_choosers.py @@ -20,42 +20,45 @@ This file is largely taken from the NanoParma ERP project, first written in 2021/9/16 by imacat (imacat@nanoparma.com). """ -from abc import ABC, abstractmethod +import typing as t from datetime import date -from accounting.models import Currency, Account, Transaction -from accounting.report.income_expense_account import IncomeExpensesAccount +from accounting.models import Transaction from accounting.report.period import YearPeriod, Period, ThisMonth, \ LastMonth, SinceLastMonth, ThisYear, LastYear, Today, Yesterday, \ TemplatePeriod -from .urls import journal_url, ledger_url, income_expenses_url, \ - trial_balance_url, income_statement_url, balance_sheet_url -class PeriodChooser(ABC): +class PeriodChooser: """The period chooser.""" - def __init__(self): - """Constructs a period chooser.""" + def __init__(self, get_url: t.Callable[[Period], str]): + """Constructs a period chooser. + + :param get_url: The callback to return the URL of the current report in + a period. + """ + self.__get_url: t.Callable[[Period], str] = get_url + """The callback to return the URL of the current report in a period.""" # Shortcut periods - self.this_month_url: str = self._url_for(ThisMonth()) + self.this_month_url: str = get_url(ThisMonth()) """The URL for this month.""" - self.last_month_url: str = self._url_for(LastMonth()) + self.last_month_url: str = get_url(LastMonth()) """The URL for last month.""" - self.since_last_month_url: str = self._url_for(SinceLastMonth()) + self.since_last_month_url: str = get_url(SinceLastMonth()) """The URL since last mint.""" - self.this_year_url: str = self._url_for(ThisYear()) + self.this_year_url: str = get_url(ThisYear()) """The URL for this year.""" - self.last_year_url: str = self._url_for(LastYear()) + self.last_year_url: str = get_url(LastYear()) """The URL for last year.""" - self.today_url: str = self._url_for(Today()) + self.today_url: str = get_url(Today()) """The URL for today.""" - self.yesterday_url: str = self._url_for(Yesterday()) + self.yesterday_url: str = get_url(Yesterday()) """The URL for yesterday.""" - self.all_url: str = self._url_for(Period(None, None)) + self.all_url: str = get_url(Period(None, None)) """The URL for all period.""" - self.url_template: str = self._url_for(TemplatePeriod()) + self.url_template: str = get_url(TemplatePeriod()) """The URL template.""" first: Transaction | None \ @@ -85,95 +88,10 @@ class PeriodChooser(ABC): self.available_years \ = reversed(range(start.year, today.year - 1)) - @abstractmethod - def _url_for(self, period: Period) -> str: - """Returns the URL for a period. - - :param period: The period. - :return: The URL for the period. - """ - pass - def year_url(self, year: int) -> str: """Returns the period URL of a year. :param year: The year :return: The period URL of the year. """ - return self._url_for(YearPeriod(year)) - - -class JournalPeriodChooser(PeriodChooser): - """The journal period chooser.""" - - def _url_for(self, period: Period) -> str: - return journal_url(period) - - -class LedgerPeriodChooser(PeriodChooser): - """The ledger period chooser.""" - - def __init__(self, currency: Currency, account: Account): - """Constructs the ledger period chooser.""" - self.currency: Currency = currency - """The currency.""" - self.account: Account = account - """The account.""" - super().__init__() - - def _url_for(self, period: Period) -> str: - return ledger_url(self.currency, self.account, period) - - -class IncomeExpensesPeriodChooser(PeriodChooser): - """The income and expenses log period chooser.""" - - def __init__(self, currency: Currency, account: IncomeExpensesAccount): - """Constructs the income and expenses log period chooser.""" - self.currency: Currency = currency - """The currency.""" - self.account: IncomeExpensesAccount = account - """The account.""" - super().__init__() - - def _url_for(self, period: Period) -> str: - return income_expenses_url(self.currency, self.account, period) - - -class TrialBalancePeriodChooser(PeriodChooser): - """The trial balance period chooser.""" - - def __init__(self, currency: Currency): - """Constructs the trial balance period chooser.""" - self.currency: Currency = currency - """The currency.""" - super().__init__() - - def _url_for(self, period: Period) -> str: - return trial_balance_url(self.currency, period) - - -class IncomeStatementPeriodChooser(PeriodChooser): - """The income statement period chooser.""" - - def __init__(self, currency: Currency): - """Constructs the income statement period chooser.""" - self.currency: Currency = currency - """The currency.""" - super().__init__() - - def _url_for(self, period: Period) -> str: - return income_statement_url(self.currency, period) - - -class BalanceSheetPeriodChooser(PeriodChooser): - """The balance sheet period chooser.""" - - def __init__(self, currency: Currency): - """Constructs the balance sheet period chooser.""" - self.currency: Currency = currency - """The currency.""" - super().__init__() - - def _url_for(self, period: Period) -> str: - return balance_sheet_url(self.currency, period) + return self.__get_url(YearPeriod(year))