-
Notifications
You must be signed in to change notification settings - Fork 761
/
transaction-report.scm
600 lines (516 loc) · 19 KB
/
transaction-report.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
;; -*-scheme-*-
;; transaction-report.scm
;; Report on all transactions in an account
;; Robert Merkel (rgmerk@mira.net)
(gnc:support "report/transaction-report.scm")
(require 'sort)
;hack alert - is this line necessary?
(gnc:depend "text-export.scm")
;; hack alert - possibly unecessary globals
;; functions for manipulating total inflow and outflow counts.
(define gnc:total-inflow 0)
(define gnc:total-outflow 0)
(define (gnc:set-total-inflow! x)
(set! gnc:total-inflow x))
(define (gnc:set-total-outflow! x)
(set! gnc:total-outflow x))
(define gnc:tr-report-initialize-inflow-and-outflow!
(begin
(set! gnc:total-inflow 0)
(set! gnc:total-outflow 0)
#f))
;;returns a list contains elements of the-list for which predictate is
;; true
(define (gnc:filter-list the-list predicate)
(cond ((not (list? the-list))
(gnc:error("Attempted to filter a non-list object")))
((null? the-list) '())
((predicate (car the-list))
(cons (car the-list)
(gnc:filter-list (cdr the-list) predicate)))
(else (gnc:filter-list (cdr the-list) predicate))))
;; like map, but restricted to one dimension, and
;; guaranteed to have inorder semantics.
(define (gnc:inorder-map the-list fn)
(cond ((not (list? the-list))
(gnc:error("Attempted to map a non-list object")))
((not (procedure? fn))
(gnc:error("Attempted to map a non-function object to a list")))
((eq? the-list '()) '())
(else (cons (fn (car the-list))
(gnc:inorder-map (cdr the-list) fn)))))
;; register a configuration option for the transaction report
(define (trep-options-generator)
(define gnc:*transaction-report-options* (gnc:new-options))
(define (gnc:register-trep-option new-option)
(gnc:register-option gnc:*transaction-report-options* new-option))
;; from date
;; hack alert - could somebody set this to an appropriate date?
(gnc:register-trep-option
(gnc:make-date-option
"Report Options" "From"
"a" "Report Items from this date"
(lambda ()
(let ((bdtime (localtime (current-time))))
(set-tm:sec bdtime 0)
(set-tm:min bdtime 0)
(set-tm:hour bdtime 0)
(set-tm:mday bdtime 1)
(set-tm:mon bdtime 0)
(let ((time (car (mktime bdtime))))
(cons time 0))))
#f))
;; to-date
(gnc:register-trep-option
(gnc:make-date-option
"Report Options" "To"
"b" "Report items up to and including this date"
(lambda () (cons (current-time) 0))
#f))
;; account to do report on
(gnc:register-trep-option
(gnc:make-account-list-option
"Report Options" "Account"
"c" "Do transaction report on this account"
(lambda ()
(let ((current-accounts (gnc:get-current-accounts))
(num-accounts (gnc:group-get-num-accounts (gnc:get-current-group)))
(first-account (gnc:group-get-account (gnc:get-current-group) 0)))
(cond ((not (null? current-accounts)) (list (car current-accounts)))
((> num-accounts 0) (list first-account))
(else ()))))
#f #f))
;; primary sorting criterion
(gnc:register-trep-option
(gnc:make-multichoice-option
"Sorting" "Primary Key"
"a" "Sort by this criterion first"
'date
(list #(date
"Date"
"Sort by date")
#(time
"Time"
"Sort by EXACT entry time")
#(corresponding-acc
"Transfer from/to"
"Sort by account transferred from/to's name")
#(amount
"Amount"
"Sort by amount")
#(description
"Description"
"Sort by description")
#(number
"Number"
"Sort by check/transaction number")
#(memo
"Memo"
"Sort by memo"))))
(gnc:register-trep-option
(gnc:make-multichoice-option
"Sorting" "Primary Sort Order"
"b" "Order of primary sorting"
'ascend
(list #(ascend "Ascending" "smallest to largest, earliest to latest")
#(descend "Descending" "largest to smallest, latest to earliest"))))
(gnc:register-trep-option
(gnc:make-multichoice-option
"Sorting" "Secondary Key"
"c"
"Sort by this criterion second"
'corresponding-acc
(list #(date
"Date"
"Sort by date")
#(time
"Time"
"Sort by EXACT entry time")
#(corresponding-acc
"Transfer from/to"
"Sort by account transferred from/to's name")
#(amount
"Amount"
"Sort by amount")
#(description
"Description"
"Sort by description")
#(number
"Number"
"Sort by check/transaction number")
#(memo
"Memo"
"Sort by memo"))))
(gnc:register-trep-option
(gnc:make-multichoice-option
"Sorting" "Secondary Sort Order"
"d" "Order of Secondary sorting"
'ascend
(list #(ascend "Ascending" "smallest to largest, earliest to latest")
#(descend "Descending" "largest to smallest, latest to earliest"))))
gnc:*transaction-report-options*)
;; extract fields out of the scheme split representation
(define (gnc:tr-report-get-memo split-scm)
(vector-ref split-scm 0))
(define (gnc:tr-report-get-action split-scm)
(vector-ref split-scm 1))
(define (gnc:tr-report-get-description split-scm)
(vector-ref split-scm 2))
(define (gnc:tr-report-get-date split-scm)
(vector-ref split-scm 3))
(define (gnc:tr-report-get-reconcile-state split-scm)
(vector-ref split-scm 4))
(define (gnc:tr-report-get-reconcile-date split-scm)
(vector-ref split-scm 5))
(define (gnc:tr-report-get-share-amount split-scm)
(vector-ref split-scm 6))
(define (gnc:tr-report-get-share-price split-scm)
(vector-ref split-scm 7))
(define (gnc:tr-report-get-value split-scm)
(vector-ref split-scm 8))
(define (gnc:tr-report-get-num split-scm)
(vector-ref split-scm 9))
(define (gnc:tr-report-get-other-splits split-scm)
(vector-ref split-scm 10))
(define (gnc:tr-report-get-first-acc-name split-scm)
(let ((other-splits (gnc:tr-report-get-other-splits split-scm)))
(cond ((= (length other-splits) 0) "-")
(else (caar other-splits)))))
;;; something like
;;; for(i = first; i < last; i+= step) { thunk(i);}
(define (gnc:for-loop thunk first last step)
(cond ((< first last) (thunk first)
(gnc:for-loop thunk (+ first step) last step))
(else #f)))
;;; applies thunk to each split in account account
(define (gnc:for-each-split-in-account account thunk)
(gnc:for-loop (lambda (x) (thunk (gnc:account-get-split account x)))
0 (gnc:account-get-split-count account) 1))
;; get transactions date from split - needs to be done indirectly
;; as it's stored in the parent transaction
(define (gnc:split-get-transaction-date split)
(gnc:transaction-get-date-posted (gnc:split-get-parent split)))
;; ditto descriptions
(define (gnc:split-get-description-from-parent split)
(gnc:transaction-get-description (gnc:split-get-parent split)))
;; get the account name of a split
(define (gnc:split-get-account-name split)
(gnc:account-get-name (gnc:split-get-account split)))
;; builds a list of the account name and values for the other
;; splits in a transaction
(define (gnc:split-get-corresponding-account-name-and-values split)
(let* ((my-sign (positive? (gnc:split-get-value split)))
(diff-list '())
(parent-transaction (gnc:split-get-parent split))
(num-splits (gnc:transaction-get-split-count parent-transaction)))
(cond
((= num-splits 1) '())
(else
(gnc:for-loop
(lambda (n)
(let ((split-in-trans
(gnc:transaction-get-split parent-transaction n)))
(if (not (eq? my-sign
(positive? (gnc:split-get-value split-in-trans))))
(set! diff-list
(cons
(list
(gnc:split-get-account-name split-in-trans)
(gnc:split-get-value split-in-trans))
diff-list)))))
0 num-splits 1)
(reverse diff-list)))))
;; takes a C split, extracts relevant data and converts to a scheme
;; representation
(define (gnc:make-split-scheme-data split)
(vector (gnc:split-get-memo split)
(gnc:split-get-action split)
(gnc:split-get-description-from-parent split)
(gnc:split-get-transaction-date split)
(gnc:split-get-reconcile-state split)
(gnc:split-get-reconciled-date split)
(gnc:split-get-share-amount split)
(gnc:split-get-share-price split)
(gnc:split-get-value split)
(gnc:transaction-get-num (gnc:split-get-parent split))
(gnc:split-get-corresponding-account-name-and-values split)))
;; timepair manipulation functions
;; hack alert - these should probably be put somewhere else
;; and be implemented PROPERLY rather than hackily
(define (gnc:timepair-to-datestring tp)
(let ((bdtime (localtime (car tp))))
(strftime "%x" bdtime)))
;; given a timepair contains any time on a certain day (local time)
;; converts it to be midday that day.
(define (gnc:timepair-canonical-day-time tp)
(let ((bdt (localtime (car tp))))
(set-tm:sec bdt 0)
(set-tm:min bdt 0)
(set-tm:hour bdt 12)
(let ((newtime (car (mktime bdt))))
(cons newtime (* 1000 newtime)))))
(define (gnc:timepair-earlier-or-eq-date t1 t2)
(let ((time1 (car (gnc:timepair-canonical-day-time t1)))
(time2 (car (gnc:timepair-canonical-day-time t2))))
(<= time1 time2)))
(define (gnc:timepair-later-date t1 t2)
(let ((time1 (car (gnc:timepair-canonical-day-time t1)))
(time2 (car (gnc:timepair-canonical-day-time t2))))
(< time1 time2)))
(define (gnc:timepair-later-or-eq-date t1 t2)
(gnc:timepair-earlier-or-eq-date t2 t1))
(define (gnc:sort-predicate-component component order)
(let ((ascending-order-comparator
(begin
; (display (symbol->string component))
(cond
((eq? component 'date)
(lambda (split-scm-a split-scm-b)
(-
(car
(gnc:timepair-canonical-day-time
(gnc:tr-report-get-date split-scm-a)))
(car
(gnc:timepair-canonical-day-time
(gnc:tr-report-get-date split-scm-b))))))
((eq? component 'time)
(lambda (split-scm-a split-scm-b)
(-
(car (gnc:tr-report-get-date split-scm-a))
(car (gnc:tr-report-get-date split-scm-b)))))
((eq? component 'amount)
(lambda (split-scm-a split-scm-b)
(-
(gnc:tr-report-get-value split-scm-a)
(gnc:tr-report-get-value split-scm-b))))
((eq? component 'description)
(lambda (split-scm-a split-scm-b)
(let ((description-a (gnc:tr-report-get-description split-scm-a))
(description-b (gnc:tr-report-get-description split-scm-b)))
(cond ((string<? description-a description-b) -1)
((string=? description-a description-b) 0)
(else 1)))))
;; hack alert - should probably use something more sophisticated
;; here - perhaps even making it user-definable
((eq? component 'number)
(lambda (split-scm-a split-scm-b)
(let ((num-a (gnc:tr-report-get-num split-scm-a))
(num-b (gnc:tr-report-get-num split-scm-b)))
(cond ((string<? num-a num-b) -1)
((string=? num-a num-b) 0)
(else 1)))))
((eq? component 'corresponding-acc)
(lambda (split-scm-a split-scm-b)
(let ((corr-acc-a (gnc:tr-report-get-first-acc-name split-scm-a))
(corr-acc-b (gnc:tr-report-get-first-acc-name split-scm-b)))
(cond ((string<? corr-acc-a corr-acc-b) -1)
((string=? corr-acc-a corr-acc-b) 0)
(else 1)))))
((eq? component 'memo)
(lambda (split-scm-a split-scm-b)
(let ((memo-a (gnc:tr-report-get-memo split-scm-a))
(memo-b (gnc:tr-report-get-memo split-scm-b)))
(cond ((string<? memo-a memo-b) -1)
((string=? memo-a memo-b) 0)
(else 1)))))
(else (gnc:error (sprintf "illegal sorting option %s- bug in transaction-report.scm" (symbol->string (component)) )))))))
(cond ((eq? order 'descend)
(lambda (my-split-a my-split-b)
(- (ascending-order-comparator my-split-a my-split-b))))
(else ascending-order-comparator))))
;; returns a predicate
(define (gnc:tr-report-make-sort-predicate primary-key-op primary-order-op
secondary-key-op secondary-order-op)
(let ((primary-comp (gnc:sort-predicate-component
(gnc:option-value primary-key-op)
(gnc:option-value primary-order-op)))
(secondary-comp (gnc:sort-predicate-component
(gnc:option-value secondary-key-op)
(gnc:option-value secondary-order-op))))
(lambda (split-a split-b)
(let ((primary-comp-value (primary-comp split-a split-b)))
(cond ((< primary-comp-value 0) #t)
((> primary-comp-value 0) #f)
(else
(let ((secondary-comp-value (secondary-comp split-a split-b)))
(cond ((< secondary-comp-value 0) #t)
(else #f)))))))))
;; returns a predicate that returns true only if a split-scm is
;; between early-date and late-date
(define (gnc:tr-report-make-filter-predicate early-date late-date)
(lambda (split-scm)
(let ((split-date (gnc:tr-report-get-date split-scm)))
(and (gnc:timepair-later-or-eq-date split-date early-date)
(gnc:timepair-earlier-or-eq-date split-date late-date)))))
;; converts a scheme split representation to a line of HTML,
;; updates the values of total-inflow and total-outflow based
;; on the split value
;; hack alert - no i8n on amount printing yet - must fix!
(define (gnc:tr-report-split-to-html split-scm
starting-balance)
(let ((other-splits (gnc:tr-report-get-other-splits split-scm)))
(string-append
"<TR><TD>"
(gnc:timepair-to-datestring
(gnc:tr-report-get-date split-scm))
"</TD><TD>"
(gnc:tr-report-get-num split-scm)
"</TD><TD>"
(gnc:tr-report-get-description split-scm)
"</TD><TD>"
(gnc:tr-report-get-memo split-scm)
"</TD><TD>"
(cond ((null? other-splits) "")
((= (length other-splits) 1)
(cond ((eqv? (caar other-splits) #f)
"-")
(else (caar other-splits))))
(else "Multi-split (not implemented yet)"))
"</TD><TD>"
(cond ((> (gnc:tr-report-get-value split-scm) 0)
(begin
(gnc:set-total-inflow! (+ gnc:total-inflow
(gnc:tr-report-get-value split-scm)))
(string-append
(sprintf #f "%.2f" (gnc:tr-report-get-value split-scm))
"</TD><TD>")))
(else
(begin
(gnc:set-total-outflow! (+ gnc:total-outflow
(- (gnc:tr-report-get-value split-scm))))
(string-append
"</TD><TD>"
(sprintf #f "%.2f"
(- (gnc:tr-report-get-value split-scm)))))))
"</TD><TD>"
(sprintf #f "%.2f" (- (+ starting-balance gnc:total-inflow)
gnc:total-outflow))
"</TD></TR>")))
;; gets the balance for a list of splits before beginning-date
;; hack alert -
;; we are doing multiple passes over the list - if it becomes a performance
;; problem some code optimisation will become necessary
(define (gnc:tr-report-get-starting-balance scm-split-list beginning-date)
(cond ((or
(eq? scm-split-list '())
(gnc:timepair-later-date
(gnc:tr-report-get-date (car scm-split-list))
beginning-date))
0)
(+
(gnc:tr-report-get-value
(car scm-split-list))
(gnc:tr-report-get-starting-balance
(cdr scm-split-list) beginning-date))))
(gnc:define-report
;; version
1
;; Name
"Account Transactions"
;; Options
trep-options-generator
;; renderer
(lambda (options)
(gnc:set-total-inflow! 0)
(gnc:set-total-outflow! 0)
(let* ((begindate (gnc:lookup-option options "Report Options" "From"))
(enddate (gnc:lookup-option options "Report Options" "To"))
(tr-report-account-op (gnc:lookup-option options
"Report Options" "Account"))
(tr-report-primary-key-op (gnc:lookup-option options
"Sorting"
"Primary Key"))
(tr-report-primary-order-op (gnc:lookup-option options
"Sorting"
"Primary Sort Order"))
(tr-report-secondary-key-op (gnc:lookup-option options
"Sorting"
"Secondary Key"))
(tr-report-secondary-order-op
(gnc:lookup-option options "Sorting" "Secondary Sort Order"))
(prefix (list "<HTML>" "<BODY bgcolor=#99ccff>" "<TABLE>"
"<TH>Date</TH>"
"<TH>Num</TH>"
"<TH>Description</TH>"
"<TH>Memo</TH>"
"<TH>Category</TH>"
"<TH>Credit</TH>"
"<TH>Debit</TH>"
"<TH>Balance<TH>"))
(suffix (list "</TABLE>" "</BODY>" "</HTML>"))
(balance-line '())
(inflow-outflow-line '())
(net-inflow-line '())
(report-lines '())
(date-filter-pred (gnc:tr-report-make-filter-predicate
(gnc:option-value begindate)
(gnc:option-value enddate)))
(starting-balance 0)
(accounts (gnc:option-value tr-report-account-op)))
gnc:tr-report-initialize-inflow-and-outflow!
(if (null? accounts)
(set! report-lines
(list "<TR><TD>There are no accounts to report on.</TD></TR>"))
(begin
(gnc:for-each-split-in-account
(car accounts)
(lambda (split)
(set! report-lines
(append! report-lines
(list (gnc:make-split-scheme-data split))))))
(set! starting-balance
(gnc:tr-report-get-starting-balance
report-lines (gnc:option-value begindate)))
(set! report-lines (gnc:filter-list report-lines date-filter-pred))
(set! report-lines
(sort!
report-lines
(gnc:tr-report-make-sort-predicate
tr-report-primary-key-op tr-report-primary-order-op
tr-report-secondary-key-op tr-report-secondary-order-op)))
(let ((html-mapper (lambda (split-scm) (gnc:tr-report-split-to-html
split-scm
starting-balance))))
(set! report-lines (gnc:inorder-map report-lines html-mapper)))
(set!
balance-line
(list "<TR><TD><STRONG>Balance at: "
(gnc:timepair-to-datestring (gnc:option-value begindate))
"</STRONG></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD><STRONG>"
(sprintf #f "%.2f" starting-balance)
"</STRONG></TD></TR>"))
(set!
inflow-outflow-line
(list "<TR><TD><STRONG>Totals:</STRONG></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD><STRONG>"
(sprintf #f "%.2f" gnc:total-inflow)
"</TD></STRONG>"
"<TD><STRONG>"
(sprintf #f "%.2f" gnc:total-outflow)
"</TD></STRONG>"
"<TD></TD></TR>"))
(set!
net-inflow-line
(list "<TR><TD><STRONG>Net Inflow</STRONG></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD></TD>"
"<TD><STRONG>"
(sprintf #f "%.2f" (- gnc:total-inflow gnc:total-outflow))
"</TD></STRONG></TR>"))))
(append prefix balance-line report-lines
inflow-outflow-line net-inflow-line suffix))))