From f0f7210097cd87e83a8cbb89bd1a610b58261542 Mon Sep 17 00:00:00 2001 From: Derek Atkins Date: Sun, 12 Feb 2006 21:46:36 +0000 Subject: [PATCH] Mike Alexander's patch to handle multiple currencies in reports. (#314554) * src/report/standard-reports/portfolio.scm: * src/report/standard-reports/advanced-portfolio.scm: Report currency option Sort accounts Handle "nearest price" option better * src/report/standard-reports/cash-flow.scm: Handle multiple currencies better * src/report/standard-reports/transaction.scm: Add common currency option. Handle "nearest price" option better. * src/report/report-system/commodity-utilities.scm (gnc:exchange-by-pricedb-nearest): Use price nearest to noon on the day specified. Fixes bug #314554. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@13244 57a11ea4-9604-0410-9ed3-97b8803252fd --- ChangeLog | 16 ++ .../report-system/commodity-utilities.scm | 2 +- .../standard-reports/advanced-portfolio.scm | 226 +++++++++--------- src/report/standard-reports/cash-flow.scm | 49 +++- src/report/standard-reports/portfolio.scm | 56 ++--- src/report/standard-reports/transaction.scm | 53 +++- 6 files changed, 244 insertions(+), 158 deletions(-) diff --git a/ChangeLog b/ChangeLog index acbd355e2b..71c126ee94 100644 --- a/ChangeLog +++ b/ChangeLog @@ -47,6 +47,22 @@ Apply dividend and brokerage to moneyin and moneyout to more accurately reflect account activity. Fixes bug #311549. + Mike Alexander's patch to handle multiple currencies in reports. + * src/report/standard-reports/portfolio.scm: + * src/report/standard-reports/advanced-portfolio.scm: + Report currency option + Sort accounts + Handle "nearest price" option better + * src/report/standard-reports/cash-flow.scm: + Handle multiple currencies better + * src/report/standard-reports/transaction.scm: + Add common currency option. + Handle "nearest price" option better. + * src/report/report-system/commodity-utilities.scm + (gnc:exchange-by-pricedb-nearest): + Use price nearest to noon on the day specified. + Fixes bug #314554. + 2006-02-11 Derek Atkins * src/report/report-gnome/gnc-plugin-page-report.c: diff --git a/src/report/report-system/commodity-utilities.scm b/src/report/report-system/commodity-utilities.scm index 82d89ddfb8..8303812807 100644 --- a/src/report/report-system/commodity-utilities.scm +++ b/src/report/report-system/commodity-utilities.scm @@ -778,7 +778,7 @@ (gnc:book-get-pricedb (gnc:get-current-book)) (gnc:gnc-monetary-amount foreign) (gnc:gnc-monetary-commodity foreign) - domestic date))) + domestic (gnc:timepair-canonical-day-time date)))) #f)) ;; Exchange by the nearest price from pricelist. This function takes diff --git a/src/report/standard-reports/advanced-portfolio.scm b/src/report/standard-reports/advanced-portfolio.scm index 1478bd3c0e..306f7834f2 100644 --- a/src/report/standard-reports/advanced-portfolio.scm +++ b/src/report/standard-reports/advanced-portfolio.scm @@ -170,113 +170,123 @@ (price-list (price-fn commodity to-date)) (price (if (> (length price-list) 0) (car price-list) #f)) - - (value (exchange-fn (gnc:make-gnc-monetary commodity units) currency to-date)) - ) + (commod-currency (gnc:price-get-currency price)) + (value (exchange-fn (gnc:make-gnc-monetary commodity units) + currency)) + ) -;; (gnc:debug "---" name "---") - (for-each - (lambda (split) - (set! work-done (+ 1 work-done)) - (gnc:report-percent-done (* 100 (/ work-done work-to-do))) - (let ((parent (gnc:split-get-parent split))) - (if (gnc:timepair-le (gnc:transaction-get-date-posted parent) to-date) - (for-each - (lambda (s) - (cond - ((same-split? s split) - ;; (gnc:debug "amount" (gnc:numeric-to-double (gnc:split-get-amount s)) ) - (cond - ((or include-gains (not (gnc:numeric-zero-p (gnc:split-get-amount s)))) - (unitscoll 'add commodity (gnc:split-get-amount s)) ;; Is the stock transaction? - (if (< 0 (gnc:numeric-to-double - (gnc:split-get-amount s))) - (set! totalunits - (+ totalunits - (gnc:numeric-to-double (gnc:split-get-amount s))))) - (set! totalunityears - (+ totalunityears - (* (gnc:numeric-to-double (gnc:split-get-amount s)) - (gnc:date-year-delta - (car (gnc:transaction-get-date-posted parent)) - (current-time))))) - (cond - ((gnc:numeric-negative-p (gnc:split-get-value s)) - (moneyoutcoll - 'add currency - (gnc:numeric-neg (gnc:split-get-value s)))) - (else (moneyincoll - 'add currency - (gnc:numeric-neg (gnc:split-get-value s)))))))) - - ((split-account-type? s 'expense) - (brokeragecoll 'add currency (gnc:split-get-value s))) - - ((split-account-type? s 'income) - (dividendcoll 'add currency (gnc:split-get-value s))) - ) - ) - (gnc:transaction-get-splits parent) - ) +;; (gnc:debug "---" name "---") + (for-each + (lambda (split) + (set! work-done (+ 1 work-done)) + (gnc:report-percent-done (* 100 (/ work-done work-to-do))) + (let ((parent (gnc:split-get-parent split))) + (if (gnc:timepair-le (gnc:transaction-get-date-posted parent) to-date) + (for-each + (lambda (s) + (cond + ((same-split? s split) +;; (gnc:debug "amount " (gnc:numeric-to-double (gnc:split-get-amount s)) +;; " acct " (gnc:account-get-name (gnc:split-get-account s)) ) +;; (gnc:debug "value " (gnc:numeric-to-double (gnc:split-get-value s)) +;; " in " (gnc:commodity-get-printname commod-currency) +;; " from " (gnc:transaction-get-description (gnc:split-get-parent s))) + (cond + ((or include-gains (not (gnc:numeric-zero-p (gnc:split-get-amount s)))) + (unitscoll 'add commodity (gnc:split-get-amount s)) ;; Is the stock transaction? + (if (< 0 (gnc:numeric-to-double + (gnc:split-get-amount s))) + (set! totalunits + (+ totalunits + (gnc:numeric-to-double (gnc:split-get-amount s))))) + (set! totalunityears + (+ totalunityears + (* (gnc:numeric-to-double (gnc:split-get-amount s)) + (gnc:date-year-delta + (car (gnc:transaction-get-date-posted parent)) + (current-time))))) + (cond + ((gnc:numeric-negative-p (gnc:split-get-value s)) + (moneyoutcoll + 'add commod-currency + (gnc:numeric-neg (gnc:split-get-value s)))) + (else (moneyincoll + 'add commod-currency + (gnc:numeric-neg (gnc:split-get-value s)))))))) + + ((split-account-type? s 'expense) + (brokeragecoll 'add commod-currency (gnc:split-get-value s))) + + ((split-account-type? s 'income) + (dividendcoll 'add commod-currency (gnc:split-get-value s))) + ) + ) + (gnc:transaction-get-splits parent) + ) ) - ) - ) - (gnc:account-get-split-list current) - ) -;; (gnc:debug "totalunits" totalunits) -;; (gnc:debug "totalunityears" totalunityears) + ) + ) + (gnc:account-get-split-list current) + ) +;; (gnc:debug "totalunits" totalunits) +;; (gnc:debug "totalunityears" totalunityears) - (moneyincoll 'minusmerge dividendcoll #f) - (moneyoutcoll 'minusmerge brokeragecoll #f) - (gaincoll 'merge moneyoutcoll #f) - (gaincoll 'add (gnc:gnc-monetary-commodity value) (gnc:gnc-monetary-amount value)) - (gaincoll 'merge moneyincoll #f) - - (if (or include-empty (not (gnc:numeric-zero-p units))) - (begin (total-value 'add (gnc:gnc-monetary-commodity value) (gnc:gnc-monetary-amount value)) - (total-moneyin 'merge moneyincoll #f) - (total-moneyout 'merge moneyoutcoll #f) - (total-gain 'merge gaincoll #f) - (gnc:html-table-append-row/markup! - table - row-style - (list (gnc:html-account-anchor current) - ticker-symbol - listing - (gnc:make-html-table-header-cell/markup - "number-cell" (gnc:amount->string units share-print-info)) - (gnc:make-html-table-header-cell/markup - "number-cell" - (if price - (gnc:html-price-anchor - price - (gnc:make-gnc-monetary - (gnc:price-get-currency price) - (gnc:price-get-value price))) - #f)) - (gnc:make-html-table-header-cell/markup - "number-cell" value) - (gnc:make-html-table-header-cell/markup - "number-cell" (gnc:monetary-neg (gnc:sum-collector-commodity moneyincoll currency exchange-fn))) - (gnc:make-html-table-header-cell/markup - "number-cell" (gnc:sum-collector-commodity moneyoutcoll currency exchange-fn)) - (gnc:make-html-table-header-cell/markup - "number-cell" (gnc:sum-collector-commodity gaincoll currency exchange-fn)) - (gnc:make-html-table-header-cell/markup - "number-cell" (sprintf #f "%.2f%%" (* 100 (/ (gnc:numeric-to-double (cadr (gaincoll 'getpair currency #f))) - (gnc:numeric-to-double (cadr (moneyincoll 'getpair currency #t))))))) - ) - ) - (table-add-stock-rows-internal rest (not odd-row?)) - ) - (table-add-stock-rows-internal rest odd-row?) + (moneyincoll 'minusmerge dividendcoll #f) + (moneyoutcoll 'minusmerge brokeragecoll #f) + (gaincoll 'merge moneyoutcoll #f) + (gaincoll 'add (gnc:gnc-monetary-commodity value) (gnc:gnc-monetary-amount value)) + (gaincoll 'merge moneyincoll #f) + + (let ((moneyin (gnc:monetary-neg + (gnc:sum-collector-commodity moneyincoll currency exchange-fn))) + (moneyout (gnc:sum-collector-commodity moneyoutcoll currency exchange-fn)) + (gain (gnc:sum-collector-commodity gaincoll currency exchange-fn)) + ) + (total-value 'add (gnc:gnc-monetary-commodity value) (gnc:gnc-monetary-amount value)) + (total-moneyin 'merge moneyincoll #f) + (total-moneyout 'merge moneyoutcoll #f) + (total-gain 'merge gaincoll #f) + (gnc:html-table-append-row/markup! + table + row-style + (list (gnc:html-account-anchor current) + ticker-symbol + listing + (gnc:make-html-table-header-cell/markup + "number-cell" (gnc:amount->string units share-print-info)) + (gnc:make-html-table-header-cell/markup + "number-cell" + (if price + (gnc:html-price-anchor + price + (gnc:make-gnc-monetary + (gnc:price-get-currency price) + (gnc:price-get-value price))) + #f)) + (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" moneyout) + (gnc:make-html-table-header-cell/markup "number-cell" gain) + (gnc:make-html-table-header-cell/markup + "number-cell" + (sprintf #f "%.2f%%" + (* 100 (/ (gnc:numeric-to-double + (gnc:gnc-monetary-amount gain)) + (gnc:numeric-to-double + (gnc:gnc-monetary-amount moneyin)))) + )) + ) + ) + (table-add-stock-rows-internal rest (not odd-row?)) + ) + (table-add-stock-rows-internal rest odd-row?) ) (gnc:price-list-destroy price-list) - ))) + ))) (set! work-to-do (gnc:accounts-count-splits accounts)) - (table-add-stock-rows-internal accounts #t))) - + (table-add-stock-rows-internal accounts #t)) + ;; Tell the user that we're starting. (gnc:report-starting reportname) @@ -309,15 +319,9 @@ report-title (sprintf #f " %s" (gnc:print-date to-date)))) -;; (gnc:debug "accounts" accounts) (if (not (null? accounts)) ; at least 1 account selected - (let* ((exchange-fn - (case price-source - ('pricedb-latest - (lambda (foreign domestic date) - (gnc:exchange-by-pricedb-latest foreign domestic))) - ('pricedb-nearest gnc:exchange-by-pricedb-nearest))) + (let* ((exchange-fn (gnc:case-exchange-fn price-source currency to-date)) (pricedb (gnc:book-get-pricedb (gnc:get-current-book))) (price-fn (case price-source @@ -326,7 +330,8 @@ (gnc:pricedb-lookup-latest-any-currency pricedb foreign))) ('pricedb-nearest (lambda (foreign date) - (gnc:pricedb-lookup-nearest-in-time-any-currency pricedb foreign date)))))) + (gnc:pricedb-lookup-nearest-in-time-any-currency + pricedb foreign (gnc:timepair-canonical-day-time date))))))) (gnc:html-table-set-col-headers! table @@ -341,6 +346,11 @@ (_ "Gain") (_ "Total Return"))) + (set! accounts (sort accounts + (lambda (a b) + (string (length price) 0)) + (let ((v (gnc:price-get-value (car price)))) + (cons (car price) v)) (cons #f (gnc:numeric-zero)))))) ('pricedb-nearest - (lambda (foreign domestic date) + (lambda (foreign date) (let ((price - (gnc:pricedb-lookup-nearest-in-time - pricedb foreign domestic date))) - (if price - (let ((v (gnc:price-get-value price))) - (cons price v)) + (gnc:pricedb-lookup-nearest-in-time-any-currency + pricedb foreign (gnc:timepair-canonical-day-time date)))) + (if (and price (> (length price) 0)) + (let ((v (gnc:price-get-value (car price)))) + (cons (car price) v)) (cons #f (gnc:numeric-zero))))))))) (gnc:html-table-set-col-headers! @@ -236,9 +231,14 @@ (_ "Price") (_ "Value"))) + (set! accounts (sort accounts + (lambda (a b) + (string