Implement editing general journal transactions
This commit is contained in:
parent
a474fc89d7
commit
371e7f39c4
@ -28,7 +28,7 @@ class GeneralJournalTransaction(Base, Transaction):
|
||||
dt = Column(DateTime)
|
||||
description = Column(String)
|
||||
|
||||
postings = relationship('GeneralJournalPosting', back_populates='transaction', cascade='all, delete')
|
||||
postings = relationship('GeneralJournalPosting', back_populates='transaction', cascade='all, delete-orphan')
|
||||
|
||||
class GeneralJournalPosting(Base, Posting):
|
||||
__tablename__ = 'general_journal_postings'
|
||||
|
@ -59,6 +59,38 @@ def general_journal_new():
|
||||
|
||||
return redirect('/general-journal')
|
||||
|
||||
@app.route('/general-journal/edit', methods=['GET', 'POST'])
|
||||
def general_journal_edit():
|
||||
transaction = db_session.get(GeneralJournalTransaction, request.args['id'])
|
||||
if not transaction:
|
||||
abort(404)
|
||||
|
||||
if request.method == 'GET':
|
||||
return render_template('general_journal/general_journal_edit.html', transaction=transaction)
|
||||
|
||||
# Edit transaction
|
||||
transaction.dt = datetime.strptime(request.form['dt'], '%Y-%m-%d')
|
||||
transaction.description = request.form['description']
|
||||
transaction.postings = []
|
||||
|
||||
for account, sign, amount_str in zip(request.form.getlist('account'), request.form.getlist('sign'), request.form.getlist('amount')):
|
||||
amount = Amount.parse(amount_str)
|
||||
if sign == 'cr':
|
||||
amount = -amount
|
||||
|
||||
posting = GeneralJournalPosting(
|
||||
account=account,
|
||||
quantity=amount.quantity,
|
||||
commodity=amount.commodity
|
||||
)
|
||||
transaction.postings.append(posting)
|
||||
|
||||
transaction.assert_valid()
|
||||
|
||||
db_session.commit()
|
||||
|
||||
return redirect('/general-journal')
|
||||
|
||||
@app.route('/balance-assertions')
|
||||
def balance_assertions():
|
||||
assertions = BalanceAssertion.query.all()
|
||||
|
@ -81,7 +81,12 @@ class Amount:
|
||||
return '{1:,.{dps}f} {0}'.format(self.commodity, self.quantity / (10**AMOUNT_DPS), dps=AMOUNT_DPS)
|
||||
|
||||
def quantity_string(self):
|
||||
if self.commodity == '$':
|
||||
return '{:.{dps}f}'.format(self.quantity / (10**AMOUNT_DPS), dps=AMOUNT_DPS)
|
||||
elif len(self.commodity) == 1:
|
||||
return '{0}{1:.{dps}f}'.format(self.commodity, self.quantity / (10**AMOUNT_DPS), dps=AMOUNT_DPS)
|
||||
else:
|
||||
return '{1:.{dps}f} {0}'.format(self.commodity, self.quantity / (10**AMOUNT_DPS), dps=AMOUNT_DPS)
|
||||
|
||||
def as_cost(self):
|
||||
"""Convert commodity to reporting currency in cost basis"""
|
||||
|
@ -38,7 +38,7 @@
|
||||
{% for transaction in transactions %}
|
||||
<tr>
|
||||
<td>{{ transaction.dt.strftime('%Y-%m-%d') }}</td>
|
||||
<td colspan="2">{{ transaction.description }}</td>
|
||||
<td colspan="2">{{ transaction.description }} <a href="/general-journal/edit?id={{ transaction.id }}"><i class="bi bi-pencil text-muted"></i></a></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
|
@ -33,11 +33,46 @@
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><input type="date" name="dt" class="form-control"></td>
|
||||
<td colspan="2"><input type="text" name="description" value="" class="form-control"></td>
|
||||
<td><input type="date" name="dt" class="form-control" value="{{ transaction.dt.strftime('%Y-%m-%d') if transaction else '' }}"></td>
|
||||
<td colspan="2"><input type="text" name="description" value="{{ transaction.description if transaction else '' }}" class="form-control"></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
{% if transaction %}
|
||||
{% for posting in transaction.postings %}
|
||||
<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"{% if posting.quantity >= 0 %} selected{% endif %}>Dr</option>
|
||||
<option value="cr"{% if posting.quantity < 0 %} selected{% endif %}>Cr</option>
|
||||
</select>
|
||||
<input type="text" name="account" class="form-control" value="{{ posting.account }}">
|
||||
<a class="btn btn-outline-primary" href="#" onclick="addPosting(this);return false;"><i class="bi bi-plus-circle-fill"></i></a>
|
||||
</div>
|
||||
</td>
|
||||
{% if posting.quantity >= 0 %}
|
||||
<td class="amount-dr has-amount">
|
||||
<div class="input-group">
|
||||
<div class="input-group-text">$</div>
|
||||
<input type="text" name="amount" value="{{ posting.amount().quantity_string() }}" class="form-control">
|
||||
</div>
|
||||
</td>
|
||||
<td class="amount-cr"></td>
|
||||
{% else %}
|
||||
<td class="amount-cr"></td>
|
||||
<td class="amount-cr has-amount">
|
||||
<div class="input-group">
|
||||
<div class="input-group-text">$</div>
|
||||
<input type="text" name="amount" value="{{ (posting.amount()|abs).quantity_string() }}" class="form-control">
|
||||
</div>
|
||||
</td>
|
||||
{% endif %}
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<tr>
|
||||
<td></td>
|
||||
<td></td>
|
||||
@ -56,6 +91,7 @@
|
||||
<div class="input-group-text">$</div>
|
||||
<input type="text" name="amount" value="" class="form-control">
|
||||
</div>
|
||||
</td>
|
||||
<td class="amount-cr"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
@ -79,6 +115,7 @@
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user