sdementen / gnucash-utilities

Set of python scripts to work with GnuCash books
Other
15 stars 3 forks source link

commodities report parameter #8

Open alensiljak opened 6 years ago

alensiljak commented 6 years ago

For (what is to become) the security analysis report (current file), there is a need for the user to select a commodity (stock/fund) value in the parameters. Is there a way to display the commodities in the report parameters and automatically limit the selection to available commodities (securities)?

Example: in the investment.gnucash database, the user should be able to see and select VEUR fund from the list of available commodities.

I saw in GnuCash reports the account selector that displays all the accounts. I'd hope for a similar selector which would apply only to commodities. This would be similar (or same) as the price update window.

If there is no such option available, would it be possible to fill a dropdown menu with the commodities retrieved via piecash? Any suggestions?

sdementen commented 6 years ago

Have a look at the file https://github.com/sdementen/gnucash-utilities/blob/develop/piecash_utilities/report/options.py where report options are defined. For the choice of commodity, it would be necessary to create a new option CommodityOption with the render_scheme method that return the appropriate Scheme code for the option. For the latter, you can look at an existing gnucash report to see what is needed

sdementen commented 6 years ago

You can also look at the section The Options-Generator in https://wiki.gnucash.org/wiki/Custom_Reports for the options to use

alensiljak commented 6 years ago

Yep, I saw the options.py earlier. Thanks for the confirmation that a new type needs to be added.

Great hint! There is a gnc:make-commodity-option in .scm. I guess that may be used to pick commodities. Will look into that after I create a few simple (but necessary) multi-column reports first.

A reference for how to use the option in Scheme (link), line 401.

alensiljak commented 6 years ago

:'( Reading Scheme code makes me wanna cry.

alensiljak commented 6 years ago

Below is the relevant code. Now I just need to learn more about Scheme and see what exactly needs to be output from Python.

;; commodity options use a specialized widget for entering commodities
;; in the GUI implementation.
(define (gnc:make-commodity-option
         section
         name
         sort-tag
         documentation-string
         default-value)

  (define (commodity->scm commodity)
    (if (string? commodity)
        (list 'commodity-scm
              GNC_COMMODITY_NS_CURRENCY
              commodity)
        (list 'commodity-scm
              (gnc-commodity-get-namespace commodity)
              (gnc-commodity-get-mnemonic commodity))))

  (define (scm->commodity scm)
    (gnc-commodity-table-lookup
     (gnc-commodity-table-get-table (gnc-get-current-book))
     (cadr scm) (caddr scm)))

   (let* ((value (commodity->scm default-value))
          (value->string (lambda ()
                           (string-append "'" (gnc:value->string value)))))
     (gnc:make-option
      section name sort-tag 'commodity documentation-string
      (lambda () (scm->commodity value))
      (lambda (x) (if (and (pair? x) (eqv? (car x) 'commodity-scm))
                      (set! value x)
                      (set! value (commodity->scm x))))
      (lambda () default-value)
      (gnc:restore-form-generator value->string)
      (lambda (f p) 
        (kvp-frame-set-slot-path-gslist f (cadr value) (append p '("ns")))
        (kvp-frame-set-slot-path-gslist f (caddr value) (append p '("monic"))))
      (lambda (f p)
        (let ((ns (kvp-frame-get-slot-path-gslist f (append p '("ns"))))
              (monic (kvp-frame-get-slot-path-gslist f (append p '("monic")))))
          (if (and ns monic (string? ns) (string? monic))
              (set! value (list 'commodity-scm ns monic)))))
      (lambda (x) (list #t x))
      #f #f #f #f)))
alensiljak commented 6 years ago

Oh, didn't notice this one before. Could be a better option. It seems more concrete and exactly what I'm after.

alensiljak commented 6 years ago

It is a bit sad that it takes less time to write the full portfolio balance report in Python than to figure out how to pass a list of securities to a report. :S