Skip to content

Commit 7b9ff1b

Browse files
committed
Refactor transaction queries and add account balance utility
Updated transaction filtering methods to validate input types and support AccountModel, UUID, and string formats. Added new utility methods for cleared and reconciled transaction queries. Introduced an account balance retrieval method in the entity model for better account insights.
1 parent b5ddcf4 commit 7b9ff1b

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

django_ledger/models/entity.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,20 @@ def create_account_by_kwargs(self,
15171517
# account_model.clean()
15181518
return coa_model, coa_model.create_account(**account_model_kwargs)
15191519

1520+
def get_account_balance(self,
1521+
account_codes: List[str],
1522+
to_date: Union[datetime, date, str],
1523+
**kwargs):
1524+
1525+
io_context = self.digest(
1526+
entity_model=self.slug,
1527+
accounts=account_codes,
1528+
to_date=to_date,
1529+
**kwargs
1530+
)
1531+
1532+
return io_context
1533+
15201534
# ### LEDGER MANAGEMENT ####
15211535
def get_ledgers(self, posted: Optional[bool] = None):
15221536
if posted is not None:
@@ -3135,12 +3149,12 @@ class EntityModel(EntityModelAbstract):
31353149
"""
31363150
Entity Model Base Class From Abstract
31373151
"""
3152+
31383153
class Meta(EntityModelAbstract.Meta):
31393154
swappable = 'DJANGO_LEDGER_ENTITY_MODEL'
31403155
abstract = False
31413156

31423157

3143-
31443158
# ## ENTITY STATE....
31453159
class EntityStateModelAbstract(Model):
31463160
KEY_JOURNAL_ENTRY = 'je'

django_ledger/models/transactions.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def posted(self) -> QuerySet:
6767
Q(journal_entry__ledger__posted=True)
6868
)
6969

70-
def for_accounts(self, account_list: List[str or AccountModel]):
70+
def for_accounts(self, account_list: List[Union[AccountModel, str, UUID]]):
7171
"""
7272
Filters transactions based on the accounts they are associated with.
7373
@@ -82,9 +82,20 @@ def for_accounts(self, account_list: List[str or AccountModel]):
8282
TransactionModelQuerySet
8383
A QuerySet filtered for transactions associated with the specified accounts.
8484
"""
85-
if isinstance(account_list, list) > 0 and isinstance(account_list[0], str):
85+
86+
if not isinstance(account_list, list) or not len(account_list) > 0:
87+
raise TransactionModelValidationError(
88+
message=_('Account list must be a list of AccountModel, UUID or str objects (codes).')
89+
)
90+
if isinstance(account_list[0], str):
8691
return self.filter(account__code__in=account_list)
87-
return self.filter(account__in=account_list)
92+
elif isinstance(account_list[0], UUID):
93+
return self.filter(account__uuid__in=account_list)
94+
elif isinstance(account_list[0], AccountModel):
95+
return self.filter(account__in=account_list)
96+
raise TransactionModelValidationError(
97+
message=_('Account list must be a list of AccountModel, UUID or str objects (codes).')
98+
)
8899

89100
def for_roles(self, role_list: Union[str, List[str], Set[str]]):
90101
"""
@@ -294,6 +305,18 @@ def with_annotated_details(self):
294305
timestamp=F('journal_entry__timestamp'),
295306
)
296307

308+
def is_cleared(self):
309+
return self.filter(cleared=True)
310+
311+
def not_cleared(self):
312+
return self.filter(cleared=False)
313+
314+
def is_reconciled(self):
315+
return self.filter(reconciled=True)
316+
317+
def not_reconciled(self):
318+
return self.filter(reconciled=False)
319+
297320

298321
class TransactionModelManager(Manager):
299322
"""
@@ -317,6 +340,7 @@ def get_queryset(self) -> TransactionModelQuerySet:
317340
"""
318341
qs = TransactionModelQuerySet(self.model, using=self._db)
319342
return qs.annotate(
343+
timestamp=F('journal_entry__timestamp'),
320344
_coa_id=F('account__coa_model_id') # Annotates the `coa_model_id` from the related `account`.
321345
).select_related(
322346
'journal_entry', # Pre-loads the related Journal Entry.

0 commit comments

Comments
 (0)