-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledger.rkt
1196 lines (1031 loc) · 51.4 KB
/
ledger.rkt
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
#lang racket
(require (prefix-in s19: (lib "19.ss" "srfi"))
(prefix-in s48: (lib "48.ss" "srfi"))
db
racket/date
plot
format-numbers
format-ymd
(file "~/.ledger-dbaccess.rkt")
(file "~/.ledger-prefs.rkt"))
(provide (all-defined-out))
(plot-new-window? #t)
(define accounts-ht
(for/hash ([a all-accounts])
(values (account-id a) a)))
(struct ledger-item (date amount dr-acctid cr-acctid payee description dr-seen cr-seen dr-deduct cr-deduct) #:transparent)
(struct statement-item (acctid date amount description) #:transparent)
(struct ledger-bal-item (ledger-item balance balance-seen diff) #:transparent)
(struct day-bal (date balance) #:transparent)
(define track-item
(class object%
(super-new)
(inspect #f)
(field [_dr-matched #f]
[_cr-matched #f])
(init-field item)
(define/public (get-item) item)
(define/public (set-dr-match) (set! _dr-matched #t))
(define/public (set-cr-match) (set! _cr-matched #t))
(define/public (dr-matched?) _dr-matched)
(define/public (dr-unmatched?) (not _dr-matched))
(define/public (cr-matched?) _cr-matched)
(define/public (cr-unmatched?) (not _cr-matched))
(define/public (dr-or-cr-matched?) (or _dr-matched _cr-matched))
(define/public (neither-dr-nor-cr-matched?) (not (or _dr-matched _cr-matched)))
))
(define jan01 (+ (* 10000 start-year) 101))
(define dec31 (+ (* 10000 end-year) 1231))
(define jan0 (- jan01 1))
(define db-source
((cond [(eq? db-engine 'mysql) mysql-data-source]
[(eq? db-engine 'postgres) postgresql-data-source]
[else (error "invalid db-engine")])
#:server db-host #:port db-port
#:user db-user #:password db-passwd
#:database db-schema))
(define (connect!)
(dsn-connect db-source))
(define the-db
(virtual-connection (connection-pool connect!)))
;;; Database Calls
(define acct-stmt-balances-ht (make-hash)) ;; all statement balances
(define acct-most-recent-balances-ht (make-hash)) ;; most recent balances
; string -> (list-of (list number number))
(define (db-get-most-recent-actual-balance acctid)
(if (hash-has-key? acct-most-recent-balances-ht acctid)
(hash-ref acct-most-recent-balances-ht acctid)
(let ([answer (let* ([accttype (substring acctid 0 1)]
[acctname (substring acctid 2)]
[rows (query-rows
the-db
(string-append
"select date, amount from balances where acct_type = '" accttype
"' and acct_name = '" acctname
"' order by date desc limit 1"))])
(map (λ (row)
(list (sql-date->ymd8 (vector-ref row 0))
(vector-ref row 1)))
rows))])
(hash-set! acct-most-recent-balances-ht acctid answer)
answer)))
; string -> (list-of (list number number))
(define (db-get-statement-balances acctid)
(if (hash-has-key? acct-stmt-balances-ht acctid)
(hash-ref acct-stmt-balances-ht acctid)
(let ([answer (let* ([accttype (substring acctid 0 1)]
[acctname (substring acctid 2)]
[rows (query-rows
the-db
(string-append
"select date, amount from balances where acct_type = '" accttype
"' and acct_name = '" acctname
"' and isstmt is true order by date"))])
(map (λ (row)
(list (sql-date->ymd8 (vector-ref row 0))
(vector-ref row 1)))
rows))])
(hash-set! acct-stmt-balances-ht acctid answer)
answer)))
(define (closest-statement acctid ymd8)
(let* ([ds (filter (λ (x) (<= x ymd8)) (get-statement-dates acctid))]
[ymd8s (member ymd8 ds)])
(if ymd8s
(first ymd8s)
(apply max ds))))
(define (sql-date->ymd8 d)
(+ (* (sql-date-year d) 10000)
(* (sql-date-month d) 100)
(* (sql-date-day d))))
; vector -> statement-item
(define (row-to-statement-item x)
(statement-item (vector-ref x 1) (sql-date->ymd8 (vector-ref x 2)) (vector-ref x 3) (vector-ref x 4)))
; num num -> (list-of statement-item)
(define (db-get-statement-items start-year end-year)
(let ([rows (query-rows
the-db (string-append "select * from statements where date>='" (number->string start-year) "-01-01' and date<='" (number->string end-year) "-12-31' order by date"))])
(map row-to-statement-item rows)))
(define (db-get-reconciliation-ledger-items acctid ymd8-end statement-balances)
(let* ([ymd8-start (find-previous-statement-date ymd8-end (reverse statement-balances))]
[rows (query-rows
the-db (string-append
"select"
" id,date,amount,dr_acct,cr_acct,payee,description,dr_seen,cr_seen,dr_deduct,cr_deduct"
" from ledger where"
" (date > '" (ymd8->ymd10 ymd8-start) "' and"
" date <= '" (ymd8->ymd10 ymd8-end) "')"
" and"
" ((dr_acct = '" acctid "' and (dr_seen is null or dr_seen = ':' )) "
" or "
" (cr_acct = '" acctid "' and (cr_seen is null or cr_seen = ':' )) )"
" order by date"))])
(map row-to-ledger-item rows)))
; string -> (list-of (list number))
(define (db-get-active-apr acctid)
(let ([rows (query-rows
the-db
(string-append
"(select apr from interest_rates where acct_type='l' and acct_name='"
(substring acctid 2)
"' and cur is true)"))])
(string->number (vector-ref (first rows) 0))))
(define (sub-false-for-sql-null x)
(if (sql-null? x) #f x))
; vector -> ledger-item
(define (row-to-ledger-item x)
(ledger-item (sql-date->ymd8 (vector-ref x 1))
(vector-ref x 2)
(vector-ref x 3)
(vector-ref x 4)
(vector-ref x 5)
(vector-ref x 6)
(sub-false-for-sql-null (vector-ref x 7))
(sub-false-for-sql-null (vector-ref x 8))
(sub-false-for-sql-null (vector-ref x 9))
(sub-false-for-sql-null (vector-ref x 10))))
; num num -> (list-of ledger-item)
(define (db-get-ledger-items start-year end-year)
(let ([rows (query-rows
the-db (string-append "select id,date,amount,dr_acct,cr_acct,payee,description,dr_seen,cr_seen,dr_deduct,cr_deduct from ledger where date>='" (number->string start-year) "-01-01' and date<='" (number->string end-year) "-12-31' order by date"))])
(map row-to-ledger-item rows)))
(define all-statement-items empty)
(define all-ledger-items empty)
(define (set-all-statement-items!)
(set! all-statement-items (db-get-statement-items start-year end-year)))
(define (set-all-ledger-items!)
(set! all-ledger-items (db-get-ledger-items start-year end-year)))
(define (refresh)
(set-all-statement-items!)
(set-all-ledger-items!)
(hash-clear! acct-stmt-balances-ht)
(hash-clear! acct-most-recent-balances-ht))
(refresh)
;;; End Database Calls
(define (get-book-ext-diff acctid ymd8)
(let* ([xs (get-ledger-bal-items acctid)]
[bals (acct-book-and-ext-balances-on-date acctid ymd8)]
[book (first bals)]
[ext (second bals)]
[book-minus-ext (- book ext)])
(list book ext book-minus-ext)))
(define (format-ledger-bal-item acctid lbi want-diff-seen?)
(if want-diff-seen?
(format "~a ~a ~a ~a"
(~a (format-exact (ledger-bal-item-diff lbi) 2) #:min-width 9 #:align 'right)
(~a (format-exact (ledger-bal-item-balance-seen lbi) 2) #:min-width 9 #:align 'right)
(~a (format-exact (ledger-bal-item-balance lbi) 2) #:min-width 9 #:align 'right)
(format-ledger-item acctid (ledger-bal-item-ledger-item lbi)))
(format "~a ~a"
(~a (format-exact (ledger-bal-item-balance lbi) 2) #:min-width 9 #:align 'right)
(format-ledger-item acctid (ledger-bal-item-ledger-item lbi)))))
(define (pr-acct-book-and-ext-balances-up-through acctid up-through)
(for-each
(λ (lbi)
(printf "~a" (format-ledger-bal-item acctid lbi #t)))
(acct-book-and-ext-balances-up-to-date acctid up-through)))
(define (pr-acct-book-and-ext-balances-between-dates acctid from through)
(for-each
(λ (lbi)
(printf "~a" (format-ledger-bal-item acctid lbi #t)))
(acct-book-and-ext-balances-between-dates acctid from through)))
(define (pr-acct-book-balances-up-through acctid up-through)
(for-each
(λ (lbi)
(printf "~a" (format-ledger-bal-item acctid lbi #f)))
(acct-book-and-ext-balances-up-to-date acctid up-through)))
(define (pr-acct-book-balances-between-dates acctid from through)
(for-each
(λ (lbi)
(printf "~a" (format-ledger-bal-item acctid lbi #f)))
(acct-book-and-ext-balances-between-dates acctid from through)))
(define (acct-book-and-ext-balances-up-to-date acctid through)
(filter (λ (lbi)
(<= (ledger-item-date (ledger-bal-item-ledger-item lbi)) through))
(get-ledger-bal-items acctid)))
(define (acct-book-and-ext-balances-between-dates acctid from through)
(filter (λ (lbi)
(let ([li-date (ledger-item-date (ledger-bal-item-ledger-item lbi))])
(and (>= li-date from)
(<= li-date through))))
(get-ledger-bal-items acctid)))
(define (acct-book-and-ext-balances-on-date acctid as-of)
(let* ([xs (get-ledger-bal-items acctid)]
[ys (filter (λ (x)
(<= (ledger-item-date (ledger-bal-item-ledger-item x)) as-of))
xs)])
(if (empty? ys)
(list 0 0)
(list (ledger-bal-item-balance (first (reverse ys)))
(ledger-bal-item-balance-seen (first (reverse ys)))))
))
(define (pr-acct-book-and-ext-balances-on-date acctid as-of)
(let* ([ls (acct-book-and-ext-balances-on-date acctid as-of)]
[book (first ls)]
[ext (second ls)]
[diff-book-ext (- book ext)]
[ymd-mr (db-get-most-recent-actual-balance acctid)])
(printf "~a ~a ~a~a\n"
(exact->inexact book)
(exact->inexact ext)
(exact->inexact diff-book-ext)
(if (not (empty? ymd-mr))
(string-append " (" (number->string (first (first ymd-mr)))
" " (format-exact (second (first ymd-mr)) 2)
" " (format-exact (- (second (first ymd-mr)) ext) 2) ")")
""))))
; to return a number ymd8 must be one of the ymd8 values in the list
; (listof (list ymd8 exact)) ymd8 -> exact
(define (get-stmt-bal-for-date date-bals ymd8)
(cond [(null? date-bals) (error "balance unavailable for date" ymd8)]
[(= (first (first date-bals)) ymd8) (second (first date-bals))]
[else (get-stmt-bal-for-date (rest date-bals) ymd8)]))
; string -> (list-of number)
(define (get-statement-dates acctid)
(map first (db-get-statement-balances acctid)))
; string number number -> (listof number number number)
(define (get-new-dr-new-cr-reconciliation acctid stmt-ymd8)
(let* ([statement-balances (db-get-statement-balances acctid)]
[stmt-bal (get-stmt-bal-for-date statement-balances stmt-ymd8)]
[reconciliation-items (db-get-reconciliation-ledger-items acctid stmt-ymd8 statement-balances)]
[new-dr (sum-amounts-new-dr acctid stmt-ymd8 reconciliation-items)]
[new-cr (sum-amounts-new-cr acctid stmt-ymd8 reconciliation-items)]
[reconciliation (+ stmt-bal new-dr new-cr)])
(list new-dr new-cr reconciliation)))
; string number number -> (listof number number number number)
(define (get-new-dr-new-cr-reconciliation-diff acctid stmt-ymd8 amt)
(let* ([dr-cr-rec (get-new-dr-new-cr-reconciliation acctid stmt-ymd8)]
[new-dr (first dr-cr-rec)]
[new-cr (second dr-cr-rec)]
[reconciliation (third dr-cr-rec)]
[amt-minus-reconciliation (- amt reconciliation)])
(list new-dr new-cr reconciliation amt-minus-reconciliation)))
; string -> (listof (list number number number number number))
(define (get-reconciliations acctid)
(map (λ (ymd8-bal)
(cons (first ymd8-bal) (get-new-dr-new-cr-reconciliation-diff acctid (first ymd8-bal) (second ymd8-bal))))
(db-get-statement-balances acctid)))
(define (find-previous-statement-date ymd8 statement-balances-going-back-in-time)
(cond [(empty? statement-balances-going-back-in-time) jan01] ;FIXME needs to be > dec31 not jan01
[else (let ([x (first (first statement-balances-going-back-in-time))])
(if (< x ymd8)
x
(find-previous-statement-date ymd8 (rest statement-balances-going-back-in-time))))]))
(define (first-ledger-unseen-discrepancy acctid)
(let ([lbis (ledger-unseen-discrepancies acctid)])
(if (empty? lbis)
#f
(let ([lbi (first lbis)])
(list (- (ledger-bal-item-balance-seen lbi) (ledger-bal-item-balance lbi)) lbi)))))
(define (ledger-unseen-discrepancies acctid)
(filter (λ (x)
(not (= (ledger-bal-item-balance x)
(ledger-bal-item-balance-seen x))))
(get-ledger-bal-items acctid)))
(define (ledger-unseen-discrepancy-changes acctid)
(define (helper acctid ins outs running-discrepancy)
(if (empty? ins)
outs
(let ([new-diff (ledger-bal-item-diff (first ins))])
(if (= new-diff running-discrepancy)
(helper acctid (rest ins) outs running-discrepancy)
(helper acctid (rest ins) (cons (first ins) outs) new-diff)))))
(reverse (helper acctid (ledger-unseen-discrepancies acctid) empty 0)))
(define (statement-acct-matches acctid statement-items)
(filter (λ (srow) (string=? (statement-item-acctid srow) acctid)) statement-items))
;(define (ledger-acct acctid ledger-items)
; (filter (λ (lrow)
; (or (string=? (ledger-item-dr-acctid lrow) acctid)
; (string=? (ledger-item-cr-acctid lrow) acctid)))
; ledger-items))
(define (statement<=ymd8 ymd8 statement-items)
(filter (λ (si) (<= (statement-item-date si) ymd8)) statement-items))
(define (statement>=ymd8 ymd8 statement-items)
(filter (λ (si) (>= (statement-item-date si) ymd8)) statement-items))
(define (statement-range ymd8-a ymd8-b statement-items)
(filter (λ (si)
(and (>= (statement-item-date si) ymd8-a) (<= (statement-item-date si) ymd8-b)))
statement-items))
(define (ledger<=ymd8 ymd8 ledger-items)
(filter (λ (li) (<= (ledger-item-date li) ymd8)) ledger-items))
(define (ledger>=ymd8 ymd8 ledger-items)
(filter (λ (li) (>= (ledger-item-date li) ymd8)) ledger-items))
(define (ledger-range ymd8-a ymd8-b ledger-items)
(filter (λ (li)
(and (>= (ledger-item-date li) ymd8-a) (<= (ledger-item-date li) ymd8-b)))
ledger-items))
(define (ledger-acct-matches acctid ledger-items)
(filter (λ (li) (ledger-acct-match acctid li)) ledger-items))
(define (ledger-acct-match acctid ledger-item)
(or (string=? acctid (ledger-item-dr-acctid ledger-item))
(string=? acctid (ledger-item-cr-acctid ledger-item))))
(define (ledger-range-acct acctid ymd8-a ymd8-b ledger-items)
(filter (λ (li)
(ledger-acct-match acctid li))
(ledger-range ymd8-a ymd8-b (ledger-acct-matches acctid ledger-items))))
(define (ledger-range-acct-dr acctid ymd8-a ymd8-b ledger-items)
(filter (λ (li)
(string=? acctid (ledger-item-dr-acctid li)))
(ledger-range ymd8-a ymd8-b (ledger-acct-matches acctid ledger-items))))
(define (ledger-range-acct-cr acctid ymd8-a ymd8-b ledger-items)
(filter (λ (li)
(string=? acctid (ledger-item-cr-acctid li)))
(ledger-range ymd8-a ymd8-b (ledger-acct-matches acctid ledger-items))))
(define (ledger-range-signed-amounts acctid ymd8-a ymd8-b ledger-items)
(map (λ (li)
(ledger-signed-amount acctid li))
(ledger-range ymd8-a ymd8-b (ledger-acct-matches acctid ledger-items))))
(define (ledger-range-signed-amounts-seen acctid ymd8-a ymd8-b ledger-items)
(map (λ (li)
(ledger-signed-amount-seen acctid li))
(ledger-range ymd8-a ymd8-b (ledger-acct-matches acctid ledger-items))))
(define (ledger-range-signed-amounts-unseen acctid ymd8-a ymd8-b ledger-items)
(map (λ (li)
(ledger-signed-amount-unseen acctid li))
(ledger-range ymd8-a ymd8-b (ledger-acct-matches acctid ledger-items))))
(define (sum-ledger-range-signed-amounts acctid ymd8-a ymd8-b ledger-items)
(foldl + 0 (ledger-range-signed-amounts acctid ymd8-a ymd8-b ledger-items)))
(define (sum-ledger-range-signed-amounts-seen acctid ymd8-a ymd8-b ledger-items)
(foldl + 0 (ledger-range-signed-amounts-seen acctid ymd8-a ymd8-b ledger-items)))
(define (sum-ledger-range-signed-amounts-unseen acctid ymd8-a ymd8-b ledger-items)
(foldl + 0 (ledger-range-signed-amounts-unseen acctid ymd8-a ymd8-b ledger-items)))
(define (ledger-amount-dr acctid a-ledger-item)
(if (string=? acctid (ledger-item-dr-acctid a-ledger-item))
(ledger-item-amount a-ledger-item)
0))
(define (ledger-amount-cr acctid a-ledger-item)
(if (string=? acctid (ledger-item-cr-acctid a-ledger-item))
(ledger-item-amount a-ledger-item)
0))
(define (ledger-amount-dr-seen acctid a-ledger-item)
(if (and (ledger-item-dr-seen a-ledger-item)
(string=? acctid (ledger-item-dr-acctid a-ledger-item)))
(ledger-item-amount a-ledger-item)
0))
(define (ledger-amount-cr-seen acctid a-ledger-item)
(if (and (ledger-item-cr-seen a-ledger-item)
(string=? acctid (ledger-item-cr-acctid a-ledger-item)))
(ledger-item-amount a-ledger-item)
0))
(define (ledger-amount-dr-unseen acctid a-ledger-item)
(if (and (not (ledger-item-dr-seen a-ledger-item))
(string=? acctid (ledger-item-dr-acctid a-ledger-item)))
(ledger-item-amount a-ledger-item)
0))
(define (ledger-amount-cr-unseen acctid a-ledger-item)
(if (and (not (ledger-item-cr-seen a-ledger-item))
(string=? acctid (ledger-item-cr-acctid a-ledger-item)))
(ledger-item-amount a-ledger-item)
0))
(define (ledger-signed-amount acctid a-ledger-item)
(let ([d (account-division acctid)])
(cond [(eq? d 'balance-sheet)
(- (ledger-amount-dr acctid a-ledger-item) (ledger-amount-cr acctid a-ledger-item))]
[(eq? d 'income-statement)
(- (ledger-amount-cr acctid a-ledger-item) (ledger-amount-dr acctid a-ledger-item))]
[else 0])))
(define (account-division acctid)
(let ([ltr (string-ref acctid 0)])
(cond [(or (eq? ltr #\a) (eq? ltr #\l) (eq? ltr #\e)) 'balance-sheet]
[(or (eq? ltr #\r) (eq? ltr #\x)) 'income-statement]
[else #f])))
(define (ledger-signed-amount-seen acctid a-ledger-item)
(let ([ltr (string-ref acctid 0)])
(cond [(or (eq? ltr #\a) (eq? ltr #\l) (eq? ltr #\e))
(- (ledger-amount-dr-seen acctid a-ledger-item)
(ledger-amount-cr-seen acctid a-ledger-item))]
[(or (eq? ltr #\r) (eq? ltr #\x))
(- (ledger-amount-cr-seen acctid a-ledger-item)
(ledger-amount-dr-seen acctid a-ledger-item))]
[else 0])))
(define (ledger-signed-amount-unseen acctid a-ledger-item)
(let ([ltr (string-ref acctid 0)])
(cond [(or (eq? ltr #\a) (eq? ltr #\l) (eq? ltr #\e))
(- (ledger-amount-dr-unseen acctid a-ledger-item) (ledger-amount-cr-unseen acctid a-ledger-item))]
[(or (eq? ltr #\r) (eq? ltr #\x))
(- (ledger-amount-cr-unseen acctid a-ledger-item) (ledger-amount-dr-unseen acctid a-ledger-item))]
[else 0])))
; string -> (listof ledger-bal-item)
(define (get-ledger-bal-items acctid)
(get-ledger-bal-items-from acctid all-ledger-items 0 0))
(define (closing-bal-each-day day-bals)
(define (dotted-pair-to-day-bal x) (day-bal (car x) (cdr x)))
(let* ([ht (for/hash ([i day-bals])
(values (day-bal-date i) (day-bal-balance i)))]
[dotted-pairs (sequence->list (in-hash-pairs ht))])
(map
dotted-pair-to-day-bal
(sort
dotted-pairs
(λ (a b) (< (car a) (car b)))))))
(define (get-day-bals acctid)
(closing-bal-each-day
(map (λ (lbi)
(let ([li (ledger-bal-item-ledger-item lbi)])
(day-bal (ledger-item-date li) (ledger-bal-item-balance lbi))))
(get-ledger-bal-items acctid))))
(define (get-day-bals-filter acctid filter-func)
(closing-bal-each-day
(map (λ (lbi)
(let ([li (ledger-bal-item-ledger-item lbi)])
(day-bal (ledger-item-date li) (ledger-bal-item-balance lbi))))
(filter filter-func (get-ledger-bal-items acctid)))))
(define (day-bals-from acctid start-ymd8)
(define filter-func
(λ (lbi)
(>= (ledger-item-date (ledger-bal-item-ledger-item lbi)) start-ymd8)))
(get-day-bals-filter acctid filter-func))
(define (accounts-bals-range accounts start-ymd8 end-ymd8)
(map (λ (x)
(list x (day-bals-range x start-ymd8 end-ymd8)))
(map (λ (a) (account-id a)) accounts)))
(define (day-bals-range acctid start-ymd8 end-ymd8)
(define filter-func
(λ (lbi)
(and (>= (ledger-item-date (ledger-bal-item-ledger-item lbi)) start-ymd8)
(<= (ledger-item-date (ledger-bal-item-ledger-item lbi)) end-ymd8))))
(let ([bals (get-day-bals-filter acctid filter-func)])
(if (and (not (empty? bals))
(> (day-bal-date (first bals)) start-ymd8))
(append (day-bals-on acctid start-ymd8) bals)
bals)))
(define (day-bals-ons acctid ymd8s)
(map (λ (ymd8)
(day-bals-on acctid ymd8))
ymd8s))
(define (day-bals-on acctid ymd8)
(define filter-func
(λ (lbi)
(= (ledger-item-date (ledger-bal-item-ledger-item lbi)) ymd8)))
(let ([bals (get-day-bals-filter acctid filter-func)])
(if (empty? bals)
(list (first (reverse (day-bals-range acctid jan01 ymd8))))
bals)))
(define (plot-day-bals-from an-account start-ymd8)
(plot-day-bals an-account (day-bals-from (account-id an-account) start-ymd8)))
(define (plot-day-bals-range an-account start-ymd8 end-ymd8)
(plot-day-bals an-account (day-bals-range (account-id an-account) start-ymd8 end-ymd8)))
(define (plot-day-bals-forward an-account ndays)
(plot-day-bals-range an-account (today->ymd8) (ymd8-plusdays->ymd8 (today->ymd8) ndays)))
(define (plot-day-bals an-account bals)
(let* ([xs (map (λ (b) (date->seconds (ymd8->date (day-bal-date b)))) bals)]
[ys (map day-bal-balance bals)])
(parameterize ([plot-title (account-id an-account)]
[plot-x-label "Date"]
[plot-x-ticks (date-ticks)]
[plot-y-label "Amount"])
(plot (lines (map vector xs ys) #:color (account-color an-account))))))
(define (plot-account-day-bals-forward account ndays)
(plot-accounts-day-bals-forward (list account) ndays))
(define (plot-accounts-day-bals-forward accounts ndays)
(plot-accounts-day-bals-range accounts
(today->ymd8)
(ymd8-plusdays->ymd8 (today->ymd8) ndays)))
(struct ayb (v-titles h-indices h-ymd8s) #:transparent)
; IN: e.g., output from accounts-bals-range
; ( ("acct1" ( (date1 bal1) (date2 bal2) ... ))
; ("acct2" ( (date1 bal1) (date2 bal2) ... )) ... )
;
; v-titles -> #("acct1" "acct2" ... "total")
;
; OUT:
; (ayb accts-ymd8s-bals
; h-ymd8s
; |
; v
; 20190101 -> #(1 2 ... 3)
; 20190102 -> #(2 5 ... 8)
; ...
; 20191129 -> #(1000 2000 ... 3000)
(define (combine-day-bal-lists lo-acct-lo-day-bals start-ymd8 end-ymd8)
(let* ([titles0 (map first lo-acct-lo-day-bals)]
[titles (append titles0 (list "total"))]
; #("acct1" "acct2" ... "acctN", "total")
[v-titles (list->vector titles)]
; "acct1" -> 0, "acct2" -> 1, ..., "accnN" -> N-1, "total" -> N
[h-indices (for/hash ([k titles]
[i (in-range (vector-length v-titles))])
(values k i))]
[ymd8s (every-ymd8-from-through start-ymd8 end-ymd8)]
[h-ymd8s (for/hash ([k ymd8s])
(values k (make-vector (vector-length v-titles) #f)))])
(for-each (λ (acct-lo-day-bals)
(set-matrix-point-balances acct-lo-day-bals h-ymd8s h-indices start-ymd8))
lo-acct-lo-day-bals)
(calculate-matrix-balances lo-acct-lo-day-bals h-ymd8s h-indices)
(ayb v-titles h-indices h-ymd8s)))
(define (set-matrix-point-balances acct-lo-day-bals h-ymd8s h-indices start-ymd8)
(let ([acct (first acct-lo-day-bals)]
[lo-day-bals (second acct-lo-day-bals)])
; set the specific balances in the matrix
(for-each (λ (a-day-bal)
(let ([ymd8 (day-bal-date a-day-bal)]
[bal (day-bal-balance a-day-bal)]
[i-acct (hash-ref h-indices acct)])
(when (and (or (hash-has-key? h-ymd8s ymd8)
(< ymd8 start-ymd8))
(or (false? (hash-ref h-ymd8s ymd8))
(false? (vector-ref (hash-ref h-ymd8s ymd8) i-acct))))
(when (false? (hash-ref h-ymd8s ymd8))
(hash-set! h-ymd8s ymd8 (make-vector (vector-length h-indices) #f)))
(vector-set! (hash-ref h-ymd8s (if (< ymd8 start-ymd8)
start-ymd8
ymd8))
i-acct bal))))
lo-day-bals)))
;; ---------------------------------------------------------------------
;; BEFORE || AFTER
;; ---------------------------------------------------------------------
;; MATRIX | MEMORY |DAY| MEMORY | MATRIX
;; ------------------+--------------|---|-------------------------------
;; . 12 . . | . . . | 1 | . 12 . | . 12 . 12
;; . . 23 . | . 12 . | 2 | . 12 23 | . 12 23 35
;; . 32 33 . | . 12 23 | 3 | . 32 33 | . 32 33 65
;; . . . . | . 32 33 | 4 | . 32 33 | . 32 33 65
;; 51 . . . | . 32 33 | 5 | 51 32 33 | 51 32 33 116
;; . 62 . . | 51 32 33 | 6 | 51 62 33 | 51 62 33 146
;; 71 . 73 . | 51 62 33 | 7 | 71 62 73 | 71 62 73 206
;;
;; foreach day:
;; matrix[day][total] = 0
;; foreach acct:
;; when matrix[day][acct] has no value AND
;; memory[acct] has a value:
;; matrix[day][acct] = memory[acct]
;; foreach acct:
;; when matrix[day][acct] has a value:
;; memory[acct] = matrix[day][account]
;; matrix[day][total] += matrix[day][account]
(define (calculate-matrix-balances lo-acct-lo-day-bals h-ymd8s h-indices)
(let* ([accts (map first lo-acct-lo-day-bals)]
[memory (make-vector (hash-count h-indices) #f)]
[i-total (hash-ref h-indices "total")]
[ymd8s-sorted (sort (hash-keys h-ymd8s) <)])
(for-each
(λ (ymd8)
(let ([v-today (hash-ref h-ymd8s ymd8)])
(vector-set! v-today i-total 0)
(for-each (λ (acct)
(let ([i-acct (hash-ref h-indices acct)])
(when (and (false? (vector-ref v-today i-acct))
(vector-ref memory i-acct))
(vector-set! v-today i-acct (vector-ref memory i-acct)))))
accts)
(for-each (λ (acct)
(let* ([i-acct (hash-ref h-indices acct)]
[prev-total (vector-ref v-today i-total)])
(vector-set! memory i-acct (vector-ref v-today i-acct))
(vector-set! v-today i-total (+ prev-total (vector-ref v-today i-acct)))))
accts)))
ymd8s-sorted)
h-ymd8s))
(define (day-bals-hash->sorted-list h)
(sort (λ (x y)
(< (day-bal-date x) (day-bal-date y)))
(hash-values h)))
(define (pr-min-acct-day-bal-forward acctid ndays)
(let ([b (min-acct-day-bal-forward acctid ndays)])
;(printf "~a ~a\n" acctid ndays)
(printf "~a ~a\n" (day-bal-date b) (format-exact (day-bal-balance b) 2))))
(define (min-acct-day-bal-forward acctid ndays)
(define (helper min-seen answer xs)
(cond [(empty? xs) answer]
[(< (day-bal-balance (first xs)) min-seen)
(helper (day-bal-balance (first xs))
(first xs)
(rest xs))]
[else
(helper min-seen
answer
(rest xs))]))
(helper +inf.0
#f
(day-bals-range acctid
(today->ymd8)
(ymd8-plusdays->ymd8 (today->ymd8) ndays))))
(define (pr-outlook-forward accts ndays want-outflow?)
(let ([d (today->ymd8)])
(for-each (λ (a)
(printf "~n===== account: ~a on ~a =====~n~nledger ext diff (most-recent):~n" a d)
(pr-acct-book-and-ext-balances-on-date a d)
(printf "~nmininum balance next ~a days:~n" ndays)
(pr-min-acct-day-bal-forward a ndays)
(printf "~noutstanding ledger items (~a):~n" a)
(pr-outstanding-ledger-items a d)
(printf "~n")
(when want-outflow?
(begin (printf "outflow (~a):~n" a)
(pr-outflow-forward a d)
(printf "~n"))))
accts)))
(define (pr-outflow-forward acct ndays)
(let* ([rx #rx"UNSCHED"]
[d0 (today->ymd8)]
[d1 (ymd8-plusdays->ymd8 d0 ndays)]
[lis (filter (λ (li)
(and (<= (ledger-item-date li) d1)
(string=? acct (ledger-item-cr-acctid li))
(or (regexp-match rx (ledger-item-description li))
(false? (ledger-item-cr-seen li)))))
all-ledger-items)]
[total 0.0])
(for-each (λ (li)
(set! total (+ total (ledger-item-amount li)))
(printf "~a ~a ~a ~a / ~a\n"
(~a (format-exact total 2)
#:min-width 9 #:align 'right)
(ledger-item-date li)
(~a (format-exact (ledger-item-amount li) 2)
#:min-width 9 #:align 'right)
(ledger-item-payee li)
(ledger-item-description li)))
lis)))
(define (plot-accounts-day-bals-range accounts start-ymd8 end-ymd8)
(parameterize ([plot-x-label "Date"]
[plot-x-ticks (date-ticks)]
[plot-y-label "Amount"])
(plot (map (λ (an-account)
(lines-acct-day-bals-range an-account start-ymd8 end-ymd8))
accounts))))
(define (lines-acct-day-bals-range an-account start-ymd8 end-ymd8)
(let* ([bals (day-bals-range (account-id an-account) start-ymd8 end-ymd8)]
[xs (map (λ (b) (date->seconds (ymd8->date (day-bal-date b)))) bals)]
[ys (map day-bal-balance bals)])
(lines (map vector xs ys) #:color (account-color an-account))))
(define (plot-combined-accounts-day-bals-range accounts start-ymd8 end-ymd8)
(let* ([lo-acct-lo-day-bals (accounts-bals-range accounts start-ymd8 end-ymd8)]
[an-ayb (combine-day-bal-lists lo-acct-lo-day-bals start-ymd8 end-ymd8)]
[accounts+ (append accounts (list (account "total" "black")))])
(parameterize ([plot-x-label "Date"]
[plot-x-ticks (date-ticks)]
[plot-y-label "Amount"])
(plot (lines-combined-accounts-day-bals-range an-ayb accounts+ start-ymd8 end-ymd8)))))
(struct ymd8-x (ymd8 seconds))
(define (lines-combined-accounts-day-bals-range an-ayb accounts start-ymd8 end-ymd8)
(let* ([h-ymd8s (ayb-h-ymd8s an-ayb)]
[sorted-ymd8-xs (map (λ (ymd8)
(ymd8-x ymd8 (date->seconds (ymd8->date ymd8))))
(sort (hash-keys h-ymd8s) <))])
(map (λ (an-account)
(let* ([xs (map ymd8-x-seconds sorted-ymd8-xs)]
[h-indices (ayb-h-indices an-ayb)]
[i-acct (hash-ref h-indices (account-id an-account))]
[ys (map (λ (a-ymd8-x)
(let* ([ymd8 (ymd8-x-ymd8 a-ymd8-x)]
[v (hash-ref h-ymd8s ymd8)])
(vector-ref v i-acct)))
sorted-ymd8-xs)])
(lines (map vector xs ys) #:color (account-color an-account))))
accounts)))
(define (acct-color acctid)
(let ([ch (string-ref acctid 0)])
(cond [(eq? ch #\a) "Forest Green"]
[(eq? ch #\l) "red"]
[else "blue"])))
; find first ledger-bal-item where book and ext differ
; string -> (or/c ledger-bal-item #f)
(define (find-first-ledger-bal-item-book-ext-difference acctid)
(let* ([items (get-ledger-bal-items acctid)]
[nz-diffs (filter (λ (x) (not (zero? (ledger-bal-item-diff x)))) items)])
(if (> (length nz-diffs) 0)
(first nz-diffs)
#f)))
(define (get-ledger-bal-items-from
acctid ledger-items starting-balance starting-balance-seen)
(define (helper ins outs running-balance running-balance-seen)
(if (empty? ins)
outs
(let* ([in (first ins)]
[signed-amount (ledger-signed-amount acctid in)]
[signed-amount-seen (ledger-signed-amount-seen acctid in)]
[newbal (+ running-balance signed-amount)]
[newbal-seen (+ running-balance-seen signed-amount-seen)])
(helper (rest ins)
(cons (ledger-bal-item in newbal newbal-seen (- newbal newbal-seen)) outs)
newbal
newbal-seen))))
(let ([ins (ledger-acct-matches acctid ledger-items)])
(reverse (helper ins empty starting-balance starting-balance-seen))))
(define (check-ledger-statements-match statement-dates acctid lti sti)
(for-each (λ (stmt-ymd8)
(check-ledger-statement-match stmt-ymd8 acctid lti sti))
statement-dates))
(define (check-ledger-statement-match stmt-ymd8 acctid lti sti)
(let* ([li (send lti get-item)]
[si (send sti get-item)]
[acct-amount-match (categorize-ledger-statement-match-amount acctid li si)])
(when (and acct-amount-match
(is-ledger-statement-match-date li si)
(does-ledger-tag-match-statement acctid stmt-ymd8 li acct-amount-match)
(is-special-ledger-tag-match acctid li acct-amount-match))
(cond [(symbol=? acct-amount-match 'acct-amount-dr-and-cr-match)
(begin
(send lti set-dr-match)
(send lti set-cr-match)
(send sti set-dr-match)
(send sti set-dc-match)
'acct-amount-dr-and-cr-match)]
[(symbol=? acct-amount-match 'acct-amount-dr-matches)
(begin
(send lti set-dr-match)
(send sti set-dr-match)
'acct-amount-dr-matches)]
[(symbol=? acct-amount-match 'acct-amount-cr-matches)
(begin
(send lti set-cr-match)
(send sti set-cr-match)
'acct-amount-cr-matches)]
[else false]))))
(define (is-ledger-statement-match-date li si)
(>= (statement-item-date si) (ledger-item-date li)))
(define (categorize-ledger-statement-match-amount acctid li si)
(let ([acct-match (compare-acct-dr-cr acctid li si)]
[li-signed-amount (ledger-signed-amount acctid li)]
[si-amount (statement-item-amount si)])
(if (= li-signed-amount si-amount)
(cond [(symbol=? acct-match 'acct-dr-and-cr-match) 'acct-amount-dr-and-cr-match]
[(symbol=? acct-match 'acct-dr-matches) 'acct-amount-dr-matches]
[(symbol=? acct-match 'acct-cr-matches) 'acct-amount-cr-matches]
[else false])
false)))
(define (compare-acct-dr-cr acctid li si)
(let ([mask (bitwise-ior (if (string=? acctid (statement-item-acctid si)) 1 0)
(if (string=? acctid (ledger-item-dr-acctid li)) 2 0)
(if (string=? acctid (ledger-item-cr-acctid li)) 4 0))])
(cond [(= mask 7) 'acct-dr-and-cr-match]
[(= mask 3) 'acct-dr-matches]
[(= mask 5) 'acct-cr-matches]
[else false])))
(define (expected-ledger-tag ymd8-statement a-ledger-item)
(let* ([statement-year (quotient ymd8-statement 10000)]
[statement-month (quotient (remainder ymd8-statement 10000) 100)]
[ledger-item-year (quotient (ledger-item-date a-ledger-item) 10000)])
(fmt-i-02d (+ statement-month (* 12 (- statement-year ledger-item-year))))))
(define (get-ledger-tag acctid a-ledger-item acct-amount-match)
(cond [(symbol=? acct-amount-match 'acct-amount-dr-matches) (ledger-item-dr-seen a-ledger-item)]
[(symbol=? acct-amount-match 'acct-amount-cr-matches) (ledger-item-cr-seen a-ledger-item)]
[(symbol=? acct-amount-match 'acct-amount-dr-and-cr-match)
(appropriate-ledger-item-tag acctid a-ledger-item)]
[else false]))
(define (does-ledger-tag-match-statement acctid ymd8-statement a-ledger-item acct-amount-match)
(let ([actual-ledger-tag (get-ledger-tag acctid a-ledger-item acct-amount-match)])
(and (string? actual-ledger-tag)
(string=? actual-ledger-tag (expected-ledger-tag ymd8-statement a-ledger-item)))))
(define (is-special-ledger-tag-match acctid a-ledger-item acct-amount-match)
(let ([actual-ledger-tag (get-ledger-tag acctid a-ledger-item acct-amount-match)])
(and (string? actual-ledger-tag)
(or (string=? actual-ledger-tag ":")
(member actual-ledger-tag std-skip-tags)))))
(define (appropriate-ledger-item-tag acctid a-ledger-item)
(let ([signed-amount (ledger-signed-amount acctid a-ledger-item)])
(cond [(< signed-amount 0) (ledger-item-cr-seen a-ledger-item)]
[(> signed-amount 0) (ledger-item-dr-seen a-ledger-item)]
[else (pick-dr-or-cr-tag acctid a-ledger-item)])))
(define (pick-dr-or-cr-tag acctid a-ledger-item)
(cond [(string=? acctid (ledger-item-dr-acctid a-ledger-item)) (ledger-item-dr-seen a-ledger-item)]
[(string=? acctid (ledger-item-cr-acctid a-ledger-item)) (ledger-item-cr-seen a-ledger-item)]
[else false]))
(define std-skip-tags (list "bf" "v"))
(define (sum-ledger-acct-items acctid ledger-items)
(foldl + 0 (map (λ (li) (ledger-signed-amount acctid li)) ledger-items)))
(define (format-ledger-items acctid ledger-items)
(apply string-append (map (λ (li) (format-ledger-item acctid li)) ledger-items)))
(define (fpr-ledger-items acctid ledger-items port)
(fprintf port (format-ledger-items acctid ledger-items)))
(define (pr-ledger-items acctid ledger-items)
(fprintf (current-output-port) (format-ledger-items acctid ledger-items)))
(define (pr-ledger-items-and-sum acctid ledger-items)
(pr-ledger-items acctid ledger-items)
(printf "TOTAL: ~a\n"
(~a (format-exact (sum-ledger-acct-items acctid ledger-items) 2)
#:min-width 9 #:align 'right)))
;; TIP: great for finding which transactions on ledger cleared after statement date
(define (pr-stmt-unmatched-ledger-items acctid ymd8-end)
(let ([lis (filtered-stmt-unmatched-ledger-items acctid ymd8-end)])
(pr-ledger-items acctid lis)
(printf "TOTAL: ~a\n"
(~a (format-exact (sum-ledger-acct-items acctid lis) 2)
#:min-width 9 #:align 'right))))
(define (pr-cleared-after-stmt-ledger-items acctid ymd8-end)
(let ([lis (filtered-cleared-after-stmt-ledger-items acctid ymd8-end)])
(pr-ledger-items acctid lis)
(printf "TOTAL: ~a\n"
(~a (format-exact (sum-ledger-acct-items acctid lis) 2)
#:min-width 9 #:align 'right))))
(define (sum-outstanding-ledger-items acctid ymd8-end)
(sum-ledger-acct-items acctid (current-outstanding-ledger-items acctid ymd8-end)))
(define (format-outstanding-ledger-items acctid ymd8-end)
(let ([lis (current-outstanding-ledger-items acctid ymd8-end)])
(format "~a~a~a\n"
(format-ledger-items acctid lis)
"TOTAL: "
(~a (format-exact (sum-ledger-acct-items acctid lis) 2)
#:min-width 9 #:align 'right))))
(define (format-stmt-unmatched-ledger-items acctid ymd8-end)
(let ([lis (filtered-stmt-unmatched-ledger-items acctid ymd8-end)])
(format "~a~a~a\n"
(format-ledger-items acctid lis)
"TOTAL: "
(~a (format-exact (sum-ledger-acct-items acctid lis) 2)
#:min-width 9 #:align 'right))))
(define (fpr-outstanding-ledger-items acctid ymd8-end port)
(fprintf port (format-outstanding-ledger-items acctid ymd8-end)))
(define (pr-outstanding-ledger-items acctid ymd8-end)
(fpr-outstanding-ledger-items acctid ymd8-end (current-output-port)))
(define (current-outstanding-ledger-items acctid ymd8-end)
(ledger-items-outstanding acctid ymd8-end all-ledger-items))
(define (filtered-stmt-unmatched-ledger-items acctid ymd8-end)
(let* ([a (unmatched-ledger-items-to-date acctid ymd8-end)]
[b (ledger-items-exclude-tags std-skip-tags acctid a)])
(ledger-items-exclude-prior-matches acctid ymd8-end b)))
(define (filtered-cleared-after-stmt-ledger-items acctid ymd8-end)
(let ([lis (unmatched-ledger-items-to-date acctid ymd8-end)])
(ledger-items-cleared-after-stmt acctid ymd8-end lis)))
(define (ledger-items-exclude-prior-matches acctid ymd8-end ledger-items)
(filter (λ (a-ledger-item)
(not (is-prior-month-ledger-item-match acctid ymd8-end a-ledger-item)))
ledger-items))
(define (ledger-items-cleared-after-stmt acctid ymd8-end ledger-items)
(filter (λ (a-ledger-item)
(is-cleared-after-stmt acctid ymd8-end a-ledger-item))
ledger-items))
(define (ledger-items-outstanding acctid ymd8-end ledger-items)
(filter (λ (li)
(and (or (string=? acctid (ledger-item-dr-acctid li))
(string=? acctid (ledger-item-cr-acctid li)))
(is-ledger-outstanding-item-as-of acctid ymd8-end li)))
ledger-items))
(define (is-prior-month-ledger-item-match acctid ymd8-end a-ledger-item)