-
Notifications
You must be signed in to change notification settings - Fork 55
/
core.py
765 lines (602 loc) · 26.2 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
"""
Design Overview
---------------
The core models consist of:
- ``Account`` - Such as 'Accounts Receivable', a bank account, etc.
Accounts can be arranged as a tree structure,
where the balance of the parent account is the summation of the balances of all its children.
- ``Transaction`` - Represents a movement between accounts. Each transaction must have two or more legs.
- ``Leg`` - Represents a flow of money into (debit) or out of (credit) a transaction.
Debits are represented by negative amounts, and credits by positive amounts.
The sum of all a transaction's legs must equal zero.
This is enforced with a database constraint.
Additionally, there are models which related to the import of external bank statement data:
- ``StatementImport`` - Represents a simple import of zero or more statement
lines relating to a specific ``Account``.
- ``StatementLine`` - Represents a statement line. ``StatementLine.create_transaction()`` may be called to
create a transaction for the statement line.
"""
from datetime import date
from django.db import connection, models
from django.db import transaction
from django.db import transaction as db_transaction
from django.db.models import F, JSONField
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from djmoney.models.fields import MoneyField
from djmoney.settings import CURRENCY_CHOICES
from moneyed import CurrencyDoesNotExist, Money
from mptt.models import MPTTModel, TreeForeignKey, TreeManager
from hordak import exceptions
from hordak.defaults import (
DECIMAL_PLACES,
DEFAULT_CURRENCY,
MAX_DIGITS,
UUID_DEFAULT,
get_internal_currency,
)
from hordak.utilities.currency import Balance
from hordak.utilities.db_functions import GetBalance
from hordak.utilities.dreprecation import deprecated
#: Debit
DEBIT = "debit"
#: Credit
CREDIT = "credit"
def json_default():
return {}
def get_currency_choices():
return CURRENCY_CHOICES
class AccountQuerySet(models.QuerySet):
def net_balance(self, raw=False):
return sum((account.balance(raw) for account in self), Balance())
def with_balances(self, as_of: date = None):
"""Annotate the account queryset with account balances
This is a much more performant way to calculate account balances,
especially when calculating balances for a lot of accounts.
Note that you will get better performance by setting the `as_of`
to `None`. This is because the underlying custom database function
can avoid a join.
"""
return self.annotate(balance=GetBalance(F("id"), as_of=as_of))
class AccountManager(TreeManager):
def get_by_natural_key(self, uuid):
return self.get(uuid=uuid)
class AccountType(models.TextChoices):
# Eg. Cash in bank
asset = "AS", _("Asset")
# Eg. Loans, bills paid after the fact (in arrears)
liability = "LI", _("Liability")
# Eg. Sales, housemate contributions
income = "IN", _("Income")
# Eg. Office supplies, paying bills
expense = "EX", _("Expense")
# Eg. Money from shares
equity = "EQ", _("Equity")
# Used to represent currency conversions
trading = "TR", _("Currency Trading")
def account_default_currencies():
return (DEFAULT_CURRENCY,)
class Account(MPTTModel):
"""Represents an account
An account may have a parent, and may have zero or more children. Only root
accounts can have a type, all child accounts are assumed to have the same
type as their parent.
An account's balance is calculated as the sum of all of the transaction Leg's
referencing the account.
Attributes:
uuid (UUID): UUID for account. Use to prevent leaking of IDs (if desired).
name (str): Name of the account. Required.
parent (Account|None): Parent account, nonen if root account
code (str): Account code. Must combine with account codes of parent
accounts to get fully qualified account code.
type (str): Type of account as defined by `AccountType`. Can only be set on
root accounts. Child accounts are assumed to have the same time as their parent.
is_bank_account (bool): Is this a bank account. This implies we can import bank statements into
it and that it only supports a single currency.
"""
uuid = models.UUIDField(
default=UUID_DEFAULT, editable=False, verbose_name=_("uuid")
)
name = models.CharField(max_length=255, verbose_name=_("name"))
parent = TreeForeignKey(
"self",
null=True,
blank=True,
related_name="children",
db_index=True,
on_delete=models.CASCADE,
verbose_name=_("parent"),
)
code = models.CharField(max_length=6, null=True, blank=True, verbose_name=_("code"))
full_code = models.CharField(
max_length=255,
db_index=True,
unique=True,
null=True,
blank=True,
verbose_name=_("full_code"),
)
# TODO: Implement this child_code_width field, as it is probably a good idea
# child_code_width = models.PositiveSmallIntegerField(default=1)
type = models.CharField(
max_length=2, choices=AccountType.choices, blank=True, verbose_name=_("type")
)
is_bank_account = models.BooleanField(
default=False,
blank=True,
help_text="Is this a bank account. This implies we can import bank "
"statements into it and that it only supports a single currency",
verbose_name=_("is bank account"),
)
currencies = JSONField(
db_index=True,
default=account_default_currencies,
verbose_name=_("currencies"),
)
objects = AccountManager.from_queryset(AccountQuerySet)()
class MPTTMeta:
order_insertion_by = ["code"]
class Meta:
unique_together = (("parent", "code"),)
verbose_name = _("account")
def __init__(self, *args, **kwargs):
super(Account, self).__init__(*args, **kwargs)
self._initial_code = self.code
def save(self, *args, **kwargs):
is_creating = not bool(self.pk)
if is_creating:
update_fields = None
else:
# See issues #19 & #31. It seems that on Django 1.2, django-mptt's left/right
# tree fields get overwritten on save. The solution here is to exclude them from
# being modified upon saving by using the save methods' update_fields argument.
update_fields = [
"uuid",
"name",
"parent",
"code",
"type",
"is_bank_account",
"currencies",
]
super(Account, self).save(*args, update_fields=update_fields, **kwargs)
if connection.vendor == "mysql":
# We need updated lft/rght/tree_id values for the mysql_run_manual_trigger() call
self.refresh_from_db()
mysql_simulate_trigger(
"update_full_account_codes", self.lft, self.rght, self.tree_id
)
do_refresh = False
# If we've just created a non-root node then we're going to need to load
# the type back from the DB (as it is set by trigger)
if is_creating and not self.is_root_node():
do_refresh = True
# If we've just create this account or if the code has changed then we're
# going to need to reload from the DB (full_code is set by trigger)
if is_creating or self._initial_code != self.code:
do_refresh = True
if do_refresh:
self.refresh_from_db()
@classmethod
def validate_accounting_equation(cls):
"""Check that all accounts sum to 0"""
balances = [
account.balance(raw=True) for account in Account.objects.root_nodes()
]
if sum(balances, Balance()) != 0:
raise exceptions.AccountingEquationViolationError(
"Account balances do not sum to zero. They sum to {}".format(
sum(balances)
)
)
def __str__(self):
name = self.name or "Unnamed Account"
if self.is_leaf_node():
try:
balance = self.balance()
except (ValueError, CurrencyDoesNotExist):
if self.full_code:
return "{} {}".format(self.full_code, name)
else:
return name
else:
if self.full_code:
return "{} {} [{}]".format(self.full_code, name, balance)
else:
return "{} [{}]".format(name, balance)
else:
return name
def natural_key(self):
return (self.uuid,)
@property
def sign(self):
"""
Returns 1 if a credit should increase the value of the
account, or -1 if a credit should decrease the value of the
account.
This is based on the account type as is standard accounting practice.
The signs can be derrived from the following expanded form of the
accounting equation:
Assets = Liabilities + Equity + (Income - Expenses)
Which can be rearranged as:
0 = Liabilities + Equity + Income - Expenses - Assets
Further details here: https://en.wikipedia.org/wiki/Debits_and_credits
"""
return -1 if self.type in (AccountType.asset, AccountType.expense) else 1
def balance(self, as_of=None, raw=False, leg_query=None, **kwargs):
"""Get the balance for this account, including child accounts
Args:
as_of (Date): Only include transactions on or before this date
raw (bool): If true the returned balance should not have its sign
adjusted for display purposes.
kwargs (dict): Will be used to filter the transaction legs
Returns:
Balance
See Also:
:meth:`simple_balance()`
"""
balances = [
account.simple_balance(as_of=as_of, raw=raw, leg_query=leg_query, **kwargs)
for account in self.get_descendants(include_self=True)
]
return sum(balances, Balance())
def simple_balance(self, as_of=None, raw=False, leg_query=None, **kwargs):
"""Get the balance for this account, ignoring all child accounts
Args:
as_of (Date): Only include transactions on or before this date
raw (bool): If true the returned balance should not have its sign
adjusted for display purposes.
leg_query (models.Q): Django Q-expression, will be used to filter the transaction legs.
allows for more complex filtering than that provided by **kwargs.
kwargs (dict): Will be used to filter the transaction legs
Returns:
Balance
"""
legs = self.legs
if as_of:
legs = legs.filter(transaction__date__lte=as_of)
if leg_query or kwargs:
leg_query = leg_query or models.Q()
legs = legs.filter(leg_query, **kwargs)
return legs.sum_to_balance() * (1 if raw else self.sign) + self._zero_balance()
def _zero_balance(self):
"""Get a balance for this account with all currencies set to zero"""
return Balance([Money("0", currency) for currency in self.currencies])
@db_transaction.atomic()
def transfer_to(self, to_account, amount, **transaction_kwargs):
"""Create a transaction which credits self and debits `to_account`.
See https://en.wikipedia.org/wiki/Double-entry_bookkeeping.
This is a shortcut utility method which simplifies the process of
transferring where `self` is Cr and `to_account` is Dr.
For example:
* Transferring income -> income will result in the former increasing and the latter decreasing
* Transferring income -> asset (i.e. bank) will result in the balance of both increasing
* Transferring asset -> asset will result in the former decreasing and the latter increasing
.. note::
LHS RHS
``{asset | expense} <-> {income | liability | equity}``
Transfers LHS (A) -> RHS (B) will decrease A and increase B
Transfers LHS (A) -> LHS (B) will decrease A and increase B
Transfers RHS (A) -> LHS (B) will increase A and increase B
Transfers RHS (A) -> RHS (B) will increase A and decrease B
Args:
to_account (Account): The destination account.
amount (Money): The amount to be transferred.
transaction_kwargs: Passed through to transaction creation. Useful for setting the
transaction `description` field.
"""
if not isinstance(amount, Money):
raise TypeError("amount must be of type Money")
transaction = Transaction.objects.create(**transaction_kwargs)
Leg.objects.create(transaction=transaction, account=self, amount=+amount)
Leg.objects.create(transaction=transaction, account=to_account, amount=-amount)
return transaction
@deprecated(
"accounting_transfer_to() has been renamed to transfer_to(). Update your "
"code to call transfer_to() directly. This will become an error in Hordak 3."
)
def accounting_transfer_to(self, *args, **kwargs):
return self.transfer_to(*args, **kwargs)
class TransactionManager(models.Manager):
def get_by_natural_key(self, uuid):
return self.get(uuid=uuid)
class Transaction(models.Model):
"""Represents a transaction
A transaction is a movement of funds between two accounts. Each transaction
will have two or more legs, each leg specifies an account and an amount.
.. note:
When working with Hordak Transaction objects you will typically need to do so
within a database transaction. This is because the database has integrity checks in
place to ensure the validity of the transaction (i.e. money in = money out).
See Also:
:meth:`Account.transfer_to()` is a useful shortcut to avoid having to create transactions manually.
Examples:
You can manually create a transaction as follows::
from django.db import transaction as db_transaction
from hordak.models import Transaction, Leg
with db_transaction.atomic():
transaction = Transaction.objects.create()
Leg.objects.create(transaction=transaction, account=my_account1, amount=Money(100, 'EUR'))
Leg.objects.create(transaction=transaction, account=my_account2, amount=Money(-100, 'EUR'))
Attributes:
uuid (models.UUIDField): UUID for transaction. Use to prevent leaking of IDs (if desired).
timestamp (datetime): The datetime when the object was created.
date (date): The date when the transaction actually occurred, as this may be different to
:attr:`timestamp`.
description (str): Optional user-provided description
"""
uuid = models.UUIDField(
default=UUID_DEFAULT, editable=False, verbose_name=_("uuid")
)
timestamp = models.DateTimeField(
default=timezone.now,
help_text="The creation date of this transaction object",
verbose_name=_("timestamp"),
)
date = models.DateField(
default=timezone.now,
help_text="The date on which this transaction occurred",
verbose_name=_("date"),
)
description = models.TextField(
default="", blank=True, verbose_name=_("description")
)
objects = TransactionManager()
class Meta:
get_latest_by = "date"
verbose_name = _("transaction")
def balance(self):
return self.legs.sum_to_balance()
def natural_key(self):
return (self.uuid,)
class LegQuerySet(models.QuerySet):
def sum_to_balance(self):
"""Sum the Legs of the QuerySet to get a `Balance`_ object"""
result = self.values("amount_currency").annotate(total=models.Sum("amount"))
return Balance([Money(r["total"], r["amount_currency"]) for r in result])
class LegManager(models.Manager):
def get_by_natural_key(self, uuid):
return self.get(uuid=uuid)
def debits(self):
"""Filter for legs that were debits"""
return self.filter(amount__gt=0)
def credits(self):
"""Filter for legs that were credits"""
return self.filter(amount__lt=0)
CustomLegManager = LegManager.from_queryset(LegQuerySet)
class Leg(models.Model):
"""The leg of a transaction
Represents a single amount either into or out of a transaction. All legs for a transaction
must sum to zero, all legs must be of the same currency.
Attributes:
uuid (UUID): UUID for transaction leg. Use to prevent leaking of IDs (if desired).
transaction (Transaction): Transaction to which the Leg belongs.
account (Account): Account the leg is transferring to/from.
amount (Money): The amount being transferred
description (str): Optional user-provided description
type (str): :attr:`hordak.models.DEBIT` or :attr:`hordak.models.CREDIT`.
"""
uuid = models.UUIDField(
default=UUID_DEFAULT, editable=False, verbose_name=_("uuid")
)
transaction = models.ForeignKey(
Transaction,
related_name="legs",
on_delete=models.CASCADE,
verbose_name=_("transaction"),
)
account = models.ForeignKey(
Account,
related_name="legs",
on_delete=models.CASCADE,
verbose_name=_("account"),
)
amount = MoneyField(
max_digits=MAX_DIGITS,
decimal_places=DECIMAL_PLACES,
help_text="Record debits as positive, credits as negative",
default_currency=get_internal_currency,
verbose_name=_("amount"),
)
description = models.TextField(
default="", blank=True, verbose_name=_("description")
)
objects = CustomLegManager()
def __str__(self):
return f"{self.type.title()} {self.account.name} ({self.account.full_code}) {self.amount}"
def save(self, *args, **kwargs):
if self.amount.amount == 0:
raise exceptions.ZeroAmountError()
leg = super(Leg, self).save(*args, **kwargs)
mysql_simulate_trigger("check_leg", self.transaction_id)
return leg
def natural_key(self):
return (self.uuid,)
@property
def type(self):
if self.amount.amount < 0:
return DEBIT
elif self.amount.amount > 0:
return CREDIT
else:
# This should have been caught earlier by the database integrity check.
# If you are seeing this then something is wrong with your DB checks.
raise exceptions.ZeroAmountError()
def is_debit(self):
return self.type == DEBIT
def is_credit(self):
return self.type == CREDIT
def account_balance_after(self):
"""Get the balance of the account associated with this leg following the transaction"""
# TODO: Consider moving to annotation,
# particularly once we can count on Django 1.11's subquery support
# Or use the new LegView.
transaction_date = self.transaction.date
return self.account.balance(
leg_query=(
models.Q(transaction__date__lt=transaction_date)
| (
models.Q(transaction__date=transaction_date)
& models.Q(transaction_id__lte=self.transaction_id)
)
)
)
def account_balance_before(self):
"""Get the balance of the account associated with this leg before the transaction"""
# TODO: Consider moving to annotation,
# particularly once we can count on Django 1.11's subquery support.
# Or use the new LegView.
transaction_date = self.transaction.date
return self.account.balance(
leg_query=(
models.Q(transaction__date__lt=transaction_date)
| (
models.Q(transaction__date=transaction_date)
& models.Q(transaction_id__lt=self.transaction_id)
)
)
)
class Meta:
verbose_name = _("Leg")
class StatementImportManager(models.Manager):
def get_by_natural_key(self, uuid):
return self.get(uuid=uuid)
class StatementImport(models.Model):
"""Records an import of a bank statement
Attributes:
uuid (UUID): UUID for statement import. Use to prevent leaking of IDs (if desired).
timestamp (datetime): The datetime when the object was created.
bank_account (Account): The account the import is for (should normally point to an asset
account which represents your bank account)
"""
uuid = models.UUIDField(
default=UUID_DEFAULT, editable=False, verbose_name=_("uuid")
)
timestamp = models.DateTimeField(default=timezone.now, verbose_name=_("timestamp"))
# TODO: Add constraint to ensure destination account expects statements (copy 0007)
bank_account = models.ForeignKey(
Account,
related_name="imports",
on_delete=models.CASCADE,
verbose_name=_("bank account"),
)
source = models.CharField(
max_length=20,
help_text="A value uniquely identifying where this data came from. "
'Examples: "csv", "teller.io".',
verbose_name=_("source"),
)
extra = JSONField(
default=json_default,
help_text="Any extra data relating to the import, probably specific "
"to the data source.",
verbose_name=_("extra"),
)
objects = StatementImportManager()
def natural_key(self):
return (self.uuid,)
class Meta:
verbose_name = _("statementImport")
class StatementLineManager(models.Manager):
def get_by_natural_key(self, uuid):
return self.get(uuid=uuid)
class StatementLine(models.Model):
"""Records an single imported bank statement line
A StatementLine is purely a utility to aid in the creation of transactions
(in the process known as reconciliation). StatementLines have no impact on
account balances.
However, the :meth:`StatementLine.create_transaction()` method can be used to create
a transaction based on the information in the StatementLine.
Attributes:
uuid (UUID): UUID for statement line. Use to prevent leaking of IDs (if desired).
timestamp (datetime): The datetime when the object was created.
date (date): The date given by the statement line
statement_import (StatementImport): The import to which the line belongs
amount (Decimal): The amount for the statement line, positive or nagative.
description (str): Any description/memo information provided
transaction (Transaction): Optionally, the transaction created for this statement line. This normally
occurs during reconciliation. See also :meth:`StatementLine.create_transaction()`.
"""
uuid = models.UUIDField(
default=UUID_DEFAULT, editable=False, verbose_name=_("uuid")
)
timestamp = models.DateTimeField(default=timezone.now, verbose_name=_("timestamp"))
date = models.DateField(verbose_name=_("date"))
statement_import = models.ForeignKey(
StatementImport,
related_name="lines",
on_delete=models.CASCADE,
verbose_name=_("statement import"),
)
amount = models.DecimalField(
max_digits=MAX_DIGITS, decimal_places=DECIMAL_PLACES, verbose_name=_("amount")
)
description = models.TextField(
default="", blank=True, verbose_name=_("description")
)
type = models.CharField(max_length=50, default="", verbose_name=_("type"))
# TODO: Add constraint to ensure transaction amount = statement line amount
# TODO: Add constraint to ensure one statement line per transaction
transaction = models.ForeignKey(
Transaction,
default=None,
blank=True,
null=True,
help_text="Reconcile this statement line to this transaction",
on_delete=models.SET_NULL,
verbose_name=_("transaction"),
)
source_data = JSONField(
default=json_default,
help_text="Original data received from the data source.",
verbose_name=_("source data"),
)
objects = StatementLineManager()
def natural_key(self):
return (self.uuid,)
@property
def is_reconciled(self):
"""Has this statement line been reconciled?
Determined as ``True`` if :attr:`transaction` has been set.
Returns:
bool: ``True`` if reconciled, ``False`` if not.
"""
return bool(self.transaction)
@db_transaction.atomic()
def create_transaction(self, to_account):
"""Create a transaction for this statement amount and account, into to_account
This will also set this StatementLine's ``transaction`` attribute to the newly
created transaction.
Args:
to_account (Account): The account the transaction is into / out of.
Returns:
Transaction: The newly created (and committed) transaction.
"""
from_account = self.statement_import.bank_account
transaction = Transaction.objects.create()
Leg.objects.create(
transaction=transaction, account=from_account, amount=+(self.amount * -1)
)
Leg.objects.create(
transaction=transaction, account=to_account, amount=-(self.amount * -1)
)
transaction.date = self.date
transaction.save()
self.transaction = transaction
self.save()
return transaction
class Meta:
verbose_name = _("statementLine")
def mysql_simulate_trigger(proc_name, *args):
# MySQL/MariaDB does not support deferred constraint triggers (unlike postgres),
# and also does not support triggers updating the table they are triggered from.
# So this function allows us to trigger manual function calls on transaction finish.
# Enforcing this at the application level is not idea. If this is important to you
# then use postgres.
# (https://stackoverflow.com/a/15300941/1908381)
def _mysql_call_proc():
with connection.cursor() as curs:
curs.callproc(proc_name, args)
if connection.vendor == "mysql":
with connection.cursor():
transaction.on_commit(_mysql_call_proc)