Commits

Robert Brewer committed 295b48e

Fulfillments now order by debit account as well as date.

Comments (0)

Files changed (4)

flowrate/__init__.py

 
         # Remove all existing obligations
         # (and their fulfillments) for this flow
+        # TODO: This isn't quite right; it only deletes existing fulfillments
+        # for this flow. It might want to delete all fulfillments which
+        # *might* apply to this flow, so it can "take over" other existing
+        # fulfillments. Tough nut.
         db.execute(
             "DELETE FROM fulfillments "
             "WHERE obligationid"

flowrate/flowrate.sql

     WITH o AS (
         SELECT o1.*,
         amount - (SELECT COALESCE(SUM(amount), 0) FROM fulfillments f
-         WHERE f.obligationID = o1.id) AS remaining
+         WHERE f.obligationid = o1.id) AS remaining
         FROM obligations o1
         WHERE o1.postdate >= (CURRENT_DATE - '1 month'::interval)
         )

flowrate/flows.html

     for (var i=0; i < rowset.childNodes.length; i++) {
         var existing = rowset.childNodes[i];
         if (existing.className == 'flowrow') {
-            if (existing.flow.debit_account <= flow.debit_account) {
+            if (existing.flow.debit <= flow.debit) {
                 rowset.insertBefore(r, existing);
                 break;
             }

flowrate/flows.py

     return False
 
 def fulfill(txrow):
-    """Use the given transaction to fulfill an obligation, if possible."""
+    """Use the given transaction to fulfill an obligation, if possible.
+
+    Any existing fulfillments for the given transaction will be deleted.
+    Then obligations which the transactions might fulfill are fulfilled
+    in order until the full amount of the transaction has been applied,
+    or no more matching obligations remain.
+
+    Obligations are ordered first by debit account, descending, so that
+    more specific expense obligations (like "Restaurants") get filled
+    first, but more generic obligations (like "Food") can still be
+    fulfilled. It's assumed that this feature is more useful for expenses
+    than for income. They are then ordered by date, ascending, within each
+    debit account.
+    """
     flowrate.db.execute(
         "DELETE FROM fulfillments WHERE transactionid = %s", (txrow.id,))
 
                 "AND isSubAccount(%s, o.debit_account) "
                 "AND (to_char(%s, o.dategroupformat) = "
                      "to_char(o.postdate, o.dategroupformat)) "
-                "ORDER BY o.postdate ASC",
+                "ORDER BY o.debit_account DESC, o.postdate ASC",
                 (txrow.credit_account, txrow.debit_account,
                  txrow.postdate)).fetchall()]