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
|
<%
my $conf = new FS::Conf;
my $maxrecords = $conf->config('maxsearchrecordsperpage');
my $orderby = ''; #removeme
my $limit = '';
$limit .= "LIMIT $maxrecords" if $maxrecords;
my $offset = $cgi->param('offset') || 0;
$limit .= " OFFSET $offset" if $offset;
my $total;
my(@cust_bill, $sortby);
if ( $cgi->keywords ) {
my($query) = $cgi->keywords;
my $open_sql =
"having 0 != charged - coalesce(sum(cust_bill_pay.amount),0)
- coalesce(sum(cust_credit_bill.amount),0)";
my $having = '';
my $where = '';
if ( $query eq 'invnum' ) {
$sortby = \*invnum_sort;
#@cust_bill = qsearch('cust_bill', {} );
} elsif ( $query eq 'date' ) {
$sortby = \*date_sort;
#@cust_bill = qsearch('cust_bill', {} );
} elsif ( $query eq 'custnum' ) {
$sortby = \*custnum_sort;
#@cust_bill = qsearch('cust_bill', {} );
} elsif ( $query eq 'OPEN_invnum' ) {
$sortby = \*invnum_sort;
#@cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} );
$having = $open_sql;
} elsif ( $query eq 'OPEN_date' ) {
$sortby = \*date_sort;
#@cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} );
$having = $open_sql;
} elsif ( $query eq 'OPEN_custnum' ) {
$sortby = \*custnum_sort;
#@cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} );
$having = $open_sql;
} elsif ( $query =~ /^OPEN(\d+)_invnum$/ ) {
my $open = $1 * 86400;
$sortby = \*invnum_sort;
#@cust_bill =
# grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} );
$having = $open_sql;
$where = "where cust_bill._date < ". (time-$open);
} elsif ( $query =~ /^OPEN(\d+)_date$/ ) {
my $open = $1 * 86400;
$sortby = \*date_sort;
#@cust_bill =
# grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} );
$having = $open_sql;
$where = "where cust_bill._date < ". (time-$open);
} elsif ( $query =~ /^OPEN(\d+)_custnum$/ ) {
my $open = $1 * 86400;
$sortby = \*custnum_sort;
#@cust_bill =
# grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} );
$having = $open_sql;
$where = "where cust_bill._date < ". (time-$open);
} else {
die "unknown query string $query";
}
my $extra_sql = "
left outer join cust_bill_pay using ( invnum )
left outer join cust_credit_bill using ( invnum )
$where
group by ". join(', ', map "cust_bill.$_", fields('cust_bill') ). ' '.
$having;
my $statement = "SELECT COUNT(*) FROM cust_bill $extra_sql";
my $sth = dbh->prepare($statement) or die dbh->errstr. " doing $statement";
$sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
$total = $sth->fetchrow_arrayref->[0];
@cust_bill = qsearch(
'cust_bill',
{},
'cust_bill.*,
charged - coalesce(sum(cust_bill_pay.amount),0)
- coalesce(sum(cust_credit_bill.amount),0) as owed',
"$extra_sql $orderby $limit"
);
} else {
$cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/;
my $invnum = $2;
@cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum } );
$sortby = \*invnum_sort;
$total = scalar(@cust_bill);
}
#if ( scalar(@cust_bill) == 1 ) {
if ( scalar(@cust_bill) == 1 && $total == 1) {
#if ( $total == 1 ) {
my $invnum = $cust_bill[0]->invnum;
print $cgi->redirect(popurl(2). "view/cust_bill.cgi?$invnum"); #redirect
} elsif ( scalar(@cust_bill) == 0 ) {
%>
<!-- mason kludge -->
<%
eidiot("Invoice not found.");
} else {
%>
<!-- mason kludge -->
<%
#$total = scalar(@cust_bill);
#begin pager
my $pager = '';
if ( $total != scalar(@cust_bill) && $maxrecords ) {
unless ( $offset == 0 ) {
$cgi->param('offset', $offset - $maxrecords);
$pager .= '<A HREF="'. $cgi->self_url.
'"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
}
my $poff;
my $page;
for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
$page++;
if ( $offset == $poff ) {
$pager .= qq!<FONT SIZE="+2">$page</FONT> !;
} else {
$cgi->param('offset', $poff);
$pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
}
}
unless ( $offset + $maxrecords > $total ) {
$cgi->param('offset', $offset + $maxrecords);
$pager .= '<A HREF="'. $cgi->self_url.
'"><B><FONT SIZE="+1">Next</FONT></B></A> ';
}
}
#end pager
print header("Invoice Search Results", menubar(
'Main Menu', popurl(2)
)), "$total matching invoices found<BR><BR>$pager", &table(), <<END;
<TR>
<TH></TH>
<TH>Balance</TH>
<TH>Amount</TH>
<TH>Date</TH>
<TH>Contact name</TH>
<TH>Company</TH>
</TR>
END
my(%saw, $cust_bill);
my($tot_balance, $tot_amount) = (0, 0); #BOGUS
foreach $cust_bill (
sort $sortby grep(!$saw{$_->invnum}++, @cust_bill)
) {
my($invnum, $owed, $charged, $date ) = (
$cust_bill->invnum,
sprintf("%.2f", $cust_bill->owed),
sprintf("%.2f", $cust_bill->charged),
$cust_bill->_date,
);
my $pdate = time2str("%b %d %Y", $date);
$tot_balance += $owed;
$tot_amount += $charged;
my $rowspan = 1;
my $view = popurl(2). "view/cust_bill.cgi?$invnum";
print <<END;
<TR>
<TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$invnum</FONT></A></TD>
<TD ROWSPAN=$rowspan ALIGN="right"><A HREF="$view"><FONT SIZE=-1>\$$owed</FONT></A></TD>
<TD ROWSPAN=$rowspan ALIGN="right"><A HREF="$view"><FONT SIZE=-1>\$$charged</FONT></A></TD>
<TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$pdate</FONT></A></TD>
END
my $custnum = $cust_bill->custnum;
my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
if ( $cust_main ) {
my $cview = popurl(2). "view/cust_main.cgi?". $cust_main->custnum;
my ( $name, $company ) = (
$cust_main->last. ', '. $cust_main->first,
$cust_main->company,
);
print <<END;
<TD ROWSPAN=$rowspan><A HREF="$cview"><FONT SIZE=-1>$name</FONT></A></TD>
<TD ROWSPAN=$rowspan><A HREF="$cview"><FONT SIZE=-1>$company</FONT></A></TD>
END
} else {
print <<END
<TD ROWSPAN=$rowspan COLSPAN=2>WARNING: couldn't find cust_main.custnum $custnum (cust_bill.invnum $invnum)</TD>
END
}
print "</TR>";
}
$tot_balance = sprintf("%.2f", $tot_balance);
$tot_amount = sprintf("%.2f", $tot_amount);
print <<END;
<TR><TD></TD><TH><FONT SIZE=-1>Total</FONT></TH><TH><FONT SIZE=-1>Total</FONT></TH></TR>
<TR><TD></TD><TD ALIGN="right"><FONT SIZE=-1>\$$tot_balance</FONT></TD><TD ALIGN="right"><FONT SIZE=-1>\$$tot_amount</FONT></TD></TD></TR>
</TABLE>$pager
</BODY>
</HTML>
END
}
#
sub invnum_sort {
$a->invnum <=> $b->invnum;
}
sub custnum_sort {
$a->custnum <=> $b->custnum || $a->invnum <=> $b->invnum;
}
sub date_sort {
$a->_date <=> $b->_date || $a->invnum <=> $b->invnum;
}
%>
|