Allow entry of general journal transaction with multiple postings

This commit is contained in:
RunasSudo 2022-12-24 20:13:11 +11:00
parent 5d13e5cbef
commit a474fc89d7
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 58 additions and 14 deletions

View File

@ -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('}')])

View File

@ -43,35 +43,36 @@
<td></td>
<td>
<div class="input-group">
<select class="form-select" name="sign" style="flex-grow:0;min-width:5em">
<select class="form-select" name="sign" style="flex-grow:0;min-width:5em" onchange="changeDrCr(this)">
<option value="dr" selected>Dr</option>
{#<option value="cr">Cr</option>#}
{# FIXME: Multiple postings, etc. #}
<option value="cr">Cr</option>
</select>
<input type="text" name="account" class="form-control">
<a class="btn btn-outline-primary" href="#" onclick="addPosting(this);return false;"><i class="bi bi-plus-circle-fill"></i></a>
</div>
</td>
<td>
<td class="amount-dr has-amount">
<div class="input-group">
<div class="input-group-text">$</div>
<input type="text" name="amount" value="" class="form-control">
</div>
<td></td>
<td class="amount-cr"></td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<div class="input-group">
<select class="form-select" name="sign" style="flex-grow:0;min-width:5em">
{#<option value="dr">Dr</option>#}
<select class="form-select" name="sign" style="flex-grow:0;min-width:5em" onchange="changeDrCr(this)">
<option value="dr">Dr</option>
<option value="cr" selected>Cr</option>
</select>
<input type="text" name="account" class="form-control">
<a class="btn btn-outline-primary" href="#" onclick="addPosting(this);return false;"><i class="bi bi-plus-circle-fill"></i></a>
</div>
</td>
<td></td>
<td>
<td class="amount-dr"></td>
<td class="amount-cr has-amount">
<div class="input-group">
<div class="input-group-text">$</div>
<input type="text" name="amount" value="" class="form-control">
@ -86,3 +87,37 @@
</div>
</form>
{% endblock %}
{% block scripts %}
<script>
function changeDrCr(el) {
let trPosting = el.parentNode.parentNode.parentNode;
let amountContent = trPosting.querySelector('.has-amount').innerHTML;
let amountValue = trPosting.querySelector('.has-amount input').value;
// Remove input boxes
for (let td of trPosting.querySelectorAll('.amount-dr, .amount-cr')) {
td.innerHTML = '';
td.classList.remove('has-amount');
}
// Add correct input box
let td = trPosting.querySelector(el.value === 'dr' ? '.amount-dr' : '.amount-cr');
td.innerHTML = amountContent;
td.classList.add('has-amount');
td.querySelector('input').value = amountValue;
}
function addPosting(el) {
let trPosting = el.parentNode.parentNode.parentNode;
let sign = trPosting.querySelector('select').value; // Use same sign as row clicked
let inputAmount = '<div class="input-group"><div class="input-group-text">$</div><input type="text" name="amount" class="form-control"></div>';
// Add new posting row
let trNew = document.createElement('tr');
trNew.innerHTML = '<tr><td></td><td></td><td><div class="input-group"><select class="form-select" name="sign" style="flex-grow:0;min-width:5em" onchange="changeDrCr(this)"><option value="dr"' + (sign === 'dr' ? ' selected' : '') + '>Dr</option><option value="cr"' + (sign === 'cr' ? ' selected' : '') + '>Cr</option></select><input type="text" name="account" class="form-control"><a class="btn btn-outline-primary" href="#" onclick="addPosting(this);return false;"><i class="bi bi-plus-circle-fill"></i></a></div></td>' + (sign === 'dr' ? ('<td class="amount-dr has-amount">' + inputAmount + '</td>') : '<td class="amount-dr"></td>') + (sign === 'cr' ? ('<td class="amount-cr has-amount">' + inputAmount + '</td>') : '<td class="amount-cr"></td>') + '</tr>';
trPosting.after(trNew);
}
</script>
{% endblock %}