*** advanced-portfolio.scm.orig	2006-04-08 18:20:03.000000000 +0200
--- advanced-portfolio.scm	2006-04-14 12:13:16.000000000 +0200
***************
*** 3,6 ****
--- 3,8 ----
  ;; by Martijn van Oosterhout (kleptog@svana.org) Feb 2002
  ;; modified for GnuCash 1.8 by Herbert Thoma (herbie@hthoma.de) Oct 2002
+ ;; modified by Fabrice Kurz (kurzf@voila.fr) April 2006
+ ;; to deal with external contrib to funds (extended notion of dividends)
  ;;
  ;; Heavily based on portfolio.scm
***************
*** 269,273 ****
                                  currency price-fn exchange-fn 
  				include-empty include-gains show-symbol show-listing show-shares show-price
!                                 basis-method prefer-pricelist total-basis total-value total-moneyin total-moneyout
                                  total-gain total-ugain)
  
--- 271,275 ----
                                  currency price-fn exchange-fn 
  				include-empty include-gains show-symbol show-listing show-shares show-price
!                                 basis-method prefer-pricelist total-basis total-value total-moneyin total-extmoneyin total-moneyout
                                  total-gain total-ugain)
  
***************
*** 295,299 ****
                   (unitscoll     (gnc:make-commodity-collector))
                   (brokeragecoll (gnc:make-commodity-collector))
!                  (dividendcoll  (gnc:make-commodity-collector))
                   (moneyincoll   (gnc:make-commodity-collector))
                   (moneyoutcoll  (gnc:make-commodity-collector))
--- 297,301 ----
                   (unitscoll     (gnc:make-commodity-collector))
                   (brokeragecoll (gnc:make-commodity-collector))
!                  (extmoneyincoll  (gnc:make-commodity-collector)) ;;extmoneyin is dividend and other income
                   (moneyincoll   (gnc:make-commodity-collector))
                   (moneyoutcoll  (gnc:make-commodity-collector))
