[Gnucash-changes] Do not try to compute cap gains if cap gains not possible.

Linas Vepstas linas at cvs.gnucash.org
Sat May 29 15:09:14 EDT 2004


Log Message:
-----------
Do not try to compute cap gains if cap gains not possible.
Gains aren't possible if the lot commodity is the same as
the transaction currency.   I think that this should fix
http://bugzilla.gnome.org/show_bug.cgi?id=139677
Untested, let me know.

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

Revision Data
-------------
Index: Scrub3.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/Scrub3.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -Lsrc/engine/Scrub3.c -Lsrc/engine/Scrub3.c -u -r1.9 -r1.10
--- src/engine/Scrub3.c
+++ src/engine/Scrub3.c
@@ -19,10 +19,10 @@
  * Boston, MA  02111-1307,  USA       gnu at gnu.org                   *
 \********************************************************************/
 
-/*  @file Scrub3.c
+/** @file Scrub3.c
  *  @breif Constrain Cap Gains to Track Sources of Gains
  *  @author Created by Linas Vepstas Sept 2003
- *  @author Copyright (c) 2003 Linas Vepstas <linas at linas.org>
+ *  @author Copyright (c) 2003,2004 Linas Vepstas <linas at linas.org>
  *
  * Provides a set of functions and utilities for checking and
  * repairing ('scrubbing clean') the usage of Cap Gains
@@ -53,6 +53,36 @@
 static short module = MOD_LOT;
 
 /* ================================================================= */
+/** Cap gains are possible only if the lot commodity is not the same
+ * as the transaction currency.  We assume here that all splits in
+ * the lot share the same transaction currency, and so we look at
+ * the first split, and see what it's currency is.
+ * This routine returns TRUE if cap gains are possible.
+ */
+
+static inline gboolean
+gains_possible (GNCLot *lot)
+{
+  SplitList *node;
+  Account *acc;
+  Split *split;
+  gboolean comeq;
+
+  acc = gnc_lot_get_account (lot);
+
+  node = gnc_lot_get_split_list (lot);
+  if (!node) return FALSE;
+  split = node->data;
+
+  comeq = gnc_commodity_equiv (acc->commodity, split->parent->common_currency);
+  return (FALSE == comeq);
+}
+
+/* ================================================================= */
+/* XXX What happens if, as a result of scrubbing, the lot is empty?
+ * I don't think this is handled properly.  I think that what will
+ * happen is we'll end up with an empty, closed lot ... ?
+ */
 
 gboolean
 xaccScrubLot (GNCLot *lot)
@@ -105,9 +135,18 @@
     splits_deleted = xaccScrubMergeLotSubSplits (lot);
   }
 
-  /* Now re-compute cap gains, and then double-check that. */
-  xaccLotComputeCapGains (lot, NULL);
-  xaccLotScrubDoubleBalance (lot);
+  /* Now re-compute cap gains, and then double-check that. 
+   * But we only compute cap-gains if gains are possible;
+   * that is if the lot commodity is not the same as the 
+   * currency. That is, one can't possibly have gains 
+   * selling dollars for dollars.  The business modules
+   * use lots with lot commodity == lot currency.
+   */
+  if (gains_possible (lot))
+  {
+    xaccLotComputeCapGains (lot, NULL);
+    xaccLotScrubDoubleBalance (lot);
+  }
   xaccAccountCommitEdit(acc);
 
   LEAVE (" deleted=%d", splits_deleted);


More information about the gnucash-changes mailing list