[commodity-utils.scm][api] gnc:html-make-rates-table with price-fn

better precision than gnc:html-make-exchangerates
This commit is contained in:
Christopher Lam 2020-10-20 10:03:48 +08:00
parent e833c8e1aa
commit e7165507c8
3 changed files with 39 additions and 0 deletions

View File

@ -21,6 +21,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use-modules (ice-9 match))
(use-modules (srfi srfi-26))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Functions to get splits with interesting data from accounts.
@ -764,6 +765,18 @@
(gnc:exchange-by-pricedb-nearest
foreign domestic to-date-tp))))))
(define (gnc:case-price-fn source target-curr date)
(define pdb (gnc-pricedb-get-db (gnc-get-current-book)))
(case source
((pricedb-nearest) (cut gnc-pricedb-get-nearest-price pdb <> target-curr date))
((pricedb-latest) (cut gnc-pricedb-get-latest-price pdb <> target-curr))
(else
(lambda (commodity)
(let* ((exchange-fn (gnc:case-exchange-fn source target-curr date))
(foreign-mon (gnc:make-gnc-monetary commodity 1))
(domestic-mon (exchange-fn foreign-mon target-curr)))
(gnc:gnc-monetary-amount domestic-mon))))))
;; Return a ready-to-use function. Which one to use is determined by
;; the value of 'source-option', whose possible values are set in
;; gnc:options-add-price-source!.

View File

@ -228,6 +228,30 @@
(G_ "Exchange rates"))))))
table))
;; Create a html-table of all prices. The report-currency is
;; 'currency', The prices are given through the function 'price-fn'
;; and the 'accounts' determine which commodities to show. Returns a
;; html-object, a <html-table>. price-fn is easily obtained from
;; gnc:case-price-fn
(define (gnc:html-make-rates-table currency price-fn accounts)
(define (cell c) (gnc:make-html-table-cell/markup "number-cell" c))
(define table (gnc:make-html-table))
(let lp ((comm-list (gnc:accounts-get-commodities accounts currency)) (entries 0))
(match comm-list
(()
(unless (zero? entries)
(gnc:html-table-set-col-headers!
table (list (gnc:make-html-table-header-cell/size
1 2 (if (= entries 1) (G_ "Exchange rate")
(G_ "Exchange rates"))))))
table)
((comm . rest)
(gnc:html-table-append-row!
table
(list (cell (gnc:make-gnc-monetary comm 1))
(cell (gnc:default-price-renderer currency (price-fn comm)))))
(lp rest (1+ entries))))))
(define (gnc:html-make-generic-budget-warning report-title-string)
(gnc:html-make-generic-simple-warning

View File

@ -58,6 +58,7 @@
(export gnc:exchange-by-pricealist-nearest)
(export gnc:case-exchange-fn)
(export gnc:case-exchange-time-fn)
(export gnc:case-price-fn)
(export gnc:sum-collector-commodity)
;; options-utilities.scm
@ -104,6 +105,7 @@
(export gnc:assign-colors)
(export gnc:html-table-append-ruler!)
(export gnc:html-make-exchangerates)
(export gnc:html-make-rates-table)
(export gnc:html-render-options-changed)
(export gnc:html-make-generic-warning)
(export gnc:html-make-no-account-warning)