***************
*** 328,337 ****
  		       (for-each
  			(lambda (s)
! 			  ;; If this is an asset type account for buy or sell, then grab a 
  			  ;; currency and a txn-value for later computation
  			  (cond
  			   ((and (not (same-account? current (gnc:split-get-account s))) 
! 				 (not (or(split-account-type? s 'expense)
! 					 (split-account-type? s 'income))))
  
  			    ;;only change the commod-currency if price failed
--- 330,338 ----
  		       (for-each
  			(lambda (s)
! 			  ;; If this is an asset or income  type account for buy or sell, then grab a 
  			  ;; currency and a txn-value for later computation
  			  (cond
  			   ((and (not (same-account? current (gnc:split-get-account s))) 
! 				 (not (split-account-type? s 'expense)))
  
  			    ;;only change the commod-currency if price failed
***************
*** 400,404 ****
  			   
  			   ((split-account-type? s 'income)
! 			     (dividendcoll 'add commod-currency (gnc:split-get-value s)))
  			   )
  			  )
--- 401,405 ----
  			   
  				 ((split-account-type? s 'income)
!  			     (extmoneyincoll 'add commod-currency (gnc:split-get-value s)))
   			     )
   			)
***************
*** 443,450 ****
  	    ;; adjust for brokers and dividends.
  	    (gaincoll 'add currency (sum-basis basis-list))
!             (moneyincoll 'minusmerge dividendcoll #f)
  	    (moneyoutcoll 'minusmerge brokeragecoll #f)
  	    (gaincoll 'merge moneyoutcoll #f)
  	    (gaincoll 'merge moneyincoll #f)
  
  
--- 444,452 ----
  	    ;; adjust for brokers and dividends.
  	    (gaincoll 'add currency (sum-basis basis-list))
!             (moneyincoll 'minusmerge extmoneyincoll #f)
  	    (moneyoutcoll 'minusmerge brokeragecoll #f)
  	    (gaincoll 'merge moneyoutcoll #f)
  	    (gaincoll 'merge moneyincoll #f)
+ 	    (gaincoll 'merge extmoneyincoll #f) ;; gain is only related to personnal investment
  
  
***************
*** 454,457 ****
--- 456,460 ----
  	    (let* ((moneyin (gnc:monetary-neg
  			    (gnc:sum-collector-commodity moneyincoll currency exchange-fn)))
+ 		  (extmoneyin (gnc:monetary-neg (gnc:sum-collector-commodity extmoneyincoll currency exchange-fn)))
  		  (moneyout (gnc:sum-collector-commodity moneyoutcoll currency exchange-fn))
  		  ;; just so you know, gain == realized gain, ugain == un-realized gain, bothgain, well..
***************
*** 470,473 ****
--- 473,477 ----
  	      (total-value 'add (gnc:gnc-monetary-commodity value) (gnc:gnc-monetary-amount value))
  	      (total-moneyin 'merge moneyincoll #f)
+ 	      (total-extmoneyin 'merge extmoneyincoll #f)
  	      (total-moneyout 'merge moneyoutcoll #f)
  	      (total-gain 'merge gaincoll #f)
***************
*** 498,501 ****
--- 502,506 ----
  					(gnc:make-html-table-header-cell/markup "number-cell" value)
  					(gnc:make-html-table-header-cell/markup "number-cell" moneyin)
+ 					(gnc:make-html-table-header-cell/markup "number-cell"  extmoneyin)
  					(gnc:make-html-table-header-cell/markup "number-cell" moneyout)
  					(gnc:make-html-table-header-cell/markup "number-cell" gain)
***************
*** 508,515 ****
  								 (gnc:gnc-monetary-amount moneyin))))
  					      (if (= 0.0 moneyinvalue)
! 						  (sprintf #f "%.2f%%" moneyinvalue)
  						  (sprintf #f "%.2f%%" (* 100 (/ (gnc:numeric-to-double 
  									     (gnc:gnc-monetary-amount bothgain))
  									    moneyinvalue))))))
  					 )
  			)
--- 513,533 ----
  								 (gnc:gnc-monetary-amount moneyin))))
  					      (if (= 0.0 moneyinvalue)
! 						  (if (= 0.0 (gnc:numeric-to-double 
! 								 (gnc:gnc-monetary-amount bothgain)))
! 								 (sprintf #f "%.2f%%" moneyinvalue)(sprintf  #f "N/A" ))
  						  (sprintf #f "%.2f%%" (* 100 (/ (gnc:numeric-to-double 
  									     (gnc:gnc-monetary-amount bothgain))
  									    moneyinvalue))))))
+ 					(gnc:make-html-table-header-cell/markup "number-cell" 
+ 					;; totalmoneyvalue=sum of all the money invested (own and external)
+ 					    (let ((totalmoneyvalue  (gnc:numeric-to-double 
+ 								 (gnc:gnc-monetary-amount  (gnc:make-gnc-monetary currency  (gnc:numeric-add (gnc:gnc-monetary-amount moneyin)
+ 									      (gnc:gnc-monetary-amount extmoneyin)
+ 									      100 GNC-RND-ROUND))))))
+ 					      (if (= 0.0 totalmoneyvalue)
+ 							(if (= 0.0 (gnc:numeric-to-double 
+ 								 (gnc:gnc-monetary-amount bothgain)))
+ 								 (sprintf #f "%.2f%%" totalmoneyvalue)(sprintf  #f "N/A" ))
+ 						  (sprintf #f "%.2f%%" (* 100 (/ (gnc:numeric-to-double (gnc:gnc-monetary-amount bothgain)) totalmoneyvalue))))))
  					 )
  			)
***************
*** 563,567 ****
  	(total-basis (gnc:make-commodity-collector))
          (total-value    (gnc:make-commodity-collector))
!         (total-moneyin  (gnc:make-commodity-collector))
          (total-moneyout (gnc:make-commodity-collector))
          (total-gain     (gnc:make-commodity-collector)) ;; realized gain
--- 581,586 ----
  	(total-basis (gnc:make-commodity-collector))
          (total-value    (gnc:make-commodity-collector))
!         (total-moneyin  (gnc:make-commodity-collector)) ;; own money in
!         (total-extmoneyin  (gnc:make-commodity-collector)) ;; external money in
          (total-moneyout (gnc:make-commodity-collector))
          (total-gain     (gnc:make-commodity-collector)) ;; realized gain
***************
*** 620,628 ****
  				    (_ "Value")
  				    (_ "Money In")
  				    (_ "Money Out")
  				    (_ "Realized Gain")
  				    (_ "Unrealized Gain")
  				    (_ "Total Gain")
! 				    (_ "Total Return")))
  
            (append! totalscols (list " "))
--- 639,649 ----
  				    (_ "Value")
  				    (_ "Money In")
+ 				    (_ "External Money In")
  				    (_ "Money Out")
  				    (_ "Realized Gain")
  				    (_ "Unrealized Gain")
  				    (_ "Total Gain")
! 				    (_ "Personnal Return") ;; ROI based on own investment
! 				    (_ "Global Return"))) ;; global ROI --> ~ fund performance
  
            (append! totalscols (list " "))
***************
*** 640,644 ****
             table accounts to-date currency price-fn exchange-fn
             include-empty include-gains show-symbol show-listing show-shares show-price 
! 	   basis-method prefer-pricelist total-basis total-value total-moneyin total-moneyout total-gain total-ugain)
  	  
  
--- 661,665 ----
             table accounts to-date currency price-fn exchange-fn
             include-empty include-gains show-symbol show-listing show-shares show-price 
! 	   basis-method prefer-pricelist total-basis total-value total-moneyin total-extmoneyin total-moneyout total-gain total-ugain)
  	  
  
***************
*** 654,658 ****
             (list
              (gnc:make-html-table-cell/size
!              1 14 (gnc:make-html-text (gnc:html-markup-hr)))))
  
  	  ;; finish building the totals columns, now that totals are complete
--- 675,679 ----
             (list
              (gnc:make-html-table-cell/size
!              1 16 (gnc:make-html-text (gnc:html-markup-hr)))))
  
  	  ;; finish building the totals columns, now that totals are complete
***************
*** 665,668 ****
--- 686,691 ----
  				"total-number-cell" (gnc:monetary-neg (gnc:sum-collector-commodity total-moneyin currency exchange-fn)))
  			       (gnc:make-html-table-cell/markup
+ 				"total-number-cell" (gnc:monetary-neg (gnc:sum-collector-commodity total-extmoneyin currency exchange-fn)))
+ 			       (gnc:make-html-table-cell/markup
  				"total-number-cell" (gnc:sum-collector-commodity total-moneyout currency exchange-fn))
  			       (gnc:make-html-table-cell/markup
***************
*** 682,685 ****
--- 705,720 ----
  								      (gnc:gnc-monetary-amount sum-total-both-gains))
  										   totalinvalue))))))
+ 			       (gnc:make-html-table-cell/markup
+ 				"total-number-cell"
+ 				;; this is obvioulsy bad scheme, sorry
+ 				(let ((totalmoneyvalue (gnc:numeric-to-double
+ 						     (gnc:gnc-monetary-amount  (gnc:make-gnc-monetary currency  (gnc:numeric-add (gnc:gnc-monetary-amount (gnc:monetary-neg (gnc:sum-collector-commodity 
+ 									       total-extmoneyin currency exchange-fn)))(gnc:gnc-monetary-amount (gnc:monetary-neg (gnc:sum-collector-commodity 
+ 									       total-moneyin currency exchange-fn)))100 GNC-RND-ROUND))))))
+ 				  (if (= 0.0 totalmoneyvalue)
+ 				      (sprintf #f "%.2f%%" totalmoneyvalue)
+ 				      (sprintf #f "%.2f%%" (* 100 (/ (gnc:numeric-to-double 
+ 								      (gnc:gnc-monetary-amount sum-total-both-gains))
+ 										   totalmoneyvalue))))))
  		       ))
  
