From a474fc89d7d1183cc66eb24048246f011e2d9654 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sat, 24 Dec 2022 20:13:11 +1100 Subject: [PATCH] Allow entry of general journal transaction with multiple postings --- drcr/models.py | 19 +++++-- .../general_journal/general_journal_edit.html | 53 +++++++++++++++---- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/drcr/models.py b/drcr/models.py index 0afcda8..57ccf2e 100644 --- a/drcr/models.py +++ b/drcr/models.py @@ -23,13 +23,20 @@ class Transaction: self.postings = postings or [] def assert_valid(self): - """Assert that debits equal credits, and commodities are compatible""" + """Assert that debits equal credits""" - if any(p.commodity != self.postings[0].commodity for p in self.postings[1:]): - raise AssertionError('Transaction contains multiple commodities') + total_dr = 0 + total_cr = 0 - if sum(p.quantity for p in self.postings) != 0: - raise AssertionError('Transaction debits and credits do not balance') + for posting in self.postings: + amount_cost = posting.amount().as_cost().quantity + if amount_cost > 0: + total_dr += amount_cost + elif amount_cost < 0: + total_cr -= amount_cost + + if total_dr != total_cr: + raise AssertionError('Transaction debits ({}) and credits ({}) do not balance'.format(total_dr, total_cr)) class Posting: def __init__(self, description=None, account=None, quantity=None, commodity=None): @@ -86,6 +93,8 @@ class Amount: if '{{' in self.commodity: cost = float(self.commodity[self.commodity.index('{{')+2:self.commodity.index('}}')]) + if self.quantity < 0: + cost = -cost return Amount(round(cost * (10**AMOUNT_DPS)), '$') elif '{' in self.commodity: cost = float(self.commodity[self.commodity.index('{')+1:self.commodity.index('}')]) diff --git a/drcr/templates/general_journal/general_journal_edit.html b/drcr/templates/general_journal/general_journal_edit.html index 8724316..e0ea354 100644 --- a/drcr/templates/general_journal/general_journal_edit.html +++ b/drcr/templates/general_journal/general_journal_edit.html @@ -43,35 +43,36 @@
- - {##} - {# FIXME: Multiple postings, etc. #} + +
- +
$
- +
- + +
- - + +
$
@@ -86,3 +87,37 @@
{% endblock %} + +{% block scripts %} + +{% endblock %}