Source

flowrate / flowrate / __init__.py

Diff from to

flowrate/__init__.py

         # Accept the new transaction definition.
         vals = req.json["body"]
         credit, debit = popint(vals['credit']), popint(vals['debit'])
+        credit_type = db.execute(
+            "SELECT type FROM accounts WHERE id = %s", (credit,)).fetchone()
+        debit_type = db.execute(
+            "SELECT type FROM accounts WHERE id = %s", (debit,)).fetchone()
+        amount = vals['amount']
         txrow = db.execute(
             "UPDATE transactions SET amount = %s, "
             "credit_account = %s, debit_account = %s, postdate = %s, "
-            "description = %s WHERE id = %s RETURNING *;",
-            (vals['amount'], credit, debit,
-             vals['postdate'], vals['description'], req.txid)).fetchone()
+            "description = %s, credit_mult = %s, debit_mult = %s "
+            "WHERE id = %s RETURNING *;",
+            (amount, credit, debit,
+             vals['postdate'], vals['description'],
+             -1 if credit_type in ('asset', 'expense') else 1,
+             1 if debit_type in ('asset', 'expense') else -1,
+             req.txid)).fetchone()
 
         flows.unfulfill(txrow.id)
         flows.fulfill(txrow)
         # Accept the new transaction definition.
         vals = req.json["body"]
         credit, debit = popint(vals['credit']), popint(vals['debit'])
+        credit_type = db.execute(
+            "SELECT type FROM accounts WHERE id = %s", (credit,)).fetchone()
+        debit_type = db.execute(
+            "SELECT type FROM accounts WHERE id = %s", (debit,)).fetchone()
+        amount = vals['amount']
         newrow = db.execute(
             "INSERT INTO transactions"
-            " (amount, credit_account, debit_account, postdate, description) "
-            "VALUES (%s, %s, %s, %s, %s) RETURNING *",
-            (vals["amount"], credit, debit,
-             vals["postdate"], vals['description'])).fetchone()
+            " (amount, credit_account, debit_account, postdate, description, "
+            "credit_mult, debit_mult) "
+            "VALUES (%s, %s, %s, %s, %s, %s, %s) RETURNING *",
+            (amount, credit, debit,
+             vals["postdate"], vals['description'],
+             -1 if credit_type in ('asset', 'expense') else 1,
+             1 if debit_type in ('asset', 'expense') else -1,
+             )).fetchone()
 
         flows.fulfill(newrow)