gnucash maint: Multiple changes pushed
Christopher Lam
clam at code.gnucash.org
Thu Jun 13 10:22:31 EDT 2019
Updated via https://github.com/Gnucash/gnucash/commit/2c04ae31 (commit)
via https://github.com/Gnucash/gnucash/commit/f6bfdabd (commit)
via https://github.com/Gnucash/gnucash/commit/241f1f92 (commit)
via https://github.com/Gnucash/gnucash/commit/497e18c3 (commit)
from https://github.com/Gnucash/gnucash/commit/ca759fb4 (commit)
commit 2c04ae310b6612e59dc2ca9dfec0002bda4c4a9f
Author: Christopher Lam <christopher.lck at gmail.com>
Date: Thu May 9 21:05:51 2019 +0800
[trep-engine] allow #:custom-source-accounts kwarg
specifying #:custom-source-accounts will override the (opt-val
"Accounts" "Accounts") option to specify source accounts
This is useful for custom-reports which may need multiple source
accounts. eg. VAT/GST report (still in development).
diff --git a/gnucash/report/report-system/trep-engine.scm b/gnucash/report/report-system/trep-engine.scm
index 3ae7ff3e5..eb1098ba3 100644
--- a/gnucash/report/report-system/trep-engine.scm
+++ b/gnucash/report/report-system/trep-engine.scm
@@ -1895,6 +1895,7 @@ be excluded from periodic reporting.")
(define* (gnc:trep-renderer
report-obj #:key custom-calculated-cells empty-report-message
custom-split-filter split->date split->date-include-false?
+ custom-source-accounts
export-type filename)
;; the trep-renderer is a define* function which, at minimum, takes
;; the report object
@@ -1910,6 +1911,7 @@ be excluded from periodic reporting.")
;; report. it can be useful for alternative date filtering, e.g. filter by
;; transaction->invoice->payment date.
;; #:export-type and #:filename - are provided for CSV export
+ ;; #:custom-source-accounts - alternate list-of-accounts to retrieve splits from
(define options (gnc:report-options report-obj))
(define (opt-val section name)
@@ -1939,7 +1941,8 @@ be excluded from periodic reporting.")
(catch 'regular-expression-syntax
(lambda () (make-regexp account-matcher))
(const 'invalid-regex))))
- (c_account_0 (opt-val gnc:pagename-accounts optname-accounts))
+ (c_account_0 (or custom-source-accounts
+ (opt-val gnc:pagename-accounts optname-accounts)))
(c_account_1 (filter
(lambda (acc)
(if (regexp? account-matcher-regexp)
commit f6bfdabd3ed5d0d75f27e1e07f6c33bbf6f6eb50
Author: Christopher Lam <christopher.lck at gmail.com>
Date: Thu May 9 21:03:58 2019 +0800
[trep-engine] allow #f for friendly-fn
During defition of custom-calculated-cells, we should allow #f as
friendly-fn which will disable it.
diff --git a/gnucash/report/report-system/trep-engine.scm b/gnucash/report/report-system/trep-engine.scm
index 65e716327..3ae7ff3e5 100644
--- a/gnucash/report/report-system/trep-engine.scm
+++ b/gnucash/report/report-system/trep-engine.scm
@@ -1384,6 +1384,9 @@ be excluded from periodic reporting.")
(case level
((primary) optname-prime-sortkey)
((secondary) optname-sec-sortkey))))
+ (renderer-fn (keylist-get-info
+ (sortkey-list BOOK-SPLIT-ACTION)
+ sortkey 'renderer-fn))
(left-indent (case level
((primary total) 0)
((secondary) primary-indent)))
@@ -1407,12 +1410,11 @@ be excluded from periodic reporting.")
(gnc:make-html-table-cell/size
1 (+ right-indent width-left-columns) data)))
(map (lambda (cell)
- (gnc:make-html-text
- (gnc:html-markup-b
- ((vector-ref cell 5)
- ((keylist-get-info (sortkey-list BOOK-SPLIT-ACTION)
- sortkey 'renderer-fn)
- split)))))
+ (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))
(list
(gnc:make-html-table-cell/size
commit 241f1f927439b05dc3e9d4a350e7d7e8ef545063
Author: Christopher Lam <christopher.lck at gmail.com>
Date: Thu Jun 13 21:09:31 2019 +0800
[test-transaction] add csv test
runs report with csv output
test csv output matches expectation
diff --git a/gnucash/report/standard-reports/test/test-transaction.scm b/gnucash/report/standard-reports/test/test-transaction.scm
index 31a791caa..4e1efcdda 100644
--- a/gnucash/report/standard-reports/test/test-transaction.scm
+++ b/gnucash/report/standard-reports/test/test-transaction.scm
@@ -858,7 +858,39 @@
(list "$0.33" "$10.33" "-$9.67" "$1.00")
(get-row-col sxml #f 6))))
(test-end "subtotal table")
- ))
+
+ (test-begin "csv-export")
+ (test-assert "csv output is valid"
+ (let ((options (default-testing-options)))
+ (set-option! options "Accounts" "Accounts"
+ (list bank usd-bank gbp-bank gbp-income income expense))
+ (set-option! options "General" "Start Date"
+ (cons 'absolute (gnc-dmy2time64 01 01 1969)))
+ (set-option! options "General" "End Date"
+ (cons 'absolute (gnc-dmy2time64 31 12 1970)))
+ (set-option! options "Display" "Subtotal Table" #t)
+ (set-option! options "General" "Common Currency" #t)
+ (set-option! options "General" "Report Currency" foreign2)
+ (set-option! options "General" "Show original currency amount" #t)
+ (set-option! options "Sorting" "Primary Key" 'account-name)
+ (set-option! options "Sorting" "Primary Subtotal" #t)
+ (set-option! options "Sorting" "Secondary Key" 'date)
+ (set-option! options "Sorting" "Secondary Subtotal for Date Key" 'monthly)
+
+ (let* ((template (gnc:find-report-template trep-uuid))
+ (constructor (record-constructor <report>))
+ (report (constructor trep-uuid "bar" options #t #t #f #f ""))
+ (renderer (gnc:report-template-renderer template)))
+ ;; run the renderer, ignore its output. we'll query the csv export.
+ (renderer report #:export-type 'csv #:filename "/tmp/export.csv"))
+ (let ((f (open-file "/tmp/export.csv" "r")))
+ (let lp ((c (read-char f)) (out '()))
+ (if (eof-object? c)
+ (string=?
+ "\"from\",\"01/01/69\"\n\"to\",\"12/31/70\"\n\"Amount (GBP)\",2.15\n\"Amount\",3.0"
+ (reverse-list->string out))
+ (lp (read-char f) (cons c out)))))))
+ (test-end "csv-export")))
(define (reconcile-tests)
(let* ((env (create-test-env))
commit 497e18c36ed59873f175ab6d67e1a2f28f26894d
Author: Christopher Lam <christopher.lck at gmail.com>
Date: Fri May 3 13:19:08 2019 +0800
[trep-engine] allow csv export of grand-totals
This commit enables CSV export of split monetary columns and
grand-totals.
Sample CSV output:
"from","2018-01-01"
"to","2018-03-31"
"Debit",4270.88
"Credit",8314.44
* Note dates are iso-format strings.
* If a grand-total has multiple currencies, all will be listed
e.g. grand-total 100GBP + 50USD will show as: "Amount",100.0,50.0
* Dual subtotals (debit/credit) will not be exportable as CSVs because
they are not merged
diff --git a/gnucash/report/report-system/trep-engine.scm b/gnucash/report/report-system/trep-engine.scm
index b4947700b..65e716327 100644
--- a/gnucash/report/report-system/trep-engine.scm
+++ b/gnucash/report/report-system/trep-engine.scm
@@ -18,6 +18,7 @@
;; - add subtotal summary grid
;; - by default, exclude closing transactions from the report
;; - converted to module in 2019
+;; - CSV export, exports the report headers and totals
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
@@ -459,6 +460,46 @@ Credit Card, and Income accounts."))
(and (keylist-get-info (sortkey-list split-action?) sortkey 'split-sortvalue)
(not (keylist-get-info (sortkey-list split-action?) sortkey 'sortkey))))
+(define (lists->csv lst)
+ ;; converts a list of lists into CSV
+ ;; this function aims to follow RFC4180, and will pad lists to
+ ;; ensure equal number of items per row.
+ ;; e.g. '(("from" "01/01/2010")
+ ;; ("to" "31/12/2010")
+ ;; ("total" 23500 30000 25/7 'sym))
+ ;; will output
+ ;; "from","01/01/2010",,,
+ ;; "to","31/12/2010",,,
+ ;; "total",23500.0,30000.0,3.5714285714285716,sym
+ (define (string-sanitize-csv str)
+ (call-with-output-string
+ (lambda (port)
+ (display #\" port)
+ (string-for-each
+ (lambda (c)
+ (if (char=? c #\") (display #\" port))
+ (display c port))
+ str)
+ (display #\" port))))
+
+ (define max-items (apply max (map length lst)))
+
+ (define (strify obj)
+ (cond
+ ((not obj) "")
+ ((string? obj) (string-sanitize-csv obj))
+ ((number? obj) (number->string (exact->inexact obj)))
+ ((list? obj) (string-join
+ (map strify
+ (append obj
+ (make-list (- max-items (length obj)) #f)))
+ ","))
+ ((gnc:gnc-monetary? obj) (strify (gnc:gnc-monetary-amount obj)))
+ (else (object->string obj))))
+
+ (string-join (map strify lst) "\n"))
+
+
;;
;; Default Transaction Report
;;
@@ -1746,7 +1787,18 @@ be excluded from periodic reporting.")
(loop rest (not odd-row?) (1+ work-done)))))
- (values table grid)))
+ (let ((csvlist (cond
+ ((any (lambda (cell) (vector-ref cell 4)) calculated-cells)
+ ;; there are mergeable cells. don't return a list.
+ (N_ "CSV disabled for double column amounts"))
+
+ (else
+ (map
+ (lambda (cell coll)
+ (cons (vector-ref cell 0)
+ (coll 'format gnc:make-gnc-monetary #f)))
+ calculated-cells total-collectors)))))
+ (values table grid csvlist))))
;; grid data structure
(define (make-grid)
@@ -1840,7 +1892,8 @@ be excluded from periodic reporting.")
(define* (gnc:trep-renderer
report-obj #:key custom-calculated-cells empty-report-message
- custom-split-filter split->date split->date-include-false?)
+ custom-split-filter split->date split->date-include-false?
+ export-type filename)
;; the trep-renderer is a define* function which, at minimum, takes
;; the report object
;;
@@ -1854,6 +1907,7 @@ be excluded from periodic reporting.")
;; split->date returns #f. useful to include unreconciled splits in reconcile
;; report. it can be useful for alternative date filtering, e.g. filter by
;; transaction->invoice->payment date.
+ ;; #:export-type and #:filename - are provided for CSV export
(define options (gnc:report-options report-obj))
(define (opt-val section name)
@@ -2081,7 +2135,7 @@ be excluded from periodic reporting.")
(gnc:html-render-options-changed options))))
(else
- (let-values (((table grid)
+ (let-values (((table grid csvlist)
(make-split-table splits options custom-calculated-cells)))
(gnc:html-document-set-title! document report-title)
@@ -2115,6 +2169,30 @@ be excluded from periodic reporting.")
(gnc:html-document-add-object!
document (grid->html-table grid list-of-rows list-of-cols))))
+ (cond
+ ((and (eq? export-type 'csv)
+ (string? filename)
+ (not (string-null? filename)))
+ (let ((old-date-fmt (qof-date-format-get))
+ (dummy (qof-date-format-set QOF-DATE-FORMAT-ISO))
+ (infolist
+ (list
+ (list "from" (qof-print-date begindate))
+ (list "to" (qof-print-date enddate)))))
+ (qof-date-format-set old-date-fmt)
+ (if (list? csvlist)
+ (catch #t
+ (lambda ()
+ (with-output-to-file filename
+ (lambda ()
+ (display (lists->csv (append infolist csvlist))))))
+ (lambda (key . args)
+ ;; Translators: ~a error type, ~a filename, ~s error details
+ (let ((fmt (N_ "error ~a during csv output to ~a: ~s")))
+ (gnc:gui-error (format #f fmt key filename args)
+ (format #f (_ fmt) key filename args)))))
+ (gnc:gui-error csvlist (_ csvlist))))))
+
(unless (and subtotal-table?
(opt-val pagename-sorting optname-show-subtotals-only))
(gnc:html-document-add-object! document table)))))))
diff --git a/gnucash/report/standard-reports/income-gst-statement.scm b/gnucash/report/standard-reports/income-gst-statement.scm
index be947db4c..bd37cd1b6 100644
--- a/gnucash/report/standard-reports/income-gst-statement.scm
+++ b/gnucash/report/standard-reports/income-gst-statement.scm
@@ -280,4 +280,13 @@ for taxes paid on expenses, and type LIABILITY for taxes collected on sales.")
'name reportname
'report-guid "5bf27f249a0d11e7abc4cec278b6b50a"
'options-generator gst-statement-options-generator
- 'renderer gst-statement-renderer)
+ 'renderer gst-statement-renderer
+ 'export-types (list (cons "CSV" 'csv))
+ 'export-thunk (lambda (report-obj export-type file-name)
+ (gnc:trep-renderer
+ report-obj
+ #:custom-calculated-cells gst-calculated-cells
+ #:empty-report-message TAX-SETUP-DESC
+ #:custom-split-filter gst-custom-split-filter
+ #:export-type export-type
+ #:filename file-name)))
Summary of changes:
gnucash/report/report-system/trep-engine.scm | 103 +++++++++++++++++++--
.../standard-reports/income-gst-statement.scm | 11 ++-
.../standard-reports/test/test-transaction.scm | 34 ++++++-
3 files changed, 136 insertions(+), 12 deletions(-)
More information about the gnucash-changes
mailing list