-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction-tags.scm
2756 lines (2463 loc) · 126 KB
/
transaction-tags.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; transaction-tags.scm : Report on all transactions in account(s)
;; with additional tags feature
;;
;; calls transaction report functions in trep-engine-tags.scm
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, contact:
;;
;; Free Software Foundation Voice: +1-617-542-5942
;; 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
;; Boston, MA 02110-1301, USA [email protected]
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-module (gnucash report transaction-tags))
(use-modules (gnucash core-utils))
(use-modules (gnucash app-utils))
(use-modules (gnucash report))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; trep-engine-tags.scm : Transaction Report engine with added
;; tag functionality. Based on official trep-engine.scm.
;;
;; Tag implementation by Vincent Dawans <[email protected]>
;;
;; Original report by Robert Merkel <[email protected]>
;; Contributions by Bryan Larsen <[email protected]>
;; More contributions for new report generation code by Robert Merkel
;; More contributions by Christian Stimming <[email protected]>
;; Modified to support the intersection of two account lists by
;; Michael T. Garrison Stuber
;; Modified account names display by Tomas Pospisek
;; <[email protected]> with a lot of help from "warlord"
;; Refactored by Christopher Lam (2017)
;; - introduced account/transaction substring/regex matcher
;; - add custom sorter in scheme
;; - common currency - optionally show original currency amount
;; and enable multiple data columns
;; - add support for indenting for better grouping
;; - add subtotal summary grid
;; - by default, exclude closing transactions from the report
;; - converted to module in 2019
;; - CSV export, exports the report headers and totals
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, contact:
;;
;; Free Software Foundation Voice: +1-617-542-5942
;; 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
;; Boston, MA 02110-1301, USA [email protected]
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Disabled in custom version
;; (define-module (gnucash report trep-engine))
(use-modules (gnucash core-utils))
(use-modules (gnucash engine))
(use-modules (gnucash app-utils))
(use-modules (gnucash utilities))
(use-modules (gnucash report report-core)
(gnucash report report-utilities)
(gnucash report options-utilities)
(gnucash report commodity-utilities)
(gnucash report html-document)
(gnucash report html-style-info)
(gnucash report html-utilities)
(gnucash report html-table)
(gnucash report html-text))
(use-modules (srfi srfi-11))
(use-modules (srfi srfi-1))
(use-modules (srfi srfi-9))
(use-modules (srfi srfi-26))
(use-modules (ice-9 match))
(use-modules (ice-9 regex))
;; Disabled in custom version
;; (export gnc:trep-tags-options-generator)
;; (export gnc:trep-tags-renderer)
;; (export gnc:lists->csv)
;; Define the strings here to avoid typos and make changes easier.
;;Accounts
(define optname-accounts (N_ "Accounts"))
(define optname-filterby (N_ "Filter By…"))
(define optname-filtertype (N_ "Filter Type"))
;;Display
(define optname-detail-level (N_ "Detail Level"))
(define optname-grid (N_ "Subtotal Table"))
(define optname-grand-total (N_ "Grand Total"))
;; Translators: a running total is a total that is continually adjusted on every line.
;; To be consistent, also consider how the term "Running Balance" is translated.
;; "Running Totals" is the plural form as it refers to the running total and running subtotals.
;; To be consistent, also consider how the singular form "Running Total" is translated.
(define optname-running-totals (N_ "Running Totals"))
;;Sorting
(define pagename-sorting (N_ "Sorting"))
(define optname-prime-sortkey (N_ "Primary Key"))
(define optname-prime-subtotal (N_ "Primary Subtotal"))
(define optname-prime-sortorder (N_ "Primary Sort Order"))
(define optname-prime-date-subtotal (N_ "Primary Subtotal for Date Key"))
(define optname-full-account-name (N_ "Show Full Account Name"))
(define optname-show-account-code (N_ "Show Account Code"))
(define optname-show-account-description (N_ "Show Account Description"))
(define optname-show-informal-headers (N_ "Show Informal Debit/Credit Headers"))
(define optname-show-subtotals-only
(N_ "Show subtotals only (hide transactional data)"))
(define optname-indenting (N_ "Add indenting columns"))
(define optname-sec-sortkey (N_ "Secondary Key"))
(define optname-sec-subtotal (N_ "Secondary Subtotal"))
(define optname-sec-sortorder (N_ "Secondary Sort Order"))
(define optname-sec-date-subtotal (N_ "Secondary Subtotal for Date Key"))
(define optname-tag-prefix (N_ "Tag Prefix"))
(define optname-remove-tp (N_ "Remove Tag Prefix from Headings"))
(define optname-no-match-heading (N_ "No-Match Heading"))
(define optname-append-tp (N_ "Append Tag to No-Match Heading"))
;;General
(define optname-startdate (N_ "Start Date"))
(define optname-enddate (N_ "End Date"))
(define optname-date-source (N_ "Date Filter"))
(define optname-table-export (N_ "Table for Exporting"))
(define optname-infobox-display (N_ "Add options summary"))
;; Currency
(define pagename-currency (N_ "Currency"))
(define optname-price-source (N_ "Price Source"))
(define optname-common-currency (N_ "Common Currency"))
(define optname-orig-currency (N_ "Show original currency amount"))
(define optname-currency (N_ "Report's currency"))
;;Filtering
(define pagename-filter (N_ "Filter"))
(define optname-account-matcher (N_ "Account Name Filter"))
(define optname-account-matcher-regex
(N_ "Use regular expressions for account name filter"))
(define optname-account-matcher-exclude
(N_ "Account Name Filter excludes matched strings"))
(define optname-transaction-matcher (N_ "Transaction Filter"))
(define optname-transaction-matcher-regex
(N_ "Use regular expressions for transaction filter"))
(define optname-transaction-matcher-exclude
(N_ "Transaction Filter excludes matched strings"))
(define optname-transaction-matcher-caseinsensitive
(N_ "Transaction Filter is case insensitive"))
(define optname-reconcile-status (N_ "Reconciled Status"))
(define optname-void-transactions (N_ "Void Transactions"))
(define optname-closing-transactions (N_ "Closing transactions"))
;;Styles
(define def:grand-total-style "grand-total")
(define def:normal-row-style "normal-row")
(define def:alternate-row-style "alternate-row")
(define def:primary-subtotal-style "primary-subheading")
(define def:secondary-subtotal-style "secondary-subheading")
(define NO-MATCHING-TRANS-HEADER (G_ "No matching transactions found"))
(define NO-MATCHING-TRANS-TEXT (G_ "No transactions were found that \
match the time interval and account selection specified \
in the Options panel."))
(define DATE-SORTING-TYPES
(list 'date 'reconciled-date))
(define ACCOUNT-SORTING-TYPES
(list 'account-name 'corresponding-acc-name
'account-code 'corresponding-acc-code))
(define SORTKEY-INFORMAL-HEADERS
(list 'account-name 'account-code))
(define reconcile-list
(list (cons #\n (G_ "Unreconciled"))
(cons #\c (G_ "Cleared"))
(cons #\y (G_ "Reconciled"))
(cons #\f (G_ "Frozen"))
(cons #\v (G_ "Voided"))))
;; Create a function which helps find a split's tag as per
;; various user-defined options in pagename-sorting section.
;; Memorize both the sortvalue and rendering in hashtable
;; for later retrieval because get-tag-from-split is slow.
;; Returns either the sort value or rendered value as per sortvalue?
(define (split-tag split parameters sortvalue?)
(let* ((tag-htable (assq-ref parameters 'tag/htable)))
(cond
((hash-ref tag-htable split) =>
(lambda (found) (if sortvalue? (car found) (cadr found))))
(else
(let ((tag (get-tag-from-split split parameters)))
(hash-set! tag-htable split tag)
(if sortvalue? (car tag) (cadr tag)))))))
;; Finds a split's tag as per user-defined options.
;; Returns a list with both (tag-sortvalue tag-rendered)
(define (get-tag-from-split split parameters)
(let* ((tag-prefix (assq-ref parameters 'tag/prefix))
(remove-tp? (assq-ref parameters 'tag/remove-tp))
(no-match-heading (assq-ref parameters 'tag/no-match-heading))
(append-tp? (assq-ref parameters 'tag/append-tp))
(regexp (assq-ref parameters 'tag/regexp))
(sm (or (regexp-exec regexp (xaccSplitGetMemo split))
(regexp-exec regexp ((compose xaccTransGetNotes xaccSplitGetParent) split))
(regexp-exec regexp ((compose xaccTransGetDescription xaccSplitGetParent) split))))
(tag-matched (and sm (match:substring sm))))
(cond ((not tag-matched) (list 'infinity-string
(string-append
(if (string-null? no-match-heading) "No Match" no-match-heading)
(if append-tp? (string-append " " tag-prefix) ""))))
(remove-tp? (list tag-matched (string-drop tag-matched (string-length tag-prefix))))
(else (list tag-matched tag-matched)))))
(define (sortkey-list parameters)
;; Defines the different sorting keys, as an association-list
;; together with the subtotal functions. Each entry:
;; 'sortkey - sort parameter sent via qof-query
;; 'split-sortvalue - function retrieves number/string for comparing splits
;; 'text - text displayed in Display tab
;; 'renderer-fn - helper function to select subtotal/subheading renderer
;; behaviour varies according to sortkey.
;; account-types converts split->account
;; #f means the sortkey cannot be subtotalled
;; otherwise it converts split->string
;;
(list (list 'account-name
(cons 'sortkey (list SPLIT-ACCT-FULLNAME))
(cons 'split-sortvalue
(compose gnc-account-get-full-name xaccSplitGetAccount))
(cons 'text (G_ "Account Name"))
(cons 'renderer-fn xaccSplitGetAccount))
(list 'account-code
(cons 'sortkey (list SPLIT-ACCOUNT ACCOUNT-CODE-))
(cons 'split-sortvalue (compose xaccAccountGetCode xaccSplitGetAccount))
(cons 'text (G_ "Account Code"))
(cons 'renderer-fn xaccSplitGetAccount))
(list 'date
(cons 'sortkey (list SPLIT-TRANS TRANS-DATE-POSTED))
(cons 'split-sortvalue (compose xaccTransGetDate xaccSplitGetParent))
(cons 'text (G_ "Date"))
(cons 'renderer-fn #f))
(list 'reconciled-date
(cons 'sortkey (list SPLIT-DATE-RECONCILED))
(cons 'split-sortvalue xaccSplitGetDateReconciled)
(cons 'text (G_ "Reconciled Date"))
(cons 'renderer-fn #f))
(list 'reconciled-status
(cons 'sortkey #f)
(cons 'split-sortvalue (lambda (s)
(length (memv (xaccSplitGetReconcile s)
(map car reconcile-list)))))
(cons 'text (G_ "Reconciled Status"))
(cons 'renderer-fn (lambda (s)
(assv-ref reconcile-list
(xaccSplitGetReconcile s)))))
(list 'register-order
(cons 'sortkey (list QUERY-DEFAULT-SORT))
(cons 'split-sortvalue #f)
(cons 'text (G_ "Register Order"))
(cons 'renderer-fn #f))
(list 'corresponding-acc-name
(cons 'sortkey (list SPLIT-CORR-ACCT-NAME))
(cons 'split-sortvalue xaccSplitGetCorrAccountFullName)
(cons 'text (G_ "Other Account Name"))
(cons 'renderer-fn (compose xaccSplitGetAccount xaccSplitGetOtherSplit)))
(list 'corresponding-acc-code
(cons 'sortkey (list SPLIT-CORR-ACCT-CODE))
(cons 'split-sortvalue xaccSplitGetCorrAccountCode)
(cons 'text (G_ "Other Account Code"))
(cons 'renderer-fn (compose xaccSplitGetAccount xaccSplitGetOtherSplit)))
(list 'amount
(cons 'sortkey (list SPLIT-VALUE))
(cons 'split-sortvalue xaccSplitGetValue)
(cons 'text (G_ "Amount"))
(cons 'renderer-fn #f))
(list 'description
(cons 'sortkey (list SPLIT-TRANS TRANS-DESCRIPTION))
(cons 'split-sortvalue (compose xaccTransGetDescription
xaccSplitGetParent))
(cons 'text (G_ "Description"))
(cons 'renderer-fn (compose xaccTransGetDescription xaccSplitGetParent)))
(if (assq-ref parameters 'split-action)
(list 'number
(cons 'sortkey (list SPLIT-ACTION))
(cons 'split-sortvalue xaccSplitGetAction)
(cons 'text (G_ "Number/Action"))
(cons 'renderer-fn #f))
(list 'number
(cons 'sortkey (list SPLIT-TRANS TRANS-NUM))
(cons 'split-sortvalue (compose xaccTransGetNum xaccSplitGetParent))
(cons 'text (G_ "Number"))
(cons 'renderer-fn #f)))
(list 't-number
(cons 'sortkey (list SPLIT-TRANS TRANS-NUM))
(cons 'split-sortvalue (compose xaccTransGetNum xaccSplitGetParent))
(cons 'text (G_ "Transaction Number"))
(cons 'renderer-fn #f))
(list 'memo
(cons 'sortkey (list SPLIT-MEMO))
(cons 'split-sortvalue xaccSplitGetMemo)
(cons 'text (G_ "Memo"))
(cons 'renderer-fn xaccSplitGetMemo))
(list 'notes
(cons 'sortkey #f)
(cons 'split-sortvalue (compose xaccTransGetNotes xaccSplitGetParent))
(cons 'text (G_ "Notes"))
(cons 'renderer-fn (compose xaccTransGetNotes xaccSplitGetParent)))
(list 'tags
(cons 'sortkey #f)
(cons 'split-sortvalue (lambda (s) (split-tag s parameters #t)))
(cons 'text (G_ "Tags"))
(cons 'renderer-fn (lambda (s) (split-tag s parameters #f))))
(list 'none
(cons 'sortkey '())
(cons 'split-sortvalue #f)
(cons 'text (G_ "None"))
(cons 'renderer-fn #f))))
(define (time64-year t64)
(gnc:date-get-year (gnc-localtime t64)))
(define (time64-quarter t64)
(+ (* 10 (gnc:date-get-year (gnc-localtime t64)))
(gnc:date-get-quarter (gnc-localtime t64))))
(define (time64-month t64)
(+ (* 100 (gnc:date-get-year (gnc-localtime t64)))
(gnc:date-get-month (gnc-localtime t64))))
(define (time64-week t64)
(gnc:date-get-week (gnc-localtime t64)))
(define (time64-day t64)
(+ (* 500 (gnc:date-get-year (gnc-localtime t64)))
(gnc:date-get-year-day (gnc-localtime t64))))
(define (split->time64 s)
(xaccTransGetDate (xaccSplitGetParent s)))
(define date-subtotal-list
;; List for date option.
;; Defines the different date sorting keys, as an association-list. Each entry:
;; 'split-sortvalue - func retrieves number/string used for comparing splits
;; 'text - text displayed in Display tab
;; 'renderer-fn - func retrieves string for subtotal/subheading renderer
;; #f means the date sortkey is not grouped
;; otherwise it converts split->string
(list
(list 'none
(cons 'split-sortvalue #f)
(cons 'date-sortvalue #f)
(cons 'text (G_ "None"))
(cons 'renderer-fn #f))
(list 'daily
(cons 'split-sortvalue (lambda (s) (time64-day (split->time64 s))))
(cons 'date-sortvalue time64-day)
(cons 'text (G_ "Daily"))
(cons 'renderer-fn (lambda (s) (qof-print-date (split->time64 s)))))
(list 'weekly
(cons 'split-sortvalue (lambda (s) (time64-week (split->time64 s))))
(cons 'date-sortvalue time64-week)
(cons 'text (G_ "Weekly"))
(cons 'renderer-fn (compose gnc:date-get-week-year-string
gnc-localtime
split->time64)))
(list 'monthly
(cons 'split-sortvalue (lambda (s) (time64-month (split->time64 s))))
(cons 'date-sortvalue time64-month)
(cons 'text (G_ "Monthly"))
(cons 'renderer-fn (compose gnc:date-get-month-year-string
gnc-localtime
split->time64)))
(list 'quarterly
(cons 'split-sortvalue (lambda (s) (time64-quarter (split->time64 s))))
(cons 'date-sortvalue time64-quarter)
(cons 'text (G_ "Quarterly"))
(cons 'renderer-fn (compose gnc:date-get-quarter-year-string
gnc-localtime
split->time64)))
(list 'yearly
(cons 'split-sortvalue (lambda (s) (time64-year (split->time64 s))))
(cons 'date-sortvalue time64-year)
(cons 'text (G_ "Yearly"))
(cons 'renderer-fn (compose gnc:date-get-year-string
gnc-localtime
split->time64)))))
(define filter-list
(list
(list 'none
(cons 'text (G_ "Do not do any filtering")))
(list 'include
(cons 'text (G_ "Include Transactions to/from Filter Accounts")))
(list 'exclude
(cons 'text (G_ "Exclude Transactions to/from Filter Accounts")))))
(define show-void-list
(list
(list 'non-void-only
(cons 'how (logand CLEARED-ALL (lognot CLEARED-VOIDED)))
(cons 'text (G_ "Non-void only")))
(list 'void-only
(cons 'how CLEARED-VOIDED)
(cons 'text (G_ "Void only")))
(list 'both
(cons 'how CLEARED-ALL)
(cons 'text (G_ "Both (and include void transactions in totals)")))))
(define show-closing-list
(list
(list 'exclude-closing
(cons 'text (G_ "Exclude closing transactions"))
(cons 'closing-match #f))
(list 'include-both
(cons 'text (G_ "Show both closing and regular transactions"))
(cons 'closing-match 'both))
(list 'closing-only
(cons 'text (G_ "Show closing transactions only"))
(cons 'closing-match #t))))
(define reconcile-status-list
;; 'filter-types must be either #f (i.e. disable reconcile filter)
;; or a value defined as defined in Query.c
;; e.g. CLEARED-NO for unreconciled
;; (logior CLEARED-NO CLEARED-CLEARED) for unreconciled & cleared
(list
(list 'all
(cons 'text (G_ "Show All Transactions"))
(cons 'filter-types CLEARED-ALL))
(list 'unreconciled
(cons 'text (G_ "Unreconciled only"))
(cons 'filter-types CLEARED-NO))
(list 'cleared
(cons 'text (G_ "Cleared only"))
(cons 'filter-types CLEARED-CLEARED))
(list 'reconciled
(cons 'text (G_ "Reconciled only"))
(cons 'filter-types CLEARED-RECONCILED))))
(define ascending-list
(list
(list 'ascend
(cons 'text (G_ "Ascending")))
(list 'descend
(cons 'text (G_ "Descending")))))
(define sign-reverse-list
(list
(list 'global
(cons 'text (G_ "Use Global Preference"))
(cons 'acct-types #f))
(list 'none
(cons 'text (G_ "Don't change any displayed amounts"))
(cons 'acct-types '()))
(list 'income-expense
(cons 'text (G_ "Income and Expense"))
(cons 'acct-types (list ACCT-TYPE-INCOME ACCT-TYPE-EXPENSE)))
(list 'credit-accounts
(cons 'text (G_ "Credit Accounts"))
(cons 'acct-types (list ACCT-TYPE-LIABILITY ACCT-TYPE-PAYABLE
ACCT-TYPE-EQUITY ACCT-TYPE-CREDIT
ACCT-TYPE-INCOME)))))
(define (keylist-get-info keylist key info)
(assq-ref (assq-ref keylist key) info))
(define (keylist->vectorlist keylist)
(map
(lambda (item)
(vector
(car item)
(keylist-get-info keylist (car item) 'text)))
keylist))
(define (SUBTOTAL-ENABLED? sortkey parameters)
;; this returns whether sortkey *can* be subtotalled/grouped.
;; it checks whether a renderer-fn is defined.
(keylist-get-info (sortkey-list parameters) sortkey 'renderer-fn))
(define (CUSTOM-SORTING? sortkey parameters)
;; sortkey -> bool
;;
;; this returns which sortkeys which *must* use the custom sorter.
;; it filters whereby a split-sortvalue is defined (i.e. the splits
;; can be compared according to their 'sortvalue) but the QofQuery
;; sortkey is not defined (i.e. their 'sortkey is #f).
(and (keylist-get-info (sortkey-list parameters) sortkey 'split-sortvalue)
(not (keylist-get-info (sortkey-list parameters) sortkey 'sortkey))))
(define (lists->csv lst)
;; converts a list of lists into CSV
;; this function aims to follow RFC4180, and will pad lists to
;; ensure equal number of items per row.
;; e.g. '(("from" "01/01/2010")
;; ("to" "31/12/2010")
;; ("total" 23500 30000 25/7 'sym))
;; will output
;; "from","01/01/2010",,,
;; "to","31/12/2010",,,
;; "total",23500.0,30000.0,3.5714285714285716,sym
(define (string-sanitize-csv str)
(call-with-output-string
(lambda (port)
(display #\" port)
(string-for-each
(lambda (c)
(if (char=? c #\") (display #\" port))
(display c port))
str)
(display #\" port))))
(define max-items
(let lp ((lst lst) (maximum 0))
(cond
((null? lst) maximum)
((pair? lst) (lp (cdr lst) (max maximum (length (car lst)))))
(else (error "strify " lst " must be a proper list")))))
(define (strify obj)
(cond
((or (null? obj) (not obj)) "")
((string? obj) (string-sanitize-csv obj))
((number? obj) (number->string (exact->inexact obj)))
((pair? obj) (let lp ((row obj) (acc '()) (pad max-items))
(cond
((zero? pad) (string-concatenate-reverse acc))
((null? row) (lp '() (cons "," acc) (1- pad)))
((pair? row) (lp (cdr row)
(cons* (if (pair? (cdr row)) "," "")
(strify (car row))
acc)
(1- pad)))
(else (error "strify " obj " must be a proper list")))))
((gnc:gnc-monetary? obj) (strify (gnc:gnc-monetary-amount obj)))
(else (object->string obj))))
(string-join (map strify lst) "\n"))
(define gnc:lists->csv lists->csv)
;;
;; Default Transaction Report
;;
(define (gnc:trep-options-generator)
(define parameters
(list
(cons 'split-action (qof-book-use-split-action-for-num-field (gnc-get-current-book)))))
;; (Feb 2018) Note to future hackers - this gnc:trep-options-generator
;; defines a long set of options to be assigned as an object in
;; the report. This long list (52 at Feb 2018 count) of options
;; may be modified in a derived report (see income-gst-statement.scm)
;; via gnc:make-internal! and gnc-unregister-option to hide
;; and remove options, respectively. If an option is unregistered,
;; don't forget to re-register them via gnc-register-option, unless
;; your derived report truly does not require them.
(let ((options (gnc-new-optiondb)))
;; General options
(gnc:options-add-date-interval!
options gnc:pagename-general optname-startdate optname-enddate "a")
(gnc-register-multichoice-option options
gnc:pagename-general optname-date-source
"a5" (G_ "Specify date to filter by…")
"posted"
(list (vector 'posted (G_ "Date Posted"))
(vector 'reconciled (G_ "Reconciled Date"))
(vector 'entered (G_ "Date Entered"))))
(gnc-register-complex-boolean-option options
pagename-currency optname-common-currency
"a" (G_ "Convert all transactions into a common currency.") #f
(lambda (x)
(gnc-optiondb-set-option-selectable-by-name
options pagename-currency optname-currency x)
(gnc-optiondb-set-option-selectable-by-name
options pagename-currency optname-orig-currency x)
(gnc-optiondb-set-option-selectable-by-name
options pagename-currency optname-price-source x)))
(gnc-register-simple-boolean-option options
pagename-currency optname-orig-currency
"b" (G_ "Also show original currency amounts") #f)
(gnc:options-add-currency!
options pagename-currency optname-currency "c")
(gnc:options-add-price-source!
options pagename-currency optname-price-source "d" 'pricedb-nearest)
(gnc-register-simple-boolean-option options
gnc:pagename-general optname-table-export
"g" (G_ "Formats the table suitable for cut & paste exporting with extra cells.")
#f)
(gnc-register-multichoice-option options
gnc:pagename-general optname-infobox-display
"h" (G_ "Add summary of options.")
"no-match"
;; This is an alist of conditions for displaying the infobox
;; 'no-match for empty-report
;; 'match for generated report
(list (vector 'no-match (G_ "If no transactions matched"))
(vector 'always (G_ "Always"))
(vector 'never (G_ "Never"))))
;; Filtering Options
(gnc-register-string-option options
pagename-filter optname-account-matcher
"a5" (G_ "Show only accounts whose full name matches this filter e.g. ':Travel' will match \
Expenses:Travel:Holiday and Expenses:Business:Travel. It can be left blank, which will \
disable the filter.")
"")
(gnc-register-simple-boolean-option options
pagename-filter optname-account-matcher-regex
"a6"
(G_ "By default the account filter will search substring only. Set this to true to \
enable full POSIX regular expressions capabilities. 'Car|Flights' will match both \
Expenses:Car and Expenses:Flights. Use a period (.) to match a single character e.g. \
'20../.' will match 'Travel 2017/1 London'. ")
#f)
(gnc-register-simple-boolean-option options
pagename-filter optname-account-matcher-exclude "a7"
(G_ "If this option is selected, accounts matching filter are excluded.")
#f)
(gnc-register-string-option options
pagename-filter optname-transaction-matcher
"i1" (G_ "Show only transactions where description, notes, or memo matches this filter.
e.g. '#gift' will find all transactions with #gift in description, notes or memo. It can be left \
blank, which will disable the filter.")
"")
(gnc-register-simple-boolean-option options
pagename-filter optname-transaction-matcher-regex
"i2"
(G_ "By default the transaction filter will search substring only. Set this to true to \
enable full POSIX regular expressions capabilities. '#work|#family' will match both \
tags within description, notes or memo.")
#f)
(gnc-register-simple-boolean-option options
pagename-filter optname-transaction-matcher-exclude
"i3"
(G_ "If this option is selected, transactions matching filter are excluded.")
#f)
(gnc-register-simple-boolean-option options
pagename-filter optname-transaction-matcher-caseinsensitive
"i4"
(G_ "If this option is selected, transactions matching filter is not case sensitive.")
#f)
(gnc-register-multichoice-option options
pagename-filter optname-reconcile-status
"j1" (G_ "Filter by reconcile status.")
"all"
(keylist->vectorlist reconcile-status-list))
(gnc-register-multichoice-option options
pagename-filter optname-void-transactions
"k" (N_ "How to handle void transactions.")
"non-void-only"
(keylist->vectorlist show-void-list))
(gnc-register-multichoice-option options
pagename-filter optname-closing-transactions
"l" (G_ "By default most users should not include closing \
transactions in a transaction report. Closing transactions are \
transfers from income and expense accounts to equity, and must usually \
be excluded from periodic reporting.")
"exclude-closing"
(keylist->vectorlist show-closing-list))
;; Accounts options
;; account to do report on
(gnc-register-account-list-option options
gnc:pagename-accounts optname-accounts
"a" (G_ "Report on these accounts.")
;; select, by default, no accounts! Selecting all accounts will
;; always imply an insanely long waiting time upon opening, and it
;; is almost never useful. So we instead display the normal error
;; message saying "Click here", and the user knows how to
;; continue.
'())
(gnc-register-account-list-option options
gnc:pagename-accounts optname-filterby
"c1" (G_ "Filter on these accounts.")
'())
(gnc-register-multichoice-callback-option options
gnc:pagename-accounts optname-filtertype
"c" (G_ "Filter account.")
"none"
(keylist->vectorlist filter-list)
(lambda (x)
(gnc-optiondb-set-option-selectable-by-name
options gnc:pagename-accounts optname-filterby
(not (eq? x 'none)))))
;; Sorting options
(let ((ascending-choice-list (keylist->vectorlist ascending-list))
(key-choice-list (keylist->vectorlist (sortkey-list parameters)))
(date-subtotal-choice-list (keylist->vectorlist date-subtotal-list))
(prime-sortkey 'account-name)
(prime-sortkey-subtotal-true #t)
(prime-date-subtotal 'monthly)
(sec-sortkey 'register-order)
(sec-sortkey-subtotal-true #f)
(sec-date-subtotal 'monthly))
(define (apply-selectable-by-name-sorting-options)
(let* ((prime-sortkey-enabled (not (eq? prime-sortkey 'none)))
(prime-sortkey-subtotal-enabled
(SUBTOTAL-ENABLED? prime-sortkey parameters))
(prime-date-sortingtype-enabled (memq prime-sortkey DATE-SORTING-TYPES))
(sec-sortkey-enabled (not (eq? sec-sortkey 'none)))
(sec-sortkey-subtotal-enabled
(SUBTOTAL-ENABLED? sec-sortkey parameters))
(sec-date-sortingtype-enabled (memq sec-sortkey DATE-SORTING-TYPES))
(tag-sortingtype-enabled
(or (eq? prime-sortkey 'tags) (eq? sec-sortkey 'tags))))
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-prime-subtotal
prime-sortkey-subtotal-enabled)
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-prime-sortorder
prime-sortkey-enabled)
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-sec-subtotal
sec-sortkey-subtotal-enabled)
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-sec-sortorder
sec-sortkey-enabled)
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-full-account-name
(or (and prime-sortkey-subtotal-enabled prime-sortkey-subtotal-true)
(and sec-sortkey-subtotal-enabled sec-sortkey-subtotal-true)))
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-show-account-code
(or (and prime-sortkey-subtotal-enabled prime-sortkey-subtotal-true)
(and sec-sortkey-subtotal-enabled sec-sortkey-subtotal-true)))
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-show-account-description
(or (and prime-sortkey-subtotal-enabled prime-sortkey-subtotal-true)
(and sec-sortkey-subtotal-enabled sec-sortkey-subtotal-true)))
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-indenting
(or (and prime-sortkey-subtotal-enabled prime-sortkey-subtotal-true)
(and sec-sortkey-subtotal-enabled sec-sortkey-subtotal-true)
(and prime-date-sortingtype-enabled (not (eq? 'none prime-date-subtotal)))
(and sec-date-sortingtype-enabled (not (eq? 'none sec-date-subtotal)))))
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-show-subtotals-only
(or (and prime-sortkey-subtotal-enabled prime-sortkey-subtotal-true)
(and sec-sortkey-subtotal-enabled sec-sortkey-subtotal-true)
(and prime-date-sortingtype-enabled (not (eq? 'none prime-date-subtotal)))
(and sec-date-sortingtype-enabled (not (eq? 'none sec-date-subtotal)))))
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-show-informal-headers
(or (memq prime-sortkey (list 'account-name 'account-code))
(memq sec-sortkey (list 'account-name 'account-code))))
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-prime-date-subtotal
prime-date-sortingtype-enabled)
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-sec-date-subtotal
sec-date-sortingtype-enabled)
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-tag-prefix
tag-sortingtype-enabled)
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-remove-tp
tag-sortingtype-enabled)
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-no-match-heading
tag-sortingtype-enabled)
(gnc-optiondb-set-option-selectable-by-name
options pagename-sorting optname-append-tp
tag-sortingtype-enabled)))
;; primary sorting criterion
(gnc-register-multichoice-callback-option options
pagename-sorting optname-prime-sortkey
"a" (G_ "Sort by this criterion first.")
(symbol->string prime-sortkey)
key-choice-list
(lambda (x)
(set! prime-sortkey x)
(apply-selectable-by-name-sorting-options)))
(gnc-register-simple-boolean-option options
pagename-sorting optname-full-account-name
"j1"
(G_ "Show the full account name for subtotals and subheadings?")
#f)
(gnc-register-simple-boolean-option options
pagename-sorting optname-show-account-code
"j2"
(G_ "Show the account code for subtotals and subheadings?")
#f)
(gnc-register-simple-boolean-option options
pagename-sorting optname-show-account-description
"j3"
(G_ "Show the account description for subheadings?")
#f)
(gnc-register-simple-boolean-option options
pagename-sorting optname-show-informal-headers
"j4"
(G_ "Show the informal headers for debit/credit accounts?")
#f)
(gnc-register-simple-boolean-option options
pagename-sorting optname-indenting
"j5"
(G_ "Add indenting columns with grouping and subtotals?")
#t)
(gnc-register-simple-boolean-option options
pagename-sorting optname-show-subtotals-only
"j6"
(G_ "Show subtotals only, hiding transactional detail?")
#f)
(gnc-register-complex-boolean-option options
pagename-sorting optname-prime-subtotal
"e5"
(G_ "Subtotal according to the primary key?") prime-sortkey-subtotal-true
(lambda (x)
(set! prime-sortkey-subtotal-true x)
(apply-selectable-by-name-sorting-options)))
(gnc-register-multichoice-callback-option options
pagename-sorting optname-prime-date-subtotal
"e2" (G_ "Do a date subtotal.")
(symbol->string prime-date-subtotal)
date-subtotal-choice-list
(lambda (x)
(set! prime-date-subtotal x)
(apply-selectable-by-name-sorting-options)))
(gnc-register-multichoice-option options
pagename-sorting optname-prime-sortorder
"e" (G_ "Order of primary sorting.")
"ascend"
ascending-choice-list)
;; Secondary sorting criterion
(gnc:register-multichoice-callback-option options
pagename-sorting optname-sec-sortkey
"f"
(G_ "Sort by this criterion second.")
(symbol->string sec-sortkey)
key-choice-list
(lambda (x)
(set! sec-sortkey x)
(apply-selectable-by-name-sorting-options)))
(gnc-register-complex-boolean-option options
pagename-sorting optname-sec-subtotal
"i5"
(G_ "Subtotal according to the secondary key?") sec-sortkey-subtotal-true
(lambda (x)
(set! sec-sortkey-subtotal-true x)
(apply-selectable-by-name-sorting-options)))
(gnc-register-multichoice-callback-option options
pagename-sorting optname-sec-date-subtotal
"i2" (G_ "Do a date subtotal.")
(symbol->string sec-date-subtotal)
date-subtotal-choice-list
(lambda (x)
(set! sec-date-subtotal x)
(apply-selectable-by-name-sorting-options)))
(gnc-register-multichoice-option options
pagename-sorting optname-sec-sortorder
"i" (G_ "Order of Secondary sorting.")
"ascend"
ascending-choice-list)
;; Tags options
(gnc-register-string-option options
pagename-sorting optname-tag-prefix
"k1"
(G_ "Use this option along with the Tags primary or secondary key \
and subtotals in the sorting options. \
By default tags start with # but you can use any other prefix of any \
length starting with any character. \
The report engine will attempt to find a match in the split memo first, \
then the transaction notes, and finally the transaction description. \
The pattern is case sensitive, so it will match uppercase and lowercase \
letters separately.")
"#")
(gnc-register-simple-boolean-option options
pagename-sorting optname-remove-tp
"k2" (G_ "Remove tag prefix from the subtotal headings displayed in report?") #f)
(gnc-register-string-option options
pagename-sorting optname-no-match-heading
"k3"
(G_ "The heading that should be displayed for the subtotal group that \
contains the transactions with no matching tags. Default is 'No Match'.")
"No Match")
(gnc-register-simple-boolean-option options
pagename-sorting optname-append-tp
"k4" (G_ "Append tag prefix to no-match heading specified above?") #t))
;; Display options
(let ((disp-memo? #t)
(disp-accname? #f)
(disp-other-accname? #t)
(disp-detail-level? 'single)
(amount-value 'double))
(define (apply-selectable-by-name-display-options)
(define detail-is-single? (eq? disp-detail-level? 'single))