From c8230c949d6a021dfa0899f17b1507a0155efa94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Wed, 8 Mar 2023 17:52:29 +0800 Subject: [PATCH] Renamed the Entry class to ReportEntry in the journal, ledger, income and expenses log, and search result, to be clear without confusion. --- .../report/reports/income_expenses.py | 59 ++++++++++--------- src/accounting/report/reports/journal.py | 27 +++++---- src/accounting/report/reports/ledger.py | 59 ++++++++++--------- src/accounting/report/reports/search.py | 21 +++---- 4 files changed, 85 insertions(+), 81 deletions(-) diff --git a/src/accounting/report/reports/income_expenses.py b/src/accounting/report/reports/income_expenses.py index 758476c..a3e27ec 100644 --- a/src/accounting/report/reports/income_expenses.py +++ b/src/accounting/report/reports/income_expenses.py @@ -38,11 +38,11 @@ from .utils.report_chooser import ReportChooser from .utils.report_type import ReportType -class Entry: - """An entry in the income and expenses log.""" +class ReportEntry: + """An entry in the report.""" def __init__(self, entry: JournalEntry | None = None): - """Constructs the entry in the income and expenses log. + """Constructs the entry in the report. :param entry: The journal entry. """ @@ -92,18 +92,18 @@ class EntryCollector: """The account.""" self.__period: Period = period """The period""" - self.brought_forward: Entry | None + self.brought_forward: ReportEntry | None """The brought-forward entry.""" - self.entries: list[Entry] + self.entries: list[ReportEntry] """The log entries.""" - self.total: Entry | None + self.total: ReportEntry | None """The total entry.""" self.brought_forward = self.__get_brought_forward_entry() self.entries = self.__query_entries() self.total = self.__get_total_entry() self.__populate_balance() - def __get_brought_forward_entry(self) -> Entry | None: + def __get_brought_forward_entry(self) -> ReportEntry | None: """Queries, composes and returns the brought-forward entry. :return: The brought-forward entry, or None if the period starts from @@ -122,7 +122,7 @@ class EntryCollector: balance: int | None = db.session.scalar(select) if balance is None: return None - entry: Entry = Entry() + entry: ReportEntry = ReportEntry() entry.is_brought_forward = True entry.date = self.__period.start entry.account = Account.accumulated_change() @@ -134,7 +134,7 @@ class EntryCollector: entry.balance = balance return entry - def __query_entries(self) -> list[Entry]: + def __query_entries(self) -> list[ReportEntry]: """Queries and returns the log entries. :return: The log entries. @@ -149,7 +149,7 @@ class EntryCollector: txn_with_account: sa.Select = sa.Select(Transaction.id).\ join(JournalEntry).join(Account).filter(*conditions) - return [Entry(x) + return [ReportEntry(x) for x in JournalEntry.query.join(Transaction).join(Account) .filter(JournalEntry.transaction_id.in_(txn_with_account), JournalEntry.currency_code == self.__currency.code, @@ -167,14 +167,14 @@ class EntryCollector: Account.base_code.startswith("22")) return Account.id == self.__account.id - def __get_total_entry(self) -> Entry | None: + def __get_total_entry(self) -> ReportEntry | None: """Composes the total entry. :return: The total entry, or None if there is no data. """ if self.brought_forward is None and len(self.entries) == 0: return None - entry: Entry = Entry() + entry: ReportEntry = ReportEntry() entry.is_total = True entry.summary = gettext("Total") entry.income = sum([x.income for x in self.entries @@ -253,10 +253,10 @@ class PageParams(BasePageParams): account: IncomeExpensesAccount, period: Period, has_data: bool, - pagination: Pagination[Entry], - brought_forward: Entry | None, - entries: list[Entry], - total: Entry | None): + pagination: Pagination[ReportEntry], + brought_forward: ReportEntry | None, + entries: list[ReportEntry], + total: ReportEntry | None): """Constructs the HTML page parameters. :param currency: The currency. @@ -275,13 +275,13 @@ class PageParams(BasePageParams): """The period.""" self.__has_data: bool = has_data """True if there is any data, or False otherwise.""" - self.pagination: Pagination[Entry] = pagination + self.pagination: Pagination[ReportEntry] = pagination """The pagination.""" - self.brought_forward: Entry | None = brought_forward + self.brought_forward: ReportEntry | None = brought_forward """The brought-forward entry.""" - self.entries: list[Entry] = entries + self.entries: list[ReportEntry] = entries """The entries.""" - self.total: Entry | None = total + self.total: ReportEntry | None = total """The total entry.""" self.period_chooser: IncomeExpensesPeriodChooser \ = IncomeExpensesPeriodChooser(currency, account) @@ -366,7 +366,7 @@ class PageParams(BasePageParams): return options -def populate_entries(entries: list[Entry]) -> None: +def populate_entries(entries: list[ReportEntry]) -> None: """Populates the report entries with relative data. :param entries: The report entries. @@ -407,11 +407,11 @@ class IncomeExpenses(BaseReport): """The period.""" collector: EntryCollector = EntryCollector( self.__currency, self.__account, self.__period) - self.__brought_forward: Entry | None = collector.brought_forward + self.__brought_forward: ReportEntry | None = collector.brought_forward """The brought-forward entry.""" - self.__entries: list[Entry] = collector.entries + self.__entries: list[ReportEntry] = collector.entries """The log entries.""" - self.__total: Entry | None = collector.total + self.__total: ReportEntry | None = collector.total """The total entry.""" def csv(self) -> Response: @@ -456,21 +456,22 @@ class IncomeExpenses(BaseReport): :return: The report as HTML. """ - all_entries: list[Entry] = [] + all_entries: list[ReportEntry] = [] if self.__brought_forward is not None: all_entries.append(self.__brought_forward) all_entries.extend(self.__entries) if self.__total is not None: all_entries.append(self.__total) - pagination: Pagination[Entry] = Pagination[Entry](all_entries) - page_entries: list[Entry] = pagination.list + pagination: Pagination[ReportEntry] \ + = Pagination[ReportEntry](all_entries) + page_entries: list[ReportEntry] = pagination.list has_data: bool = len(page_entries) > 0 populate_entries(page_entries) - brought_forward: Entry | None = None + brought_forward: ReportEntry | None = None if len(page_entries) > 0 and page_entries[0].is_brought_forward: brought_forward = page_entries[0] page_entries = page_entries[1:] - total: Entry | None = None + total: ReportEntry | None = None if len(page_entries) > 0 and page_entries[-1].is_total: total = page_entries[-1] page_entries = page_entries[:-1] diff --git a/src/accounting/report/reports/journal.py b/src/accounting/report/reports/journal.py index c11abd3..fcf25e8 100644 --- a/src/accounting/report/reports/journal.py +++ b/src/accounting/report/reports/journal.py @@ -36,11 +36,11 @@ from .utils.report_chooser import ReportChooser from .utils.report_type import ReportType -class Entry: - """An entry in the journal.""" +class ReportEntry: + """An entry in the report.""" def __init__(self, entry: JournalEntry | None = None): - """Constructs the entry in the journal. + """Constructs the entry in the report. :param entry: The journal entry. """ @@ -117,8 +117,8 @@ class PageParams(BasePageParams): """The HTML page parameters.""" def __init__(self, period: Period, - pagination: Pagination[Entry], - entries: list[Entry]): + pagination: Pagination[ReportEntry], + entries: list[ReportEntry]): """Constructs the HTML page parameters. :param period: The period. @@ -126,9 +126,9 @@ class PageParams(BasePageParams): """ self.period: Period = period """The period.""" - self.pagination: Pagination[Entry] = pagination + self.pagination: Pagination[ReportEntry] = pagination """The pagination.""" - self.entries: list[Entry] = entries + self.entries: list[ReportEntry] = entries """The entries.""" self.period_chooser: JournalPeriodChooser \ = JournalPeriodChooser() @@ -152,7 +152,7 @@ class PageParams(BasePageParams): period=self.period) -def populate_entries(entries: list[Entry]) -> None: +def populate_entries(entries: list[ReportEntry]) -> None: """Populates the report entries with relative data. :param entries: The report entries. @@ -184,10 +184,10 @@ class Journal(BaseReport): """The account.""" self.__period: Period = period """The period.""" - self.__entries: list[Entry] = self.__query_entries() + self.__entries: list[ReportEntry] = self.__query_entries() """The journal entries.""" - def __query_entries(self) -> list[Entry]: + def __query_entries(self) -> list[ReportEntry]: """Queries and returns the journal entries. :return: The journal entries. @@ -197,7 +197,7 @@ class Journal(BaseReport): conditions.append(Transaction.date >= self.__period.start) if self.__period.end is not None: conditions.append(Transaction.date <= self.__period.end) - return [Entry(x) for x in db.session + return [ReportEntry(x) for x in db.session .query(JournalEntry).join(Transaction).filter(*conditions) .order_by(Transaction.date, JournalEntry.is_debit.desc(), @@ -232,8 +232,9 @@ class Journal(BaseReport): :return: The report as HTML. """ - pagination: Pagination[Entry] = Pagination[Entry](self.__entries) - page_entries: list[Entry] = pagination.list + pagination: Pagination[ReportEntry] \ + = Pagination[ReportEntry](self.__entries) + page_entries: list[ReportEntry] = pagination.list populate_entries(page_entries) params: PageParams = PageParams(period=self.__period, pagination=pagination, diff --git a/src/accounting/report/reports/ledger.py b/src/accounting/report/reports/ledger.py index c2ae22a..50576ef 100644 --- a/src/accounting/report/reports/ledger.py +++ b/src/accounting/report/reports/ledger.py @@ -37,11 +37,11 @@ from .utils.report_chooser import ReportChooser from .utils.report_type import ReportType -class Entry: - """An entry in the ledger.""" +class ReportEntry: + """An entry in the report.""" def __init__(self, entry: JournalEntry | None = None): - """Constructs the entry in the ledger. + """Constructs the entry in the report. :param entry: The journal entry. """ @@ -90,18 +90,18 @@ class EntryCollector: """The account.""" self.__period: Period = period """The period""" - self.brought_forward: Entry | None + self.brought_forward: ReportEntry | None """The brought-forward entry.""" - self.entries: list[Entry] + self.entries: list[ReportEntry] """The ledger entries.""" - self.total: Entry | None + self.total: ReportEntry | None """The total entry.""" self.brought_forward = self.__get_brought_forward_entry() self.entries = self.__query_entries() self.total = self.__get_total_entry() self.__populate_balance() - def __get_brought_forward_entry(self) -> Entry | None: + def __get_brought_forward_entry(self) -> ReportEntry | None: """Queries, composes and returns the brought-forward entry. :return: The brought-forward entry, or None if the ledger starts from @@ -119,7 +119,7 @@ class EntryCollector: balance: int | None = db.session.scalar(select) if balance is None: return None - entry: Entry = Entry() + entry: ReportEntry = ReportEntry() entry.is_brought_forward = True entry.date = self.__period.start entry.summary = gettext("Brought forward") @@ -130,7 +130,7 @@ class EntryCollector: entry.balance = balance return entry - def __query_entries(self) -> list[Entry]: + def __query_entries(self) -> list[ReportEntry]: """Queries and returns the ledger entries. :return: The ledger entries. @@ -142,20 +142,20 @@ class EntryCollector: conditions.append(Transaction.date >= self.__period.start) if self.__period.end is not None: conditions.append(Transaction.date <= self.__period.end) - return [Entry(x) for x in JournalEntry.query.join(Transaction) + return [ReportEntry(x) for x in JournalEntry.query.join(Transaction) .filter(*conditions) .order_by(Transaction.date, JournalEntry.is_debit.desc(), JournalEntry.no).all()] - def __get_total_entry(self) -> Entry | None: + def __get_total_entry(self) -> ReportEntry | None: """Composes the total entry. :return: The total entry, or None if there is no data. """ if self.brought_forward is None and len(self.entries) == 0: return None - entry: Entry = Entry() + entry: ReportEntry = ReportEntry() entry.is_total = True entry.summary = gettext("Total") entry.debit = sum([x.debit for x in self.entries @@ -230,10 +230,10 @@ class PageParams(BasePageParams): account: Account, period: Period, has_data: bool, - pagination: Pagination[Entry], - brought_forward: Entry | None, - entries: list[Entry], - total: Entry | None): + pagination: Pagination[ReportEntry], + brought_forward: ReportEntry | None, + entries: list[ReportEntry], + total: ReportEntry | None): """Constructs the HTML page parameters. :param currency: The currency. @@ -252,13 +252,13 @@ class PageParams(BasePageParams): """The period.""" self.__has_data: bool = has_data """True if there is any data, or False otherwise.""" - self.pagination: Pagination[Entry] = pagination + self.pagination: Pagination[ReportEntry] = pagination """The pagination.""" - self.brought_forward: Entry | None = brought_forward + self.brought_forward: ReportEntry | None = brought_forward """The brought-forward entry.""" - self.entries: list[Entry] = entries + self.entries: list[ReportEntry] = entries """The entries.""" - self.total: Entry | None = total + self.total: ReportEntry | None = total """The total entry.""" self.period_chooser: LedgerPeriodChooser \ = LedgerPeriodChooser(currency, account) @@ -326,7 +326,7 @@ class PageParams(BasePageParams): .order_by(Account.base_code, Account.no).all()] -def populate_entries(entries: list[Entry]) -> None: +def populate_entries(entries: list[ReportEntry]) -> None: """Populates the report entries with relative data. :param entries: The report entries. @@ -361,11 +361,11 @@ class Ledger(BaseReport): """The period.""" collector: EntryCollector = EntryCollector( self.__currency, self.__account, self.__period) - self.__brought_forward: Entry | None = collector.brought_forward + self.__brought_forward: ReportEntry | None = collector.brought_forward """The brought-forward entry.""" - self.__entries: list[Entry] = collector.entries + self.__entries: list[ReportEntry] = collector.entries """The ledger entries.""" - self.__total: Entry | None = collector.total + self.__total: ReportEntry | None = collector.total """The total entry.""" def csv(self) -> Response: @@ -408,21 +408,22 @@ class Ledger(BaseReport): :return: The report as HTML. """ - all_entries: list[Entry] = [] + all_entries: list[ReportEntry] = [] if self.__brought_forward is not None: all_entries.append(self.__brought_forward) all_entries.extend(self.__entries) if self.__total is not None: all_entries.append(self.__total) - pagination: Pagination[Entry] = Pagination[Entry](all_entries) - page_entries: list[Entry] = pagination.list + pagination: Pagination[ReportEntry] \ + = Pagination[ReportEntry](all_entries) + page_entries: list[ReportEntry] = pagination.list has_data: bool = len(page_entries) > 0 populate_entries(page_entries) - brought_forward: Entry | None = None + brought_forward: ReportEntry | None = None if len(page_entries) > 0 and page_entries[0].is_brought_forward: brought_forward = page_entries[0] page_entries = page_entries[1:] - total: Entry | None = None + total: ReportEntry | None = None if len(page_entries) > 0 and page_entries[-1].is_total: total = page_entries[-1] page_entries = page_entries[:-1] diff --git a/src/accounting/report/reports/search.py b/src/accounting/report/reports/search.py index bc5e4a4..ec73dd1 100644 --- a/src/accounting/report/reports/search.py +++ b/src/accounting/report/reports/search.py @@ -28,7 +28,7 @@ from accounting.models import Currency, CurrencyL10n, Account, AccountL10n, \ Transaction, JournalEntry from accounting.utils.pagination import Pagination from accounting.utils.query import parse_query_keywords -from .journal import Entry, CSVRow, populate_entries +from .journal import ReportEntry, CSVRow, populate_entries from .utils.base_page_params import BasePageParams from .utils.base_report import BaseReport from .utils.csv_export import csv_download @@ -39,15 +39,15 @@ from .utils.report_type import ReportType class PageParams(BasePageParams): """The HTML page parameters.""" - def __init__(self, pagination: Pagination[Entry], - entries: list[Entry]): + def __init__(self, pagination: Pagination[ReportEntry], + entries: list[ReportEntry]): """Constructs the HTML page parameters. :param entries: The search result entries. """ - self.pagination: Pagination[Entry] = pagination + self.pagination: Pagination[ReportEntry] = pagination """The pagination.""" - self.entries: list[Entry] = entries + self.entries: list[ReportEntry] = entries """The entries.""" @property @@ -73,10 +73,10 @@ class Search(BaseReport): def __init__(self): """Constructs a search.""" """The account.""" - self.__entries: list[Entry] = self.__query_entries() + self.__entries: list[ReportEntry] = self.__query_entries() """The journal entries.""" - def __query_entries(self) -> list[Entry]: + def __query_entries(self) -> list[ReportEntry]: """Queries and returns the journal entries. :return: The journal entries. @@ -99,7 +99,7 @@ class Search(BaseReport): except ArithmeticError: pass conditions.append(sa.or_(*sub_conditions)) - return [Entry(x) for x in JournalEntry.query.filter(*conditions)] + return [ReportEntry(x) for x in JournalEntry.query.filter(*conditions)] @staticmethod def __get_account_condition(k: str) -> sa.Select: @@ -197,8 +197,9 @@ class Search(BaseReport): :return: The report as HTML. """ - pagination: Pagination[Entry] = Pagination[Entry](self.__entries) - page_entries: list[Entry] = pagination.list + pagination: Pagination[ReportEntry] \ + = Pagination[ReportEntry](self.__entries) + page_entries: list[ReportEntry] = pagination.list populate_entries(page_entries) params: PageParams = PageParams(pagination=pagination, entries=page_entries)