diff --git a/drcr/general_journal/models.py b/drcr/general_journal/models.py index c9c7be5..359216f 100644 --- a/drcr/general_journal/models.py +++ b/drcr/general_journal/models.py @@ -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' diff --git a/drcr/general_journal/views.py b/drcr/general_journal/views.py index d5fbb9b..84eead1 100644 --- a/drcr/general_journal/views.py +++ b/drcr/general_journal/views.py @@ -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() diff --git a/drcr/models.py b/drcr/models.py index 57ccf2e..2494387 100644 --- a/drcr/models.py +++ b/drcr/models.py @@ -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): - return '{:.{dps}f}'.format(self.quantity / (10**AMOUNT_DPS), dps=AMOUNT_DPS) + 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""" diff --git a/drcr/templates/general_journal/general_journal.html b/drcr/templates/general_journal/general_journal.html index 8d9d8c6..463b76c 100644 --- a/drcr/templates/general_journal/general_journal.html +++ b/drcr/templates/general_journal/general_journal.html @@ -38,7 +38,7 @@ {% for transaction in transactions %} {{ transaction.dt.strftime('%Y-%m-%d') }} - {{ transaction.description }} + {{ transaction.description }} diff --git a/drcr/templates/general_journal/general_journal_edit.html b/drcr/templates/general_journal/general_journal_edit.html index e0ea354..0530ec7 100644 --- a/drcr/templates/general_journal/general_journal_edit.html +++ b/drcr/templates/general_journal/general_journal_edit.html @@ -33,52 +33,89 @@ - - + + - - - - -
- - - -
- - -
-
$
- -
- - - - - - -
- - - -
- - - -
-
$
- -
- - + {% if transaction %} + {% for posting in transaction.postings %} + + + + +
+ + + +
+ + {% if posting.quantity >= 0 %} + +
+
$
+ +
+ + + {% else %} + + +
+
$
+ +
+ + {% endif %} + + {% endfor %} + {% else %} + + + + +
+ + + +
+ + +
+
$
+ +
+ + + + + + + +
+ + + +
+ + + +
+
$
+ +
+ + + {% endif %}