[Gnucash-changes] partial fix for the failure of the test-lots test case: Change the FIFO

Linas Vepstas linas at cvs.gnucash.org
Sat May 29 16:06:04 EDT 2004


Log Message:
-----------
partial fix for the failure of the test-lots test case:
Change the FIFO policy to avoid mixed-currency lots.

Modified Files:
--------------
    gnucash/src/engine:
        Scrub2.c
        cap-gains.c
        cap-gains.h
        policy.c

Revision Data
-------------
Index: Scrub2.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/Scrub2.c,v
retrieving revision 1.32
retrieving revision 1.33
diff -Lsrc/engine/Scrub2.c -Lsrc/engine/Scrub2.c -u -r1.32 -r1.33
--- src/engine/Scrub2.c
+++ src/engine/Scrub2.c
@@ -55,6 +55,10 @@
 static short module = MOD_LOT;
 
 /* ============================================================== */
+/** Loop over all splits, and make sure that every split
+ * belongs to some lot.  If a split does not belong to 
+ * any lots, poke it into one.
+ */
 
 void
 xaccAccountAssignLots (Account *acc)
@@ -66,10 +70,6 @@
    ENTER ("acc=%s", acc->accountName);
    xaccAccountBeginEdit (acc);
 
-   /* Loop over all splits, and make sure that every split
-    * belongs to some lot.  If a split does not belong to 
-    * any lots, poke it into one.
-    */
 restart_loop:
    for (node=acc->splits; node; node=node->next)
    {
@@ -83,7 +83,6 @@
    LEAVE ("acc=%s", acc->accountName);
 }
 
-
 /* ============================================================== */
 
 /** The xaccLotFill() routine attempts to assign splits to the 
Index: policy.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/policy.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -Lsrc/engine/policy.c -Lsrc/engine/policy.c -u -r1.6 -r1.7
--- src/engine/policy.c
+++ src/engine/policy.c
@@ -109,7 +109,9 @@
 static GNCLot * 
 FIFOPolicyGetLot (GNCPolicy *pcy, Split *split)
 {
-   return xaccAccountFindEarliestOpenLot (split->acc, split->amount);
+   if (!split) return NULL;
+   return xaccAccountFindEarliestOpenLot (split->acc, split->amount,
+                                          split->parent->common_currency);
 }
 
 static Split * 
@@ -169,7 +171,9 @@
 static GNCLot * 
 LIFOPolicyGetLot (GNCPolicy *pcy, Split *split)
 {
-   return xaccAccountFindLatestOpenLot (split->acc, split->amount);
+   if (!split) return NULL;
+   return xaccAccountFindLatestOpenLot (split->acc, split->amount,
+                                        split->parent->common_currency);
 }
 
 static Split * 
Index: cap-gains.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/cap-gains.h,v
retrieving revision 1.8
retrieving revision 1.9
diff -Lsrc/engine/cap-gains.h -Lsrc/engine/cap-gains.h -u -r1.8 -r1.9
--- src/engine/cap-gains.h
+++ src/engine/cap-gains.h
@@ -40,7 +40,7 @@
 /** @file cap-gains.h
  *  @brief Utilities to Automatically Compute Capital Gains/Losses.
  *  @author Created by Linas Vepstas August 2003
- *  @author Copyright (c) 2003 Linas Vepstas <linas at linas.org>
+ *  @author Copyright (c) 2003,2004 Linas Vepstas <linas at linas.org>
  */
 
 #ifndef XACC_CAP_GAINS_H
@@ -81,9 +81,15 @@
  *   The sign comparison helps identify a lot that can be 
  *   added to: usually, one wants to add splits to a lot so
  *   that the balance only decreases.
+ *   If 'currency' is non-null, then this attempts to find
+ *   a lot whose opening transaction has the same currency.
  */
