Tech Is Hard

Credibility = Talent x Years of experience + Proven hardcore accomplishment

REBOL/GnuCash: Listing the Transactions


With a couple additions to the end-states block!, the end-element function now lists the transacions in my GnuCash file, with the account references (contained in the <trn:splits> element of each transaction) dereferenced:

>> do %gc2iam.r
3-Jan-2011/13:02:43-7:00 Hobby Lobby fix chair pic glass 
[[4106 100] ["Home Repair" "EXPENSE" 3b7ddd7ff3110b9bc94f6425bba8fc83] 
[-4106 100] ["Chase MC" "CREDIT" 74bee4c0f2e95ff570291a9d3ea3a3ba]]
12-Jun-2011/10:19:19-6:00 BEST carpet cleaning 
[[12000 100] ["Home Repair" "EXPENSE" 3b7ddd7ff3110b9bc94f6425bba8fc83] 
[-12000 100] ["Discover" "CREDIT" 74bee4c0f2e95ff570291a9d3ea3a3ba]]
10-Sep-2011/0:46:22-6:00 lunch meeting
[[200 100] ["Parking" "EXPENSE" b0b714a0163f90222c5a6769c78ca791] 
[-200 100] ["Cash" "CASH" 74bee4c0f2e95ff570291a9d3ea3a3ba]]
16-Mar-2011/21:02:47-6:00 Jenny's Market 
[[3255 100] ["Gas" "EXPENSE" b0b714a0163f90222c5a6769c78ca791] 
[-3255 100] ["Discover" "CREDIT" 74bee4c0f2e95ff570291a9d3ea3a3ba]

Here’s the relevant part of the handler. I added a /local word to end-element, value, for some manipulation of the string that GnuCash stores, which is a fraction. For instance, $328.23 is represented as 32823/100. There is other currency metadata used to make conversions between these amounts and stock prices, for example. Right now I’m just going to be concerned about “normal” transactions like spending and transfers, but I don’t want to lose any precision until I know more, so I split the string using “/” and convert each component to an integer. I changed the to use to make; the implication is that make will give me a new item of my desired type, while to technically converts the argument. Remembering the admonition to use copy when initializing from something that will change, I think make must be safer here.

trnAccts: copy []
guid:               name:               class: 
parent:             trnDate:            description:  
none

end-states: [
    "act:id"          [guid:           make word! content]
    "act:name"        [name:                 copy content]
    "act:type"        [class:                copy content]
    "act:parent"      [parent:         make word! content]
    "trn:description" [description:          copy content]
    "ts:date"         [trnDate:        make date! content]
    "split:value"     [
        value: split content #"/" 
        append/only trnAccts reduce [make integer! value/1 make integer! value/2]
    ] 
    "split:account"   [append trnAccts to word! content]

    "gnc:account"     [set guid reduce [name class parent]]
    "gnc:transaction" [
        print [trnDate description remold trnAccts]
        clear head trnAccts
    ]
]
end-element: func [
    ns-uri		[string! none!]  ns-prefix [string! none!]
    local-name	[string! none!]  q-name    [string!]
    /local value
][ 
    switch q-name end-states
    clear head content
]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: