aboutsummaryrefslogtreecommitdiffstats
path: root/lib/fi-report.php
blob: d5c2b3d83b4cca135fa29f7918808fd9badf65fe (plain)
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
<?php
/**
*/

class Mageia_Financial_Report
{
    function __construct()
    {
        
    }
    
    /**
    */
    function build($files)
    {
        $i = new self;
        $i->report_file = $files['report'];
        $i->forecast_file = $files['forecast'];
        $i->accounts_file = $files['accounts'];
        
        $i->CUR = '<span class="currency">EUR</span>';

        return $i;
    }
    
    /**
    */
    function html_summary()
    {
        $account2010 = 7523.96;
        $this->hAccount2010 = number_format($account2010, 2);
        $this->hAccountLeft = number_format($account2010 + $this->balance, 2);

        $balanceSign = $this->balance >= 0 ? 'plus' : 'minus';

        return <<<S
        <table class="fr-table noborder">
        <tr>
            <td class="labelR">At the end of <a href="../2010/">2010</a> we had</td>
            <td class="money">{$this->CUR}&nbsp;{$this->hAccount2010}.</td>
        </tr>
        <tr>
            <td class="labelR">Since then, we received</td>
            <td class="money">{$this->CUR}&nbsp;{$this->hInTotal},</td>
            <td class="labelR">and spent</td>
            <td class="money" style="text-align: left;">{$this->CUR}&nbsp;{$this->hOutTotal}
                <span style="font-size: 80%; color: #888;">(<a href="#report">see detailed report</a>)</span>.</td>
        </tr>
        <tr>
            <td class="labelR">Our balance so far is of</td>
            <td class="money">{$this->CUR}&nbsp;<span class="{$balanceSign}Sign">{$this->hBalance}</span>,</td>
        </tr>
        <tr>
            <td class="labelR">and we still have</td>
            <td class="money">{$this->CUR}&nbsp;{$this->hAccountLeft}:</td>
            <td class="money">{$this->CUR}&nbsp;{$this->accounts[0][1]}</td>
            <td>in our {$this->accounts[0][0]}</td>
        </tr>
        <tr>
            <td colspan="2"></td>
            <td class="money">{$this->CUR}&nbsp;{$this->accounts[1][1]}</td>
            <td>in our {$this->accounts[1][0]}</td>
        </tr>
        <tr>
            <td colspan="2"></td>
            <td class="money">{$this->CUR}&nbsp;{$this->accounts[2][1]}</td>
            <td>in our {$this->accounts[2][0]}</td>
        </tr>
        </table>
S;
    }
    
    /**
    */
    function html_report()
    {
        $f = file($this->report_file);
        $hFileMTime = $this->html_source_file($this->report_file);
        
        $inTotal  = 0;
        $outTotal = 0;
        $hLines   = '';

        $line_tmpl = <<<S
        <tr>
            <td>%s</td>
            <td>%s</td>
            <td class="money">%s</td>
            <td class="money">%s</td>
        </tr>
S;
        $cat_tmpl = '<tr><td colspan="4"><em>%s</em></td></tr>';

        foreach ($f as $l)
        {
            $l = str_getcsv($l, ';');
            if ('cat' === trim($l[0])) {
                $hLines .= sprintf($cat_tmpl, $l[1]);
            }
            elseif ('' === trim($l[0])) {
                $hLines .= sprintf($line_tmpl,
                    $l[1], $l[2],
                    $l[3] != '' ? $this->CUR . '&nbsp;' . number_format($l[3], 2) : '',
                    $l[4] != '' ? $this->CUR . '&nbsp;' . number_format($l[4], 2) : '');

                $inTotal += $l[4];
                $outTotal += $l[3];
            }
        }
        $this->balance   = $balance = $inTotal - $outTotal;
        $this->hInTotal  = $hInTotal  = number_format($inTotal, 2);
        $this->hOutTotal = $hOutTotal = number_format($outTotal, 2);
        $this->hBalance  = $hBalance  = number_format($balance, 2);

        $s = <<<S
        <table class="fr-table"><thead>
            <tr><th>Date</th>
                <th>Transaction description</th>
                <th>Outgoing</th>
                <th>Incoming</th>
                </tr>
            </thead><tbody>
            {$hLines}
        <tr>
            <td colspan="2">Total</td>
            <td class="money">{$this->CUR}&nbsp;{$hOutTotal}</td>
            <td class="money">{$this->CUR}&nbsp;{$hInTotal}</td>
        </tr>
        <tr>
            <td colspan="2">Balance</td>
            <td class="money" colspan="2" style="text-align: center;">{$this->CUR}&nbsp;{$hBalance}</td>
        </tr>
        </tbody></table>
        {$hFileMTime}
S;
        return $s;
    }
    
