/
taxtxf.scm
3413 lines (3324 loc) · 185 KB
/
taxtxf.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
;; -*-scheme-*-
;; by Richard -Gilligan- Uschold
;;
;; updated by J. Alex Aycinena, July 2008, October 2009
;;
;; This report prints transaction details and account totals for accounts
;; relevant to United States taxes, sorted by form/schedule, copy, line
;; and tax code, and exports TXF files for import to TaxCut, TurboTax, etc.
;;
;; For this to work, the user has to segregate taxable and not taxable
;; income to different accounts, as well as deductible and non-
;; deductible expenses and the accounts need to be referenced to the tax codes.
;; However, there is no need to limit tax codes to just one account. For codes
;; like N286 (Dividend, Ordinary) that can have the "payer" printed on
;; Schedule B on separate lines, to have amounts from different accounts
;; summarized together for one "payer" line, the accounts referenced to the
;; same tax code for a given "payer" need to be adjacent to each other in the
;; account hierarchy.
;;
;; The user selects the accounts(s) to be printed; if none are specified, all
;; are selected. Includes all sub-account levels below selected account, that
;; are coded for taxes.
;;
;; Optionally, does NOT print tax codes and accounts with $0.00 values.
;; Prints data between the From and To dates, inclusive.
;; Optional alternate periods:
;; "Last Year", "1st Est Tax Quarter", ... "4th Est Tax Quarter"
;; "Last Yr Est Tax Qtr", ... "Last Yr Est Tax Qtr"
;; Estimated Tax Quarters: Dec 31, Mar 31, Jun 30, Aug 31
;; Optionally prints brief or full account names
;; Optionally prints multi-split details for transactions
;; Optionally prints TXF export parameters for codes and accounts
;; Optionally prints Action/Memo data for a transaction split
;; Optionally prints transaction detail
;; Optionally uses special date processing for selected accounts (see
;; definition for 'txf-special-split?' in the code below)
;; Optionally shades alternate transactions for ease of reading
;; Converts non-USD transaction amounts based on transaction data or, if
;; transaction data is not applicable, on pricedb and user specified date:
;; nearest transaction date or nearest report end date. Converts to zero
;; if there is no entry in pricedb and provides comment accordingly.
;;
;; November, 2009 Update:
;;
;; Add support for multiple copies of Forms/Schedules
;; Add support for Format 6
;; Use Form/Schedule line #'s to sort report.
;; Update from "V037" to "V041"
;; Add support for taxpayer types other than F1040
;;
;; September, 2010 Update:
;;
;; Add support for code N673, Format 4
;;
;; September, 2012 Update:
;;
;; Add support of book option for num-source; use function gnc-get-num-action in
;; place of xaccTransGetNum and function gnc-get-action-num in place of
;; xaccSplitGetAction and modify report headings accordingly
;;
;; February, 2013 Update:
;;
;; Fix beginning balance sign and signs for Transfer From/To amounts for
;; liability/equity accounts
;;
;; January, 2019 Update:
;;
;; Update from "V041" to "V042", although added codes are not implemented
;; because cost/gain data not reliably available
;; The format for code 673 can be 4 or 5, per spec, so leave as 4
;; Fix beginning balance off-by-one-day error for B/S accounts
;;
;; From prior version:
;; NOTE: setting of specific dates is squirly! and seems
;; to be current-date dependent! Actually, time of day dependent! Just
;; after midnight gives diffenent dates than just before! Referencing
;; all times to noon seems to fix this. Subtracting 1 year sometimes
;; subtracts 2! see "(to-value"
;;
;; Based on prior taxtxf.scm and with references to transaction.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 gnu@gnu.org
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-module (gnucash report taxtxf))
(use-modules (gnucash utilities))
(use-modules (srfi srfi-1))
(use-modules (gnucash gnc-module))
(use-modules (gnucash gettext))
(eval-when
(compile load eval expand)
(load-extension "libgncmod-gnome-utils" "scm_init_sw_gnome_utils_module"))
(use-modules (sw_gnome_utils)) ;; to get to gnc-error-dialog
(use-modules (gnucash core-utils)) ; for gnc:version
(gnc:module-load "gnucash/html" 0)
(gnc:module-load "gnucash/tax/us" 0)
(gnc:module-load "gnucash/report/report-system" 0)
(define reportname (N_ "Tax Schedule Report/TXF Export"))
;(define USD-currency (gnc-commodity-table-lookup
; (gnc-commodity-table-get-table (gnc-get-current-book))
; "CURRENCY"
; "USD"))
;; this comes back as PEN??? rather than USD; need to define further down to get
;; to work - weird
(define selected-accounts-sorted-by-form-line-acct (list))
(define today (time64CanonicalDayTime (current-time)))
(define bdtm
(let ((result (gnc-localtime today)))
(set-tm:mday result 16) ; 16
(set-tm:mon result 3) ; Apr
(set-tm:isdst result -1)
result))
(define tax-day (gnc-mktime bdtm))
(define after-tax-day (< tax-day today))
(define (make-split-list account split-filter-pred)
(filter split-filter-pred (xaccAccountGetSplitList account)))
;; returns a predicate that returns true only if a split is
;; between early-date and late-date
(define (split-report-make-date-filter-predicate begin-date-t64 end-date-t64)
(lambda (split)
(let ((t64 (xaccTransGetDate (xaccSplitGetParent split))))
(and (>= t64 begin-date-t64)
(<= t64 end-date-t64)))))
;; This is nearly identical to, and could be shared with
;; display-report-list-item in report.scm. This adds warn-msg parameter
(define (gnc:display-report-list-item item port warn-msg)
(cond
((string? item) (display item port))
((null? item) #t)
((list? item) (map (lambda (item)
(gnc:display-report-list-item item port warn-msg))
item))
(else (gnc:warn warn-msg item " is the wrong type."))))
;; IRS asked congress to make the tax quarters the same as real quarters
;; This is the year it is effective. THIS IS A Y10K BUG!
(define tax-qtr-real-qtr-year 10000)
(define (tax-options-generator)
(define options (gnc:new-options))
(define (gnc:register-tax-option new-option)
(gnc:register-option options new-option))
;; date at which to report
(gnc:options-add-date-interval!
options gnc:pagename-general
(N_ "From") (N_ "To") "a")
(gnc:register-tax-option
(gnc:make-multichoice-option
gnc:pagename-general (N_ "Alternate Period")
"c" (N_ "Override or modify From: & To:.")
(if after-tax-day 'from-to 'last-year)
(list (list->vector
(list 'from-to (N_ "Use From - To") (N_ "Use From - To period.")))
(list->vector
(list '1st-est (N_ "1st Est Tax Quarter") (N_ "Jan 1 - Mar 31.")))
(list->vector
(list '2nd-est (N_ "2nd Est Tax Quarter") (N_ "Apr 1 - May 31.")))
(list->vector
;; Translators: The US tax quarters are different from
;; actual year's quarters! See the definition of
;; tax-qtr-real-qtr-year variable above.
(list '3rd-est (N_ "3rd Est Tax Quarter") (N_ "Jun 1 - Aug 31.")))
(list->vector
(list '4th-est (N_ "4th Est Tax Quarter") (N_ "Sep 1 - Dec 31.")))
(list->vector
(list 'last-year (N_ "Last Year") (N_ "Last Year.")))
(list->vector
(list '1st-last (N_ "Last Yr 1st Est Tax Qtr")
(N_ "Jan 1 - Mar 31, Last year.")))
(list->vector
(list '2nd-last (N_ "Last Yr 2nd Est Tax Qtr")
(N_ "Apr 1 - May 31, Last year.")))
(list->vector
(list '3rd-last (N_ "Last Yr 3rd Est Tax Qtr")
;; Translators: The US tax quarters are different from
;; actual year's quarters! See the definition of
;; tax-qtr-real-qtr-year variable above.
(N_ "Jun 1 - Aug 31, Last year.")))
(list->vector
(list '4th-last (N_ "Last Yr 4th Est Tax Qtr")
(N_ "Sep 1 - Dec 31, Last year."))))))
(gnc:register-tax-option
(gnc:make-account-list-option
gnc:pagename-accounts (N_ "Select Accounts (none = all)")
"d" (N_ "Select accounts.")
(lambda () '())
#f #t))
(gnc:register-tax-option
(gnc:make-simple-boolean-option
gnc:pagename-display (N_ "Suppress $0.00 values")
"f" (N_ "$0.00 valued Tax codes won't be printed.") #f))
(gnc:register-tax-option
(gnc:make-simple-boolean-option
gnc:pagename-display (N_ "Do not print full account names")
"g" (N_ "Do not print all Parent account names.") #f))
(gnc:register-tax-option
(gnc:make-simple-boolean-option
gnc:pagename-display (N_ "Print all Transfer To/From Accounts")
"h" (N_ "Print all split details for multi-split transactions.") #f))
(gnc:register-tax-option
(gnc:make-simple-boolean-option
gnc:pagename-display (N_ "Print TXF export parameters")
"i" (N_ "Show TXF export parameters for each TXF code/account on report.") #f))
(if (qof-book-use-split-action-for-num-field (gnc-get-current-book))
(gnc:register-tax-option
(gnc:make-simple-boolean-option
gnc:pagename-display (N_ "Do not print T-Num:Memo data")
"j" (N_ "Do not print T-Num:Memo data for transactions.") #f))
(gnc:register-tax-option
(gnc:make-simple-boolean-option
gnc:pagename-display (N_ "Do not print Action:Memo data")
"j" (N_ "Do not print Action:Memo data for transactions.") #f)))
(gnc:register-tax-option
(gnc:make-simple-boolean-option
gnc:pagename-display (N_ "Do not print transaction detail")
"k" (N_ "Do not print transaction detail for accounts.") #f))
(gnc:register-tax-option
(gnc:make-simple-boolean-option
gnc:pagename-display (N_ "Do not use special date processing")
"l" (N_ "Do not print transactions out of specified dates.") #f))
(gnc:register-tax-option
(gnc:make-multichoice-option
gnc:pagename-display (N_ "Currency conversion date")
"m" (N_ "Select date to use for PriceDB lookups.")
'conv-to-tran-date
(list (list->vector
(list 'conv-to-tran-date (N_ "Nearest transaction date") (N_ "Use nearest to transaction date.")))
(list->vector
(list 'conv-to-report-date (N_ "Nearest report date") (N_ "Use nearest to report date.")))
)))
#t
(gnc:options-set-default-section options gnc:pagename-general)
options)
;; Render txf information
(define crlf (string #\return #\newline)) ; TurboTax seems to want these
(define txf-last-payer "") ; if same as current, inc txf-l-count
; this only works if different
; accounts from the same payer are
; grouped in the accounts list
(define txf-l-count 0) ; count repeated N codes
;; stores invalid txf codes so we can list
(define txf-invalid-alist '())
(define txf-account-name "")
(define (gnc:account-get-txf account)
(and (xaccAccountGetTaxRelated account)
(not (equal? (gnc:account-get-txf-code account) 'N000))))
(define (gnc:account-get-txf-code account)
(let ((code (xaccAccountGetTaxUSCode account)))
(string->symbol (if (string-null? code) "N000" code))))
(define (get-acct-txf-info info-type acct-type code)
(let ((categories (assv-ref
(list (cons ACCT-TYPE-INCOME txf-income-categories)
(cons ACCT-TYPE-EXPENSE txf-expense-categories)
(cons ACCT-TYPE-BANK txf-asset-categories)
(cons ACCT-TYPE-CASH txf-asset-categories)
(cons ACCT-TYPE-ASSET txf-asset-categories)
(cons ACCT-TYPE-STOCK txf-asset-categories)
(cons ACCT-TYPE-MUTUAL txf-asset-categories)
(cons ACCT-TYPE-RECEIVABLE txf-asset-categories)
(cons ACCT-TYPE-CREDIT txf-liab-eq-categories)
(cons ACCT-TYPE-LIABILITY txf-liab-eq-categories)
(cons ACCT-TYPE-EQUITY txf-liab-eq-categories)
(cons ACCT-TYPE-PAYABLE txf-liab-eq-categories))
acct-type))
(get-info-fn
(case info-type
((form) gnc:txf-get-form)
((desc) gnc:txf-get-description)
((pns) gnc:txf-get-payer-name-sourcecategories)
((format) gnc:txf-get-formatcategories)
((multiple) gnc:txf-get-multiplecategories)
((cat-key) gnc:txf-get-category-keycategories)
((line) gnc:txf-get-line-datacategories)
((last-yr) gnc:txf-get-last-yearcategories)
(else #f))))
(and categories
get-info-fn
(get-info-fn categories code (gnc-get-current-book-tax-type)))))
(define (gnc:account-get-txf-payer-source account)
(let ((pns (xaccAccountGetTaxUSPayerNameSource account)))
(string->symbol (if (string-null? pns) "none" pns))))
;; some codes require split detail, only two for now, Federal estimated tax,
;; qrtrly and state estimated tax, qrtrly
(define (txf-special-split? code)
(member code (list 'N521 'N522)))
;; some codes require special date handling, only one for now, Federal estimated
;; tax, qrtrly
(define (txf-special-date? code)
(member code (list 'N521)))
(define (txf-beg-bal-only? code)
(member code (list 'N440))) ;only one so far: F8606, IRA basis at beg of year
(define (fill-clamp-sp str len)
(string-append (substring (string-append str (make-string len #\space))
0 (- len 1)) " "))
(define (fill-clamp str len)
(string-append (substring (string-append str (make-string len #\space))
0 len)))
(define (render-header-row table heading-line-text)
(let ((heading (gnc:make-html-text)))
(gnc:html-text-append! heading (gnc:html-markup-b heading-line-text))
(let ((heading-cell (gnc:make-html-table-cell/markup
"header-just-top" heading)))
(gnc:html-table-cell-set-colspan! heading-cell 6)
(gnc:html-table-append-row!
table
(list heading-cell)
)
)
)
)
(define (render-account-detail-header-row table suppress-action-memo? format4?)
(gnc:html-table-append-row!
table
(append (list (gnc:make-html-table-header-cell/markup
"column-heading-center" "Date"))
(list (gnc:make-html-table-header-cell/markup
"column-heading-center"
(if (qof-book-use-split-action-for-num-field
(gnc-get-current-book))
"Num/Action"
"Num")))
(list (gnc:make-html-table-header-cell/markup
"column-heading-center" "Description"))
(list (gnc:make-html-table-header-cell/markup
"column-heading-center"
(if suppress-action-memo?
"Notes"
(if (qof-book-use-split-action-for-num-field
(gnc-get-current-book))
"Notes/T-Num:Memo"
"Notes/Action:Memo"))))
(list (gnc:make-html-table-header-cell/markup
"column-heading-center"
(if format4?
"Capital Gains Data"
"Transfer To/From Account(s)")))
(list (gnc:make-html-table-header-cell/markup
"column-heading-center" "Amount"))
)
)
)
(define (render-total-row table total-amount total-line-text tax_code?
transaction-details? end-bal-text total-amount-neg?
format4? cap-gain-sales-total cap-gain-basis-total)
(let ((description (gnc:make-html-text))
(total (gnc:make-html-text)))
(if (or tax_code? transaction-details?)
(gnc:html-text-append! description (gnc:html-markup-b
(string-append " "
(if end-bal-text end-bal-text "Total For "))))
(if (not tax_code?)
(gnc:html-text-append! description (gnc:html-markup-b
" "))
)
)
(gnc:html-text-append! description (gnc:html-markup-b
total-line-text))
(gnc:html-text-append! description (gnc:html-markup-b
" "))
(gnc:html-text-append! total (gnc:html-markup-b
total-amount))
(let ((description-cell (if (or tax_code? transaction-details?)
(gnc:make-html-table-cell/markup
"column-heading-right" description)
(gnc:make-html-table-cell description)))
(amount-table (gnc:make-html-table)) ;; to line up totals to details
(cap-gains-detail-table (gnc:make-html-table))
)
(gnc:html-table-set-style! amount-table "table"
'attribute (list "border" "0")
'attribute (list "cellspacing" "0")
'attribute (list "cellpadding" "0")
'attribute (list "width" "100%"))
(gnc:html-table-set-style! cap-gains-detail-table "table"
'attribute (list "border" "0")
'attribute (list "cellspacing" "0")
'attribute (list "cellpadding" "0")
'attribute (list "width" "100%"))
(let ((total-amnt (if total-amount-neg?
(gnc:make-html-table-cell/markup
"number-cell-bot-neg" total)
(gnc:make-html-table-cell/markup
"number-cell-bot" total)))
)
(gnc:html-table-append-row! amount-table total-amnt)
)
(if format4?
(let* ((total-cell (gnc:make-html-table-cell/markup
"column-heading-right"
"Capital Gains Totals: Sales Amount: "))
(combined-cell (gnc:make-html-table-cell))
)
(gnc:html-table-cell-append-objects!
total-cell
cap-gain-sales-total
" Cost/Basis: Not Available"
)
(gnc:html-table-append-row! cap-gains-detail-table
(append (list total-cell)))
(gnc:html-table-append-row! cap-gains-detail-table
(append (list description-cell)))
(gnc:html-table-cell-append-objects!
combined-cell
cap-gains-detail-table
)
(gnc:html-table-cell-set-colspan! combined-cell 5)
(gnc:html-table-append-row! table
(append (list combined-cell)
(list (gnc:make-html-table-cell/markup
"number-cell-bot" amount-table))))
)
(begin
(gnc:html-table-cell-set-colspan! description-cell 5)
(gnc:html-table-append-row! table
(append (list description-cell)
(list (gnc:make-html-table-cell/markup
"number-cell-bot" amount-table))))
)
) ;; end of if
) ;; end of let
) ;; end of let
)
(define (render-txf-account account account-value d? date x? x-date
type code copy tax-entity-type sold-desc)
(let* ((print-info (gnc-account-print-info account #f))
(txf? (gnc:account-get-txf account)))
(if (and txf?
(not (gnc-numeric-zero-p account-value)))
(let* ((date-str (if date
(strftime "%m/%d/%Y" (gnc-localtime date))
#f))
(x-date-str (if x-date
(strftime "%m/%d/%Y" (gnc-localtime x-date))
#f))
;; Only formats 1,3,4,6 implemented now! Others are treated as 1.
(format_type (get-acct-txf-info 'format type code))
(action (if (eq? type ACCT-TYPE-INCOME)
(case code
((N286 N488) "ReinvD")
(else "Income"))
(if (eq? type ACCT-TYPE-EXPENSE)
"Expense"
(if (or (eq? type ACCT-TYPE-BANK)
(eq? type ACCT-TYPE-CASH)
(eq? type ACCT-TYPE-ASSET)
(eq? type ACCT-TYPE-STOCK)
(eq? type ACCT-TYPE-MUTUAL)
(eq? type ACCT-TYPE-RECEIVABLE))
"Asset"
(if (or (eq? type ACCT-TYPE-CREDIT)
(eq? type ACCT-TYPE-LIABILITY)
(eq? type ACCT-TYPE-PAYABLE))
"Liability"
(if (eq? type ACCT-TYPE-EQUITY)
"Equity"
""))))))
(category-key (get-acct-txf-info 'cat-key type code))
(value-name (cond
((string=? tax-entity-type "F1040")
(if (equal? "ReinvD" action)
(string-append
(xaccPrintAmount
(gnc-numeric-neg account-value) print-info)
" " txf-account-name)
txf-account-name))
((or (string=? tax-entity-type "F1065")
(string=? tax-entity-type "F1120S"))
(if (string=? (xaccAccountGetName account)
txf-account-name)
""
(xaccAccountGetName account)))
(else "")))
(value (string-append "$" ; in txf output, income is positive; expense negative
; liabilities are positive, assets are negative;
; essentially, just reverse signs on dr's & cr's
(format #f "~0,2f" (gnc-numeric-to-double
(gnc-numeric-neg
account-value)))))
)
;; Based on TXF Spec of 11/30/11, V 042, and Quicken 98 output, the
;; fields by format are as follows, for F1040:
;; Format Type Fields Comments/Status
;; 0 D T, N, C, L, X Spec unclear, unverified
;; 0 S T, N, C, L Spec unclear, unverified
;; 1 D T, N, C, L, $, X Spec clear & verified Q98
;; 1 S T, N, C, L, $ Spec clear & verified Q98
;; 2 D T, N, C, L, P, X Spec unclear, unverified
;; 2 S T, N, C, L, P Spec unclear, unverified
;; 3 D T, N, C, L, $, X Spec clear & verified Q98
;; 3 S T, N, C, L, $, P Spec clear & verified Q98
;; 4 D T, N, C, L, P, D, D, $, $, X Spec clear, unverified
;; 4 S T, N, C, L, $, $ Spec unclear, unverified
;; 5 D T, N, C, L, P, D, D, $, $, $ Spec unclear, unverified
;; 5 S T, N, C, L, $, $, $ Spec unclear, unverified
;; 6 D T, N, C, L, D, $, P, X Spec unclear, verified Q98
;; 6 S T, N, C, L, $ Spec unclear, verified Q98
;;
;; For F1040, support only formats 1, 3 and 6 (based on Q98) and
;; for 4 (guessed at) for code 673 only (no date acquired or basis)
;; For F1065, F1120 and F1120S, support only format 1
;;
(list (if x? "TD" "TS") crlf
(symbol->string code) crlf
(string-append "C" copy) crlf
;; not to be confused with Form/Sched line number: so for
;; example, Schedule B line 5 for 2008 has separate lines for
;; individual payers of ordinary dividends so these are like
;; sub-lines of line 5 starting with 1 for first reported payer
;; these apply if pns is either 'current or 'parent', but not
;; otherwise
"L" (number->string txf-l-count) crlf
(if (= format_type 4)
(if x?
(list "P" sold-desc crlf "D" crlf "D" date-str crlf
"$" crlf)
(list "$" crlf))
'())
(if (and d? (= format_type 6) x?)
(list "D" date-str crlf)
'())
(case format_type
((1 3 4 6) (list value crlf))
((0 2 5) '()))
(case format_type
((3) (if (not x?) (list "P" txf-account-name crlf) '()))
((6) (if x?
(if (string=? "N521" (symbol->string code))
(list "P" crlf) ;; Federal, no state initials
(list "P" txf-account-name crlf));; state initials
'())) ;; not detail
(else '()))
(if x?
(cond
((string=? tax-entity-type "F1040")
(list "X" x-date-str " "
(fill-clamp-sp txf-account-name 31)
(fill-clamp-sp action 7)
(fill-clamp-sp value-name 82)
(fill-clamp category-key 15) crlf))
((or (string=? tax-entity-type "F1065")
(string=? tax-entity-type "F1120")
(string=? tax-entity-type "F1120S"))
(list "X"
(fill-clamp "" 47)
(fill-clamp-sp txf-account-name 41)
(if (string=? value-name "")
(list crlf)
(list
(fill-clamp "" 41)
(fill-clamp value-name 15) crlf))))
(else '())))
"^" crlf))
"")))
(define (process-currency-conversion split
USD-currency
account-commodity
lookup-date
trans-currency
splt-rpt-amount
print-info
neg?)
;; called if account-commodity does not equal USD-currency or if
;; trans-currency not equal USD-currency
;; if trans-currency = USD-currency and account-commodity does not equal
;; USD-currency, use split value & transaction rate
;; if account-commodity = USD-currency and trans-currency does not equal
;; USD-currency, use split amount & inverse of transaction rate
;; if neither trans-currency nor account-commodity = USD-currency,
;; use split amount & pricedb lookup using lookup date
;; returns the converted amount, the conversion text, and, if the conversion
;; price was looked up, the pricedb-lookup-price and addtitional text in
;; a list
(let*
((splt-value (if (and split
(or (gnc-commodity-equiv trans-currency USD-currency)
(gnc-commodity-equiv account-commodity
USD-currency)))
(xaccSplitGetValue split)
100/100))
(missing-pricedb-entry? #f)
(pricedb-lookup-price #f)
(pricedb-lookup-price-value (gnc-numeric-zero))
(pricedb-lookup-price-time 0)
(amount (if (gnc-commodity-equiv trans-currency USD-currency)
splt-value
(if (gnc-commodity-equiv account-commodity USD-currency)
(if neg?
(gnc-numeric-neg splt-rpt-amount)
splt-rpt-amount)
;; otherwise lookup from pricedb
(let ((pricedb (gnc-pricedb-get-db (gnc-get-current-book)))
) ;; if we can convert to USD
(if (gnc-pricedb-has-prices pricedb
account-commodity
USD-currency)
(begin ;; do so
(set! missing-pricedb-entry? #f)
(set! pricedb-lookup-price
(let ((price (gnc-pricedb-lookup-nearest-in-time64
pricedb
account-commodity
USD-currency
(time64CanonicalDayTime
lookup-date))))
(if (gnc-commodity-equiv account-commodity (gnc-price-get-currency price))
(set! price (gnc-price-invert price)))
price))
(set! pricedb-lookup-price-value
(gnc-price-get-value
pricedb-lookup-price))
(set! pricedb-lookup-price-time
(gnc-price-get-time64 pricedb-lookup-price))
(gnc-pricedb-convert-balance-nearest-price-t64
pricedb
(if neg?
(gnc-numeric-neg splt-rpt-amount)
splt-rpt-amount)
account-commodity
USD-currency
;; Use midday as the transaction time so it matches a price
;; on the same day. Otherwise it uses midnight which will
;; likely match a price on the previous day
(time64CanonicalDayTime lookup-date))
)
(begin ;; otherwise set flag and set to zero
(set! missing-pricedb-entry? #t)
(gnc-numeric-zero)
)))
)
)
)
(amount (if neg?
(gnc-numeric-neg amount)
amount))
(converted-qty (xaccPrintAmount
(if (gnc-commodity-equiv account-commodity USD-currency)
(if neg?
(gnc-numeric-neg splt-value)
splt-value
)
splt-rpt-amount
)
print-info))
(conversion-text (if missing-pricedb-entry?
(string-append
"(Missing priceDB entry to convert "
(gnc-commodity-get-mnemonic account-commodity)
" "
converted-qty
" to USD. Go to the Tools->Price Editor dialog to enter prices. Set to zero for this report.)"
)
(string-append
"(Converted "
(if (gnc-commodity-equiv account-commodity
USD-currency)
(gnc-commodity-get-mnemonic trans-currency)
(gnc-commodity-get-mnemonic account-commodity)
)
" "
converted-qty
(if
(and (not (gnc-commodity-equiv account-commodity
USD-currency))
(not (gnc-commodity-equiv trans-currency
USD-currency))
)
(string-append " @ PriceDB lookup rate of ")
(string-append
" @ transaction split rate of "
(xaccPrintAmount
(if (not (gnc-commodity-equiv
trans-currency
USD-currency))
(gnc-numeric-div
100/100
(xaccSplitGetSharePrice split)
GNC-DENOM-AUTO
(logior (GNC-DENOM-SIGFIGS 6)
GNC-RND-ROUND))
(xaccSplitGetSharePrice split)
)
print-info)
")"
)
)
)
)
)
(conversion-text2 (if missing-pricedb-entry?
""
(if (and (not (gnc-commodity-equiv account-commodity
USD-currency))
(not (gnc-commodity-equiv trans-currency
USD-currency))
)
(string-append
" on "
(strftime "%Y-%b-%d"
(gnc-localtime pricedb-lookup-price-time))
")"
)
""))
)
)
(list amount conversion-text pricedb-lookup-price conversion-text2)
)
)
(define (process-transaction-multi-transfer-detail split parent
USD-currency full-names? trans-date trans-currency acct-type
currency-conversion-date to-date transfer-table print-amnt format_type
split-details? tax-mode? account account-type tax-code copy
tax-entity-type)
(let* ((all-tran-splits (xaccTransGetSplitList parent))
(tran-splits-to-render (- (length all-tran-splits) 1))
(acct-print-info (gnc-account-print-info account #f))
(output '())
(trans-rpt-currency-total (gnc-numeric-zero)) ;;for USD-currency
(cap-gains-detail-table (gnc:make-html-table))
(trans-sub-heading-table (gnc:make-html-table))
(trans-sub-table (gnc:make-html-table))
(trans-cap-gain-sales-USD-total (gnc-numeric-zero))
(trans-cap-gain-basis-USD-total (gnc-numeric-zero))
)
(if (= 4 format_type)
(begin
(gnc:html-table-set-style! cap-gains-detail-table "table"
'attribute (list "border" "0")
'attribute (list "cellspacing" "0")
'attribute (list "cellpadding" "3")
'attribute (list "width" "100%"))
(gnc:html-table-set-style! trans-sub-heading-table "table"
'attribute (list "cellspacing" "0")
'attribute (list "cellpadding" "0")
'attribute (list "width" "100%"))
(gnc:html-table-set-style! trans-sub-table "table"
'attribute (list "border" "0")
'attribute (list "cellspacing" "0")
'attribute (list "cellpadding" "0")
'attribute (list "width" "100%"))
(gnc:html-table-append-row! cap-gains-detail-table
(append (list (gnc:make-html-table-cell/markup
"column-heading-center" "Item"))
(list (gnc:make-html-table-cell/markup
"column-heading-center"
"Date Acquired"))
(list (gnc:make-html-table-cell/markup
"column-heading-center"
"Date Sold"))
(list (gnc:make-html-table-cell/markup
"column-heading-center"
"Sales Amount"))
(list (gnc:make-html-table-cell/markup
"column-heading-center"
"Cost/Basis"))
)
)
(gnc:html-table-append-row! trans-sub-heading-table
(append (list (gnc:make-html-table-header-cell/markup
"column-heading-center"
"Transfer To/From Account(s)"))))
)
)
(map (lambda (tran-split)
(if (not (xaccSplitEqual split tran-split #t #f #f))
(let* ((split-acct (xaccSplitGetAccount tran-split))
(split-acct-commodity
(xaccAccountGetCommodity split-acct))
(splt-amnt (xaccSplitGetAmount tran-split))
(splt-amnt (if (or (eq? acct-type ACCT-TYPE-INCOME)
(eq? acct-type ACCT-TYPE-CREDIT)
(eq? acct-type ACCT-TYPE-PAYABLE)
(eq? acct-type ACCT-TYPE-LIABILITY)
(eq? acct-type ACCT-TYPE-EQUITY))
splt-amnt
(gnc-numeric-neg splt-amnt)))
(print-info (gnc-account-print-info split-acct #f))
(splt-curr-conv-note "")
(splt-curr-conv-data (list splt-amnt
splt-curr-conv-note #f ""))
(splt-curr-conv-data (if (and (gnc-commodity-equiv
split-acct-commodity
USD-currency)
(gnc-commodity-equiv
trans-currency
USD-currency))
splt-curr-conv-data
(process-currency-conversion
tran-split
USD-currency
split-acct-commodity
(if (equal?
currency-conversion-date
'conv-to-tran-date)
trans-date
to-date)
trans-currency
splt-amnt
print-info
(if (eq? acct-type
ACCT-TYPE-INCOME)
#f
#t))
))
(splt-print-amnt (car splt-curr-conv-data))
(splt-account-name (if full-names?
(gnc-account-get-full-name
split-acct)
(xaccAccountGetName split-acct)
))
(cell (gnc:make-html-table-cell
(if (string=? (cadr splt-curr-conv-data) "")
splt-account-name
(string-append splt-account-name " "
(cadr splt-curr-conv-data))
)
))
)
(if (caddr splt-curr-conv-data)
(begin
(gnc:html-table-cell-append-objects!
cell
(gnc:html-price-anchor
(caddr splt-curr-conv-data) #f))
(gnc:html-table-cell-append-objects!
cell
(car (cdddr splt-curr-conv-data)))
)
#f
)
(if (and (= 4 format_type) (gnc-numeric-negative-p
(xaccSplitGetAmount tran-split)))
(begin
(if tax-mode?
(gnc:html-table-append-row!
cap-gains-detail-table
(append (list (gnc:make-html-table-cell
(string-append
(xaccPrintAmount
(gnc-numeric-neg
(xaccSplitGetAmount
tran-split))
print-info)
" "
(gnc-commodity-get-mnemonic
split-acct-commodity))))
(list (gnc:make-html-table-cell/markup
"text-cell-center"
"Not Available"))
(list (gnc:make-html-table-cell/markup
"text-cell-center"
(strftime "%Y-%b-%d" (gnc-localtime
trans-date))))
(list (gnc:make-html-table-cell/markup
"number-cell-bot"
(xaccPrintAmount
(gnc-numeric-neg
splt-print-amnt)
acct-print-info)))
(list (gnc:make-html-table-cell/markup
"number-cell-bot"
"Not Available"))
)
)
(let ((cap-gain-output
(list (render-txf-account
account
splt-print-amnt
#f
trans-date
#t
trans-date
account-type
tax-code
copy
tax-entity-type
(string-append
(xaccPrintAmount
(gnc-numeric-neg
(xaccSplitGetAmount
tran-split)) print-info)
" "
(gnc-commodity-get-mnemonic
split-acct-commodity))
)))
)
(set! output
(if (null? output)
(if (null? cap-gain-output)
'()
(list cap-gain-output))
(if (null? cap-gain-output)
(list output)
(list output
cap-gain-output)))
)
)
)
(set! trans-cap-gain-sales-USD-total
(gnc-numeric-add-fixed
trans-cap-gain-sales-USD-total
(gnc-numeric-neg splt-print-amnt))
)
; (set! trans-cap-gain-basis-USD-total #f)
)
)
(let* ((splt-amnt-anchor (gnc:html-split-anchor
tran-split
splt-print-amnt))
(splt-amnt-anchor
(if (gnc-numeric-negative-p splt-print-amnt)
(gnc:make-html-table-cell/markup
"number-cell-bot-neg" splt-amnt-anchor)
(gnc:make-html-table-cell/markup
"number-cell-bot" splt-amnt-anchor)))
)
(if (= 4 format_type)
(gnc:html-table-append-row!
trans-sub-table
(append (list cell)
(list splt-amnt-anchor
)))
(gnc:html-table-append-row!
transfer-table
(append (list cell)
(list splt-amnt-anchor
)))
)
)
(set! trans-rpt-currency-total (gnc-numeric-add-fixed
trans-rpt-currency-total
splt-print-amnt))
) ;; end of let*
) ;; end of if
) ;; end of lamda
all-tran-splits) ;; end of map
;; if several splits are converted from several currencies, it is