-GNCLot * xaccAccountFindEarliestOpenLot (Account *acc, gnc_numeric sign);
-GNCLot * xaccAccountFindLatestOpenLot (Account *acc, gnc_numeric sign);
+GNCLot * xaccAccountFindEarliestOpenLot (Account *acc, 
+                                         gnc_numeric sign,
+                                         gnc_commodity *currency);
+GNCLot * xaccAccountFindLatestOpenLot (Account *acc, 
+                                       gnc_numeric sign,
+                                       gnc_commodity *currency);
 
 /** The xaccAccountGetDefaultGainAccount() routine will return
  *   the account to which realized gains/losses may be posted.  
Index: cap-gains.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/cap-gains.c,v
retrieving revision 1.20
retrieving revision 1.21
diff -Lsrc/engine/cap-gains.c -Lsrc/engine/cap-gains.c -u -r1.20 -r1.21
--- src/engine/cap-gains.c
+++ src/engine/cap-gains.c
@@ -22,7 +22,7 @@
 /** @file cap-gains.c
  *  @breif Utilities to Automatically Compute Capital Gains/Losses.
  *  @author Created by Linas Vepstas August 2003
- *  @author Copyright (c) 2003 Linas Vepstas <linas at linas.org>
+ *  @author Copyright (c) 2003,2004 Linas Vepstas <linas at linas.org>
  *
  *  This file implements the various routines to automatically
  *  compute and handle Cap Gains/Losses resulting from trading 
@@ -110,6 +110,7 @@
 struct find_lot_s
 {
    GNCLot *lot;
+   gnc_commodity *currency;
    Timespec ts;
    int (*numeric_pred)(gnc_numeric);
    gboolean (*date_pred)(Timespec e, Timespec tr);
@@ -145,6 +146,13 @@
    
    s = gnc_lot_get_earliest_split (lot);
    trans = s->parent;
+   if (els->currency && 
+       (FALSE == gnc_commodity_equiv (els->currency,
+                                      trans->common_currency)))
+   {
+      return NULL;
+   }
+
    if (els->date_pred (els->ts, trans->date_posted))
    {
       els->ts = trans->date_posted;
@@ -156,12 +164,14 @@
 
 static inline GNCLot *
 xaccAccountFindOpenLot (Account *acc, gnc_numeric sign, 
+   gnc_commodity *currency,
    long long guess,
    gboolean (*date_pred)(Timespec, Timespec))
 {
    struct find_lot_s es;
 
    es.lot = NULL;
+   es.currency = currency;
    es.ts.tv_sec = guess;
    es.ts.tv_nsec = 0;
    es.date_pred = date_pred;
@@ -174,24 +184,26 @@
 }
 
 GNCLot *
-xaccAccountFindEarliestOpenLot (Account *acc, gnc_numeric sign)
+xaccAccountFindEarliestOpenLot (Account *acc, gnc_numeric sign, 
+                                gnc_commodity *currency)
 {
    GNCLot *lot;
    ENTER (" sign=%lld/%lld", sign.num, sign.denom);
       
-   lot = xaccAccountFindOpenLot (acc, sign, 
+   lot = xaccAccountFindOpenLot (acc, sign, currency,
                    10000000LL * ((long long) LONG_MAX), earliest_pred);
    LEAVE ("found lot=%p %s", lot, gnc_lot_get_title (lot));
    return lot;
 }
 
 GNCLot *
-xaccAccountFindLatestOpenLot (Account *acc, gnc_numeric sign)
+xaccAccountFindLatestOpenLot (Account *acc, gnc_numeric sign,
+                              gnc_commodity *currency)
 {
    GNCLot *lot;
    ENTER (" sign=%lld/%lld", sign.num, sign.denom);
       
-   lot = xaccAccountFindOpenLot (acc, sign, 
+   lot = xaccAccountFindOpenLot (acc, sign, currency,
                    -10000000LL * ((long long) LONG_MAX), latest_pred);
    LEAVE ("found lot=%p %s", lot, gnc_lot_get_title (lot));
    return lot;
@@ -539,8 +551,8 @@
     * block is written in the form of a while loop, since we
     * may have to bust a split across several lots.
     */
-  while (split)
-  {
+   while (split)
+   {
      PINFO ("have split amount=%s", gnc_numeric_to_string (split->amount));
      split->gains |= GAINS_STATUS_VDIRTY;
      lot = pcy->PolicyGetLot (pcy, split);


More information about the gnucash-changes mailing list