Difference between revisions of "Custom Reports"

From GnuCash
Jump to: navigation, search
(The Options-Generator)
(The Options-Generator)
Line 267: Line 267:
 
*call <code>gnc:register-option</code>
 
*call <code>gnc:register-option</code>
  
Simply read through the <code>define (options-generator)</code> statement in <code>hello-world.scm</code> and copy what you need.
+
The <code>gnc:...</code>-procedures to create this database and to fill it with values are contained in <code>gnucash/src/app-utils/options.scm</code>.
  
 +
<code>gnucash/src/report/utility-reports/hello-world.scm</code> presents a nice selection of example option definitions of following types:
 +
<pre>
 +
gnc:make-simple-boolean-option
 +
gnc:make-multichoice-option
 +
gnc:make-string-option
 +
gnc:make-date-option
 +
gnc:make-number-range-option
 +
gnc:make-color-option
 +
gnc:make-account-list-option
 +
gnc:make-list-option
 +
</pre>
 +
 +
On top of these, you find
 +
<pre>
 +
gnc:make-text-option
 +
gnc:make-font-option
 +
gnc:make-currency-option
 +
gnc:make-budget-option
 +
gnc:make-commodity-option
 +
gnc:make-complex-boolean-option
 +
gnc:make-pixmap-option
 +
gnc:make-account-list-limited-option
 +
gnc:make-account-sel-option
 +
gnc:make-account-sel-limited-option
 +
gnc:make-multichoice-callback-option
 +
gnc:make-radiobutton-option
 +
gnc:make-radiobutton-callback-option
 +
gnc:make-internal-option
 +
gnc:make-query-option
 +
gnc:make-dateformat-option
 +
</pre>
 +
in <code>gnucash/src/app-utils/options.scm</code>.
 +
 +
Also, <code>gnucash/src/business/business-utils/business-options.scm</code> provides some options
 +
<pre>
 +
gnc:make-invoice-option
 +
gnc:make-customer-option
 +
gnc:make-vendor-option
 +
gnc:make-employee-option
 +
gnc:make-owner-option
 +
gnc:make-taxtable-option
 +
gnc:make-counter-option
 +
gnc:make-counter-format-option
 +
</pre>
 +
 +
In general, the syntax for creating an option is
 +
<pre>
 +
