From 04ec51afbe8181ea33df14b5b670d6bfac471d21 Mon Sep 17 00:00:00 2001 From: imacat Date: Fri, 7 Apr 2023 16:03:23 +0800 Subject: [PATCH] Changed the "offsets" relationship to a pseudo property, to apply the correct but complex ordering rules. --- src/accounting/models.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/accounting/models.py b/src/accounting/models.py index 1ec1a17..3f3c5da 100644 --- a/src/accounting/models.py +++ b/src/accounting/models.py @@ -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.