Removed the period filter from the unapplied original line items and unmatched offsets. It does not make sense for these two reports.

This commit is contained in:
2023-04-18 09:21:42 +08:00
parent 26b4d4388f
commit 014d67f7b8
16 changed files with 148 additions and 304 deletions

View File

@ -24,17 +24,14 @@ import sqlalchemy as sa
from accounting import db
from accounting.models import Currency, Account, JournalEntry, \
JournalEntryLineItem
from accounting.report.period import Period
from accounting.utils.cast import be
from accounting.utils.offset_alias import offset_alias
def get_accounts_with_unapplied(currency: Currency,
period: Period) -> list[Account]:
def get_accounts_with_unapplied(currency: Currency) -> list[Account]:
"""Returns the accounts with unapplied original line items.
:param currency: The currency.
:param period: The period.
:return: The accounts with unapplied original line items.
"""
offset: sa.Alias = offset_alias()
@ -44,24 +41,18 @@ def get_accounts_with_unapplied(currency: Currency,
(be(offset.c.is_debit == JournalEntryLineItem.is_debit),
offset.c.amount),
else_=-offset.c.amount))).label("net_balance")
conditions: list[sa.BinaryExpression] \
= [Account.is_need_offset,
be(JournalEntryLineItem.currency_code == currency.code),
sa.or_(sa.and_(Account.base_code.startswith("2"),
sa.not_(JournalEntryLineItem.is_debit)),
sa.and_(Account.base_code.startswith("1"),
JournalEntryLineItem.is_debit))]
if period.start is not None:
conditions.append(JournalEntry.date >= period.start)
if period.end is not None:
conditions.append(JournalEntry.date <= period.end)
select_unapplied: sa.Select \
= sa.select(JournalEntryLineItem.id)\
.join(JournalEntry).join(Account)\
.join(offset, be(JournalEntryLineItem.id
== offset.c.original_line_item_id),
isouter=True)\
.filter(*conditions)\
.filter(Account.is_need_offset,
be(JournalEntryLineItem.currency_code == currency.code),
sa.or_(sa.and_(Account.base_code.startswith("2"),
sa.not_(JournalEntryLineItem.is_debit)),
sa.and_(Account.base_code.startswith("1"),
JournalEntryLineItem.is_debit)))\
.group_by(JournalEntryLineItem.id)\
.having(sa.or_(sa.func.count(offset.c.id) == 0, net_balance != 0))
@ -81,13 +72,12 @@ def get_accounts_with_unapplied(currency: Currency,
return accounts
def get_net_balances(currency: Currency, account: Account,
period: Period | None) -> dict[int, Decimal | None]:
def get_net_balances(currency: Currency, account: Account) \
-> dict[int, Decimal | None]:
"""Returns the net balances of the unapplied line items of the account.
:param currency: The currency.
:param account: The account.
:param period: The period, or None for all time.
:return: The net balances of the unapplied line items of the account.
"""
offset: sa.Alias = offset_alias()
@ -97,25 +87,18 @@ def get_net_balances(currency: Currency, account: Account,
(be(offset.c.is_debit == JournalEntryLineItem.is_debit),
offset.c.amount),
else_=-offset.c.amount))).label("net_balance")
conditions: list[sa.BinaryExpression] \
= [be(Account.id == account.id),
be(JournalEntryLineItem.currency_code == currency.code),
sa.or_(sa.and_(Account.base_code.startswith("2"),
sa.not_(JournalEntryLineItem.is_debit)),
sa.and_(Account.base_code.startswith("1"),
JournalEntryLineItem.is_debit))]
if period is not None:
if period.start is not None:
conditions.append(be(JournalEntry.date >= period.start))
if period.end is not None:
conditions.append(be(JournalEntry.date <= period.end))
select_net_balances: sa.Select \
= sa.select(JournalEntryLineItem.id, net_balance) \
.join(JournalEntry).join(Account) \
.join(offset, be(JournalEntryLineItem.id
== offset.c.original_line_item_id),
isouter=True) \
.filter(*conditions) \
.filter(be(Account.id == account.id),
be(JournalEntryLineItem.currency_code == currency.code),
sa.or_(sa.and_(Account.base_code.startswith("2"),
sa.not_(JournalEntryLineItem.is_debit)),
sa.and_(Account.base_code.startswith("1"),
JournalEntryLineItem.is_debit))) \
.group_by(JournalEntryLineItem.id) \
.having(sa.or_(sa.func.count(offset.c.id) == 0, net_balance != 0))
return {x.id: x.net_balance