Skip to content

Commit

Permalink
[reports] use gnc-print-time64
Browse files Browse the repository at this point in the history
removes the need to store and reset the current QofDateFormat via
qof-date-format-set and qof-date-format-get.

also fix test-transaction csv output test.
  • Loading branch information
christopherlam committed Sep 2, 2020
1 parent 094b4ba commit f503f70
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 58 deletions.
10 changes: 5 additions & 5 deletions gnucash/report/reports/standard/balance-forecast.scm
Expand Up @@ -169,6 +169,7 @@ date point, a projected minimum balance including scheduled transactions."))
price currency
(gnc:accounts-get-commodities accounts #f)
to-date #f #f))
(iso-date (qof-date-format-get-string QOF-DATE-FORMAT-ISO))
(accounts-balancelist
(map
(lambda (acc)
Expand Down Expand Up @@ -281,11 +282,10 @@ date point, a projected minimum balance including scheduled transactions."))
(gnc:html-chart-set-y-axis-label!
chart (gnc-commodity-get-mnemonic currency))
;; Set series labels
(let ((old-fmt (qof-date-format-get)))
(qof-date-format-set QOF-DATE-FORMAT-ISO)
(gnc:html-chart-set-data-labels!
chart (map qof-print-date (map cadr intervals)))
(qof-date-format-set old-fmt))
(gnc:html-chart-set-data-labels!
chart (map (lambda (data)
(gnc-print-time64 (cadr data) iso-date))
intervals))

;; Set currency symbol
(gnc:html-chart-set-currency-iso!
Expand Down
9 changes: 4 additions & 5 deletions gnucash/report/reports/standard/category-barchart.scm
Expand Up @@ -26,6 +26,7 @@
(define-module (gnucash reports standard category-barchart))
(use-modules (srfi srfi-1))
(use-modules (srfi srfi-9))
(use-modules (srfi srfi-26))
(use-modules (ice-9 match))
(use-modules (gnucash engine))
(use-modules (gnucash utilities))
Expand Down Expand Up @@ -640,8 +641,7 @@ developing over time"))

(cond
((eq? export-type 'csv)
(let ((old-fmt (qof-date-format-get)))
(qof-date-format-set QOF-DATE-FORMAT-ISO)
(let ((iso-date (qof-date-format-get-string QOF-DATE-FORMAT-ISO)))
(gnc:html-document-set-export-string
document
(gnc:lists->csv
Expand All @@ -665,9 +665,8 @@ developing over time"))
(if (pair? (cdr all-data))
(list (apply gnc:monetary+ row))
'())))
(map qof-print-date dates-list)
(apply zip (map cadr all-data))))))
(qof-date-format-set old-fmt)))))
(map (cut gnc-print-time64 <> iso-date) dates-list)
(apply zip (map cadr all-data))))))))))

;; else if empty data
(gnc:html-document-add-object!
Expand Down
8 changes: 3 additions & 5 deletions gnucash/report/reports/standard/net-charts.scm
Expand Up @@ -444,19 +444,17 @@

(cond
((eq? export-type 'csv)
(let ((old-fmt (qof-date-format-get)))
(qof-date-format-set QOF-DATE-FORMAT-ISO)
(let ((iso-date (qof-date-format-get-string QOF-DATE-FORMAT-ISO)))
(gnc:html-document-set-export-string
document
(gnc:lists->csv
(cons (if inc-exp?
(map G_ '("Date" "Income" "Expense" "Net Profit"))
(map G_ '("Date" "Assets" "Liabilities" "Net Worth")))
(map list
(map qof-print-date dates-list)
(map (cut gnc-print-time64 <> iso-date) dates-list)
minuend-balances
subtrahend-balances difference-balances))))
(qof-date-format-set old-fmt)))))
subtrahend-balances difference-balances))))))))
(gnc:html-document-add-object!
document
(gnc:html-make-empty-data-warning
Expand Down
57 changes: 26 additions & 31 deletions gnucash/report/reports/standard/test/test-transaction.scm
Expand Up @@ -885,37 +885,32 @@
(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 ((call-with-input-file "/tmp/export.csv"))
(lambda (f)
(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))))))))
(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))
(document (renderer report #:export-type 'csv)))
(test-assert "csv output has no export error"
(not (gnc:html-document-export-error document)))
(test-equal "csv output is valid"
"\"from\",\"1969-01-01\"\n\"to\",\"1970-12-31\"\n\"Amount (GBP)\",2.15\n\"Amount\",3.0"
(gnc:html-document-export-string document))))
(test-end "csv-export")))

(define (reconcile-tests)
Expand Down
23 changes: 11 additions & 12 deletions gnucash/report/trep-engine.scm
Expand Up @@ -2266,20 +2266,19 @@ warning will be removed in GnuCash 5.0"))

(cond
((eq? export-type 'csv)
(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)
(cond
((list? csvlist)
(cond
((pair? csvlist)
(let ((iso-date (qof-date-format-get-string QOF-DATE-FORMAT-ISO)))
(gnc:html-document-set-export-string
document (lists->csv (append infolist csvlist))))
document
(lists->csv
(cons*
`("from" ,(gnc-print-time64 begindate iso-date))
`("to" ,(gnc-print-time64 enddate iso-date))
csvlist)))))

(else
(gnc:html-document-set-export-error document csvlist)))))))))))
(else
(gnc:html-document-set-export-error document csvlist))))))))))

(gnc:report-finished)

Expand Down

0 comments on commit f503f70

Please sign in to comment.