;; -*-scheme-*- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; This program is free software; you can redistribute it and/or ;; modify it under the terms of the GNU General Public License as ;; published by the Free Software Foundation; either version 2 of ;; the License, or (at your option) any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with this program; if not, contact: ;; ;; Free Software Foundation Voice: +1-617-542-5942 ;; 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 ;; Boston, MA 02110-1301, USA gnu@gnu.org ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; This is a sample guile report generator for GnuCash. ;; It illustrates the basic techniques used to create ;; new reports for GnuCash. (define-module (gnucash report my-piecash-report-simple)) (use-modules (gnucash main)) ;; FIXME: delete after we finish modularizing. (use-modules (gnucash gnc-module)) (use-modules (gnucash gettext)) (use-modules (gnucash core-utils)) ;; 'debug is deprecated and unused since guile 2 (cond-expand (guile-2 ) (else (debug-enable 'debug))) (debug-enable 'backtrace) (gnc:module-load "gnucash/report/report-system" 0) (gnc:module-load "gnucash/html" 0) ;for gnc-build-url ;; This function will generate a set of options that GnuCash ;; will use to display a dialog where the user can select ;; values for your report's parameters. (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)))) ;; This is a number range option. The user can enter a number ;; between a lower and upper bound given below. There are also ;; arrows the user can click to go up or down, the amount changed ;; by a single click is given by the step size. (add-option (gnc:make-number-range-option (N_ "Hello, World!") (N_ "Number Option") "ee" (N_ "This is a number option.") 1500.0 ;; default 0.0 ;; lower bound 10000.0 ;; upper bound 2.0 ;; number of decimals 0.01 ;; step size )) (gnc:options-set-default-section options "Hello, World!") options)) ;; This is the rendering function. It accepts a database of options. (define (python-renderer report-obj) ;; These are some helper functions for looking up option values. (define (get-op section name) (gnc:lookup-option (gnc:report-options report-obj) section name)) (define (op-value section name) (gnc:option-value (get-op section name))) ;; call the python script ;; TODO: find a way to pipe the option to the python script (as yaml/json ?) ;; TODO: find a way send the URI of the book to the python script (let ((num-val (op-value "Hello, World!" "Number Option")) ) (gnc:run-outside-program "simple_report.py") ) ) (define (gnc:run-outside-program py-script) (let ((program '()) (from-child #f)) (define (start-program) (set! program (gnc-spawn-process-async (list "python" (gnc-build-dotgnucash-path py-script)) #t))) (define (read-port port) (let iter ((result '()) (chr (read-char port))) (if (eof-object? chr) (list->string result) (iter (append result (list chr)) (read-char port))))) (define (get-sources) (let ((results #f)) (set! from-child (fdes->inport (gnc-process-get-fd program 1))) (catch #t (lambda () (set! results (read-port from-child)) results) (lambda (key . args) key)))) (define (kill-program) (if (not (null? program)) (gnc-detach-process program #t))) (dynamic-wind start-program get-sources kill-program))) ;; Here we define the actual report with gnc:define-report (gnc:define-report ;; The version of this report. 'version 1 ;; The name of this report. This will be used, among other things, ;; for making its menu item in the main menu. You need to use the ;; untranslated value here! 'name (N_ "Piecash simple report") ;; The GUID for this report. This string should be unique, set once ;; and left alone forever after that. In theory, you could use any ;; unique string, even a meaningful one (!) but its probably best to ;; use a true uuid. Get them from `uuidgen | sed -e s/-//g` and paste ;; the results in here. You must make a new guid for each report! 'report-guid "222d78ec92854403bf76e20a36d24ccc" ;; The name in the menu ;; (only necessary if it differs from the name) ;; 'menu-name (N_ "Sample Report with Examples") ;; A tip that is used to provide additional information about the ;; report to the user. 'menu-tip (N_ "A simple test for a piecash report.") ;; A path describing where to put the report in the menu system. ;; In this case, it's going under the utility menu. 'menu-path (list gnc:menuname-utility) ;; The options generator function defined above. 'options-generator options-generator ;; The rendering function defined above. 'renderer python-renderer)