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
|
#!/usr/bin/perl
use strict;
use warnings;
use Math::BigFloat;
use Date::Manip;
use Data::PowerSet;
Math::BigFloat->precision(-2);
my $ZERO = Math::BigFloat->new("0.00");
my $ONE_HUNDRED = Math::BigFloat->new("100.00");
my $VERBOSE = 1;
my $DEBUG = 0;
my $LEDGER_BIN = "/usr/local/bin/ledger";
######################################################################
sub BruteForceSubSetSumSolver ($$$) {
my($numberList, $totalSought, $extractNumber) = @_;
my($P, $N) = (0, 0);
my $size = scalar(@{$numberList});
my %Q;
my(@L) =
map { { val => &$extractNumber($_), obj => $_ } } @{$numberList};
my $powerset = Data::PowerSet->new(@L);
while (my $set = $powerset->next) {
my $total = $ZERO;
foreach my $ee (@{$set}) {
$total += $ee->{val};
}
if ($totalSought == $total) {
my(@list) = map { $_->{obj} } @{$set};
return (1, \@list);
}
}
return (0, []);
}
######################################################################
sub DynamicProgrammingSubSetSumSolver ($$$) {
my($numberList, $totalSought, $extractNumber) = @_;
my($P, $N) = (0, 0);
my $size = scalar(@{$numberList});
my %Q;
my(@L) =
map { { val => &$extractNumber($_), obj => $_ } } @{$numberList};
print STDERR " TotalSought:", $totalSought if $VERBOSE;
print STDERR " L in this iteration:\n [" if $VERBOSE;
foreach my $ee (@L) {
if ($ee->{val} < 0) {
$N += $ee->{val}
} else {
$P += $ee->{val};
}
print STDERR $ee->{val}, ", " if $VERBOSE;
}
print STDERR "]\n P = $P, N = $N\n" if ($VERBOSE);
for (my $ii = 0 ; $ii <= $size ; $ii++ ) {
$Q{$ii}{0}{value} = 1;
$Q{$ii}{0}{list} = [];
}
for (my $jj = $N; $jj <= $P ; $jj++) {
$Q{0}{$jj}{value} = ($L[0]{val} == $jj);
$Q{0}{$jj}{list} = $Q{0}{$jj}{value} ? [ $L[0]{obj} ] : [];
}
for (my $ii = 1; $ii <= $size ; $ii++ ) {
for (my $jj = $N; $jj <= $P ; $jj++) {
if ($Q{$ii-1}{$jj}{value}) {
$Q{$ii}{$jj}{value} = 1;
$Q{$ii}{$jj}{list} = [] unless defined $Q{$ii}{$jj}{list};
push(@{$Q{$ii}{$jj}{list}}, @{$Q{$ii-1}{$jj}{list}});
} elsif ($L[$ii]{val} == $jj) {
$Q{$ii}{$jj}{value} = 1;
$Q{$ii}{$jj}{list} = [] unless defined $Q{$ii}{$jj}{list};
push(@{$Q{$ii}{$jj}{list}}, $jj);
} elsif ($Q{$ii-1}{$jj - $L[$ii]{val}}{value}) {
$Q{$ii}{$jj}{value} = 1;
$Q{$ii}{$jj}{list} = [] unless defined $Q{$ii}{$jj}{list};
push(@{$Q{$ii}{$jj}{list}}, $L[$ii]{obj}, @{$Q{$ii-1}{$jj - $L[$ii]{val}}{list}});
} else {
$Q{$ii}{$jj}{value} = 0;
$Q{$ii}{$jj}{list} = [];
}
}
}
foreach (my $ii = 0; $ii <= $size; $ii++) {
foreach (my $jj = $N; $jj <= $P; $jj++) {
print "Q($ii, $jj) == $Q{$ii}{$jj}{value} with List of ", join(", ", @{$Q{$ii}{$jj}{list}}), "\n";
}
}
return [ $Q{$size}{$totalSought}{value}, \@{$Q{$size}{$totalSought}{list}}];
}
######################################################################
sub Commify ($) {
my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scalar reverse $text;
}
######################################################################
sub ParseNumber($) {
$_[0] =~ s/,//g;
return Math::BigFloat->new($_[0]);
}
######################################################################
sub ConvertTwoDigitPrecisionToInteger ($) {
return sprintf("%d", $_[0] * $ONE_HUNDRED);
}
######################################################################
sub ConvertTwoDigitPrecisionToIntegerInEntry ($) {
return ConvertTwoDigitPrecisionToInteger($_[0]->{amount});
}
######################################################################
my $firstArg = shift @ARGV;
my $solver = \&BruteForceSubSetSumSolver;
if (@ARGV < 7) {
print STDERR "usage: $0 [-d] <TITLE> <ACCOUNT_REGEX> <END_DATE> <START_SEARCH_FROM_DATE> <END_SEARCH_TO_DATE> <BANK_STATEMENT_BALANCE> <LEDGER_OPTIONS>\n";
exit 1;
}
if ($firstArg eq '-d') {
$solver = \&DynamicProgrammingSubSetSumSolver;
} else {
unshift(@ARGV, $firstArg);
}
my($title, $account, $endDate, $startSearchFromDate, $endSearchToDate, $bankBalance, @mainLedgerOptions) = @ARGV;
$bankBalance = ParseNumber($bankBalance);
my(@fullCommand) = ($LEDGER_BIN, @mainLedgerOptions, '-V', '-X', '$',
'-e', $endDate, '-F', '%t\n', 'bal', "/$account/");
open(FILE, "-|", @fullCommand) or die "unable to run command ledger command: @fullCommand: $!";
my $total;
foreach my $line (<FILE>) {
chomp $line;
die "Unable to parse output line from: \"$line\""
unless $line =~ /^\s*\$\s*([\-\d\.\,]+)\s*$/ and not defined $total;
$total = $1;
$total = ParseNumber($total);
}
close FILE;
if (not defined $total or $? != 0) {
die "unable to run ledger @fullCommand: $!";
}
my $differenceSought = $total - $bankBalance;
my $err;
my $formattedEndDate = UnixDate(DateCalc(ParseDate($endDate), ParseDateDelta("- 1 day"), \$err),
"%Y-%m-%d");
die "Date calculation error on $endDate" if ($err);
my $earliestStartDate = DateCalc(ParseDate($endDate), ParseDateDelta("- 1 month"), \$err);
die "Date calculation error on $endDate" if ($err);
my $startDate = ParseDate($startSearchFromDate);
my @solution;
while ($startDate ge $earliestStartDate) {
$startDate = DateCalc(ParseDate($startDate), ParseDateDelta("- 1 day"), \$err);
die "Date calculation error on $endDate" if ($err);
my $formattedStartDate = UnixDate($startDate, "%Y-%m-%d");
print STDERR "Testing $formattedStartDate through $endSearchToDate for a total of ", Commify($differenceSought), ": \n"
if $VERBOSE;
my(@fullCommand) = ($LEDGER_BIN, @mainLedgerOptions, '-V', '-X', '$',
'-b', $formattedStartDate, '-e', $endSearchToDate,
'-F', '"%(date)","%C","%P","%t"\n',
'reg', "/$account/");
open(FILE, "-|", @fullCommand)
or die "unable to run command ledger command: @fullCommand: $!";
my @entries;
foreach my $line (<FILE>) {
die "Unable to parse output line from: $line"
unless $line =~ /^\s*"([^"]*)","([^"]*)","([^"]*)","([^"]*)"\s*$/;
my($date, $checkNum, $payee, $amount) = ($1, $2, $3, $4);
die "$amount is not a valid amount"
unless $amount =~ s/\s*\$\s*([\-\d\.\,]+)\s*$/$1/;
$amount = ParseNumber($amount);
push(@entries, { date => $date, checkNum => $checkNum,
payee => $payee, amount => $amount });
}
close FILE;
die "unable to properly run ledger command: @fullCommand: $!" unless ($? == 0);
@solution = $solver->(\@entries,
ConvertTwoDigitPrecisionToInteger($differenceSought),
\&ConvertTwoDigitPrecisionToIntegerInEntry);
if ($VERBOSE) {
if ($solution[0]) {
use Data::Dumper;
print STDERR "Solution for $formattedStartDate to $formattedEndDate, $differenceSought: \n",
Data::Dumper->Dump(\@solution);
} else {
print STDERR "No Solution Found. :(\n";
}
}
last if ($solution[0]);
}
if ($solution[0]) {
print "\"title:$formattedEndDate: $title\"\n\"BANK RECONCILATION: $account\",\"ENDING\",\"$formattedEndDate\"\n";
print "\n\n\"DATE\",\"CHECK NUM\",\"PAYEE\",\"AMOUNT\"\n\n";
print "\"$formattedEndDate\",\"\",\"BANK ACCOUNT BALANCE\",\"\$$bankBalance\"\n\n";
foreach my $ee (@{$solution[1]}) {
print "\"$ee->{date}\",\"$ee->{checkNum}\",\"$ee->{payee}\",\"\$$ee->{amount}\"\n";
}
print "\n\"$formattedEndDate\",\"\",\"OUR ACCOUNT BALANCE\",\"\$$total\"\n\n";
}
###############################################################################
#
# Local variables:
# compile-command: "perl -c bank-reconcilation.plx"
# End:
|