[Gnucash-changes] partial fix for http://bugzilla.gnome.org/show_bug.cgi?id=139677

Linas Vepstas linas at cvs.gnucash.org
Sat May 29 14:04:41 EDT 2004


Log Message:
-----------
partial fix for http://bugzilla.gnome.org/show_bug.cgi?id=139677
requires that all splits in lot share a common transaction currency.

Modified Files:
--------------
    gnucash/src/engine:
        policy.c

Revision Data
-------------
Index: policy.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/policy.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -Lsrc/engine/policy.c -Lsrc/engine/policy.c -u -r1.3 -r1.4
--- src/engine/policy.c
+++ src/engine/policy.c
@@ -46,37 +46,74 @@
 
 /* static short module = MOD_LOT; */
 
-/* ============================================================== */
-
-static GNCLot * 
-FIFOPolicyGetLot (GNCPolicy *pcy, Split *split)
-{
-   return xaccAccountFindEarliestOpenLot (split->acc, split->amount);
-}
-
 static Split * 
-FIFOPolicyGetSplit (GNCPolicy *pcy, GNCLot *lot)
+DirectionPolicyGetSplit (GNCPolicy *pcy, GNCLot *lot, short reverse)
 {
+   Split *split;
    SplitList *node;
+   gnc_commodity *common_currency;
    gboolean want_positive;
+   gnc_numeric baln;
+
+	if (!pcy || !lot || !lot->account || !lot->splits) return NULL;
+
+   /* Recomputing the balance re-evaluates the lot closure */
+   baln = gnc_lot_get_balance (lot);
+	if (gnc_lot_is_closed(lot)) return NULL;
 
-   want_positive = gnc_numeric_negative_p (gnc_lot_get_balance (lot));
+   want_positive = gnc_numeric_negative_p (baln);
 
-   /* Make use of the fact that the splits in a lot are already
-    * in date order; so we don't have to search for the earliest. */
-   for (node = xaccAccountGetSplitList (lot->account); node; node=node->next)
+   /* All splits in lot must share a common transaction currency. */
+   split = lot->splits->data;
+   common_currency = split->parent->common_currency;
+
+   /* Walk over *all* splits in the account, till we find one that
+    * hasn't been assigned to a lot.  Return that split.
+    * Make use of the fact that the splits in an account are 
+    * already in date order; so we don't have to sort. */
+   node = xaccAccountGetSplitList (lot->account);
+   if (reverse)
+   {
+       node = g_list_last (node);
+   }
+   while (node)
    {
       gboolean is_positive;
-      Split *split = node->data;
-      if (split->lot) continue;
+      split = node->data;
+      if (split->lot) goto donext;
+
+		if (common_currency != split->parent->common_currency) goto donext;
 
       is_positive = gnc_numeric_positive_p (split->amount);
       if ((want_positive && is_positive) ||
           ((!want_positive) && (!is_positive))) return split;
+donext:
+      if (reverse)
+      {
+         node=node->prev;
+      }
+      else
+      {
+         node=node->next;
+      }
    }
    return NULL;
 }
 
+/* ============================================================== */
+
+static GNCLot * 
+FIFOPolicyGetLot (GNCPolicy *pcy, Split *split)
+{
+   return xaccAccountFindEarliestOpenLot (split->acc, split->amount);
+}
+
+static Split * 
+FIFOPolicyGetSplit (GNCPolicy *pcy, GNCLot *lot)
+{
+   return DirectionPolicyGetSplit (pcy, lot, 0);
+}
+
 static void
 FIFOPolicyGetLotOpening (GNCPolicy *pcy,
         GNCLot *lot,
@@ -134,27 +171,7 @@
 static Split * 
 LIFOPolicyGetSplit (GNCPolicy *pcy, GNCLot *lot)
 {
-   SplitList *node;
-   gboolean want_positive;
-
-   want_positive = gnc_numeric_negative_p (gnc_lot_get_balance (lot));
-
-   /* Make use of the fact that the splits in a lot are already
-    * in date order; so we don't have to search for the latest,
-    * we merely start at the end and go backwards. */
-   node = xaccAccountGetSplitList (lot->account);
-   node = g_list_last (node);
-   for (; node; node=node->prev)
-   {
-      gboolean is_positive;
-      Split *split = node->data;
-      if (split->lot) continue;
-
-      is_positive = gnc_numeric_positive_p (split->amount);
-      if ((want_positive && is_positive) ||
-          ((!want_positive) && (!is_positive))) return split;
-   }
-   return NULL;
+   return DirectionPolicyGetSplit (pcy, lot, 1);
 }
 
 /* This routine is actually identical to FIFO... */


More information about the gnucash-changes mailing list