gnucash maint: [trep-engine] upgrade to include 'balance brought forward'

Christopher Lam clam at code.gnucash.org
Mon Jan 6 09:36:31 EST 2020


Updated	 via  https://github.com/Gnucash/gnucash/commit/741eb480 (commit)
	from  https://github.com/Gnucash/gnucash/commit/0f4265d9 (commit)



commit 741eb480168f1ecb8b10b4b61240a565be87bdad
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Mon Jan 6 22:03:53 2020 +0800

    [trep-engine] upgrade to include 'balance brought forward'
    
    This fairly complex-looking change is actually simple.
    
    default-calculated-cells is redefined so that the running-balance
    column has a custom subheading renderer. The subheading renderer was
    formerly used solely for the account->friendly-debit/credit-string
    renderer, and is now upgraded to handle hard-coded symbols.
    
    If the friendly-heading-fn is a symbol 'bal-bf, it'll print the
    split->account->balance at begindate.
    
    Other columns may also be upgraded for novel subheading renderers. No
    forward/backward compatibility issues are expected at all. This change
    looks complex because the begindate is not available to
    make-split-table and has to be passed on as an argument to be
    available for use, and the subheading-renderer will test whether to
    display the friendly-fn-renderer at a later stage than previously.

diff --git a/gnucash/report/report-system/trep-engine.scm b/gnucash/report/report-system/trep-engine.scm
index 5e83d506d..f39a47a7b 100644
--- a/gnucash/report/report-system/trep-engine.scm
+++ b/gnucash/report/report-system/trep-engine.scm
@@ -42,6 +42,7 @@
 (use-modules (gnucash gettext))
 (use-modules (srfi srfi-11))
 (use-modules (srfi srfi-1))
+(use-modules (ice-9 match))
 
 ;; Define the strings here to avoid typos and make changes easier.
 
@@ -1033,7 +1034,8 @@ be excluded from periodic reporting.")
 ;; ;;;;;;;;;;;;;;;;;;;;
 ;; Here comes the big function that builds the whole table.
 
-(define (make-split-table splits options custom-calculated-cells)
+(define (make-split-table splits options custom-calculated-cells
+                          begindate)
 
   (define (opt-val section name)
     (let ((option (gnc:lookup-option options section name)))
@@ -1120,6 +1122,11 @@ be excluded from periodic reporting.")
                              'multi-line))
          (export? (opt-val gnc:pagename-general optname-table-export)))
 
+    (define (acc-reverse? acc)
+      (if account-types-to-reverse
+          (memv (xaccAccountGetType acc) account-types-to-reverse)
+          (gnc-reverse-balance acc)))
+
     (define (column-uses? param)
       (cdr (assq param used-columns)))
 
@@ -1315,6 +1322,7 @@ be excluded from periodic reporting.")
          ;;                             column must be the credit side
          ;;         friendly-heading-fn (friendly-heading-fn account) to retrieve
          ;;                             friendly name for account debit/credit
+         ;;                             or 'bal-bf for balance-brought-forward
 
          (if (column-uses? 'amount-single)
              (list (vector (header-commodity (_ "Amount"))
@@ -1351,7 +1359,7 @@ be excluded from periodic reporting.")
          (if (column-uses? 'running-balance)
              (list (vector (_ "Running Balance")
                            running-balance #t #f #f
-                           (lambda (a) "")))
+                           'bal-bf))
              '()))))
 
     (define calculated-cells
@@ -1406,29 +1414,35 @@ be excluded from periodic reporting.")
            table subheading-style
            (append
             (gnc:html-make-empty-cells left-indent)
-            (if (and (opt-val pagename-sorting optname-show-informal-headers)
-                     (column-uses? 'amount-double)
-                     (memq sortkey SORTKEY-INFORMAL-HEADERS))
-                (append
-                 (if export?
-                     (cons
-                      (gnc:make-html-table-cell data)
-                      (gnc:html-make-empty-cells
-                       (+ right-indent width-left-columns -1)))
-                     (list
-                      (gnc:make-html-table-cell/size
-                       1 (+ right-indent width-left-columns) data)))
-                 (map (lambda (cell)
-                        (let ((friendly-fn (vector-ref cell 5)))
-                          (and friendly-fn
-                               (gnc:make-html-text
-                                (gnc:html-markup-b
-                                 (friendly-fn (renderer-fn split)))))))
-                      calculated-cells))
+            (if export?
+                (cons
+                 (gnc:make-html-table-cell data)
+                 (gnc:html-make-empty-cells
+                  (+ right-indent width-left-columns -1)))
                 (list
                  (gnc:make-html-table-cell/size
-                  1 (+ right-indent width-left-columns width-right-columns)
-                  data))))))))
+                  1 (+ right-indent width-left-columns) data)))
+            (map
+             (lambda (cell)
+               (match (vector-ref cell 5)
+                 (#f #f)
+                 ('bal-bf
+                  (let* ((acc (xaccSplitGetAccount split))
+                         (bal (xaccAccountGetBalanceAsOfDate acc begindate)))
+                    (and (memq sortkey ACCOUNT-SORTING-TYPES)
+                         (gnc:make-html-table-cell/markup
+                          "number-cell"
+                          (gnc:make-gnc-monetary
+                           (xaccAccountGetCommodity acc)
+                           (if (acc-reverse? acc) (- bal) bal))))))
+                 (fn
+                  (and (opt-val pagename-sorting optname-show-informal-headers)
+                       (column-uses? 'amount-double)
+                       (memq sortkey SORTKEY-INFORMAL-HEADERS)
+                       (gnc:make-html-text
+                        (gnc:html-markup-b
+                         (fn (xaccSplitGetAccount split))))))))
+             calculated-cells))))))
 
     (define (add-subtotal-row subtotal-string subtotal-collectors
                               subtotal-style level row col)
@@ -1605,10 +1619,7 @@ be excluded from periodic reporting.")
 
     (define (add-split-row split cell-calculators row-style transaction-row?)
       (let* ((account (xaccSplitGetAccount split))
-             (reversible-account? (if account-types-to-reverse
-                                      (memv (xaccAccountGetType account)
-                                            account-types-to-reverse)
-                                      (gnc-reverse-balance account)))
+             (reversible-account? (acc-reverse? account))
              (cells (map (lambda (cell)
                            (let ((split->monetary (vector-ref cell 1)))
                              (vector (split->monetary split)
@@ -2173,7 +2184,8 @@ be excluded from periodic reporting.")
 
        (else
         (let-values (((table grid csvlist)
-                      (make-split-table splits options custom-calculated-cells)))
+                      (make-split-table splits options custom-calculated-cells
+                                        begindate)))
 
           (gnc:html-document-set-title! document report-title)
 



Summary of changes:
 gnucash/report/report-system/trep-engine.scm | 68 ++++++++++++++++------------
 1 file changed, 40 insertions(+), 28 deletions(-)



More information about the gnucash-changes mailing list