(define (gnc:make-<__type__>-option
 +
        section                      ;; the tab-string in the option dialog
 +
        name                        ;; the text for this option in the option dialog
 +
        sort-tag                    ;; used to define the order in which the options are listed
 +
        documentation-string        ;; help string shown when the mouse moved over the option
 +
        default-value
 +
        <__optioinal type specific parameters__>)
 +
</pre>
 +
 +
Note that <code>section</code> is free to be any text string. If section-string has not been used before, a new tab with this name will be created automatically. Nevertheless, note that <code>gnucash/src/report/report-system/report.scm</code> provides three default names. Re-use these before creating new names:
 +
<pre>
 +
(define gnc:pagename-general (N_ "General"))
 +
(define gnc:pagename-accounts (N_ "Accounts"))
 +
(define gnc:pagename-display (N_ "Display"))
 +
</pre>
 +
 +
For a start, simply read through the <code>define (options-generator)</code> statement in <code>hello-world.scm</code> and copy what you need.
  
 
Nice to know:
 
Nice to know:
  
* The <code>gnc:...</code>-procedures to create this database and to fill it with values are contained in <code>gnucash/src/app-utils/options.scm</code>.
 
 
* The ''General'' option group does not need to be defined, it is part of the report definition per default. If you run the above minimum report definition, the ''General'' option group will automatically include a text field for the <code>Report Name</code>, a selection box for the <code>Stylesheet</code>, and a button to <code>Reset defaults</code>.
 
* The ''General'' option group does not need to be defined, it is part of the report definition per default. If you run the above minimum report definition, the ''General'' option group will automatically include a text field for the <code>Report Name</code>, a selection box for the <code>Stylesheet</code>, and a button to <code>Reset defaults</code>.
  

Revision as of 09:46, 5 May 2012

Custom Reports in GnuCash

The term Custom Reports has several contexts in GnuCash:

  • Customizing settings for standard reports ([[1]])
  • Combining selected standard reports into one view ([[2]])
  • Customized output formart for reports ([[3]])
  • Reports written in Scheme which are not delivered as part of the standard reports (this page)

If you are not up into coding, you should check the

  • report options
  • stylesheets and
  • stylesheet options and
  • file properties.

Possibly there are ways to get, what you desired.

If you are ready for coding (and even already firm in Scheme and Guile) then this is the place to contribute.

Introduction

Unfortunately, at the present time writing a custom report for GnuCash requires a little bit of hacking. This wiki page should contain some information to help you get started, since there is no official documentation.

In the GnuCash project the reports are coded in Scheme, not in C. This is one of the pre-requisites to make the behaviour ready for customization. It leads to the drawback that one must learn yet another programming language (if not already familiar with it) but saves the effort to get into the issue how to build GnuCash from source.

Guile is used as interface to get the Scheme code executed during runtime. Again, if not already familiar with it, this is the second piece of work that one has to swallow.

Last but not least the API for that sort of thing is not formally specified.

Still reading? Not yet discouraged? Good on you! Because there is lots of help available.

First of all there is good educational stuff available on the web to get you into the topic of Scheme and Guile. Secondly there is a nice hello-world.scm example report specifically written to show you the base layout of a GnuCash report. And finally, this Wiki page might give you some guidence when doing the first steps.

Getting Started

Get to know Scheme

If you are new to Scheme it is not necessary to read and understand each bit of it right now. But you should at least have gone through the Getting Started section.

Get to know Guile

This comes with a marvellous Tutorial. You don't need this for writing the reports themselves, but it gives a clou of how reports are working in GnuCash and by that hopefully a good motivation to go and take the learning curve for Scheme.

Where to find existing reports

The location varies a little with different operating systems:

  • On BSD look in /usr/local/share/gnucash/guile-modules/gnucash/report
  • On OpenSuse up to 10.x look in /opt/gnome/share/gnucash/guile-modules/gnucash/report, more recent versions use /usr/share/gnucash...
  • On Gentoo, Ubuntu, Foresight look in /usr/share/gnucash/guile-modules/gnucash/report
  • On Windows look in C:\Program Files\gnucash\share\gnucash\guile-modules\gnucash\report
  • On OSX (Gnucash.app, not Fink or MacPorts), look in Gnucash.app/Contents/Resources/share/gnucash/guile-modules/gnucash/report)

Read the hello-world.scm report

hello-world.scm is specially written to demonstrate the basic structure of a report. However, don't get too confused by the huge number of options which are shown in that report - those were just added to show off the variety of options which are available inside gnucash.

By looking at the same time at the result of this report, you will easily get the clou out of it. You find this report in GnuCash under

  • Reports
    • Sample & Custom
      • Sample Report with Examples

account-piechart

As another example, here's how the file src/reports/standard-reports/account-piechart.scm works (line numbers for SVN r20800 version): There are two long Scheme functions: The first one is "option-generator" starting in line 84 which defines, well, the options for this report. The second one is "piechart-renderer" (line 191ff) and contains the code that does the actual calculation: The list of values that should be shown is calculated by the function call in line 377, using the selected accounts in the variable "topl-accounts". That function "traverse-accounts" is defined in line 336 and it is a recursive function that calls itself in line 349 to traverse through all children accounts of the account tree. For a single account, in line 346 it calls "profit-fn" which gets the balance amount of one single account. You can see that in "profit-fn"'s definition in line 253, which obtains the account balance either in one interval or at a given date. The function there "gnc:account-get-comm-balance-at-date" is defined in the file src/reports/report-system/html-utilities.scm, where it finally will call the functions from the C API about QofQuery. It will create a query for the account balance at the given date.

Well, long story short, this calculation is rather involved. Sorry.

Setup a prototype report

Even if you havn't ever written a report before, and you can't come up with a shiny track record of great Scheme experience, setting up a prototype at this moment will have the benefits getting productinve after some time of pure theory.

