Renamed the Entry class to ReportEntry in the journal, ledger, income and expenses log, and search result, to be clear without confusion.

This commit is contained in:
依瑪貓 2023-03-08 17:52:29 +08:00
parent 3c98960efe
commit c8230c949d
4 changed files with 85 additions and 81 deletions

View File

@ -38,11 +38,11 @@ from .utils.report_chooser import ReportChooser
from .utils.report_type import ReportType from .utils.report_type import ReportType
class Entry: class ReportEntry:
"""An entry in the income and expenses log.""" """An entry in the report."""
def __init__(self, entry: JournalEntry | None = None): 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. :param entry: The journal entry.
""" """
@ -92,18 +92,18 @@ class EntryCollector:
"""The account.""" """The account."""
self.__period: Period = period self.__period: Period = period
"""The period""" """The period"""
self.brought_forward: Entry | None self.brought_forward: ReportEntry | None
"""The brought-forward entry.""" """The brought-forward entry."""
self.entries: list[Entry] self.entries: list[ReportEntry]
"""The log entries.""" """The log entries."""
self.total: Entry | None self.total: ReportEntry | None
"""The total entry.""" """The total entry."""
self.brought_forward = self.__get_brought_forward_entry() self.brought_forward = self.__get_brought_forward_entry()
self.entries = self.__query_entries() self.entries = self.__query_entries()
self.total = self.__get_total_entry() self.total = self.__get_total_entry()
self.__populate_balance() 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. """Queries, composes and returns the brought-forward entry.
:return: The brought-forward entry, or None if the period starts from :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) balance: int | None = db.session.scalar(select)
if balance is None: if balance is None:
return None return None
entry: Entry = Entry() entry: ReportEntry = ReportEntry()
entry.is_brought_forward = True entry.is_brought_forward = True
entry.date = self.__period.start entry.date = self.__period.start
entry.account = Account.accumulated_change() entry.account = Account.accumulated_change()
@ -134,7 +134,7 @@ class EntryCollector:
entry.balance = balance entry.balance = balance
return entry return entry
def __query_entries(self) -> list[Entry]: def __query_entries(self) -> list[ReportEntry]:
"""Queries and returns the log entries. """Queries and returns the log entries.
:return: The log entries. :return: The log entries.
@ -149,7 +149,7 @@ class EntryCollector:
txn_with_account: sa.Select = sa.Select(Transaction.id).\ txn_with_account: sa.Select = sa.Select(Transaction.id).\
join(JournalEntry).join(Account).filter(*conditions) join(JournalEntry).join(Account).filter(*conditions)
return [Entry(x) return [ReportEntry(x)
for x in JournalEntry.query.join(Transaction).join(Account) for x in JournalEntry.query.join(Transaction).join(Account)
.filter(JournalEntry.transaction_id.in_(txn_with_account), .filter(JournalEntry.transaction_id.in_(txn_with_account),
JournalEntry.currency_code == self.__currency.code, JournalEntry.currency_code == self.__currency.code,
@ -167,14 +167,14 @@ class EntryCollector:
Account.base_code.startswith("22")) Account.base_code.startswith("22"))
return Account.id == self.__account.id return Account.id == self.__account.id
def __get_total_entry(self) -> Entry | None: def __get_total_entry(self) -> ReportEntry | None:
"""Composes the total entry. """Composes the total entry.
:return: The total entry, or None if there is no data. :return: The total entry, or None if there is no data.
""" """
if self.brought_forward is None and len(self.entries) == 0: if self.brought_forward is None and len(self.entries) == 0:
return None return None
entry: Entry = Entry() entry: ReportEntry = ReportEntry()
entry.is_total = True entry.is_total = True
entry.summary = gettext("Total") entry.summary = gettext("Total")
entry.income = sum([x.income for x in self.entries entry.income = sum([x.income for x in self.entries
@ -253,10 +253,10 @@ class PageParams(BasePageParams):
account: IncomeExpensesAccount, account: IncomeExpensesAccount,
period: Period, period: Period,
has_data: bool, has_data: bool,
pagination: Pagination[Entry], pagination: Pagination[ReportEntry],
brought_forward: Entry | None, brought_forward: ReportEntry | None,
entries: list[Entry], entries: list[ReportEntry],
total: Entry | None): total: ReportEntry | None):
"""Constructs the HTML page parameters. """Constructs the HTML page parameters.
:param currency: The currency. :param currency: The currency.
@ -275,13 +275,13 @@ class PageParams(BasePageParams):
"""The period.""" """The period."""
self.__has_data: bool = has_data self.__has_data: bool = has_data
"""True if there is any data, or False otherwise.""" """True if there is any data, or False otherwise."""
self.pagination: Pagination[Entry] = pagination self.pagination: Pagination[ReportEntry] = pagination
"""The pagination.""" """The pagination."""
self.brought_forward: Entry | None = brought_forward self.brought_forward: ReportEntry | None = brought_forward
"""The brought-forward entry.""" """The brought-forward entry."""
self.entries: list[Entry] = entries self.entries: list[ReportEntry] = entries
"""The entries.""" """The entries."""
self.total: Entry | None = total self.total: ReportEntry | None = total
"""The total entry.""" """The total entry."""
self.period_chooser: IncomeExpensesPeriodChooser \ self.period_chooser: IncomeExpensesPeriodChooser \
= IncomeExpensesPeriodChooser(currency, account) = IncomeExpensesPeriodChooser(currency, account)
@ -366,7 +366,7 @@ class PageParams(BasePageParams):
return options return options
def populate_entries(entries: list[Entry]) -> None: def populate_entries(entries: list[ReportEntry]) -> None:
"""Populates the report entries with relative data. """Populates the report entries with relative data.
:param entries: The report entries. :param entries: The report entries.
@ -407,11 +407,11 @@ class IncomeExpenses(BaseReport):
"""The period.""" """The period."""
collector: EntryCollector = EntryCollector( collector: EntryCollector = EntryCollector(
self.__currency, self.__account, self.__period) 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.""" """The brought-forward entry."""
self.__entries: list[Entry] = collector.entries self.__entries: list[ReportEntry] = collector.entries
"""The log entries.""" """The log entries."""
self.__total: Entry | None = collector.total self.__total: ReportEntry | None = collector.total
"""The total entry.""" """The total entry."""
def csv(self) -> Response: def csv(self) -> Response:
@ -456,21 +456,22 @@ class IncomeExpenses(BaseReport):
:return: The report as HTML. :return: The report as HTML.
""" """
all_entries: list[Entry] = [] all_entries: list[ReportEntry] = []
if self.__brought_forward is not None: if self.__brought_forward is not None:
all_entries.append(self.__brought_forward) all_entries.append(self.__brought_forward)
all_entries.extend(self.__entries) all_entries.extend(self.__entries)
if self.__total is not None: if self.__total is not None:
all_entries.append(self.__total) all_entries.append(self.__total)
pagination: Pagination[Entry] = Pagination[Entry](all_entries) pagination: Pagination[ReportEntry] \
page_entries: list[Entry] = pagination.list = Pagination[ReportEntry](all_entries)
page_entries: list[ReportEntry] = pagination.list
has_data: bool = len(page_entries) > 0 has_data: bool = len(page_entries) > 0
populate_entries(page_entries) 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: if len(page_entries) > 0 and page_entries[0].is_brought_forward:
brought_forward = page_entries[0] brought_forward = page_entries[0]
page_entries = page_entries[1:] page_entries = page_entries[1:]
total: Entry | None = None total: ReportEntry | None = None
if len(page_entries) > 0 and page_entries[-1].is_total: if len(page_entries) > 0 and page_entries[-1].is_total:
total = page_entries[-1] total = page_entries[-1]
page_entries = page_entries[:-1] page_entries = page_entries[:-1]

