gnucash maint: Add new price source - 'nearest before report date'

Christopher Lam clam at code.gnucash.org
Fri Apr 23 09:50:43 EDT 2021


Updated	 via  https://github.com/Gnucash/gnucash/commit/2ba93071 (commit)
	from  https://github.com/Gnucash/gnucash/commit/5957b3c2 (commit)



commit 2ba930713a852b1584a1359504e45422fd59f543
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Fri Apr 23 21:46:03 2021 +0800

    Add new price source - 'nearest before report date'
    
    Add new price source - 'nearest before report date' will ignore
    prices *after* report date. Will be useful to fix bug 743753.

diff --git a/gnucash/report/commodity-utilities.scm b/gnucash/report/commodity-utilities.scm
index 21f723832..84e218bde 100644
--- a/gnucash/report/commodity-utilities.scm
+++ b/gnucash/report/commodity-utilities.scm
@@ -741,6 +741,18 @@
              (gnc:gnc-monetary-commodity foreign)
              domestic (time64CanonicalDayTime date))))))
 
+(define (gnc:exchange-by-pricedb-nearest-before foreign domestic date)
+  (and (gnc:gnc-monetary? foreign) date
+       (or (gnc:exchange-by-euro foreign domestic date)
+           (gnc:exchange-if-same foreign domestic)
+           (gnc:make-gnc-monetary
+            domestic
+            (gnc-pricedb-convert-balance-nearest-before-price-t64
+             (gnc-pricedb-get-db (gnc-get-current-book))
+             (gnc:gnc-monetary-amount foreign)
+             (gnc:gnc-monetary-commodity foreign)
+             domestic (time64CanonicalDayTime date))))))
+
 ;; Exchange by the nearest price from pricelist. This function takes
 ;; the <gnc-monetary> 'foreign' amount, the <gnc:commodity*>
 ;; 'domestic' commodity, a <gnc:time64> 'date' and the
@@ -783,6 +795,7 @@
                          (gnc:make-exchange-alist
                           report-currency to-date-tp)))
     ((pricedb-latest) gnc:exchange-by-pricedb-latest)
+    ((pricedb-before) (cut gnc:exchange-by-pricedb-nearest-before <> <> to-date-tp))
     ((pricedb-nearest) (lambda (foreign domestic)
                          (gnc:exchange-by-pricedb-nearest
                           foreign domestic to-date-tp)))
@@ -805,6 +818,8 @@
   (case source
     ((pricedb-nearest) (cut gnc-pricedb-get-nearest-price pdb <> target-curr
                             (time64CanonicalDayTime date)))
+    ((pricedb-before) (cut gnc-pricedb-get-nearest-before-price pdb <> target-curr
+                           (time64CanonicalDayTime date)))
     ((pricedb-latest)  (cut gnc-pricedb-get-latest-price pdb <> target-curr))
     (else
      (lambda (commodity)
@@ -848,6 +863,7 @@
                           (lambda (foreign domestic date)
                             (gnc:exchange-by-pricealist-nearest
                              pricealist foreign domestic date))))
+    ((pricedb-before) gnc:exchange-by-pricedb-nearest-before)
     ((pricedb-latest) (lambda (foreign domestic date)
                         (gnc:exchange-by-pricedb-latest foreign domestic)))
     ((pricedb-nearest) gnc:exchange-by-pricedb-nearest)
diff --git a/gnucash/report/options-utilities.scm b/gnucash/report/options-utilities.scm
index 2d011b76e..ce982363b 100644
--- a/gnucash/report/options-utilities.scm
+++ b/gnucash/report/options-utilities.scm
@@ -144,6 +144,7 @@
     (list (vector 'average-cost (N_ "Average cost of purchases by volume-weighted"))
           (vector 'weighted-average (N_ "Weighted average of all past currency transactions"))
           (vector 'pricedb-latest (N_ "Most recent"))
+          (vector 'pricedb-before (N_ "Nearest before report date"))
           (vector 'pricedb-nearest (N_ "Nearest to report date"))))))
 
 ;; The width- and height- options for charts



Summary of changes:
 gnucash/report/commodity-utilities.scm | 16 ++++++++++++++++
 gnucash/report/options-utilities.scm   |  1 +
 2 files changed, 17 insertions(+)



More information about the gnucash-changes mailing list