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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
<%
my $conf = new FS::Conf;
my $maxrecords = $conf->config('maxsearchrecordsperpage');
my %part_pkg = map { $_->pkgpart => $_ } qsearch('part_pkg', {});
my $limit = '';
$limit .= "LIMIT $maxrecords" if $maxrecords;
my $offset = $cgi->param('offset') || 0;
$limit .= " OFFSET $offset" if $offset;
my $total;
my($query) = $cgi->keywords;
my $sortby;
my @cust_pkg;
if ( $cgi->param('magic') && $cgi->param('magic') eq 'bill' ) {
$sortby=\*bill_sort;
#false laziness with cust_pay.cgi
my $range = '';
if ( $cgi->param('beginning')
&& $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
my $beginning = str2time($1);
$range = " WHERE bill >= $beginning ";
}
if ( $cgi->param('ending')
&& $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
my $ending = str2time($1) + 86399;
$range .= ( $range ? ' AND ' : ' WHERE ' ). " bill <= $ending ";
}
$range .= ( $range ? 'AND ' : ' WHERE ' ). '( cancel IS NULL OR cancel = 0 )';
if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
$range .= ( $range ? 'AND ' : ' WHERE ' ).
"$1 = ( SELECT agentnum FROM cust_main".
" WHERE cust_main.custnum = cust_pkg.custnum )";
}
#false laziness with below
my $statement = "SELECT COUNT(*) FROM cust_pkg $range";
warn $statement;
my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
$sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
$total = $sth->fetchrow_arrayref->[0];
@cust_pkg = qsearch('cust_pkg',{}, '', " $range ORDER BY bill $limit" );
} else {
my $qual = '';
if ( $cgi->param('magic') &&
$cgi->param('magic') =~ /^(active|suspended|canceled)$/
) {
if ( $cgi->param('magic') eq 'active' ) {
$qual = 'WHERE ( susp IS NULL OR susp = 0 )'.
' AND ( cancel IS NULL OR cancel = 0)';
} elsif ( $cgi->param('magic') eq 'suspended' ) {
$qual = 'WHERE susp IS NOT NULL AND susp != 0'.
' AND ( cancel IS NULL OR cancel = 0)';
} elsif ( $cgi->param('magic') eq 'canceled' ) {
$qual = 'WHERE cancel IS NOT NULL AND cancel != 0';
} else {
die "guru meditation #420";
}
$sortby = \*pkgnum_sort;
if ( $cgi->param('pkgpart') =~ /^(\d+)$/ ) {
$qual .= " AND pkgpart = $1";
}
} elsif ( $query eq 'pkgnum' ) {
$sortby=\*pkgnum_sort;
} elsif ( $query eq 'APKG_pkgnum' ) {
$sortby=\*pkgnum_sort;
#@cust_pkg=();
##perhaps this should go in cust_pkg as a qsearch-like constructor?
#my($cust_pkg);
#foreach $cust_pkg (
# qsearch('cust_pkg',{}, '', "ORDER BY pkgnum $limit" )
#) {
# my($flag)=0;
# my($pkg_svc);
# PKG_SVC:
# foreach $pkg_svc (qsearch('pkg_svc',{ 'pkgpart' => $cust_pkg->pkgpart })) {
# if ( $pkg_svc->quantity
# > scalar(qsearch('cust_svc',{
# 'pkgnum' => $cust_pkg->pkgnum,
# 'svcpart' => $pkg_svc->svcpart,
# }))
# )
# {
# $flag=1;
# last PKG_SVC;
# }
# }
# push @cust_pkg, $cust_pkg if $flag;
#}
if ( driver_name eq 'mysql' ) {
#$query = "DROP TABLE temp1_$$,temp2_$$;";
#my $sth = dbh->prepare($query);
#$sth->execute;
$query = "CREATE TEMPORARY TABLE temp1_$$ TYPE=MYISAM
SELECT cust_svc.pkgnum,cust_svc.svcpart,COUNT(*) as count
FROM cust_pkg,cust_svc,pkg_svc
WHERE cust_pkg.pkgnum = cust_svc.pkgnum
AND cust_svc.svcpart = pkg_svc.svcpart
AND cust_pkg.pkgpart = pkg_svc.pkgpart
GROUP BY cust_svc.pkgnum,cust_svc.svcpart";
my $sth = dbh->prepare($query) or die dbh->errstr. " preparing $query";
$sth->execute or die "Error executing \"$query\": ". $sth->errstr;
$query = "CREATE TEMPORARY TABLE temp2_$$ TYPE=MYISAM
SELECT cust_pkg.pkgnum FROM cust_pkg
LEFT JOIN pkg_svc ON (cust_pkg.pkgpart=pkg_svc.pkgpart)
LEFT JOIN temp1_$$ ON (cust_pkg.pkgnum = temp1_$$.pkgnum
AND pkg_svc.svcpart=temp1_$$.svcpart)
WHERE ( pkg_svc.quantity > temp1_$$.count
OR temp1_$$.pkgnum IS NULL )
AND pkg_svc.quantity != 0;";
$sth = dbh->prepare($query) or die dbh->errstr. " preparing $query";
$sth->execute or die "Error executing \"$query\": ". $sth->errstr;
$qual = " LEFT JOIN temp2_$$ ON cust_pkg.pkgnum = temp2_$$.pkgnum
WHERE temp2_$$.pkgnum IS NOT NULL";
} else {
$qual = "
WHERE 0 <
( SELECT count(*) FROM pkg_svc
WHERE pkg_svc.pkgpart = cust_pkg.pkgpart
AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
WHERE cust_svc.pkgnum = cust_pkg.pkgnum
AND cust_svc.svcpart = pkg_svc.svcpart
)
)
";
}
} else {
die "Empty or unknown QUERY_STRING!";
}
my $statement = "SELECT COUNT(*) FROM cust_pkg $qual";
my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
$sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
$total = $sth->fetchrow_arrayref->[0];
my $tblname = driver_name eq 'mysql' ? 'cust_pkg.' : '';
@cust_pkg =
qsearch('cust_pkg',{}, '', "$qual ORDER BY ${tblname}pkgnum $limit" );
if ( driver_name eq 'mysql' ) {
$query = "DROP TABLE temp1_$$,temp2_$$;";
my $sth = dbh->prepare($query) or die dbh->errstr. " doing $query";
$sth->execute; # or die "Error executing \"$query\": ". $sth->errstr;
}
}
if ( scalar(@cust_pkg) == 1 ) {
print $cgi->redirect("${p}view/cust_main.cgi?". $cust_pkg[0]->custnum.
"#cust_pkg". $cust_pkg[0]->pkgnum );
#exit;
} elsif ( scalar(@cust_pkg) == 0 ) { #error
%>
<!-- mason kludge -->
<%
eidiot("No packages found");
} else {
%>
<!-- mason kludge -->
<%
$total ||= scalar(@cust_pkg);
#begin pager
my $pager = '';
if ( $total != scalar(@cust_pkg) && $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('Package Search Results',''),
"$total matching packages found<BR><BR>$pager", &table(), <<END;
<TR>
<TH>Package</TH>
<TH><FONT SIZE=-1>Setup</FONT></TH>
END
print '<TH><FONT SIZE=-1>Last<BR>bill</FONT></TH>'
if defined dbdef->table('cust_pkg')->column('last_bill');
print <<END;
<TH><FONT SIZE=-1>Next<BR>bill</FONT></TH>
<TH><FONT SIZE=-1>Susp.</FONT></TH>
<TH><FONT SIZE=-1>Expire</FONT></TH>
<TH><FONT SIZE=-1>Cancel</FONT></TH>
<TH><FONT SIZE=-1>Cust#</FONT></TH>
<TH>(bill) name</TH>
<TH>company</TH>
END
print '<TH>(service) name</TH><TH>company</TH>'
if defined dbdef->table('cust_main')->column('ship_last');
print '<TH COLSPAN=2>Services</TH></TR>';
my $n1 = '<TR>';
my(%saw,$cust_pkg);
foreach $cust_pkg (
sort $sortby grep(!$saw{$_->pkgnum}++, @cust_pkg)
) {
my($cust_main)=qsearchs('cust_main',{'custnum'=>$cust_pkg->custnum});
my($pkgnum, $setup, $bill, $susp, $expire, $cancel,
$custnum, $last, $first, $company ) = (
$cust_pkg->pkgnum,
$cust_pkg->getfield('setup')
? time2str("%D", $cust_pkg->getfield('setup') )
: '',
$cust_pkg->getfield('bill')
? time2str("%D", $cust_pkg->getfield('bill') )
: '',
$cust_pkg->getfield('susp')
? time2str("%D", $cust_pkg->getfield('susp') )
: '',
$cust_pkg->getfield('expire')
? time2str("%D", $cust_pkg->getfield('expire') )
: '',
$cust_pkg->getfield('cancel')
? time2str("%D", $cust_pkg->getfield('cancel') )
: '',
$cust_pkg->custnum,
$cust_main ? $cust_main->last : '',
$cust_main ? $cust_main->first : '',
$cust_main ? $cust_main->company : '',
);
my $last_bill = $cust_pkg->getfield('last_bill')
? time2str("%D", $cust_pkg->getfield('last_bill') )
: ''
if defined dbdef->table('cust_pkg')->column('last_bill');
my($ship_last, $ship_first, $ship_company);
if ( defined dbdef->table('cust_main')->column('ship_last') ) {
($ship_last, $ship_first, $ship_company) = (
$cust_main
? ( $cust_main->ship_last || $cust_main->getfield('last') )
: '',
$cust_main
? ( $cust_main->ship_last
? $cust_main->ship_first
: $cust_main->first )
: '',
$cust_main
? ( $cust_main->ship_last
? $cust_main->ship_company
: $cust_main->company )
: '',
);
}
my $pkg = $part_pkg{$cust_pkg->pkgpart}->pkg;
#$pkg .= ' - '. $part_pkg{$cust_pkg->pkgpart}->comment;
my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
my $rowspan = scalar(@cust_svc) || 1;
my $p = popurl(2);
print $n1, <<END;
<TD ROWSPAN=$rowspan><A HREF="${p}view/cust_main.cgi?$custnum#cust_pkg$pkgnum"><FONT SIZE=-1>$pkgnum - $pkg</FONT></A></TD>
<TD ROWSPAN=$rowspan>$setup</TD>
END
print "<TD ROWSPAN=$rowspan>$last_bill</TD>"
if defined dbdef->table('cust_pkg')->column('last_bill');
print <<END;
<TD ROWSPAN=$rowspan>$bill</TD>
<TD ROWSPAN=$rowspan>$susp</TD>
<TD ROWSPAN=$rowspan>$expire</TD>
<TD ROWSPAN=$rowspan>$cancel</TD>
END
if ( $cust_main ) {
print <<END;
<TD ROWSPAN=$rowspan><FONT SIZE=-1><A HREF="${p}view/cust_main.cgi?$custnum">$custnum</A></FONT></TD>
<TD ROWSPAN=$rowspan><FONT SIZE=-1><A HREF="${p}view/cust_main.cgi?$custnum">$last, $first</A></FONT></TD>
<TD ROWSPAN=$rowspan><FONT SIZE=-1><A HREF="${p}view/cust_main.cgi?$custnum">$company</A></FONT></TD>
END
if ( defined dbdef->table('cust_main')->column('ship_last') ) {
print <<END;
<TD ROWSPAN=$rowspan><FONT SIZE=-1><A HREF="${p}view/cust_main.cgi?$custnum">$ship_last, $ship_first</A></FONT></TD>
<TD ROWSPAN=$rowspan><FONT SIZE=-1><A HREF="${p}view/cust_main.cgi?$custnum">$ship_company</A></FONT></TD>
END
}
} else {
my $colspan = defined dbdef->table('cust_main')->column('ship_last')
? 5 : 3;
print <<END;
<TD ROWSPAN=$rowspan COLSPAN=$colspan>WARNING: couldn't find cust_main.custnum $custnum (cust_pkg.pkgnum $pkgnum)</TD>
END
}
my $n2 = '';
foreach my $cust_svc ( @cust_svc ) {
my($label, $value, $svcdb) = $cust_svc->label;
my $svcnum = $cust_svc->svcnum;
my $sview = $p. "view";
print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
$n2="</TR><TR>";
}
$n1 = "</TR><TR>";
}
print '</TR>';
print "</TABLE>$pager</BODY></HTML>";
}
sub pkgnum_sort {
$a->getfield('pkgnum') <=> $b->getfield('pkgnum');
}
sub bill_sort {
$a->getfield('bill') <=> $b->getfield('bill');
}
%>
|