1<?scm
2
3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4;; balsheet-eg.eguile.scm
5;; by Chris Dennis  chris@starsoftanalysis.co.uk
6;;
7;; This eguile template is designed to be called from
8;; balsheet-eg.scm via the eguile mechanism.
9;;
10;; $Author: chris $ $Date: 2009/06/19 22:40:38 $ $Revision: 1.54 $
11;;
12;; This program is free software; you can redistribute it and/or
13;; modify it under the terms of the GNU General Public License as
14;; published by the Free Software Foundation; either version 2 of
15;; the License, or (at your option) any later version.
16;;
17;; This program is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20;; GNU General Public License for more details.
21;;
22;; You should have received a copy of the GNU General Public License
23;; along with this program; if not, contact:
24;;
25;; Free Software Foundation           Voice:  +1-617-542-5942
26;; 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
27;; Boston, MA  02110-1301,  USA       gnu@gnu.org
28;;
29;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30
31(let* ((version 0.01))
32
33  ;; Display a row of the accounts table, given the account name and amount,
34  ;; and several parameters for adjusting layout and styling.
35  (define (display-acc-row
36            maxdepth
37            depth
38            rshift
39            name
40            amount
41            total?
42            leftoverrule? ; put rule over cell to the left?
43            )
44    (let ((accname-class "accname")
45          (balance-class "balance")
46          (lo-adjust 0)
47          (lo-cell "")
48          (bold (lambda (x) x))); hack for non-CSS systems
49      (if total? (begin
50                   (set! accname-class "accnametotal")
51                   (if (and (= depth 0) (not (string=? amount "&nbsp;")))
52                     (set! balance-class "ruledtotal")
53                     (set! balance-class "balancetotal"))
54                   (set! bold (lambda (x) (string-append "<b>" x "</b>")))))
55      (set! depth (max depth 1)); hack for depth=0
56      (if leftoverrule?
57        (begin
58          (set! lo-adjust -1)
59          (set! lo-cell "<td class=\"overruled\">&nbsp;</td>")))
60?>
61<tr valign="bottom">
62<?scm (indent-cells (1- depth)) ?>
63<td colspan="<?scm:d (1+ (- maxdepth depth)) ?>" class="<?scm:d accname-class ?>">
64<?scm:d (bold name) ?></td>
65<?scm (empty-cells (+ (- maxdepth depth) rshift lo-adjust)) (display lo-cell) ?>
66<td class="<?scm:d balance-class ?>" align="right"><?scm:d (bold amount) ?></td>
67<?scm (empty-cells (- (1- depth) rshift)) ?>
68</tr>
69<?scm ))
70
71  (define (display-accounts-table-r
72            tree        ; list of accrecs
73            neg?
74            maxdepth
75            rshift
76            onedepth1)
77    ;; Recursively display the accounts table from the given tree
78    ;; (as returned by process-acc-list)
79    (for-each
80     (lambda (accrec)
81       (display-acc-row
82        maxdepth
83        (accrec-depth accrec)
84        ;; has sub-accounts: shift left to put balance in same column
85        ;; as sub-accounts
86        (+ rshift (if (accrec-sublist accrec) -1 0))
87        (accrec-namelink accrec)
88        ;; Don't show zero amount for a placeholder -- the value to
89        ;; test for zero depends on whether or not this is a 'summary'
90        ;; value (i.e. a total of sub-accounts that are not shown
91        ;; separately)
92        (cond
93         ((and (accrec-placeholder? accrec)
94               (if (accrec-summary? accrec)
95                   (not (accrec-non-zero? accrec))
96                   (zero? (accrec-balance-num accrec))))
97          "&nbsp;")
98         ((accrec-summary? accrec) (format-comm-coll (accrec-subtotal-cc accrec)))
99         (else (format-monetary (accrec-balance-mny accrec))))
100        (< (accrec-depth accrec) 1); total?
101        #f) ; leftoverrule?
102       (when (accrec-sublist accrec)
103         ;; recurse to deeper accounts...
104         (display-accounts-table-r
105          (accrec-sublist accrec) neg? maxdepth rshift onedepth1)
106         ;; ...and then display the total
107         ;; unless there is only one depth-1 account
108         (unless (and onedepth1 (= 1 (accrec-depth accrec)))
109           (display-acc-row
110            maxdepth
111            (accrec-depth accrec)
112            (if (> (accrec-depth accrec) 1) rshift 0)
113            (string-append (G_ "Total") " " (accrec-namelink accrec))
114            (format-comm-coll-total (accrec-subtotal-cc accrec))
115            (<= (accrec-depth accrec) 1)        ; total?
116            (> (accrec-depth accrec) 0)))))   ; leftoverrule?
117
118     tree))
119?>
120
121<!-- The HTML starts here... -->
122<html dir='auto'>
123<head>
124<meta http-equiv="content-type" content="text-html; charset=utf-8">
125<title><?scm:d coyname ?> <?scm:d opt-report-title ?> <?scm:d (qof-print-date opt-date) ?></title>
126
127<link rel="stylesheet" href="<?scm:d opt-css-file ?>" type="text/css">
128<!-- Note that the stylesheet file is overridden by some options, i.e.
129     opt-font-family and opt-font-size                                 -->
130<style type="text/css">
131  body {
132    <?scm (if opt-font-family (begin ?>
133      font-family: <?scm:d opt-font-family ?>;
134    <?scm )) ?>
135    <?scm (if opt-font-size (begin ?>
136      font-size: <?scm:d opt-font-size ?>;
137    <?scm )) ?>
138  }
139  table { /* table does not inherit font sizes for some reason */
140    <?scm (if opt-font-size (begin ?>
141      font-size: <?scm:d opt-font-size ?>;
142    <?scm )) ?>
143  }
144</style>
145
146</head>
147<body>
148<h3><?scm:d coyname ?></h3>
149<h2><?scm:d opt-report-title ?> <?scm:d (qof-print-date opt-date) ?></h2>
150
151<?scm
152  ;; This is where the work is done.
153  ;; Create three accounts trees, make a few adjustments, then display them
154  (let* ((accrec-as (process-acc-list asset-accounts #f))
155         (accrec-li (process-acc-list liability-accounts #t))
156         (accrec-eq (process-acc-list equity-accounts #t))
157         (accrec-tr (process-acc-list trading-accounts #t))
158         (accrec-ie (process-acc-list income-expense-accounts #t))
159         (maxdepth 0)
160         (rshift-as 0)
161         (rshift-li 0)
162         (rshift-eq 0)
163         (rshift-tr 0)
164         (rshift-ie 0)
165         (balancing-cc (gnc:make-commodity-collector))
166         (etl-cc (gnc:make-commodity-collector)))
167    (accrec-set-namelink! accrec-as (G_ "Assets Accounts"))
168    (accrec-set-placeholder?! accrec-as #t)
169    (balancing-cc 'merge (accrec-subtotal-cc accrec-as) #f)
170    (if (and (one-depth-1 accrec-as)
171             (> (accrec-treedepth accrec-as) 1))
172      (set! rshift-as 1))
173    (accrec-set-namelink! accrec-li (G_ "Liability Accounts"))
174    (accrec-set-placeholder?! accrec-li #t)
175    (etl-cc 'merge (accrec-subtotal-cc accrec-li) #f)
176    (if (and (one-depth-1 accrec-li)
177             (> (accrec-treedepth accrec-li) 1))
178      (set! rshift-li 1))
179    (accrec-set-namelink! accrec-eq (G_ "Equity Accounts"))
180    (accrec-set-placeholder?! accrec-eq #t)
181    (etl-cc 'merge (accrec-subtotal-cc accrec-eq) #f)
182    (accrec-set-namelink! accrec-tr (G_ "Trading Accounts"))
183    (accrec-set-placeholder?! accrec-tr #t)
184    (etl-cc 'merge (accrec-subtotal-cc accrec-tr) #f)
185    (balancing-cc 'minusmerge etl-cc #f)
186    (accrec-set-namelink! accrec-ie
187                          (if (gnc-numeric-negative-p (accrec-balance-num accrec-ie))
188                            (G_ "Retained Losses")
189                            (G_ "Retained Earnings")))
190    (accrec-set-placeholder?! accrec-ie #t)
191    (balancing-cc 'minusmerge (accrec-subtotal-cc accrec-ie) #f)
192    (if (and (one-depth-1 accrec-eq)
193             (> (accrec-treedepth accrec-eq) 1))
194      (set! rshift-eq 1))
195    (if (and (one-depth-1 accrec-tr)
196             (> (accrec-treedepth accrec-tr) 1))
197      (set! rshift-tr 1))
198    (if (and (one-depth-1 accrec-ie)
199             (> (accrec-treedepth accrec-ie) 1))
200      (set! rshift-ie 1))
201
202?>
203<table border="0" class="outer"><tr valign="top"><td valign="top"> <!-- outer table to control columns -->
204<table border="0" class="accounts" align="left">
205<?scm
206
207    (set! maxdepth (max (accrec-treedepth accrec-as)
208                        (accrec-treedepth accrec-li)
209                        (accrec-treedepth accrec-eq)
210                        (accrec-treedepth accrec-ie)
211                        (accrec-treedepth accrec-tr)))
212
213    ; Display assets section
214    (display-accounts-table-r (list accrec-as) #f maxdepth rshift-as (one-depth-1 accrec-as))
215    (hrule (* maxdepth 2))
216
217    ; Split table across columns if required
218    (case opt-columns
219      ((autocols)
220        ?>
221        </table>
222        <!-- <table border="0" align="left"><tr><td>&nbsp;</td></tr></table> -->
223        &nbsp;&nbsp;<table border="0" align="left">
224        <?scm
225        )
226      ((twocols)
227        ?>
228        </table></td><td valign="top"><table border="0">
229        <?scm
230        ))
231
232    ; Display liabilities and equity sections
233    (display-accounts-table-r (list accrec-li) #t maxdepth rshift-li (one-depth-1 accrec-li))
234    (hrule (* maxdepth 2))
235    (display-accounts-table-r (list accrec-tr) #t maxdepth rshift-tr (one-depth-1 accrec-tr))
236    (hrule (* maxdepth 2))
237    (display-accounts-table-r (list accrec-eq) #t maxdepth rshift-eq (one-depth-1 accrec-eq))
238    (hrule (* maxdepth 2))
239    (display-acc-row
240      maxdepth 0 0
241      (G_ "Total Equity, Trading, and Liabilities")
242      (format-comm-coll-total etl-cc)
243      #t #f)
244    (hrule (* maxdepth 2))
245    (display-accounts-table-r (list accrec-ie) #t maxdepth 0 (one-depth-1 accrec-ie))
246    (hrule (* maxdepth 2))
247    (if (not (gnc-commodity-collector-allzero? balancing-cc))
248        (display-acc-row
249          maxdepth 0 0
250          (G_ "Imbalance Amount")
251          (format-comm-coll-total balancing-cc)
252          #t #f))
253
254?>
255</table>
256</table>
257<?scm
258    ); end of let
259?>
260
261<?scm
262  ;; Display exchange rates table
263  (set! xlist (assoc-remove! xlist opt-report-commodity))
264  (if (not (null? xlist))
265    (begin
266?>
267<p><?scm:d (G_ "<strong>Exchange Rates</strong> used for this report") ?>
268<table border="0">
269<?scm
270      (for-each
271       (lambda (xpair)
272         (let* ((comm (car xpair))
273                (one-foreign-mny (gnc:make-gnc-monetary comm 1))
274                (one-local-mny (exchange-fn one-foreign-mny opt-report-commodity))
275                (conv-amount (gnc:gnc-monetary-amount one-local-mny))
276                (price-str (gnc:default-price-renderer
277                            opt-report-commodity conv-amount)))
278?>
279<tr>
280  <td align="right"><?scm:d (gnc:monetary->string one-foreign-mny) ?></td>
281  <td>=</td>
282  <td align="right"><?scm:d price-str ?></td>
283</tr>
284<?scm
285        ))
286       xlist)
287?>
288</table>
289<?scm
290  )) ; end of exchange rates table
291?>
292
293<br clear="both">
294<p><?scm:d opt-extra-notes ?>
295
296</body>
297</html>
298
299<?scm
300) ; enclosing let
301?>
302
303