Merged the "accounting.utils.unapplied" module into the "accounting.utils.offset_matcher" module as the "__get_unapplied" method of the OffsetMatcher class. It is only used in the offset matcher.
This commit is contained in:
parent
260e3cbe82
commit
84ad065782
@ -17,11 +17,15 @@
|
|||||||
"""The forms for the unmatched offset management.
|
"""The forms for the unmatched offset management.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from sqlalchemy.orm import selectinload
|
from sqlalchemy.orm import selectinload
|
||||||
|
|
||||||
|
from accounting import db
|
||||||
from accounting.models import Account, JournalEntry, JournalEntryLineItem
|
from accounting.models import Account, JournalEntry, JournalEntryLineItem
|
||||||
from accounting.utils.unapplied import get_unapplied_original_line_items
|
from accounting.utils.cast import be
|
||||||
|
from accounting.utils.offset_alias import offset_alias
|
||||||
|
|
||||||
|
|
||||||
class OffsetPair:
|
class OffsetPair:
|
||||||
@ -67,8 +71,7 @@ class OffsetMatcher:
|
|||||||
|
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
self.unapplied: list[JournalEntryLineItem] \
|
self.unapplied: list[JournalEntryLineItem] = self.__get_unapplied()
|
||||||
= get_unapplied_original_line_items(self.account)
|
|
||||||
self.total = len(self.unapplied)
|
self.total = len(self.unapplied)
|
||||||
if self.total == 0:
|
if self.total == 0:
|
||||||
self.is_having_matches = False
|
self.is_having_matches = False
|
||||||
@ -95,6 +98,47 @@ class OffsetMatcher:
|
|||||||
remains.remove(offset_candidates[0])
|
remains.remove(offset_candidates[0])
|
||||||
self.is_having_matches = len(self.matched_pairs) > 0
|
self.is_having_matches = len(self.matched_pairs) > 0
|
||||||
|
|
||||||
|
def __get_unapplied(self) -> list[JournalEntryLineItem]:
|
||||||
|
"""Returns the unapplied original line items of the account.
|
||||||
|
|
||||||
|
:return: The unapplied original line items of the account.
|
||||||
|
"""
|
||||||
|
offset: sa.Alias = offset_alias()
|
||||||
|
net_balance: sa.Label \
|
||||||
|
= (JournalEntryLineItem.amount
|
||||||
|
+ sa.func.sum(sa.case(
|
||||||
|
(be(offset.c.is_debit == JournalEntryLineItem.is_debit),
|
||||||
|
offset.c.amount),
|
||||||
|
else_=-offset.c.amount))).label("net_balance")
|
||||||
|
select_net_balances: sa.Select \
|
||||||
|
= sa.select(JournalEntryLineItem.id, net_balance) \
|
||||||
|
.join(Account) \
|
||||||
|
.join(offset, be(JournalEntryLineItem.id
|
||||||
|
== offset.c.original_line_item_id),
|
||||||
|
isouter=True) \
|
||||||
|
.filter(be(Account.id == self.account.id),
|
||||||
|
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))
|
||||||
|
net_balances: dict[int, Decimal] \
|
||||||
|
= {x.id: x.net_balance
|
||||||
|
for x in db.session.execute(select_net_balances).all()}
|
||||||
|
line_items: list[JournalEntryLineItem] = JournalEntryLineItem.query \
|
||||||
|
.filter(JournalEntryLineItem.id.in_({x for x in net_balances})) \
|
||||||
|
.join(JournalEntry) \
|
||||||
|
.order_by(JournalEntry.date, JournalEntry.no,
|
||||||
|
JournalEntryLineItem.is_debit, JournalEntryLineItem.no) \
|
||||||
|
.options(selectinload(JournalEntryLineItem.currency),
|
||||||
|
selectinload(JournalEntryLineItem.journal_entry)).all()
|
||||||
|
for line_item in line_items:
|
||||||
|
line_item.net_balance = line_item.amount \
|
||||||
|
if net_balances[line_item.id] is None \
|
||||||
|
else net_balances[line_item.id]
|
||||||
|
return line_items
|
||||||
|
|
||||||
def __get_unmatched_offsets(self) -> list[JournalEntryLineItem]:
|
def __get_unmatched_offsets(self) -> list[JournalEntryLineItem]:
|
||||||
"""Returns the unmatched offsets of an account.
|
"""Returns the unmatched offsets of an account.
|
||||||
|
|
||||||
|
@ -1,72 +0,0 @@
|
|||||||
# The Mia! Accounting Project.
|
|
||||||
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/4/8
|
|
||||||
|
|
||||||
# Copyright (c) 2023 imacat.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
# you may not use this file except in compliance with the License.
|
|
||||||
# You may obtain a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
# See the License for the specific language governing permissions and
|
|
||||||
# limitations under the License.
|
|
||||||
"""The unapplied original line item utilities.
|
|
||||||
|
|
||||||
"""
|
|
||||||
from decimal import Decimal
|
|
||||||
|
|
||||||
import sqlalchemy as sa
|
|
||||||
from sqlalchemy.orm import selectinload
|
|
||||||
|
|
||||||
from accounting import db
|
|
||||||
from accounting.models import Account, JournalEntry, JournalEntryLineItem
|
|
||||||
from accounting.utils.cast import be
|
|
||||||
from accounting.utils.offset_alias import offset_alias
|
|
||||||
|
|
||||||
|
|
||||||
def get_unapplied_original_line_items(account: Account) \
|
|
||||||
-> list[JournalEntryLineItem]:
|
|
||||||
"""Queries and returns the unapplied original line items in an account.
|
|
||||||
|
|
||||||
:param account: The account.
|
|
||||||
:return: The unapplied original line items in the account.
|
|
||||||
"""
|
|
||||||
offset: sa.Alias = offset_alias()
|
|
||||||
net_balance: sa.Label \
|
|
||||||
= (JournalEntryLineItem.amount
|
|
||||||
+ sa.func.sum(sa.case(
|
|
||||||
(be(offset.c.is_debit == JournalEntryLineItem.is_debit),
|
|
||||||
offset.c.amount),
|
|
||||||
else_=-offset.c.amount))).label("net_balance")
|
|
||||||
select_net_balances: sa.Select \
|
|
||||||
= sa.select(JournalEntryLineItem.id, net_balance) \
|
|
||||||
.join(Account) \
|
|
||||||
.join(offset, be(JournalEntryLineItem.id
|
|
||||||
== offset.c.original_line_item_id),
|
|
||||||
isouter=True) \
|
|
||||||
.filter(be(Account.id == account.id),
|
|
||||||
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))
|
|
||||||
net_balances: dict[int, Decimal] \
|
|
||||||
= {x.id: x.net_balance
|
|
||||||
for x in db.session.execute(select_net_balances).all()}
|
|
||||||
line_items: list[JournalEntryLineItem] = JournalEntryLineItem.query \
|
|
||||||
.filter(JournalEntryLineItem.id.in_({x for x in net_balances})) \
|
|
||||||
.join(JournalEntry) \
|
|
||||||
.order_by(JournalEntry.date, JournalEntry.no,
|
|
||||||
JournalEntryLineItem.is_debit, JournalEntryLineItem.no) \
|
|
||||||
.options(selectinload(JournalEntryLineItem.currency),
|
|
||||||
selectinload(JournalEntryLineItem.journal_entry)).all()
|
|
||||||
for line_item in line_items:
|
|
||||||
line_item.net_balance = line_item.amount \
|
|
||||||
if net_balances[line_item.id] is None \
|
|
||||||
else net_balances[line_item.id]
|
|
||||||
return line_items
|
|
Loading…
Reference in New Issue
Block a user