Revised the __set_data method of the trial balance and the __query_balances of the income statement for consistency.

This commit is contained in:
依瑪貓 2023-03-09 10:49:11 +08:00
parent ed7a8ac0fd
commit 8abe20dba5
2 changed files with 8 additions and 10 deletions

View File

@ -269,16 +269,15 @@ class IncomeStatement(BaseReport):
balance_func: sa.Function = sa.func.sum(sa.case( balance_func: sa.Function = sa.func.sum(sa.case(
(JournalEntry.is_debit, -JournalEntry.amount), (JournalEntry.is_debit, -JournalEntry.amount),
else_=JournalEntry.amount)).label("balance") else_=JournalEntry.amount)).label("balance")
select_balance: sa.Select \ select_balances: sa.Select = sa.select(Account.id, balance_func)\
= sa.select(JournalEntry.account_id, balance_func)\
.join(Transaction).join(Account)\ .join(Transaction).join(Account)\
.filter(*conditions)\ .filter(*conditions)\
.group_by(JournalEntry.account_id)\ .group_by(Account.id)\
.order_by(Account.base_code, Account.no) .order_by(Account.base_code, Account.no)
balances: list[sa.Row] = db.session.execute(select_balance).all() balances: list[sa.Row] = db.session.execute(select_balances).all()
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.account_id for x in balances])).all()} .filter(Account.id.in_([x.id for x in balances])).all()}
def get_url(account: Account) -> str: def get_url(account: Account) -> str:
"""Returns the ledger URL of an account. """Returns the ledger URL of an account.
@ -293,9 +292,9 @@ class IncomeStatement(BaseReport):
currency=self.__currency, account=account, currency=self.__currency, account=account,
period=self.__period) period=self.__period)
return [ReportAccount(account=accounts[x.account_id], return [ReportAccount(account=accounts[x.id],
amount=x.balance, amount=x.balance,
url=get_url(accounts[x.account_id])) url=get_url(accounts[x.id]))
for x in balances] for x in balances]
def csv(self) -> Response: def csv(self) -> Response:

View File

@ -191,11 +191,10 @@ class TrialBalance(BaseReport):
balance_func: sa.Function = sa.func.sum(sa.case( balance_func: sa.Function = sa.func.sum(sa.case(
(JournalEntry.is_debit, JournalEntry.amount), (JournalEntry.is_debit, JournalEntry.amount),
else_=-JournalEntry.amount)).label("balance") else_=-JournalEntry.amount)).label("balance")
select_balances: sa.Select \ select_balances: sa.Select = sa.select(Account.id, balance_func)\
= sa.select(Account.id, balance_func)\
.join(Transaction).join(Account)\ .join(Transaction).join(Account)\
.filter(*conditions)\ .filter(*conditions)\
.group_by(JournalEntry.account_id)\ .group_by(Account.id)\
.order_by(Account.base_code, Account.no) .order_by(Account.base_code, Account.no)
balances: list[sa.Row] = db.session.execute(select_balances).all() balances: list[sa.Row] = db.session.execute(select_balances).all()
accounts: dict[int, Account] \ accounts: dict[int, Account] \