Changed the "offsets" relationship to a pseudo property, to apply the correct but complex ordering rules.

This commit is contained in:
依瑪貓 2023-04-07 16:03:23 +08:00
parent fe7a8842ce
commit 04ec51afbe

View File

@ -660,12 +660,8 @@ class JournalEntryLineItem(db.Model):
nullable=True)
"""The ID of the original line item."""
original_line_item = db.relationship("JournalEntryLineItem",
back_populates="offsets",
remote_side=id, passive_deletes=True)
"""The original line item."""
offsets = db.relationship("JournalEntryLineItem",
back_populates="original_line_item")
"""The offset items."""
currency_code = db.Column(db.String,
db.ForeignKey(Currency.code, onupdate="CASCADE"),
nullable=False)
@ -758,6 +754,21 @@ class JournalEntryLineItem(db.Model):
"""
setattr(self, "__net_balance", net_balance)
@property
def offsets(self) -> list[t.Self]:
"""Returns the offset items.
:return: The offset items.
"""
if not hasattr(self, "__offsets"):
cls: t.Type[t.Self] = self.__class__
offsets: list[t.Self] = cls.query.join(JournalEntry)\
.filter(JournalEntryLineItem.original_line_item_id == self.id)\
.order_by(JournalEntry.date, JournalEntry.no,
cls.is_debit, cls.no).all()
setattr(self, "__offsets", offsets)
return getattr(self, "__offsets")
@property
def query_values(self) -> list[str]:
"""Returns the values to be queried.