The beginners amongst us might not have a final picture of what report to write. No worries, take the hello-world.scm report source file, store it in your preferred working directory, and rename it to a unique name, say my-world.scm. As, for sure, you already have read through this report, you know that this report is very wordy, giving a lot of text printouts. These can be used to easily see your own first changes on the screen once this prototype is loaded into GnuCash, and even better, it will let you differentiate between the my-world.scm that you have loaded and the hello-world.scm that is available in GnuCash by default (see previous section). So feel free to implement some fancy printout changes.

Prerequisites

There are two edits that you have to do in your report source file before loading the report into GnuCash:

  • To avoid symbol definition conflicts with other reports, make sure your report code begins with a define-module statement:
    (define-module (gnucash report unique-report-name))
    like for example
    (define-module (gnucash report my-world-report))
  • To enable GnuCash to register your report into the Report menu, go to the end of the code and update the gnc:define-report statement:
    • set the name that will be shown in the menu
      'name (N_ "My World")
      and further down below comment out the menu-name line
      ;;'menu-name (N_ "Sample Report with Examples")
    • update the tool tip
      'menu-tip (N_ "Unstable. Used for Testing.")
    • make sure your report has a unique name (for GnuCash 2.2.x and before) or a unique id (GnuCash 2.3.x and later). If you need some newly generated unique id, run the program gnucash-make-guids which is being installed by gnucash as well. Copy the newly generated ID into
      'report-guid "copy-here-the-new-generated-unique-ID"

There are two ways to load a report at startup of GnuCash: from a user account, or from the installation tree. You will likely want to use the user account approach.

Load the report from a user account

This method is used if you edit your report source file in a working directory.

In your settings folder

Operating System Setting Folder
Linux/Unix .gnucash in your home directory
Windows Vista and newer C:\Users\YourName\.gnucash
Windows XP C:\Documents and Settings\YourName\.gnucash
OSX Bundle (Gnucash.app) Library/Applications Defaults/Gnucash or Library/Applications Support/Gnucash (Lion) in your home folder

edit config.user to add a line of the form:

    (load "/path/to/my/personal/report.scm") ;; Linux and OS X
    (load "X:\\path\\to\\my\\personal\\report.scm")  ;; Windows

If this file is not yet found in your settings folder, simply create it.

Note: Without a leading '/', all paths are taken relative to your settings folder (see below).

Note: On Windows, double backslashes are required since the backslash is a special character and must be escaped.

Caution: For older versions of GnuCash (before 2.1.x ?) load-from-path was suggested to load reports instead of load, but this is not compatible with the module definition that is now found in all reports. Using load-from-path on a report with module defninition will cause a crash

To load your report, you must restart GnuCash. You will have to do this every time you change a report.

Thanks to DeanCording for clarifying the define-module/load bits

Load the report from the installed report directory

Alternatively, you can copy the report to the installed report directory. Make sure you have included a module definition as shown above. In gnucash-2.3.x or higher, all *.scm files from the directory guile-modules/gnucash/report/standard-reports/ will be loaded automatically. This applies only to the standard-reports/ sub-directory, not to any of the other directories with reports. Hence, the easiest way to have your report available to gnucash is to copy your *.scm file into that directory.

Then restart GnuCash. You will have to do this every time you change the report definition file.

Alternatively (or for gnucash versions before 2.3.0), you can copy your *.scm file to one of the other report definition directories, but then you have to edit the appropriate Scheme file to register your report with the reporting system. You will have to register the report in one of the following places, depending on which submenu you want it to appear.

  • Standard Reports: standard-reports.scm
  • Business Reports: business-reports.scm
  • Utility Reports: utility-reports.scm

Then restart GnuCash. You will have to do this every time you change a report.

Debugging your report

There is not really a debugger available for guile, but you can have your code write debug messages to help you trace your code.

  • Start GnuCash like
    gnucash --debug --log gnc.scm=debug
    to enable debugging messages.
  • Add debugging messages with the (gnc:debug) variable, e.g. (gnc:debug "here I am").
  • There are also (gnc:msg), (gnc:warn) and (gnc:error) functions; see src/scm/main.scm for more details.
  • Messages will be logged to /tmp/gnucash.trace by default, you can change this with --logto (try 'stderr' or 'stdout').