View File

@ -36,11 +36,11 @@ from .utils.report_chooser import ReportChooser
from .utils.report_type import ReportType from .utils.report_type import ReportType
class Entry: class ReportEntry:
"""An entry in the journal.""" """An entry in the report."""
def __init__(self, entry: JournalEntry | None = None): def __init__(self, entry: JournalEntry | None = None):
"""Constructs the entry in the journal. """Constructs the entry in the report.
:param entry: The journal entry. :param entry: The journal entry.
""" """
@ -117,8 +117,8 @@ class PageParams(BasePageParams):
"""The HTML page parameters.""" """The HTML page parameters."""
def __init__(self, period: Period, def __init__(self, period: Period,
pagination: Pagination[Entry], pagination: Pagination[ReportEntry],
entries: list[Entry]): entries: list[ReportEntry]):
"""Constructs the HTML page parameters. """Constructs the HTML page parameters.
:param period: The period. :param period: The period.
@ -126,9 +126,9 @@ class PageParams(BasePageParams):
""" """
self.period: Period = period self.period: Period = period
"""The period.""" """The period."""
self.pagination: Pagination[Entry] = pagination self.pagination: Pagination[ReportEntry] = pagination
"""The pagination.""" """The pagination."""
self.entries: list[Entry] = entries self.entries: list[ReportEntry] = entries
"""The entries.""" """The entries."""
self.period_chooser: JournalPeriodChooser \ self.period_chooser: JournalPeriodChooser \
= JournalPeriodChooser() = JournalPeriodChooser()
@ -152,7 +152,7 @@ class PageParams(BasePageParams):
period=self.period) period=self.period)
def populate_entries(entries: list[Entry]) -> None: def populate_entries(entries: list[ReportEntry]) -> None:
"""Populates the report entries with relative data. """Populates the report entries with relative data.
:param entries: The report entries. :param entries: The report entries.
@ -184,10 +184,10 @@ class Journal(BaseReport):
"""The account.""" """The account."""
self.__period: Period = period self.__period: Period = period
"""The period.""" """The period."""
self.__entries: list[Entry] = self.__query_entries() self.__entries: list[ReportEntry] = self.__query_entries()
"""The journal entries.""" """The journal entries."""
def __query_entries(self) -> list[Entry]: def __query_entries(self) -> list[ReportEntry]:
"""Queries and returns the journal entries. """Queries and returns the journal entries.
:return: The journal entries. :return: The journal entries.
@ -197,7 +197,7 @@ class Journal(BaseReport):
conditions.append(Transaction.date >= self.__period.start) conditions.append(Transaction.date >= self.__period.start)
if self.__period.end is not None: if self.__period.end is not None:
conditions.append(Transaction.date <= self.__period.end) 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) .query(JournalEntry).join(Transaction).filter(*conditions)
.order_by(Transaction.date, .order_by(Transaction.date,
JournalEntry.is_debit.desc(), JournalEntry.is_debit.desc(),
@ -232,8 +232,9 @@ class Journal(BaseReport):
:return: The report as HTML. :return: The report as HTML.
""" """
pagination: Pagination[Entry] = Pagination[Entry](self.__entries) pagination: Pagination[ReportEntry] \
page_entries: list[Entry] = pagination.list = Pagination[ReportEntry](self.__entries)
page_entries: list[ReportEntry] = pagination.list
populate_entries(page_entries) populate_entries(page_entries)
params: PageParams = PageParams(period=self.__period, params: PageParams = PageParams(period=self.__period,
pagination=pagination, pagination=pagination,

View File

@ -37,11 +37,11 @@ from .utils.report_chooser import ReportChooser
from .utils.report_type import ReportType from .utils.report_type import ReportType
class Entry: class ReportEntry:
"""An entry in the ledger.""" """An entry in the report."""
def __init__(self, entry: JournalEntry | None = None): def __init__(self, entry: JournalEntry | None = None):
"""Constructs the entry in the ledger. """Constructs the entry in the report.
:param entry: The journal entry. :param entry: The journal entry.
""" """
@ -90,18 +90,18 @@ class EntryCollector:
"""The account.""" """The account."""
self.__period: Period = period self.__period: Period = period
"""The period""" """The period"""
self.brought_forward: Entry | None self.brought_forward: ReportEntry | None
"""The brought-forward entry.""" """The brought-forward entry."""
self.entries: list[Entry] self.entries: list[ReportEntry]
"""The ledger entries.""" """The ledger entries."""
self.total: Entry | None self.total: ReportEntry | None
"""The total entry.""" """The total entry."""
self.brought_forward = self.__get_brought_forward_entry() self.brought_forward = self.__get_brought_forward_entry()
self.entries = self.__query_entries() self.entries = self.__query_entries()
self.total = self.__get_total_entry() self.total = self.__get_total_entry()
self.__populate_balance() 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. """Queries, composes and returns the brought-forward entry.
:return: The brought-forward entry, or None if the ledger starts from :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) balance: int | None = db.session.scalar(select)
if balance is None: if balance is None:
return None return None
entry: Entry = Entry() entry: ReportEntry = ReportEntry()
entry.is_brought_forward = True entry.is_brought_forward = True
entry.date = self.__period.start entry.date = self.__period.start
entry.summary = gettext("Brought forward") entry.summary = gettext("Brought forward")
@ -130,7 +130,7 @@ class EntryCollector:
entry.balance = balance entry.balance = balance
return entry return entry
def __query_entries(self) -> list[Entry]: def __query_entries(self) -> list[ReportEntry]:
"""Queries and returns the ledger entries. """Queries and returns the ledger entries.
:return: The ledger entries. :return: The ledger entries.
@ -142,20 +142,20 @@ class EntryCollector:
conditions.append(Transaction.date >= self.__period.start) conditions.append(Transaction.date >= self.__period.start)
if self.__period.end is not None: if self.__period.end is not None:
conditions.append(Transaction.date <= self.__period.end) 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) .filter(*conditions)
.order_by(Transaction.date, .order_by(Transaction.date,
JournalEntry.is_debit.desc(), JournalEntry.is_debit.desc(),
JournalEntry.no).all()] JournalEntry.no).all()]
def __get_total_entry(self) -> Entry | None: def __get_total_entry(self) -> ReportEntry | None:
"""Composes the total entry. """Composes the total entry.
:return: The total entry, or None if there is no data. :return: The total entry, or None if there is no data.
""" """
if self.brought_forward is None and len(self.entries) == 0: if self.brought_forward is None and len(self.entries) == 0:
return None return None
entry: Entry = Entry() entry: ReportEntry = ReportEntry()
entry.is_total = True entry.is_total = True
entry.summary = gettext("Total") entry.summary = gettext("Total")
entry.debit = sum([x.debit for x in self.entries entry.debit = sum([x.debit for x in self.entries
@ -230,10 +230,10 @@ class PageParams(BasePageParams):
account: Account, account: Account,
period: Period, period: Period,
has_data: bool, has_data: bool,
pagination: Pagination[Entry], pagination: Pagination[ReportEntry],
brought_forward: Entry | None, brought_forward: ReportEntry | None,
entries: list[Entry], entries: list[ReportEntry],
total: Entry | None): total: ReportEntry | None):
"""Constructs the HTML page parameters. """Constructs the HTML page parameters.
:param currency: The currency. :param currency: The currency.
@ -252,13 +252,13 @@ class PageParams(BasePageParams):
"""The period.""" """The period."""
self.__has_data: bool = has_data self.__has_data: bool = has_data
"""True if there is any data, or False otherwise.""" """True if there is any data, or False otherwise."""
self.pagination: Pagination[Entry] = pagination self.pagination: Pagination[ReportEntry] = pagination
"""The pagination.""" """The pagination."""
self.brought_forward: Entry | None = brought_forward self.brought_forward: ReportEntry | None = brought_forward
"""The brought-forward entry.""" """The brought-forward entry."""
self.entries: list[Entry] = entries self.entries: list[ReportEntry] = entries
"""The entries.""" """The entries."""
self.total: Entry | None = total self.total: ReportEntry | None = total
"""The total entry.""" """The total entry."""
self.period_chooser: LedgerPeriodChooser \ self.period_chooser: LedgerPeriodChooser \
= LedgerPeriodChooser(currency, account) = LedgerPeriodChooser(currency, account)
@ -326,7 +326,7 @@ class PageParams(BasePageParams):
.order_by(Account.base_code, Account.no).all()] .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. """Populates the report entries with relative data.
:param entries: The report entries. :param entries: The report entries.
@ -361,11 +361,11 @@ class Ledger(BaseReport):
"""The period.""" """The period."""
collector: EntryCollector = EntryCollector( collector: EntryCollector = EntryCollector(
self.__currency, self.__account, self.__period) 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.""" """The brought-forward entry."""
self.__entries: list[Entry] = collector.entries self.__entries: list[ReportEntry] = collector.entries
"""The ledger entries.""" """The ledger entries."""
self.__total: Entry | None = collector.total self.__total: ReportEntry | None = collector.total
"""The total entry.""" """The total entry."""
def csv(self) -> Response: def csv(self) -> Response:
@ -408,21 +408,22 @@ class Ledger(BaseReport):
:return: The report as HTML. :return: The report as HTML.
""" """
all_entries: list[Entry] = [] all_entries: list[ReportEntry] = []
if self.__brought_forward is not None: if self.__brought_forward is not None:
all_entries.append(self.__brought_forward) all_entries.append(self.__brought_forward)
all_entries.extend(self.__entries) all_entries.extend(self.__entries)
if self.__total is not None: if self.__total is not None:
all_entries.append(self.__total) all_entries.append(self.__total)
pagination: Pagination[Entry] = Pagination[Entry](all_entries) pagination: Pagination[ReportEntry] \
page_entries: list[Entry] = pagination.list = Pagination[ReportEntry](all_entries)
page_entries: list[ReportEntry] = pagination.list
has_data: bool = len(page_entries) > 0 has_data: bool = len(page_entries) > 0
populate_entries(page_entries) 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: if len(page_entries) > 0 and page_entries[0].is_brought_forward:
brought_forward = page_entries[0] brought_forward = page_entries[0]
page_entries = page_entries[1:] page_entries = page_entries[1:]
total: Entry | None = None total: ReportEntry | None = None
if len(page_entries) > 0 and page_entries[-1].is_total: if len(page_entries) > 0 and page_entries[-1].is_total:
total = page_entries[-1] total = page_entries[-1]
page_entries = page_entries[:-1] page_entries = page_entries[:-1]

View File

@ -28,7 +28,7 @@ from accounting.models import Currency, CurrencyL10n, Account, AccountL10n, \
Transaction, JournalEntry Transaction, JournalEntry
from accounting.utils.pagination import Pagination from accounting.utils.pagination import Pagination
from accounting.utils.query import parse_query_keywords 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_page_params import BasePageParams
from .utils.base_report import BaseReport from .utils.base_report import BaseReport
from .utils.csv_export import csv_download from .utils.csv_export import csv_download
@ -39,15 +39,15 @@ from .utils.report_type import ReportType
class PageParams(BasePageParams): class PageParams(BasePageParams):
"""The HTML page parameters.""" """The HTML page parameters."""
def __init__(self, pagination: Pagination[Entry], def __init__(self, pagination: Pagination[ReportEntry],
entries: list[Entry]): entries: list[ReportEntry]):
"""Constructs the HTML page parameters. """Constructs the HTML page parameters.
:param entries: The search result entries. :param entries: The search result entries.
""" """
self.pagination: Pagination[Entry] = pagination self.pagination: Pagination[ReportEntry] = pagination
"""The pagination.""" """The pagination."""
self.entries: list[Entry] = entries self.entries: list[ReportEntry] = entries
"""The entries.""" """The entries."""
@property @property
@ -73,10 +73,10 @@ class Search(BaseReport):
def __init__(self): def __init__(self):
"""Constructs a search.""" """Constructs a search."""
"""The account.""" """The account."""
self.__entries: list[Entry] = self.__query_entries() self.__entries: list[ReportEntry] = self.__query_entries()
"""The journal entries.""" """The journal entries."""
def __query_entries(self) -> list[Entry]: def __query_entries(self) -> list[ReportEntry]:
"""Queries and returns the journal entries. """Queries and returns the journal entries.
:return: The journal entries. :return: The journal entries.
@ -99,7 +99,7 @@ class Search(BaseReport):
except ArithmeticError: except ArithmeticError:
pass pass
conditions.append(sa.or_(*sub_conditions)) 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 @staticmethod
def __get_account_condition(k: str) -> sa.Select: def __get_account_condition(k: str) -> sa.Select:
@ -197,8 +197,9 @@ class Search(BaseReport):
:return: The report as HTML. :return: The report as HTML.
""" """
pagination: Pagination[Entry] = Pagination[Entry](self.__entries) pagination: Pagination[ReportEntry] \
page_entries: list[Entry] = pagination.list = Pagination[ReportEntry](self.__entries)
page_entries: list[ReportEntry] = pagination.list
populate_entries(page_entries) populate_entries(page_entries)
params: PageParams = PageParams(pagination=pagination, params: PageParams = PageParams(pagination=pagination,
entries=page_entries) entries=page_entries)