Fixed the ledger and the income and expenses log not to show the total entry when there is actually no data.

This commit is contained in:
依瑪貓 2023-03-07 23:32:28 +08:00
parent 3814f0cb18
commit ede1160943
2 changed files with 28 additions and 20 deletions

View File

@ -93,7 +93,7 @@ class EntryCollector:
"""The brought-forward entry.""" """The brought-forward entry."""
self.entries: list[Entry] self.entries: list[Entry]
"""The log entries.""" """The log entries."""
self.total: Entry self.total: Entry | 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()
@ -154,11 +154,13 @@ class EntryCollector:
JournalEntry.is_debit, JournalEntry.is_debit,
JournalEntry.no)] JournalEntry.no)]
def __get_total_entry(self) -> Entry: def __get_total_entry(self) -> Entry | None:
"""Composes the total entry. """Composes the total entry.
:return: None. :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: Entry = Entry()
entry.is_total = True entry.is_total = True
entry.summary = gettext("Total") entry.summary = gettext("Total")
@ -383,7 +385,7 @@ class IncomeExpenses:
"""The brought-forward entry.""" """The brought-forward entry."""
self.__entries: list[Entry] = collector.entries self.__entries: list[Entry] = collector.entries
"""The log entries.""" """The log entries."""
self.__total: Entry = collector.total self.__total: Entry | None = collector.total
"""The total entry.""" """The total entry."""
def csv(self) -> Response: def csv(self) -> Response:
@ -417,6 +419,7 @@ class IncomeExpenses:
rows.extend([CSVRow(x.date, str(x.account).title(), x.summary, rows.extend([CSVRow(x.date, str(x.account).title(), x.summary,
x.income, x.expense, x.balance, x.note) x.income, x.expense, x.balance, x.note)
for x in self.__entries]) for x in self.__entries])
if self.__total is not None:
rows.append(CSVRow(gettext("Total"), None, None, rows.append(CSVRow(gettext("Total"), None, None,
self.__total.income, self.__total.expense, self.__total.income, self.__total.expense,
self.__total.balance, None)) self.__total.balance, None))
@ -431,17 +434,18 @@ class IncomeExpenses:
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:
all_entries.append(self.__total) all_entries.append(self.__total)
pagination: Pagination[Entry] = Pagination[Entry](all_entries) pagination: Pagination[Entry] = Pagination[Entry](all_entries)
page_entries: list[Entry] = pagination.list page_entries: list[Entry] = 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: Entry | None = None
if 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: Entry | None = None
if 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]
params: IncomeExpensesPageParams = IncomeExpensesPageParams( params: IncomeExpensesPageParams = IncomeExpensesPageParams(

View File

@ -93,7 +93,7 @@ class EntryCollector:
"""The brought-forward entry.""" """The brought-forward entry."""
self.entries: list[Entry] self.entries: list[Entry]
"""The ledger entries.""" """The ledger entries."""
self.total: Entry self.total: Entry | 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()
@ -147,11 +147,13 @@ class EntryCollector:
JournalEntry.is_debit.desc(), JournalEntry.is_debit.desc(),
JournalEntry.no).all()] JournalEntry.no).all()]
def __get_total_entry(self) -> Entry: def __get_total_entry(self) -> Entry | None:
"""Composes the total entry. """Composes the total entry.
:return: None. :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: Entry = Entry()
entry.is_total = True entry.is_total = True
entry.summary = gettext("Total") entry.summary = gettext("Total")
@ -362,7 +364,7 @@ class Ledger:
"""The brought-forward entry.""" """The brought-forward entry."""
self.__entries: list[Entry] = collector.entries self.__entries: list[Entry] = collector.entries
"""The ledger entries.""" """The ledger entries."""
self.__total: Entry = collector.total self.__total: Entry | None = collector.total
"""The total entry.""" """The total entry."""
def csv(self) -> Response: def csv(self) -> Response:
@ -394,6 +396,7 @@ class Ledger:
rows.extend([CSVRow(x.date, x.summary, rows.extend([CSVRow(x.date, x.summary,
x.debit, x.credit, x.balance, x.note) x.debit, x.credit, x.balance, x.note)
for x in self.__entries]) for x in self.__entries])
if self.__total is not None:
rows.append(CSVRow(gettext("Total"), None, rows.append(CSVRow(gettext("Total"), None,
self.__total.debit, self.__total.credit, self.__total.debit, self.__total.credit,
self.__total.balance, None)) self.__total.balance, None))
@ -408,17 +411,18 @@ class Ledger:
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:
all_entries.append(self.__total) all_entries.append(self.__total)
pagination: Pagination[Entry] = Pagination[Entry](all_entries) pagination: Pagination[Entry] = Pagination[Entry](all_entries)
page_entries: list[Entry] = pagination.list page_entries: list[Entry] = 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: Entry | None = None
if 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: Entry | None = None
if 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]
params: LedgerPageParams = LedgerPageParams( params: LedgerPageParams = LedgerPageParams(