summaryrefslogtreecommitdiff
path: root/contrib/non-profit-audit-reports/fund-report.plx
blob: 65de85f7376e19edc77b05663ea2126aab788053 (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
#!/usr/bin/perl
# fund-report.plx                                    -*- Perl -*-
#
#    Script to generate a Restricted Fund Report.  Usefulness of this
#    script may be confined to those who track separate funds in their
#    accounts by having accounts that match this format:
#     /^(Income|Expenses|Unearned Income|(Accrued:[^:]+:):PROJECTNAME/

#  Conservancy does this because we carefully track fund balances for our
#  fiscal sponsored projects.  Those who aren't fiscal sponsors won't find
#  this report all that useful, I suspect.  Note that the name
#  "Conservancy" is special-cased in a few places, mainly because our
#  "General" fund is called "Conservancy".

#
# Copyright (C) 2011, 2012, 2013 Bradley M. Kuhn
#
# This program gives you software freedom; you can copy, modify, convey,
# and/or redistribute it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program in a file called 'GPLv3'.  If not, write to the:
#    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor
#                                    Boston, MA 02110-1301, USA.

use strict;
use warnings;

use Math::BigFloat;
use Date::Manip;

my $LEDGER_CMD = "/usr/local/bin/ledger";

my $ACCT_WIDTH = 70;

sub ParseNumber($) {
  $_[0] =~ s/,//g;
  return Math::BigFloat->new($_[0]);
}
Math::BigFloat->precision(-2);
my $ZERO =  Math::BigFloat->new("0.00");
my $TWO_CENTS =  Math::BigFloat->new("0.02");

if (@ARGV < 2) {
  print STDERR "usage: $0 <START_DATE> <END_DATE> <LEDGER_OPTIONS>\n";
  exit 1;
}
my($startDate, $endDate, @mainLedgerOptions) = @ARGV;

my $err;
my $formattedEndDate = UnixDate(DateCalc(ParseDate($endDate), ParseDateDelta("- 1 day"), \$err),
                                "%Y/%m/%d");
die "Date calculation error on $endDate" if ($err);
my $formattedStartDate = UnixDate(ParseDate($startDate), "%Y/%m/%d");
die "Date calculation error on $startDate" if ($err);

# First, get balances for starting and ending for each fund

my %funds;

foreach my $type ('starting', 'ending') {
  my(@ledgerOptions) = (@mainLedgerOptions,
                      '-V', '-X', '$', '-F', "%-.70A %22.108t\n", '-s');

  if ($type eq 'starting') {
    push(@ledgerOptions, '-e', $startDate);
  } else {
    push(@ledgerOptions,'-e', $endDate);
  }
  push(@ledgerOptions, 'reg', '/^(Income|Expenses):([^:]+):/');

  open(LEDGER_FUNDS, "-|", $LEDGER_CMD, @ledgerOptions)
    or die "Unable to run $LEDGER_CMD @ledgerOptions: $!";

  while (my $fundLine = <LEDGER_FUNDS>) {
    die "Unable to parse output line from first funds command: \"$fundLine\""
      unless $fundLine =~ /^\s*([^\$]+)\s+\$\s*\s*([\-\d\.\,]+)/;
    my($account, $amount) = ($1, $2);
    $amount = ParseNumber($amount);
    $account =~ s/\s+$//;
    next if $account =~ /\<Adjustment\>/ and (abs($amount) <= $TWO_CENTS);
    die "Weird account found, $account with amount of $amount in command: @ledgerOptions\n"
      unless $account =~ s/^\s*(?:Income|Expenses):([^:]+)://;
    $account = $1;
    $account = 'General' if $account eq 'Conservancy';   # FIXME: this is a special case for Consrevancy
    $funds{$account}{$type} += $amount;
  }
  close LEDGER_FUNDS;
  die "Failure on ledger command @ledgerOptions: $!" unless ($? == 0);
}
foreach my $fund (keys %funds) {
  foreach my $type (keys %{$funds{$fund}}) {
    $funds{$fund}{$type} = $ZERO - $funds{$fund}{$type};
  }
}
my(@ledgerOptions) = (@mainLedgerOptions,
                  '-V', '-X', '$', '-F', "%-.70A %22.108t\n",  '-w', '-s',
                  '-b', $startDate, '-e', $endDate, 'reg');

my @possibleTypes = ('Income', 'Expenses', 'Unearned Income', 'Retained Earnings', 'Retained Costs',
                     'Accrued:Loans Receivable', 'Accrued:Accounts Payable',
                     'Accrued:Accounts Receivable', 'Accrued:Expenses');

foreach my $type (@possibleTypes) {
  foreach my $fund (keys %funds) {
    my $query;
    $query = ($fund eq 'General') ? "/^${type}:Conservancy/": "/^${type}:$fund/";
    open(LEDGER_INCOME, "-|", $LEDGER_CMD, @ledgerOptions, $query)
      or die "Unable to run $LEDGER_CMD for funds: $!";
    $funds{$fund}{$type} = $ZERO;
    while (my $line = <LEDGER_INCOME>) {
      die "Unable to parse output line from $type line command: $line"
        unless $line =~ /^\s*([^\$]+)\s+\$\s*\s*([\-\d\.\,]+)/;
      my($account, $amount) = ($1, $2);
      $amount = ParseNumber($amount);
      $funds{$fund}{$type} += $amount;
    }
    close LEDGER_INCOME;
    die "Failure on ledger command for ${type}:$fund: $!" unless ($? == 0);
  }
}

my %tot;
($tot{Start}, $tot{End}) = ($ZERO, $ZERO);

my %beforeEndings = ('Income' => 1, 'Expenses' => 1);
my %afterEndings;

# For other @possibleTypes, build up @fieldsList to just those that are present. 

foreach my $fund (keys %funds) {
  foreach my $type (@possibleTypes) {
    if ($funds{$fund}{$type} != $ZERO) {
      if ($type =~ /^(Unearned Income|Accrued)/) {
        $afterEndings{$type} = 1;
      } else {
        $beforeEndings{$type} = 1;
      }
    }
  }
}
my(@beforeEndingFields, @afterEndingFields);

foreach my $ii (@possibleTypes) {
  push(@beforeEndingFields, $ii) if defined $beforeEndings{$ii};
  push(@afterEndingFields, $ii)  if defined $afterEndings{$ii};
}
# Make sure fieldLists present items are zero for those that should be zero.
foreach my $fund (keys %funds) {
  foreach my $type ('starting', @beforeEndingFields, 'ending', @afterEndingFields) {
    $funds{$fund}{$type} = $ZERO unless defined $funds{$fund}{$type};
  }
}

print '"RESTRICTED AND GENERAL FUND REPORT",', "\"BEGINNING:\",\"$formattedStartDate\",\"ENDING:\",\"$formattedEndDate\"\n\n";
print '"FUND","STARTING BALANCE",';
my @finalPrints;
foreach my $type (@beforeEndingFields) {
  $tot{$type} = $ZERO;
  my $formattedType = $type;
  print "\"$formattedType\",";
}
print '"ENDING BALANCE",""';
foreach my $type (@afterEndingFields) {
  $tot{$type} = $ZERO;
  my $formattedType = $type;
  $formattedType = "Prepaid Expenses" if $formattedType eq 'Accrued:Expenses'; 
  $formattedType =~ s/^Accrued://;
  print ",\"$formattedType\"";
}
print "\n\n";

sub printTotal ($$) {
  my($label, $tot) = @_;
  print "\"$label\",\"\$$tot->{Start}\",";
  foreach my $type (@beforeEndingFields) {
    print "\"\$$tot->{$type}\",";
  }
  print "\"\$$tot->{End}\",\"\"";
  foreach my $type (@afterEndingFields) {
    print ",\"\$$tot->{$type}\"";
  }
  print "\n";
}

foreach my $fund (sort {
                        if ($a eq "General") { return 1 }
                        elsif ($b eq "General") { return -1 }
                        else { return $a cmp $b } }
                  keys %funds) {
  my $sanityTotal = $funds{$fund}{starting};

  if ($fund eq 'General') {
    print "\n";
    printTotal("Restricted Subtotal", \%tot);
    print "\n";
  }
  $tot{Start} += $funds{$fund}{starting};
  $tot{End} += $funds{$fund}{ending};

  print "\"$fund\",\"\$$funds{$fund}{starting}\",";
  foreach my $type (@beforeEndingFields) {
    print "\"\$$funds{$fund}{$type}\",";
    $tot{$type} += $funds{$fund}{$type};
  }
  print "\"\$$funds{$fund}{ending}\",\"\"";
  foreach my $type (@afterEndingFields) {
    print ",\"\$$funds{$fund}{$type}\"";
    $tot{$type} += $funds{$fund}{$type};
  }
  print "\n";
  # Sanity check:
  if (abs($funds{$fund}{ending} -
      ($funds{$fund}{starting}
         - $funds{$fund}{Income} - $funds{$fund}{Expenses}))
      > $TWO_CENTS) {
    print "$fund FAILED SANITY CHECK: Ending: $funds{$fund}{ending} \n\n\n";
    warn "$fund FAILED SANITY CHECK";
  }
}
print "\n";
printTotal("OVERALL TOTAL", \%tot);
###############################################################################
#
# Local variables:
# compile-command: "perl -c fund-report.plx"
# End: