-
Notifications
You must be signed in to change notification settings - Fork 4
/
tabular.pl
executable file
·297 lines (240 loc) · 8.48 KB
/
tabular.pl
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
#!/usr/bin/perl
# vim:set ts=4 sw=4 ai et smarttab:
use JSON;
use POSIX;
use strict;
use warnings;
@ARGV = qw(expenses.json noisetor.json oneoff.json)
unless @ARGV;
my @transactions;
my $balance = 0;
my $colo_end_date = 0;
my $year_income = 0;
my $year_fee = 0;
my $year_noisebridge = 0;
my $year_expense = 0;
my $month_income = 0;
my $month_fee = 0;
my $month_noisebridge = 0;
my $month_expense = 0;
sub display_header {
print "
<html>
<head>
<title>Noisetor Finances</title>
</head>
<body>
<table bgcolor=ffffff cellpadding=4>
<tr bgcolor=00ff00 valign=bottom>
<td bgcolor=dddddd>Date
<td bgcolor=dddddd>Item
<td bgcolor=dddddd>Income
<td bgcolor=dddddd>PayPal Fees
<td bgcolor=dddddd align=center>Noisebridge<br>5% Donation
<td bgcolor=dddddd>Expense
<td bgcolor=dddddd>Balance\n";
}
sub format_dollar {
for my $i (@_) {
if (defined $i) {
$i = sprintf "%.02f", $i;
}
else {
$i = " ";
}
}
}
my $lasttime = 0;
sub display {
my ($time, $desc, $income, $expense, $paypal_fees, $noisebridge_fee) = @_;
my $nl;
if (!$time || ($lasttime && (localtime $time)[4] != (localtime $lasttime)[4])) {
for my $i ($month_income, $month_fee, $month_noisebridge, $month_expense) {
format_dollar $i;
}
my $date = POSIX::strftime "%B %Y totals", localtime $lasttime;
$date .= " (so far)" unless $time;
printf "
<tr bgcolor=ffffff>
<td align=left bgcolor=ffffff>
<td align=right bgcolor=ffffff><i><small>$date:</small></i>
<td align=right bgcolor=ffffff><i><small>$month_income</small></i>
<td align=right bgcolor=ffffff><i><small>$month_fee</small></i>
<td align=right bgcolor=ffffff><i><small>$month_noisebridge</small></i>
<td align=right bgcolor=ffffff><i><small>$month_expense</small></i>
<td align=right bgcolor=ffffff> \n";
$month_income = 0;
$month_fee = 0;
$month_noisebridge = 0;
$month_expense = 0;
$nl++;
}
if (!$time || ($lasttime && (localtime $time)[5] != (localtime $lasttime)[5])) {
for my $i ($year_income, $year_fee, $year_noisebridge, $year_expense) {
format_dollar $i;
}
my $date = POSIX::strftime "Year %Y totals", localtime $lasttime;
$date .= " (so far)" unless $time;
printf "
<tr bgcolor=ffffff>
<td align=left bgcolor=ffffff>
<td align=right bgcolor=ffffff><i><small>$date:</small></i>
<td align=right bgcolor=ffffff><i><small>$year_income</small></i>
<td align=right bgcolor=ffffff><i><small>$year_fee</small></i>
<td align=right bgcolor=ffffff><i><small>$year_noisebridge</small></i>
<td align=right bgcolor=ffffff><i><small>$year_expense</small></i>
<td align=right bgcolor=ffffff> \n";
$year_income = 0;
$year_fee = 0;
$year_noisebridge = 0;
$year_expense = 0;
$nl++;
}
$lasttime = $time;
return unless $time;
if ($nl) {
printf "
<tr bgcolor=ffffff>
<td align=left bgcolor=ffffff><tiny> </tiny>
<td align=right bgcolor=ffffff><tiny> </tiny>
<td align=right bgcolor=ffffff><tiny> </tiny>
<td align=right bgcolor=ffffff><tiny> </tiny>
<td align=right bgcolor=ffffff><tiny> </tiny>
<td align=right bgcolor=ffffff><tiny> </tiny>
<td align=right bgcolor=ffffff><tiny> </tiny>\n";
}
my $date = POSIX::strftime "%Y-%m-%d", localtime $time;
if ($income) {
$balance += $income - $paypal_fees - $noisebridge_fee;
$month_income += $income;
$month_fee += $paypal_fees;
$month_noisebridge += $noisebridge_fee;
$year_income += $income;
$year_fee += $paypal_fees;
$year_noisebridge += $noisebridge_fee;
}
if ($expense) {
$balance -= $expense;
$month_expense += $expense;
$year_expense += $expense;
}
my $displaybalance = $balance;
for my $i ($income, $expense, $paypal_fees, $noisebridge_fee, $displaybalance) {
format_dollar $i;
}
printf "
<tr bgcolor=00ff00>
<td align=left bgcolor=00ff00>$date
<td align=left bgcolor=00ff00>$desc
<td align=right bgcolor=00ff00>$income
<td align=right bgcolor=00ff00>$paypal_fees
<td align=right bgcolor=00ff00>$noisebridge_fee
<td align=right bgcolor=00ff00>$expense
<td align=right bgcolor=00ff00>$displaybalance\n";
}
for my $file (@ARGV) {
local $/;
open my $fd, $file or die "open: $file: $!";
my $hash = decode_json <$fd>;
for my $type (qw(expenses donations)) {
if ($hash->{$type}) {
for my $i (@{ $hash->{$type} }) {
$i->{type} = $type;
push @transactions, $i;
}
}
}
}
@transactions = sort { $a->{timestamp} <=> $b->{timestamp} } @transactions;
display_header;
for my $i (@transactions) {
if ($i->{type} eq "donations") {
# paypal
if ($i->{txn_type}) {
if ($i->{recurring}) {
display $i->{timestamp},
"PayPal Subscription",
$i->{payment_gross},
undef,
$i->{payment_fee},
int($i->{payment_gross} * 100 * .05)/100;
}
else {
display $i->{timestamp},
"PayPal",
$i->{payment_gross},
undef,
$i->{payment_fee},
int($i->{payment_gross} * 100 * .05)/100;
}
}
# one-off
else {
die "payment_fee != 0" unless $i->{payment_fee} == 0;
die "payment_net != payment_gross" unless $i->{payment_net} == $i->{payment_gross};
display $i->{timestamp},
$i->{documentation_note},
$i->{payment_gross},
undef,
0,
int($i->{payment_gross} * 100 * .05)/100;
}
}
if ($i->{type} eq "expenses") {
if ($i->{documentation_note} eq "Colo Payment"
&& defined $i->{service_end}
&& $i->{service_end} > $colo_end_date)
{
$colo_end_date = $i->{service_end};
}
if ($i->{service_start} and $i->{service_end}) {
my $start = POSIX::strftime("%b %e", localtime $i->{service_start});
my $end = POSIX::strftime("%b %e", localtime $i->{service_end});
$i->{documentation_note} .= " ($start - $end)";
}
display $i->{timestamp},
$i->{documentation_note},
undef,
$i->{expense_amount},
undef,
undef;
}
}
display 0;
print "
</table>
<br>
<ul>
\n";
format_dollar $balance;
print "
<a name=summary></a>
<blockquote>
Summary, suitable for cutting-and-pasting into the <a href='https://noisebridge.net/wiki/Next_meeting#Financial_Report'>Noisebridge weekly meeting notes</a>:
<blockquote>
<li>There are \$$balance earmarked NoiseTor funds
\n";
if ($colo_end_date) {
my $monthly_fee = 787;
my $months = int($balance / $monthly_fee);
print "<li>Colo service has been paid through ",
POSIX::strftime("%b %e, %Y", localtime $colo_end_date), "\n";
if ($months == 0) {
printf "<li>An additional \$%0.2f needs to be raised to pay for another month of service\n", $monthly_fee - $balance;
}
else {
print "<li>There are enough funds to pay for an additional $months months of colo\n";
}
}
print "<li>This information was updated at ", scalar localtime, "\n";
print "
</blockquote>
</blockquote>
</ul>
<br>
<br>
</body>
</html>\n";