r17392 - gnucash/branches/2.2/src/report/standard-reports - [r17342] Trial balance report: Skip unrealized gain calculation when price source is "average cost".

Andreas Köhler andi5 at cvs.gnucash.org
Tue Jul 22 18:34:57 EDT 2008


Author: andi5
Date: 2008-07-22 18:34:57 -0400 (Tue, 22 Jul 2008)
New Revision: 17392
Trac: http://svn.gnucash.org/trac/changeset/17392

Modified:
   gnucash/branches/2.2/src/report/standard-reports/trial-balance.scm
Log:
[r17342] Trial balance report: Skip unrealized gain calculation when price source is "average cost".

This significantly improves performance with the default report
options. Centralize and simplify unrealized gain code.

Committed by cedayiv.


Modified: gnucash/branches/2.2/src/report/standard-reports/trial-balance.scm
===================================================================
--- gnucash/branches/2.2/src/report/standard-reports/trial-balance.scm	2008-07-22 22:34:47 UTC (rev 17391)
+++ gnucash/branches/2.2/src/report/standard-reports/trial-balance.scm	2008-07-22 22:34:57 UTC (rev 17392)
@@ -421,9 +421,7 @@
 	       (acct-table #f)
 	       (debit-tot (gnc:make-commodity-collector))
 	       (credit-tot (gnc:make-commodity-collector))
-               (unrealized-gain-collector #f)
-               (neg-unrealized-gain-collector #f)
-	       (book-balance #f) ;; assets - liabilities - equity, norm 0
+               (neg-unrealized-gain-collector (gnc:make-commodity-collector))
 	       (table-env #f)    ;; parameters for :make-
 	       (account-cols #f)
 	       (indented-depth #f)
@@ -502,15 +500,7 @@
 	       )
 	      cell)
 	    )
-	  
-	  ;; set default cell alignment
-	  (gnc:html-table-set-style!
-	   build-table "td"
-	   'attribute '("align" "right")
-	   'attribute '("valign" "top")
-	   )
-	  
-	  (gnc:report-percent-done 4)
+
 	  ;; sum any unrealized gains
 	  ;; 
 	  ;; Hm... unrealized gains....  This is when you purchase
@@ -521,40 +511,63 @@
 	  ;; I *think* a decrease in the value of a liability or
 	  ;; equity constitutes an unrealized loss.  I'm unsure about
 	  ;; that though....
-	  ;; 
-	  (set! book-balance (gnc:make-commodity-collector))
-	  (map (lambda (acct)
-		 (book-balance
-		  'merge
-		  (gnc:account-get-comm-balance-at-date acct end-date-tp #f)
-		  #f)
-		 )
-	       all-accounts)
-          (set! unrealized-gain-collector (gnc:make-commodity-collector))
-          (let* ((cost-fn
-                  (gnc:case-exchange-fn 'average-cost
-                                        report-commodity end-date-tp))
-		 
-                 (value
-                  (gnc:gnc-monetary-amount
-                   (gnc:sum-collector-commodity book-balance
-                                                report-commodity
-                                                exchange-fn)))
-		 
-                 (cost
-                  (gnc:gnc-monetary-amount
-                   (gnc:sum-collector-commodity book-balance
-                                                report-commodity
-                                                cost-fn)))
-		 
-                 (unrealized-gain (gnc-numeric-sub-fixed value cost)))
-	    
-            (unrealized-gain-collector 'add report-commodity unrealized-gain)
-	    )
-	  (set! neg-unrealized-gain-collector (gnc:make-commodity-collector))
-	  (neg-unrealized-gain-collector 'minusmerge
-					 unrealized-gain-collector
-					 #f)
+          ;;
+          ;; This procedure returns a commodity collector.
+          (define (collect-unrealized-gains)
+            (if (equal? price-source 'average-cost)
+                ;; No need to calculate if doing valuation at cost.
+                (gnc:make-commodity-collector)
+                (let ((book-balance (gnc:make-commodity-collector))
+                      (unrealized-gain-collector (gnc:make-commodity-collector))
+                      (cost-fn (gnc:case-exchange-fn 'average-cost
+                                                     report-commodity
+                                                     end-date-tp))
+                      (value #f)
+                      (cost #f))
+
+                  ;; Calculate book balance.
+                  ;; assets - liabilities - equity; normally 0
+                  (map
+                   (lambda (acct)
+                     (book-balance 'merge
+                                   (gnc:account-get-comm-balance-at-date
+                                    acct end-date-tp #f)
+                     #f))
+                   all-accounts)
+
+                 ;; Get the value of all holdings.
+                 (set! value (gnc:gnc-monetary-amount
+                              (gnc:sum-collector-commodity book-balance
+                                                           report-commodity
+                                                           exchange-fn)))
+
+                 ;; Get the cost of all holdings.
+                 (set! cost (gnc:gnc-monetary-amount
+                             (gnc:sum-collector-commodity book-balance
+                                                          report-commodity
+                                                          cost-fn)))
+
+                 ;; Get the unrealized gain or loss (value minus cost).
+                 (unrealized-gain-collector 'add
+                                            report-commodity
+                                            (gnc-numeric-sub-fixed value cost))
+                 unrealized-gain-collector)))
+
+
+	  ;; set default cell alignment
+	  (gnc:html-table-set-style!
+	   build-table "td"
+	   'attribute '("align" "right")
+	   'attribute '("valign" "top")
+	   )
+	  
+	  (gnc:report-percent-done 4)
+
+          ;; Get any unrealized gains/losses.
+          (neg-unrealized-gain-collector 'minusmerge
+                                         (collect-unrealized-gains)
+                                         #f)
+
 	  (set! table-env
 		(list
 		 (list 'start-date #f)



More information about the gnucash-changes mailing list