Development Environment

Programming tools

Any reasonable source/text editor, including emacs and vi, should provide basic syntax support for source-code editing. One of the most basic and important for editings scheme/lisp sources is parenthesis matching. Other scheme environments (such as drScheme) might also be useful for editing the sources, but note that they might allow constructs that are not valid in guile.

Simple Proposal for Linux

This is a simple proposal for a development environment in Linux. Assume

  • there is a working directory ~/GnuCash/CustomReports in your home directory.
  • the new report is stored in the test00.scm file

Here is the proposal how to setup up your environment

cd ~/GnuCash/CustomReports
ls -s test00.scm prototype.scm
cat > config.user << "EOF"
(load "~/GnuCash/CustomReport/prototype.scm")
EOF
cd ~/.gnucash
ls -s ~/GnuCash/CustomReport/config.user
uuidgen | sed -e s/-//g

Prepare test00.scm to include

  • the define-module statement
(define-module (gnucash report prototype-report))
  • following updates in the gnc:define-report statement
'name (N_ "Prototype")
;;'menu-name (N_ "Sample Report with Examples")
'menu-tip (N_ "Unstable. Used for Testing.")
'report-guid "the-new-generated-unique-ID"

See also the minimum report defintion in the #Designing new Reports chapter.

By this you can

  • use test00.scm as a template test report and use the lines as described above without further changes
  • collect all our test reports as e.g. testXX.scm files in this directory
  • switch between them by making the prototype.scm point to the desired report
  • restart GnuCash
  • always find the report under Reports->Sample & Custom Report->Prototype

Finally, you should also extract a GnuCash source tar ball in ~/GnuCash/CustomReport/ to have access to the Scheme files referenced in this page.

Designing new Reports

In general, reports are devided into three sections

  • the Options-Generator (define the options for the report)
  • the Report-Renderer (create the HTML report)
  • the gnc:define-report statement (make the report available in GnuCash)

This is the minimum defintion needed for a new GnuCash report:

;; -*-scheme-*-

;; This is a minimum report definition in GnuCash.
;; It illustrates the the minimum definitions needed to create
;; a new GnuCash report.
;; It will create an empty page with heading 'Prototype'.
;; To be used as template.

;; ------------------------------------------------------------------
;; Top-level definitions
;; ------------------------------------------------------------------

(define-module (gnucash report prototype))
(use-modules (gnucash gnc-module))
(gnc:module-load "gnucash/report/report-system" 0)

;; ------------------------------------------------------------------
;; Define the Options for this report
;; ------------------------------------------------------------------

(define (options-generator)    
  (let* ((options (gnc:new-options)))         
    options))

;; ------------------------------------------------------------------
;; Render the HTML document
;; ------------------------------------------------------------------

(define (document-renderer report-obj)
  (let ((document (gnc:make-html-document)))
      document))

;; ------------------------------------------------------------------
;; Define the actual report
;; ------------------------------------------------------------------

