Please see attached scheme report, specifically line 201. When configured to output "text", the script works. However, attempting to output an account balance fails without clear error messages. Would appreciate a more expert GNUCash scheme API developer diagnosing the fault, or explaining how to properly extract this information.
Goal is to create simple custom reports showing basic financial health statistics, such as net worth, working capital, cash buffer, and cash ratio. Net Worth = Assets - Liabilities Working Capital = (Assets > Current Assets) - Liabilities Cash Buffer = (Assets > Current Assets > Cash) - Liabilities Cash Ratio = (Assets > Current Assets > Cash) / Liabilities In the long-term, it might be even more efficient and versatile to add a simple command-line argument parser to the GNUCash binary, allowing access to C API functions through a variety of shell scripting languages.
;; -*-scheme-*- ;; Based upon built-in GNUCash reports. Same license as GNUCash. ;; ------------------------------------------------------------------ ;; Top-level definitions ;; ------------------------------------------------------------------ (define-module (gnucash report valuation)) (use-modules (gnucash main)) ;; FIXME: delete after we finish modularizing. (use-modules (gnucash gnc-module)) (use-modules (gnucash gettext)) (gnc:module-load "gnucash/report/report-system" 0) (gnc:module-load "gnucash/html" 0) ;for gnc-build-url ;; ------------------------------------------------------------------ ;; Define the Options for this report ;; ------------------------------------------------------------------ (define reportname (N_ "Valuation")) (define optname-report-title (N_ "Report Title")) (define opthelp-report-title (N_ "Title for this report.")) (define optname-party-name (N_ "Company name")) (define opthelp-party-name (N_ "Name of company/individual.")) (define optname-accounts (N_ "Accounts")) (define opthelp-accounts (N_ "Report on these accounts, if display depth allows.")) (define optname-date (N_ "Balance Sheet Date")) (define (options-generator) (let* ((options (gnc:new-options)) ;; This is just a helper function for making options. ;; See gnucash/src/app-utils/options.scm for details. (add-option (lambda (new-option) (gnc:register-option options new-option)))) (add-option (gnc:make-string-option gnc:pagename-general optname-report-title "a" opthelp-report-title (_ reportname))) (add-option (gnc:make-string-option gnc:pagename-general optname-party-name "b" opthelp-party-name (or (gnc:company-info gnc:*company-name*) ""))) ;; accounts to work on (add-option (gnc:make-account-list-option gnc:pagename-accounts optname-accounts "a" opthelp-accounts (lambda () (gnc:filter-accountlist-type (list ACCT-TYPE-BANK ACCT-TYPE-CASH ACCT-TYPE-CREDIT ACCT-TYPE-ASSET ACCT-TYPE-LIABILITY ACCT-TYPE-STOCK ACCT-TYPE-MUTUAL ACCT-TYPE-CURRENCY ACCT-TYPE-PAYABLE ACCT-TYPE-RECEIVABLE ACCT-TYPE-EQUITY ACCT-TYPE-INCOME ACCT-TYPE-EXPENSE ACCT-TYPE-TRADING) (gnc-account-get-descendants-sorted (gnc-get-current-root-account)))) #f #t)) ;; date at which to report balance (gnc:options-add-report-date! options gnc:pagename-general optname-date "c") ;; Set the accounts page as default option tab (gnc:options-set-default-section options gnc:pagename-accounts) options)) ;; ------------------------------------------------------------------ ;; Render the HTML document ;; ------------------------------------------------------------------ (define (document-renderer report-obj) (define (get-option pagename optname) (gnc:option-value (gnc:lookup-option (gnc:report-options report-obj) pagename optname))) (gnc:report-starting reportname) (let* ( (report-title (get-option gnc:pagename-general optname-report-title)) (company-name (get-option gnc:pagename-general optname-party-name)) (date-tp (gnc:timepair-end-day-time (gnc:date-option-absolute-time (get-option gnc:pagename-general optname-date)))) (date-secs (gnc:timepair->secs date-tp)) (accounts (get-option gnc:pagename-accounts optname-accounts)) ;; decompose the account list (split-up-accounts (gnc:decompose-accountlist accounts)) (asset-accounts (assoc-ref split-up-accounts ACCT-TYPE-ASSET)) (liability-accounts (assoc-ref split-up-accounts ACCT-TYPE-LIABILITY)) (income-expense-accounts (append (assoc-ref split-up-accounts ACCT-TYPE-INCOME) (assoc-ref split-up-accounts ACCT-TYPE-EXPENSE))) (equity-accounts (assoc-ref split-up-accounts ACCT-TYPE-EQUITY)) (trading-accounts (assoc-ref split-up-accounts ACCT-TYPE-TRADING)) (document (gnc:make-html-document))) ;; Wrapper to call gnc:html-table-add-labeled-amount-line! ;; with the proper arguments. (define (add-subtotal-line table pos-label neg-label signed-balance) (define allow-same-column-totals #t) (let* ((neg? (and signed-balance neg-label (gnc-numeric-negative-p (gnc:gnc-monetary-amount (gnc:sum-collector-commodity signed-balance report-commodity exchange-fn))))) (label (if neg? (or neg-label pos-label) pos-label)) (balance (if neg? (let ((bal (gnc:make-commodity-collector))) (bal 'minusmerge signed-balance #f) bal) signed-balance)) ) (gnc:html-table-add-labeled-amount-line! table (+ indent (* tree-depth 2) (if (equal? tabbing 'canonically-tabbed) 1 0)) "primary-subheading" (and (not allow-same-column-totals) balance use-rules?) label indent 1 "total-label-cell" (gnc:sum-collector-commodity balance report-commodity exchange-fn) (+ indent (* tree-depth 2) (- 0 1) (if (equal? tabbing 'canonically-tabbed) 1 0)) 1 "total-number-cell") ) ) ;; Return a commodity collector containing the sum of the balance of all of ;; the accounts on acct-list as of the time given in date-secs (define (account-list-balance acct-list date-secs) (let ((balance-collector (gnc:make-commodity-collector))) (for-each (lambda (x) (balance-collector 'add (xaccAccountGetCommodity x) (xaccAccountGetBalanceAsOfDate x date-secs))) acct-list) balance-collector)) (gnc:html-document-set-title! document (_ "Valuation")) (let* ((asset-balance #f) ) ;; sum assets (set! asset-balance (account-list-balance asset-accounts date-secs)) (gnc:html-document-add-object! document (gnc:make-html-text (gnc:html-markup-p (gnc:html-markup/format ;;(_ "Total Assets") #f asset-balance) (_ "The string option is %s.") (gnc:html-markup-b (account-list-balance asset-accounts date-secs)))) ;;(gnc:html-markup-b asset-balance))) ;;(gnc:html-markup-b "text"))) ) ) ) document)) ;; ------------------------------------------------------------------ ;; Define the actual report ;; ------------------------------------------------------------------ (gnc:define-report 'version 1 'name (N_ "zzValuation") 'report-guid "193ff0519a46d0a298afd24934bff3aa" 'menu-name (N_ "zzValuation") 'menu-tip (N_ "zzValuation") 'menu-path (list gnc:menuname-business-reports) 'options-generator options-generator 'renderer document-renderer)
_______________________________________________ gnucash-devel mailing list [email protected] https://lists.gnucash.org/mailman/listinfo/gnucash-devel
