paged package browse
[freeside.git] / httemplate / search / cust_main.cgi
1 <%
2 #<!-- $Id: cust_main.cgi,v 1.17 2001-12-03 10:59:25 ivan Exp $ -->
3
4 use strict;
5 #use vars qw( $conf %ncancelled_pkgs %all_pkgs $cgi @cust_main $sortby );
6 #use vars qw( $conf %all_pkgs $cgi @cust_main $sortby );
7 use vars qw( $conf %all_pkgs $cgi @cust_main $sortby
8              $orderby $maxrecords $limit $offset );
9 use CGI;
10 use CGI::Carp qw(fatalsToBrowser);
11 use IO::Handle;
12 use String::Approx qw(amatch);
13 use FS::UID qw(dbh cgisuidsetup);
14 use FS::Conf;
15 use FS::Record qw(qsearch qsearchs dbdef jsearch);
16 use FS::CGI qw(header menubar eidiot popurl table);
17 use FS::cust_main;
18 use FS::cust_svc;
19
20 $cgi = new CGI;
21 cgisuidsetup($cgi);
22
23 $conf = new FS::Conf;
24 $maxrecords = $conf->config('maxsearchrecordsperpage');
25
26 #my $cache;
27
28 #my $monsterjoin = <<END;
29 #cust_main left outer join (
30 #  ( cust_pkg left outer join part_pkg using(pkgpart)
31 #  ) left outer join (
32 #    (
33 #      (
34 #        ( cust_svc left outer join part_svc using (svcpart)
35 #        ) left outer join svc_acct using (svcnum)
36 #      ) left outer join svc_domain using(svcnum)
37 #    ) left outer join svc_forward using(svcnum)
38 #  ) using (pkgnum)
39 #) using (custnum)
40 #END
41
42 #my $monsterjoin = <<END;
43 #cust_main left outer join (
44 #  ( cust_pkg left outer join part_pkg using(pkgpart)
45 #  ) left outer join (
46 #    (
47 #      (
48 #        ( cust_svc left outer join part_svc using (svcpart)
49 #        ) left outer join (
50 #          svc_acct left outer join (
51 #            select svcnum, domain, catchall from svc_domain
52 #            ) as svc_acct_domsvc (
53 #              svc_acct_svcnum, svc_acct_domain, svc_acct_catchall
54 #          ) on svc_acct.domsvc = svc_acct_domsvc.svc_acct_svcnum
55 #        ) using (svcnum)
56 #      ) left outer join svc_domain using(svcnum)
57 #    ) left outer join svc_forward using(svcnum)
58 #  ) using (pkgnum)
59 #) using (custnum)
60 #END
61
62 $orderby = ''; #removeme
63
64 $limit = '';
65 $limit .= "LIMIT $maxrecords" if $maxrecords;
66
67 $offset = $cgi->param('offset') || 0;
68 $limit .= " OFFSET $offset" if $offset;
69
70 my $total;
71
72 if ( $cgi->param('browse') ) {
73   my $query = $cgi->param('browse');
74   if ( $query eq 'custnum' ) {
75     $sortby=\*custnum_sort;
76     $orderby = 'ORDER BY custnum';
77   } elsif ( $query eq 'last' ) {
78     $sortby=\*last_sort;
79     $orderby = 'ORDER BY last';
80   } elsif ( $query eq 'company' ) {
81     $sortby=\*company_sort;
82     $orderby = 'ORDER BY company';
83   } else {
84     die "unknown browse field $query";
85   }
86
87   my $ncancelled = '';
88
89   if (  $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
90        || ( $conf->exists('hidecancelledcustomers')
91              && ! $cgi->param('showcancelledcustomers') )
92      ) {
93     #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
94     #needed for MySQL???    OR cust_pkg.cancel = \"\"
95     $ncancelled = "
96       WHERE 0 < ( SELECT COUNT(*) FROM cust_pkg
97                     WHERE cust_pkg.custnum = cust_main.custnum
98                       AND ( cust_pkg.cancel IS NULL
99                             OR cust_pkg.cancel = 0
100                           )
101                 )
102          OR 0 = ( SELECT COUNT(*) FROM cust_pkg
103                     WHERE cust_pkg.custnum = cust_main.custnum
104                 )
105     ";
106   }
107
108   my $statement = "SELECT COUNT(*) FROM cust_main $ncancelled";
109   my $sth = dbh->prepare($statement)
110     or die dbh->errstr. " doing $statement";
111   $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
112
113   $total = @{$sth->fetchrow_arrayref}[0];
114
115   my @just_cust_main = qsearch('cust_main',{}, '',
116     "$ncancelled $orderby $limit"
117   );    
118
119   @cust_main = @just_cust_main;
120
121 #  foreach my $cust_main ( @just_cust_main ) {
122 #
123 #    my @one_cust_main;
124 #    $FS::Record::DEBUG=1;
125 #    ( $cache, @one_cust_main ) = jsearch(
126 #      "$monsterjoin",
127 #      { 'custnum' => $cust_main->custnum },
128 #      '',
129 #      '',
130 #      'cust_main',
131 #      'custnum',
132 #    );
133 #    push @cust_main, @one_cust_main;
134 #  }
135
136 } else {
137   @cust_main=();
138   &cardsearch if $cgi->param('card_on') && $cgi->param('card');
139   &lastsearch if $cgi->param('last_on') && $cgi->param('last_text');
140   &companysearch if $cgi->param('company_on') && $cgi->param('company_text');
141   &referralsearch if $cgi->param('referral_custnum');
142
143   @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
144     if $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
145        || ( $conf->exists('hidecancelledcustomers')
146              && ! $cgi->param('showcancelledcustomers') );
147 }
148
149 if ( $conf->exists('hidecancelledpackages' ) ) {
150   %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
151 } else {
152   %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
153 }
154 #%all_pkgs = ();
155
156 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
157   print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
158   exit;
159 } elsif ( scalar(@cust_main) == 0 ) {
160   eidiot "No matching customers found!\n";
161 } else { 
162
163   $total ||= scalar(@cust_main);
164   print header("Customer Search Results",menubar(
165     'Main Menu', popurl(2)
166   )), "$total matching customers found ";
167
168   #begin pager
169   my $pager = '';
170   if ( $total != scalar(@cust_main) && $maxrecords ) {
171     unless ( $offset == 0 ) {
172       $cgi->param('offset', $offset - $maxrecords);
173       $pager .= '<A HREF="'. $cgi->self_url.
174                 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
175     }
176     my $poff;
177     my $page;
178     for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
179       $page++;
180       if ( $offset == $poff ) {
181         $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
182       } else {
183         $cgi->param('offset', $poff);
184         $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
185       }
186     }
187     unless ( $offset + $maxrecords > $total ) {
188       $cgi->param('offset', $offset + $maxrecords);
189       $pager .= '<A HREF="'. $cgi->self_url.
190                 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
191     }
192   }
193   #end pager
194   
195   if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
196        || ( $conf->exists('hidecancelledcustomers')
197             && ! $cgi->param('showcancelledcustomers')
198           )
199      ) {
200     $cgi->param('showcancelledcustomers', 1);
201     $cgi->param('offset', 0);
202     print qq!( <a href="!. $cgi->self_url. qq!">show cancelled customers</a> )!;
203   } else {
204     $cgi->param('showcancelledcustomers', 0);
205     $cgi->param('offset', 0);
206     print qq!( <a href="!. $cgi->self_url. qq!">hide cancelled customers</a> )!;
207   }
208   if ( $cgi->param('referral_custnum') ) {
209     $cgi->param('referral_custnum') =~ /^(\d+)$/
210       or eidiot "Illegal referral_custnum\n";
211     my $referral_custnum = $1;
212     my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
213     print '<FORM METHOD=POST>'.
214           qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
215           'referrals of <A HREF="'. popurl(2).
216           "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
217           ( $cust_main->company
218             || $cust_main->last. ', '. $cust_main->first ).
219           '</A>';
220     print "\n",<<END;
221       <SCRIPT>
222       function changed(what) {
223         what.form.submit();
224       }
225       </SCRIPT>
226 END
227     print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
228     my $max = 8; #config file
229     $cgi->param('referral_depth') =~ /^(\d*)$/ 
230       or eidiot "Illegal referral_depth";
231     my $referral_depth = $1;
232
233     foreach my $depth ( 1 .. $max ) {
234       print '<OPTION',
235             ' SELECTED'x($depth == $referral_depth),
236             ">$depth";
237     }
238     print "</SELECT> levels deep".
239           '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
240           '</FORM>';
241   }
242
243   print "<BR><BR>". $pager. &table(). <<END;
244       <TR>
245         <TH></TH>
246         <TH>(bill) name</TH>
247         <TH>company</TH>
248 END
249
250 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
251   print <<END;
252       <TH>(service) name</TH>
253       <TH>company</TH>
254 END
255 }
256
257 print <<END;
258         <TH>Packages</TH>
259         <TH COLSPAN=2>Services</TH>
260       </TR>
261 END
262
263   my(%saw,$cust_main);
264   my $p = popurl(2);
265   foreach $cust_main (
266     sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
267   ) {
268     my($custnum,$last,$first,$company)=(
269       $cust_main->custnum,
270       $cust_main->getfield('last'),
271       $cust_main->getfield('first'),
272       $cust_main->company,
273     );
274
275     my(@lol_cust_svc);
276     my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
277     foreach ( @{$all_pkgs{$custnum}} ) {
278       #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
279       my @cust_svc = $_->cust_svc;
280       push @lol_cust_svc, \@cust_svc;
281       $rowspan += scalar(@cust_svc) || 1;
282     }
283
284     #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
285     my $view = $p. 'view/cust_main.cgi?'. $custnum;
286     print <<END;
287     <TR>
288       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
289       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
290       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$company</FONT></A></TD>
291 END
292     if ( defined dbdef->table('cust_main')->column('ship_last') ) {
293       my($ship_last,$ship_first,$ship_company)=(
294         $cust_main->ship_last || $cust_main->getfield('last'),
295         $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
296         $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
297       );
298 print <<END;
299       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
300       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A></TD>
301 END
302     }
303
304     my($n1)='';
305     foreach ( @{$all_pkgs{$custnum}} ) {
306       my $pkgnum = $_->pkgnum;
307 #      my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
308       my $part_pkg = $_->part_pkg;
309
310       my $pkg = $part_pkg->pkg;
311       my $comment = $part_pkg->comment;
312       my $pkgview = $p. 'view/cust_pkg.cgi?'. $pkgnum;
313       my @cust_svc = @{shift @lol_cust_svc};
314       #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
315       my $rowspan = scalar(@cust_svc) || 1;
316
317       print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
318       my($n2)='';
319       foreach my $cust_svc ( @cust_svc ) {
320          my($label, $value, $svcdb) = $cust_svc->label;
321          my($svcnum) = $cust_svc->svcnum;
322          my($sview) = $p.'view';
323          print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
324                qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
325          $n2="</TR><TR>";
326       }
327       #print qq!</TR><TR>\n!;
328       $n1="</TR><TR>";
329     }
330     print "</TR>";
331   }
332  
333   print "</TABLE>$pager</BODY></HTML>";
334
335 }
336
337 #undef $cache; #does this help?
338
339 #
340
341 sub last_sort {
342   $a->getfield('last') cmp $b->getfield('last');
343 }
344
345 sub company_sort {
346   return -1 if $a->company && ! $b->company;
347   return 1 if ! $a->company && $b->company;
348   $a->getfield('company') cmp $b->getfield('company');
349 }
350
351 sub custnum_sort {
352   $a->getfield('custnum') <=> $b->getfield('custnum');
353 }
354
355 sub cardsearch {
356
357   my($card)=$cgi->param('card');
358   $card =~ s/\D//g;
359   $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
360   my($payinfo)=$1;
361
362   push @cust_main, qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'});
363   $sortby=\*last_sort;
364 }
365
366 sub referralsearch {
367   $cgi->param('referral_custnum') =~ /^(\d+)$/
368     or eidiot "Illegal referral_custnum";
369   my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
370     or eidiot "Customer $1 not found";
371   my $depth;
372   if ( $cgi->param('referral_depth') ) {
373     $cgi->param('referral_depth') =~ /^(\d+)$/
374       or eidiot "Illegal referral_depth";
375     $depth = $1;
376   } else {
377     $depth = 1;
378   }
379   push @cust_main, $cust_main->referral_cust_main($depth);
380   $sortby=\*last_sort;
381 }
382
383 sub lastsearch {
384   my(%last_type);
385   foreach ( $cgi->param('last_type') ) {
386     $last_type{$_}++;
387   }
388
389   $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
390     or eidiot "Illegal last name";
391   my($last)=$1;
392
393   if ( $last_type{'Exact'}
394        && ! $last_type{'Fuzzy'} 
395      #  && ! $last_type{'Sound-alike'}
396   ) {
397
398     push @cust_main, qsearch('cust_main',{'last'=>$last});
399
400     push @cust_main, qsearch('cust_main',{'ship_last'=>$last})
401       if defined dbdef->table('cust_main')->column('ship_last');
402
403   } else {
404
405     &FS::cust_main::check_and_rebuild_fuzzyfiles;
406     my $all_last = &FS::cust_main::all_last;
407
408     my %last;
409     if ($last_type{'Fuzzy'}) { 
410       foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
411         $last{$_}++; 
412       }
413     }
414
415     #if ($last_type{'Sound-alike'}) {
416     #}
417
418     foreach ( keys %last ) {
419       push @cust_main, qsearch('cust_main',{'last'=>$_});
420       push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
421         if defined dbdef->table('cust_main')->column('ship_last');
422     }
423
424   }
425   $sortby=\*last_sort;
426 }
427
428 sub companysearch {
429
430   my(%company_type);
431   foreach ( $cgi->param('company_type') ) {
432     $company_type{$_}++ 
433   };
434
435   $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
436     or eidiot "Illegal company";
437   my($company)=$1;
438
439   if ( $company_type{'Exact'}
440        && ! $company_type{'Fuzzy'} 
441      #  && ! $company_type{'Sound-alike'}
442   ) {
443
444     push @cust_main, qsearch('cust_main',{'company'=>$company});
445
446     push @cust_main, qsearch('cust_main',{'ship_company'=>$company})
447       if defined dbdef->table('cust_main')->column('ship_last');
448
449   } else {
450
451     &FS::cust_main::check_and_rebuild_fuzzyfiles;
452     my $all_company = &FS::cust_main::all_company;
453
454     my %company;
455     if ($company_type{'Fuzzy'}) { 
456       foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
457         $company{$_}++;
458       }
459     }
460
461     #if ($company_type{'Sound-alike'}) {
462     #}
463
464     foreach ( keys %company ) {
465       push @cust_main, qsearch('cust_main',{'company'=>$_});
466       push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
467         if defined dbdef->table('cust_main')->column('ship_last');
468     }
469
470   }
471   $sortby=\*company_sort;
472
473 }
474 %>