From 375013f9ea4857f46effc4d4a8c2a68956cb3184 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sat, 20 Apr 2019 14:11:03 +0800 Subject: [PATCH] [commodity-utils] simplify gnc:make-exchange-function Remove assigning exchangelist to exchange-alist. Removes need to test foreign-amount against 0 - multiplication will result in 0 anyway. The only functional change is the condition whereby foreign's commodity is not present in exchangelist; previously it would return (gnc:make-gnc-monetary domestic 0); now it returns #f. This function *is* allowed to return #f for invalid or missing prices; see the (and foreign ...) conditional. --- .../report-system/commodity-utilities.scm | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/gnucash/report/report-system/commodity-utilities.scm b/gnucash/report/report-system/commodity-utilities.scm index b4c73a116e..a0877a1c59 100644 --- a/gnucash/report/report-system/commodity-utilities.scm +++ b/gnucash/report/report-system/commodity-utilities.scm @@ -690,23 +690,18 @@ construct with gnc:make-gnc-monetary and gnc:monetary->string instead.") ;; the domestic-commodity, exchanges the amount into ;; the domestic currency and returns a . (define (gnc:make-exchange-function exchange-alist) - (let ((exchangelist exchange-alist)) - (lambda (foreign domestic) - (gnc:debug "foreign: " (gnc:monetary->string foreign)) - (gnc:debug "domestic: " (gnc-commodity-get-printname domestic)) - (and foreign - (or (gnc:exchange-by-euro foreign domestic #f) - (gnc:exchange-if-same foreign domestic) - (gnc:make-gnc-monetary - domestic - (let ((pair (assoc (gnc:gnc-monetary-commodity foreign) - exchangelist)) - (foreign-amount (gnc:gnc-monetary-amount foreign))) - (if (or (not pair) - (zero? foreign-amount)) - 0 - (* foreign-amount - (cadr pair)))))))))) + (lambda (foreign domestic) + (gnc:debug "foreign: " (gnc:monetary->string foreign)) + (gnc:debug "domestic: " (gnc-commodity-get-printname domestic)) + (and foreign + (or (gnc:exchange-by-euro foreign domestic #f) + (gnc:exchange-if-same foreign domestic) + (let* ((foreign-comm (gnc:gnc-monetary-commodity foreign)) + (pair (assoc foreign-comm exchange-alist))) + (and pair + (gnc:make-gnc-monetary + domestic + (* (gnc:gnc-monetary-amount foreign) (cadr pair))))))))) ;; Helper for the gnc:exchange-by-pricalist* below. Exchange the ;; 'foreign' into the 'domestic' by