[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.
This commit is contained in:
Christopher Lam 2019-04-20 14:11:03 +08:00
parent e111c5bb07
commit 375013f9ea

View File

@ -690,23 +690,18 @@ construct with gnc:make-gnc-monetary and gnc:monetary->string instead.")
;; the <gnc:commodity*> domestic-commodity, exchanges the amount into ;; the <gnc:commodity*> domestic-commodity, exchanges the amount into
;; the domestic currency and returns a <gnc-monetary>. ;; the domestic currency and returns a <gnc-monetary>.
(define (gnc:make-exchange-function exchange-alist) (define (gnc:make-exchange-function exchange-alist)
(let ((exchangelist exchange-alist))
(lambda (foreign domestic) (lambda (foreign domestic)
(gnc:debug "foreign: " (gnc:monetary->string foreign)) (gnc:debug "foreign: " (gnc:monetary->string foreign))
(gnc:debug "domestic: " (gnc-commodity-get-printname domestic)) (gnc:debug "domestic: " (gnc-commodity-get-printname domestic))
(and foreign (and foreign
(or (gnc:exchange-by-euro foreign domestic #f) (or (gnc:exchange-by-euro foreign domestic #f)
(gnc:exchange-if-same foreign domestic) (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 (gnc:make-gnc-monetary
domestic domestic
(let ((pair (assoc (gnc:gnc-monetary-commodity foreign) (* (gnc:gnc-monetary-amount foreign) (cadr pair)))))))))
exchangelist))
(foreign-amount (gnc:gnc-monetary-amount foreign)))
(if (or (not pair)
(zero? foreign-amount))
0
(* foreign-amount
(cadr pair))))))))))
;; Helper for the gnc:exchange-by-pricalist* below. Exchange the ;; Helper for the gnc:exchange-by-pricalist* below. Exchange the
;; <gnc:monetary> 'foreign' into the <gnc:commodity*> 'domestic' by ;; <gnc:monetary> 'foreign' into the <gnc:commodity*> 'domestic' by