gnucash maint: Multiple changes pushed

Christopher Lam clam at code.gnucash.org
Mon Apr 20 23:57:43 EDT 2020


Updated	 via  https://github.com/Gnucash/gnucash/commit/80230650 (commit)
	 via  https://github.com/Gnucash/gnucash/commit/96a6e1b0 (commit)
	 via  https://github.com/Gnucash/gnucash/commit/d6c5b115 (commit)
	from  https://github.com/Gnucash/gnucash/commit/6939945f (commit)



commit 802306503057a8f4476acc830eaa0e1f777e4c86
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Tue Apr 21 11:39:40 2020 +0800

    [fin.scm] return #f instead of -1 if n is out of range

diff --git a/libgnucash/app-utils/fin.scm b/libgnucash/app-utils/fin.scm
index 9c276ee66..bfac97a8a 100644
--- a/libgnucash/app-utils/fin.scm
+++ b/libgnucash/app-utils/fin.scm
@@ -289,8 +289,7 @@
 ;; The payment number (n) must be non-negative for amort_balance.  (In this
 ;;  case, payment zero is at the _beginning_ of the first period, so
 ;;  amort_balance will just be the initial balance.)
-;; If the above conditions on n are violated, the functions return -1 (#f is
-;;  not used, because it causes gnucash to crash).
+;; If the above conditions on n are violated, the functions returns #f
 ;;
 ;; A negative interest rate works (if you can find a lender who charges
 ;;  negative rates), but negative compounding frequency, or negative payment
@@ -301,7 +300,7 @@
 (define (gnc:amort_balance py cy iy pv pmt n places)
   (cond
     ((< pv 0) 0)
-    ((< n 0) -1) ;; Returning #f here causes gnucash to crash on startup
+    ((< n 0) #f)
     ((and (zero? pv) (>= pmt 0)) 0)
     ((zero? n) pv)
     (else
@@ -315,28 +314,27 @@
 ;; then zero if you keep trying to make payments)
 ;; (n must be greater than zero)
 (define (gnc:amort_pmt py cy iy pv pmt n places)
-  (if (< n 1) -1 ;; Returning #f here causes gnucash to crash on startup
-    (let* ((prevBal (gnc:amort_balance py cy iy pv pmt (- n 1) places))
-           (balBeforePayment
-		     (amort_balanceAfterInterest prevBal py cy iy places))
-           (balAfterPayment (amort_balanceAfterPayment balBeforePayment pmt)))
-      (- balBeforePayment balAfterPayment))))
+  (and (>= n 1)
+       (let* ((prevBal (gnc:amort_balance py cy iy pv pmt (- n 1) places))
+              (balBeforePayment (amort_balanceAfterInterest prevBal py cy iy places))
+              (balAfterPayment (amort_balanceAfterPayment balBeforePayment pmt)))
+         (- balBeforePayment balAfterPayment))))
 
 ;; Calculate the amount of the nth payment that is principal
 ;; (n must be greater than zero)
 (define (gnc:amort_ppmt py cy iy pv pmt n places)
-  (if (< n 1) -1
-    (let* ((prevBal (gnc:amort_balance py cy iy pv pmt (- n 1) places))
-	       (bal-after-int (amort_balanceAfterInterest prevBal py cy iy places))
-           (newBal (amort_balanceAfterPayment bal-after-int pmt)))
-      (- prevBal newBal))))
+  (and (>= n 1)
+       (let* ((prevBal (gnc:amort_balance py cy iy pv pmt (- n 1) places))
+              (bal-after-int (amort_balanceAfterInterest prevBal py cy iy places))
+              (newBal (amort_balanceAfterPayment bal-after-int pmt)))
+         (- prevBal newBal))))
 
 ;; Calculate the amount of the nth payment that is interest
 ;; (n must be greater than zero)
 (define (gnc:amort_ipmt py cy iy pv pmt n places)
-  (if (< n 1) -1
-    (let* ((prevBal(gnc:amort_balance py cy iy pv pmt (- n 1) places)))
-      (amort_interest prevBal py cy iy places))))
+  (and (>= n 1)
+       (amort_interest (gnc:amort_balance py cy iy pv pmt (- n 1) places)
+                       py cy iy places)))
 
 ;; "Private" helper functions:
 

commit 96a6e1b0d5be311bfb2ba0c8213e42d04fb40205
Merge: 6939945fc d6c5b115d
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Tue Apr 21 11:39:28 2020 +0800

    Merge branch 'patch-1' of git://github.com/thetedmunds/gnucash into maint


commit d6c5b115d836f0345f928141381dd53eec554888
Author: thetedmunds <49656923+thetedmunds at users.noreply.github.com>
Date:   Tue Apr 14 07:45:54 2020 -0700

    Adding lines missing from fix for Bug 797196
    
    Adding definition of gnc:amort_balance() that got lost in the original pull-request for Bug 797196.

diff --git a/libgnucash/app-utils/fin.scm b/libgnucash/app-utils/fin.scm
index 857db25b9..9c276ee66 100644
--- a/libgnucash/app-utils/fin.scm
+++ b/libgnucash/app-utils/fin.scm
@@ -298,6 +298,22 @@
 
 ;; Calculate the balance remaining after the nth payment
 ;; (n must be greater than or equal to zero)
+(define (gnc:amort_balance py cy iy pv pmt n places)
+  (cond
+    ((< pv 0) 0)
+    ((< n 0) -1) ;; Returning #f here causes gnucash to crash on startup
+    ((and (zero? pv) (>= pmt 0)) 0)
+    ((zero? n) pv)
+    (else
+      (let* ((bal-after-int (amort_balanceAfterInterest pv py cy iy places))
+            (bal-after-pmt (amort_balanceAfterPayment bal-after-int pmt)))
+        (gnc:amort_balance py cy iy bal-after-pmt pmt (- n 1) places)))))
+
+;; Calculate the size of the nth payment
+;; (it will just be pmt for all the payments before the final payment,
+;; then less,
+;; then zero if you keep trying to make payments)
+;; (n must be greater than zero)
 (define (gnc:amort_pmt py cy iy pv pmt n places)
   (if (< n 1) -1 ;; Returning #f here causes gnucash to crash on startup
     (let* ((prevBal (gnc:amort_balance py cy iy pv pmt (- n 1) places))



Summary of changes:
 libgnucash/app-utils/fin.scm | 46 +++++++++++++++++++++++++++++---------------
 1 file changed, 30 insertions(+), 16 deletions(-)



More information about the gnucash-changes mailing list