-
Notifications
You must be signed in to change notification settings - Fork 761
/
balance-and-pnl.scm
169 lines (137 loc) · 6.06 KB
/
balance-and-pnl.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
;; -*-scheme-*-
(gnc:support "report/balance-and-pnl.scm")
(gnc:depend "text-export.scm")
(let ()
;; Just a private scope.
(define (render-level-2-account level-2-account level-2-balance)
(let ((account-name (gnc:account-get-name level-2-account))
(type-name (gnc:account-get-type-string
(gnc:account-get-type level-2-account))))
(string-append
"<tr><td>" account-name "<td>" type-name
(sprintf #f "<td align=right nowrap> $%10.2f\n"
level-2-balance))))
(define (render-level-1-account account level-1-balance level-2-balance)
(let ((name (gnc:account-get-name account))
(type (gnc:account-get-type-string (gnc:account-get-type account))))
(string-append
"<tr><td>" name "<td>" type
(sprintf #f "<td align=right nowrap> $%10.2f" level-2-balance)
(sprintf #f "<td align=right nowrap> <u>$%10.2f</u> \n"
level-1-balance)
"<tr><td> <td> <td> \n"))) ;; blank line
(define (render-total level-0-balance)
(string-append
"<tr><td> <td> <td> \n" ;; blank line
"<tr><td><b>Net</b><td> "
"<td> "
(sprintf #f "<td align=right nowrap> <u>$%10.2f</u> \n"
level-0-balance)))
(define (generate-balance-sheet-or-pnl report-name
report-description
balance-sheet?)
;; currency symbol that is printed is a dollar sign, for now
;; currency amounts get printed with two decimal places
;; balance sheet doesn't print income or expense
;; top-level accounts get printed in right-most column
;; This code could definitely be more "schemy", but for now I mostly
;; just translated it directly from the old ePerl with a few
;; schemifications.
(let ((level-0-balance 0)
(level-1-balance 0)
(level-2-balance 0)
(current-group (gnc:get-current-group))
(output '()))
(define (handle-level-2-account account)
(let ((type (gnc:account-type->symbol (gnc:account-get-type account)))
(balance (gnc:account-get-balance account)))
(if (not balance-sheet?) (set! balance (- balance)))
(if (not (or (and balance-sheet?
(not (eq? type 'INCOME))
(not (eq? type 'EXPENSE)))
(and (not balance-sheet?)
(or (eq? type 'INCOME)
(eq? type 'EXPENSE)))))
;; Ignore
'()
;; add in balances for any sub-sub groups
(let ((grandchildren (gnc:account-get-children account)))
(if (not (pointer-token-null? grandchildren))
(set! balance
((if balance-sheet? + -)
balance (gnc:group-get-balance grandchildren))))
(set! level-2-balance (+ level-2-balance balance))
(set! level-1-balance (+ level-1-balance level-2-balance))
(let ((result (render-level-2-account account level-2-balance)))
(set! level-2-balance 0)
result)))))
(define (handle-level-1-account account)
(let ((type (gnc:account-type->symbol (gnc:account-get-type account))))
(if (not (or (and balance-sheet?
(not (eq? type 'INCOME))
(not (eq? type 'EXPENSE)))
(and (not balance-sheet?)
(or (eq? type 'INCOME)
(eq? type 'EXPENSE)))))
;; Ignore
'()
(let ((childrens-output (gnc:group-map-accounts
handle-level-2-account
(gnc:account-get-children account)))
(account-balance (gnc:account-get-balance account)))
(if (not balance-sheet?)
(set! account-balance (- account-balance)))
(set! level-2-balance (+ level-2-balance account-balance))
(set! level-1-balance (+ level-1-balance account-balance))
(set! level-0-balance (+ level-0-balance level-1-balance))
(let ((level-1-output (render-level-1-account account
level-1-balance
level-2-balance)))
(set! level-1-balance 0)
(set! level-2-balance 0)
(list childrens-output level-1-output))))))
(if (not (pointer-token-null? current-group))
(set! output
(list
(gnc:group-map-accounts handle-level-1-account current-group)
(render-total level-0-balance))))
(list
"<html>"
"<head>"
"<title>" (gnc:_ report-name) "</title>"
"</head>"
"<body bgcolor=#ccccff>"
(gnc:_ report-description)
"<p>"
"<table cellpadding=1>"
"<caption><b>" (gnc:_ report-name) "</b></caption>"
"<tr><th>"(gnc:_ "Account Name")"<th align=center>"(gnc:_ "Type")
"<th> <th align=center>"(gnc:_ "Balance")
output
"</table>"
"</body>"
"</html>")))
(gnc:define-report
;; version
1
;; Menu name
"Balance sheet"
;; Options Generator (none currently)
#f
;; Code to generate the report
(lambda (options)
(generate-balance-sheet-or-pnl "Balance Sheet"
"This page shows your net worth."
#t)))
(gnc:define-report
;; version
1
;; Menu name
"Profit and Loss"
;; Options (none currently)
#f
;; Code to generate the report
(lambda (options)
(generate-balance-sheet-or-pnl "Profit and Loss"
"This page shows your profits and losses."
#f))))