(gnc:define-report
 'version 1
 'name (N_ "Prototype")
 'report-guid "<copy-here-new-created-report-ID>"
 'menu-tip (N_ "Unstable. Used for Testing.")
 'menu-path (list gnc:menuname-utility)
 'options-generator options-generator
 'renderer document-renderer)


The Options-Generator

Each report takes responsibility to build up an own database that holds the options for the report.

Usually it is a good habit to copy the relavant code from the hello-world.scm. In there you will find the database initializing

  • call to gnc:new-options

which will

  • create the container called options

which will later on be filled by

  • calling the help function add-option

which is a nice wrapper to eventually

  • call gnc:register-option

The gnc:...-procedures to create this database and to fill it with values are contained in gnucash/src/app-utils/options.scm.

gnucash/src/report/utility-reports/hello-world.scm presents a nice selection of example option definitions of following types:

gnc:make-simple-boolean-option
gnc:make-multichoice-option
gnc:make-string-option
gnc:make-date-option
gnc:make-number-range-option
gnc:make-color-option
gnc:make-account-list-option
gnc:make-list-option

On top of these, you find

gnc:make-text-option
gnc:make-font-option
gnc:make-currency-option
gnc:make-budget-option
gnc:make-commodity-option
gnc:make-complex-boolean-option
gnc:make-pixmap-option 
gnc:make-account-list-limited-option
gnc:make-account-sel-option
gnc:make-account-sel-limited-option
gnc:make-multichoice-callback-option
gnc:make-radiobutton-option
gnc:make-radiobutton-callback-option
gnc:make-internal-option
gnc:make-query-option
gnc:make-dateformat-option

in gnucash/src/app-utils/options.scm.

Also, gnucash/src/business/business-utils/business-options.scm provides some options

gnc:make-invoice-option
gnc:make-customer-option
gnc:make-vendor-option
gnc:make-employee-option
gnc:make-owner-option
gnc:make-taxtable-option
gnc:make-counter-option
gnc:make-counter-format-option

In general, the syntax for creating an option is

(define (gnc:make-<__type__>-option
         section                      ;; the tab-string in the option dialog
         name                         ;; the text for this option in the option dialog
         sort-tag                     ;; used to define the order in which the options are listed
         documentation-string         ;; help string shown when the mouse moved over the option
         default-value
         <__optioinal type specific parameters__>)

Note that section is free to be any text string. If section-string has not been used before, a new tab with this name will be created automatically. Nevertheless, note that gnucash/src/report/report-system/report.scm provides three default names. Re-use these before creating new names:

(define gnc:pagename-general (N_ "General"))
(define gnc:pagename-accounts (N_ "Accounts"))
(define gnc:pagename-display (N_ "Display"))

For a start, simply read through the define (options-generator) statement in hello-world.scm and copy what you need.

Nice to know:

  • The General option group does not need to be defined, it is part of the report definition per default. If you run the above minimum report definition, the General option group will automatically include a text field for the Report Name, a selection box for the Stylesheet, and a button to Reset defaults.

The Report-Renderer

Define how to apply the options (the actual report contents).

The overall goal of the Report-Renderer is to create an HTML-document and fill it in with the reports results to display it out to the user.

Similar as with the Options-Generator, hello-world.scm gives you a great introduction how this works in general.

The gnc:...-procedures are found in the src/report/report-system/html-*.scm source files.


The gnc:define-report Statement

If you want to follow the #Simple Proposal for Linux to setup a simple development environment then you need updates to this section only

  • once that you set up the development environment
  • once that your report is ready, fully tested, and shall become part of your standard reports

And again, hello-world.scm already proposes how you should proceed in the latter case

[...] 
to contribute your brand new, totally cool report, consult the mailing list "mailto:gnucash-devel@gnucash.org"
[...]

The GnuCash API

The GnuCash application programming interface is quite intimidating, unfortunately. Luckily, for the reports, most of the stuff needed can be found in the scheme sources. Here some random examples of handy procedures (paths shown refer to the location in a source tarball; might be copied into the location as above in your installation):

  • create a monetary object: gnc:make-gnc-monetary src/engine/gnc-numeric.scm
  • get the amount of a monetary object: gnc:gnc-monetary-amount src/engine/gnc-numeric.scm
  • get an account name: gnc-account-get-name src/engine/swig-engine.c
  • create a commodity collector: gnc:make-commodity-collector src/report/report-system/report-utilities.scm

Watch out for the gnucash version in use: In 2.0.x, the Scheme wrappers were created by G-WRAP, started with gnc: (with a colon) and were written into scheme files of the name gw-something.scm. In 2.1.x and later, the Scheme wrappers are created by SWIG, start with gnc- (a dash, not a colon) and were written into C files of the name swig-something.c. Please keep that in mind if the old documentation points you towards functions like these: Get an account name: gnc:account-get-name src/engine/gw-engine-spec.scm - such a function would now be called gnc-account-get-name and would be found in src/engine/engine.i or the generated file src/engine/swig-engine.c.


Other Resources

One other way to create custom reports outside GnuCash, but directly from GnuCash data, is to use Ledger-CLI.

Old GnuCash Report Information

(may be very out of date)

Learning Scheme