Reports - getting numeric value from commodity collector

Darryl Cousins darryl at darrylcousins.net.nz
Fri Nov 25 22:42:02 EST 2005


Hi Derek,

That's very nice. Thank you for the advice. Now it works as I'd like:

Problem though that I used options make-number-range which returns a
figure like 33.0 which gnc:numeric-create doesn't like. After some
struggle I gave up.

<error>
 306: 27*  [gnc:numeric-create 32.0 100]
/usr/share/gnucash/guile-modules/gnucash/report/statement-performance.scm:306:31: In procedure gnc:numeric-create in expression (gnc:numeric-create tax-rate 100):
/usr/share/gnucash/guile-modules/gnucash/report/statement-performance.scm:306:31: Wrong type argument in position 1: 32.0
</error>

    (if show-taxes?
        (begin

     ;; collect total profit - procedure taken from html-utilities.scm
     (set! myprofit (gnc:accounts-get-comm-total-profit (filter use-acct? accounts) my-get-balance))
     (set! myprofit (gnc:sum-collector-commodity myprofit report-currency exchange-fn))

     ;; collect the numeric bit
     (set! num (gnc:gnc-monetary-amount myprofit))

     ;; check for zero or negative profit
     (cond ((gnc:numeric-zero-p num)
             (set! mytaxes (gnc:numeric-create 0 100)))
           ((gnc:numeric-negative-p num)
             (set! mytaxes (gnc:numeric-create 0 100)))
           (else
             ;; calculate taxes
             (set! mytaxes (gnc:numeric-mul num
                              ;(gnc:double-to-gnc-numeric tax-rate 100 GNC-DENOM-REDUCE)
                              ;; always getting a figure 100 times the actual. Tried different denom but to no avail
                              (gnc:numeric-create 33 100)
                              100 GNC-DENOM-REDUCE))
             ))

     ;; reconstruct monetary for printing
     (set! mytaxes (gnc:make-gnc-monetary report-currency mytaxes))

     ;; tab it to the table
     (gnc:html-acct-table-row-helper!
       table tree-depth
       1 (_ "Taxes Payable")
       mytaxes
       #f "primary-subheading" #t #f)
      ))

          ;; add the table
          (gnc:html-document-add-object! doc table)


On Fri, 2005-11-25 at 09:24 -0500, Derek Atkins wrote:
> Try using gnc-numeric math functions:
> 
> (set! mytaxes (gnc:gnc-numeric-mul myprofit (gnc:gnc-numeric-create 33 100)))
> 
> You need to use gnc-numeric mathematics -- you cannot convert from
> a gnc-numeric to a scheme number and then back again like you're trying.
> 
> -derek
> 
> Quoting Darryl Cousins <darryl at darrylcousins.net.nz>:
> 
> > Hi Everyone,
> >
> > I appear to have made some progress with my problem, I have been able to
> > extract the number from the profit total (returned as a
> > commodity-collector). Now I want to reconstruct that number for printing
> > in the report as follows. Any suggestions? I'll back to it tomorrow.
> >
> >    (if show-taxes?
> >        (begin
> >
> >     ;; collect total profit - procedure taken from html-utilities.scm
> >     (set! myprofit (gnc:accounts-get-comm-total-profit (filter 
> > use-acct? accounts) my-get-balance))
> >     (set! myprofit (gnc:sum-collector-commodity myprofit 
> > report-currency exchange-fn))
> >
> >     ;; collect the bits
> >     (set! num (gnc:gnc-monetary-amount myprofit))
> >     (set! denom (gnc:gnc-numeric-denom num))
> >
> >     ;; make a number I can compute with
> >     (set! myprofit (gnc:gnc-numeric-num num))
> >
> >     ;; calculate taxes - TODO check for zero or negative profit
> >     ;; TODO add tax percentage to options
> >     ;; this will print no problem but lacks the currency prefix
> >     ;; and hasn't been reconfigured with numeric-denom
> >     (set! mytaxes (* myprofit 33 0.01))
> >
> >     ;; now try to reconstruct with denominator and currency
> >
> >     ;; reconstruct numeric
> >     ;; this won't print (error #1 below)
> >     ;(set! mytaxes (gnc:make-gnc-numeric mytaxes denom))
> >
> >     ;; reconstruct monetary for printing
> >     ;; this won't print (error #2 below)
> >     ;(set! mytaxes (gnc:make-gnc-monetary report-currency mytaxes))
> >
> >
> >     (gnc:html-acct-table-row-helper!
> >       table tree-depth
> >       1 (_ "Taxes Payable")
> >       mytaxes
> >       #f "primary-subheading" #t #f)
> >      ))
> >
> >          ;; add the table
> >          (gnc:html-document-add-object! doc table)
> >
> >
> > Error #1 tail
> >
> > In /usr/share/gnucash/scm/html-style-info.scm:
> >      ...
> > 284: 97    [gnc:amount->string # #]
> > /usr/share/gnucash/scm/html-style-info.scm:284:3: In procedure 
> > gnc_scm_to_gint64 in expression (gnc:amount->string datum 
> > (gnc:default-print-info #f)):
> > /usr/share/gnucash/scm/html-style-info.scm:284:3: Wrong type 
> > (inexact) argument in position 1: 107.72
> >
> > Error #2 tail
> >
> > In unknown file:
> >      ...
> >   ?: 97    [gnc:default-html-gnc-monetary-renderer # #f]
> > In /usr/share/gnucash/scm/html-style-info.scm:
> > 287: 98    (let* ((result #) (ind #)) (if ind (string-append # 
> > "&euro;" #) result))
> > 287: 99*   [gnc:amount->string # #]
> > /usr/share/gnucash/scm/html-style-info.scm:287:18: In procedure 
> > gnc_scm_to_gint64 in expression (gnc:amount->string 
> > (gnc:gnc-monetary-amount datum) (gnc:commodity-print-info # #t)):
> > /usr/share/gnucash/scm/html-style-info.scm:287:18: Wrong type 
> > (inexact) argument in position 1: 107.72
> >
> > On Fri, 2005-11-25 at 15:53 +1300, Darryl Cousins wrote:
> >> Hi Everyone,
> >>
> >> I'm rather new to GnuCash and guile/schema however I've made some
> >> progress in creating my own 'Statement of Financial Performance using
> >> reports/pnl.scm as a starting point. All well and good.
> >>
> >> My problem is in adding a final row to the report after Profit which
> >> calculates the income tax owing on the profit (company tax at 33% here
> >> in NZ).
> >>
> >> I've managed thus:
> >>
> >>     (if show-taxes?
> >>         (begin
> >>      (set! myprofit (gnc:accounts-get-comm-total-profit (filter 
> >> use-acct? accounts) my-get-balance))
> >>      (set! myprofit (gnc:sum-collector-commodity myprofit 
> >> report-currency exchange-fn))
> >>      (set! myprofit (gnc:gnc-monetary-amount myprofit))
> >>      (gnc:html-acct-table-row-helper!
> >>        table tree-depth
> >>        1 (_ "Taxes Payable")
> >>        myprofit
> >>        #f "primary-subheading" #t #f)
> >>       ))
> >>
> >> Which so far just gives the total profit figure. My dilemma is to
> >> extract the number alone from the commodity collector (as returned from
> >> gncsum-collector-commodity OR extract the number from the gnc-numeric
> >> construct. Once I have the number itself then I can (* myprofit 0.33).
> >> Probably then I'll be in trouble turning that back into a commodity
> >> object.
> >>
> >> Any help would be appreciated.
> >>
> >> Best regards,
> >> Darryl Cousins
> >>
> >> _______________________________________________
> >> gnucash-user mailing list
> >> gnucash-user at gnucash.org
> >> https://lists.gnucash.org/mailman/listinfo/gnucash-user
> > --
> > Darryl Cousins
> > Tree Fern Web Services (NZ) Ltd
> > 106 Sandes St
> > Thames 2801
> > New Zealand
> > +64 7 868 3139
> > darryl.cousins at treefernwebservices.co.nz
> > http://www.treefernwebservices.co.nz/
> >
> > _______________________________________________
> > gnucash-user mailing list
> > gnucash-user at gnucash.org
> > https://lists.gnucash.org/mailman/listinfo/gnucash-user
> >
> 
> 
> 
-- 
Darryl Cousins
Tree Fern Web Services (NZ) Ltd
106 Sandes St
Thames 2801
New Zealand
+64 7 868 3139
darryl.cousins at treefernwebservices.co.nz
http://www.treefernwebservices.co.nz/



More information about the gnucash-user mailing list