    /**
    */
    function html_forecast()
    {
        $f = file($this->forecast_file);
        $hFileMTime = $this->html_source_file($this->forecast_file);

        $total     = 0;
        $doneTotal = 0;
        $hLines    = '';
        $CUR       = '<span style="color: #777; font-size: 80%%;">EUR</span>';

        $line_tmpl = <<<S
        <tr>
            <td>%s</td>
            <td class="money">{$CUR}&nbsp;%s</td>
            <td class="money">%d</td>
            <td class="money">%d</td>
            <td class="money">{$CUR}&nbsp;%s</td>
            <td>%s</td>
        </tr>
S;

        foreach ($f as $l)
        {
            $l = str_getcsv($l, ';');
            if ('' === trim($l[0])) {
                $subtotal = $l[2] * $l[3] * $l[4];
                $hLines .= sprintf($line_tmpl,
                    $l[1], number_format($l[2], 2), $l[3], $l[4], number_format($subtotal, 2), $l[6]);

                $total += $subtotal;
                if (strtolower($l[6]) != 'pending') {
                    $doneTotal += $subtotal;
                }
            }
        }

        $hTotal     = number_format($total, 2);
        $hDoneTotal = number_format($doneTotal, 2);
        $hProgress  = round($doneTotal / $total * 100, 2);

        $s = <<<S
            <table class="fr-table"><thead>
                <tr><th>Description of planned expenditure</th>
                    <th>Unit Price</th>
                    <th>Quantity</th>
                    <th>Recurrence</th>
                    <th>Total Price</th>
                    <th>Status</th></tr>
                </thead><tbody>
                {$hLines}
            <tr>
                <td colspan="4">Total forecasted</td>
                <td class="money">{$CUR}&nbsp;{$hTotal}</td>
                <td></td>
            </tr>
            <tr>
                <td colspan="4">Total done</td>
                <td class="money">{$CUR}&nbsp;{$hDoneTotal}</td>
                <td class="money">{$hProgress}%</td>
            </tr>
        </tbody></table>
        {$hFileMTime}
S;
        return $s;
    }
    
    /**
    */
    function html_accounts()
    {
        $f = file($this->accounts_file);
        $hFileMTime = $this->html_source_file($this->accounts_file);

        $total     = 0;
        $doneTotal = 0;
        $hLines    = '';
        $CUR       = '<span style="color: #777; font-size: 80%%;">EUR</span>';
        $line_tmpl = <<<S
        <tr>
            <td>%s</td>
            <td class="money">{$CUR}&nbsp;%s</td>
        </tr>
S;

        $this->accounts = array();
        foreach ($f as $l)
        {
            $l = str_getcsv($l, ';');
            if ('' === trim($l[0])) {
                $this->accounts[] = array(
                    str_replace(array('(', ')'), array('<span style="font-size: 80%; color: #888; display: block;">(', ')</span>'), $l[1]),
                    number_format($l[2], 2)
                );
                $hLines .= sprintf($line_tmpl,
                    $l[1], number_format($l[2], 2));

                $total += $l[2];
            }
        }

        $hTotal = number_format($total, 2);

        $s = <<<S
            <table class="fr-table"><thead>
                <tr><th>Account</th>
                    <th>Amount</th>
                </thead><tbody>
                {$hLines}
            <tr>
                <td>Total</td>
                <td class="money">{$CUR}&nbsp;{$hTotal}</td>
            </tr>
        </tbody></table>
S;
        $s =<<<S
        {$hFileMTime}
S;
        return $s;
    }
    
    /**
    */
    function html_source_file($file)
    {
        return sprintf('<p style="font-size: 80%%; color: #777;">Source: <a href="%s">%s</a> (last updated on %s).</p>',
            $file, $file, date('c', filemtime($file)));
    }
}