From db47705f12f001f50adf1de686804cb072046580 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sat, 21 Oct 2023 14:39:35 +0800 Subject: [PATCH] [book-to-hledger.scm] export current book to hledger format. gnucash-cli book.gnucash --script book-to-hledger.scm > ~/.hledger.journal hledger bs hledger bs Balance Sheet 2023-10-20 || 2023-10-20 =====================++============ Assets || ---------------------++------------ Assets:Current:Bank || -168.3 USD ---------------------++------------ || -168.3 USD =====================++============ Liabilities || ---------------------++------------ ---------------------++------------ || =====================++============ Net: || -168.3 USD --- doc/examples/book-to-hledger.scm | 62 ++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 doc/examples/book-to-hledger.scm diff --git a/doc/examples/book-to-hledger.scm b/doc/examples/book-to-hledger.scm new file mode 100644 index 00000000000..a67e2263ee1 --- /dev/null +++ b/doc/examples/book-to-hledger.scm @@ -0,0 +1,62 @@ +;; this file is meant to be run via the gnucash-cli interface: --script simple-book-add-txn.scm +;; +;; gnucash-cli book.gnucash --script simple-book-add-txn.scm +;; + +(use-modules (gnucash core-utils)) +(use-modules (gnucash engine)) +(use-modules (gnucash app-utils)) +(use-modules (gnucash report)) +(use-modules (ice-9 match)) + +(define iso-date (qof-date-format-get-string QOF-DATE-FORMAT-ISO)) +(define book (gnc-get-current-book)) +(define root (gnc-get-current-root-account)) +(define query (qof-query-create-for-splits)) +(qof-query-set-book query (gnc-get-current-book)) +(xaccQueryAddAccountMatch query (gnc-account-get-descendants root) QOF-GUID-MATCH-ANY QOF-QUERY-AND) +(qof-query-set-sort-order + query + (list SPLIT-TRANS TRANS-DATE-POSTED) + '() + (list QUERY-DEFAULT-SORT)) + +(define (dump-transaction trans) + (format #t "~a ~a\n" + (gnc-print-time64 (xaccTransGetDate trans) iso-date) + (xaccTransGetDescription trans)) + (define (split->account s) + (gnc-account-get-full-name (xaccSplitGetAccount s))) + (define (split->amount s) + (format #f "~a ~a" + (exact->inexact (xaccSplitGetAmount s)) + (gnc-commodity-get-mnemonic (xaccAccountGetCommodity (xaccSplitGetAccount s))))) + (define max-width + (let lp ((splits (xaccTransGetSplitList trans)) (maximum 0)) + (match splits + (() (+ maximum 2)) + ((s . rest) + (lp rest (max maximum (+ (string-length (split->account s)) + (string-length (split->amount s))))))))) + (for-each + (lambda (s) + (define txn (xaccSplitGetParent s)) + (define acc-name (split->account s)) + (define amt-str (split->amount s)) + (format #t " ~a~a~a\n" + acc-name + (make-string (- max-width (string-length acc-name) (string-length amt-str)) #\space) + amt-str)) + (xaccTransGetSplitList trans))) + +(define split-has-no-account? (compose null? xaccSplitGetAccount)) + +(let lp ((splits (xaccQueryGetSplitsUniqueTrans query))) + (newline) + (match splits + (() #f) + (((? split-has-no-account?) . rest) (lp rest)) + ((split . rest) (dump-transaction (xaccSplitGetParent split)) (lp rest)))) + +(qof-query-destroy query) +(gnc-clear-current-session)