expedited check entry
[freeside.git] / httemplate / search / cust_main.cgi
1 <%
2 #<!-- $Id: cust_main.cgi,v 1.19 2001-12-26 05:19:01 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   if ( $cgi->param('quickpay') eq 'yes' ) {
158     print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
159   } else {
160     print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
161   }
162   exit;
163 } elsif ( scalar(@cust_main) == 0 ) {
164   eidiot "No matching customers found!\n";
165 } else { 
166
167   $total ||= scalar(@cust_main);
168   print header("Customer Search Results",menubar(
169     'Main Menu', popurl(2)
170   )), "$total matching customers found ";
171
172   #begin pager
173   my $pager = '';
174   if ( $total != scalar(@cust_main) && $maxrecords ) {
175     unless ( $offset == 0 ) {
176       $cgi->param('offset', $offset - $maxrecords);
177       $pager .= '<A HREF="'. $cgi->self_url.
178                 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
179     }
180     my $poff;
181     my $page;
182     for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
183       $page++;
184       if ( $offset == $poff ) {
185         $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
186       } else {
187         $cgi->param('offset', $poff);
188         $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
189       }
190     }
191     unless ( $offset + $maxrecords > $total ) {
192       $cgi->param('offset', $offset + $maxrecords);
193       $pager .= '<A HREF="'. $cgi->self_url.
194                 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
195     }
196   }
197   #end pager
198   
199   if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
200        || ( $conf->exists('hidecancelledcustomers')
201             && ! $cgi->param('showcancelledcustomers')
202           )
203      ) {
204     $cgi->param('showcancelledcustomers', 1);
205     $cgi->param('offset', 0);
206     print qq!( <a href="!. $cgi->self_url. qq!">show cancelled customers</a> )!;
207   } else {
208     $cgi->param('showcancelledcustomers', 0);
209     $cgi->param('offset', 0);
210     print qq!( <a href="!. $cgi->self_url. qq!">hide cancelled customers</a> )!;
211   }
212   if ( $cgi->param('referral_custnum') ) {
213     $cgi->param('referral_custnum') =~ /^(\d+)$/
214       or eidiot "Illegal referral_custnum\n";
215     my $referral_custnum = $1;
216     my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
217     print '<FORM METHOD=POST>'.
218           qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
219           'referrals of <A HREF="'. popurl(2).
220           "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
221           ( $cust_main->company
222             || $cust_main->last. ', '. $cust_main->first ).
223           '</A>';
224     print "\n",<<END;
225       <SCRIPT>
226       function changed(what) {
227         what.form.submit();
228       }
229       </SCRIPT>
230 END
231     print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
232     my $max = 8; #config file
233     $cgi->param('referral_depth') =~ /^(\d*)$/ 
234       or eidiot "Illegal referral_depth";
235     my $referral_depth = $1;
236
237     foreach my $depth ( 1 .. $max ) {
238       print '<OPTION',
239             ' SELECTED'x($depth == $referral_depth),
240             ">$depth";
241     }
242     print "</SELECT> levels deep".
243           '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
244           '</FORM>';
245   }
246
247   print "<BR><BR>". $pager. &table(). <<END;
248       <TR>
249         <TH></TH>
250         <TH>(bill) name</TH>
251         <TH>company</TH>
252 END
253
254 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
255   print <<END;
256       <TH>(service) name</TH>
257       <TH>company</TH>
258 END
259 }
260
261 print <<END;
262         <TH>Packages</TH>
263         <TH COLSPAN=2>Services</TH>
264       </TR>
265 END
266
267   my(%saw,$cust_main);
268   my $p = popurl(2);
269   foreach $cust_main (
270     sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
271   ) {
272     my($custnum,$last,$first,$company)=(
273       $cust_main->custnum,
274       $cust_main->getfield('last'),
275       $cust_main->getfield('first'),
276       $cust_main->company,
277     );
278
279     my(@lol_cust_svc);
280     my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
281     foreach ( @{$all_pkgs{$custnum}} ) {
282       #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
283       my @cust_svc = $_->cust_svc;
284       push @lol_cust_svc, \@cust_svc;
285       $rowspan += scalar(@cust_svc) || 1;
286     }
287
288     #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
289     my $view;
290     if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
291       $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
292     } else {
293       $view = $p. 'view/cust_main.cgi?'. $custnum;
294     }
295     print <<END;
296     <TR>
297       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
298       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
299       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$company</FONT></A></TD>
300 END
301     if ( defined dbdef->table('cust_main')->column('ship_last') ) {
302       my($ship_last,$ship_first,$ship_company)=(
303         $cust_main->ship_last || $cust_main->getfield('last'),
304         $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
305         $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
306       );
307 print <<END;
308       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
309       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A></TD>
310 END
311     }
312
313     my($n1)='';
314     foreach ( @{$all_pkgs{$custnum}} ) {
315       my $pkgnum = $_->pkgnum;
316 #      my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
317       my $part_pkg = $_->part_pkg;
318
319       my $pkg = $part_pkg->pkg;
320       my $comment = $part_pkg->comment;
321       my $pkgview = $p. 'view/cust_pkg.cgi?'. $pkgnum;
322       my @cust_svc = @{shift @lol_cust_svc};
323       #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
324       my $rowspan = scalar(@cust_svc) || 1;
325
326       print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
327       my($n2)='';
328       foreach my $cust_svc ( @cust_svc ) {
329          my($label, $value, $svcdb) = $cust_svc->label;
330          my($svcnum) = $cust_svc->svcnum;
331          my($sview) = $p.'view';
332          print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
333                qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
334          $n2="</TR><TR>";
335       }
336       #print qq!</TR><TR>\n!;
337       $n1="</TR><TR>";
338     }
339     print "</TR>";
340   }
341  
342   print "</TABLE>$pager</BODY></HTML>";
343
344 }
345
346 #undef $cache; #does this help?
347
348 #
349
350 sub last_sort {
351   $a->getfield('last') cmp $b->getfield('last');
352 }
353
354 sub company_sort {
355   return -1 if $a->company && ! $b->company;
356   return 1 if ! $a->company && $b->company;
357   $a->getfield('company') cmp $b->getfield('company');
358 }
359
360 sub custnum_sort {
361   $a->getfield('custnum') <=> $b->getfield('custnum');
362 }
363
364 sub cardsearch {
365
366   my($card)=$cgi->param('card');
367   $card =~ s/\D//g;
368   $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
369   my($payinfo)=$1;
370
371   push @cust_main, qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'});
372   $sortby=\*last_sort;
373 }
374
375 sub referralsearch {
376   $cgi->param('referral_custnum') =~ /^(\d+)$/
377     or eidiot "Illegal referral_custnum";
378   my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
379     or eidiot "Customer $1 not found";
380   my $depth;
381   if ( $cgi->param('referral_depth') ) {
382     $cgi->param('referral_depth') =~ /^(\d+)$/
383       or eidiot "Illegal referral_depth";
384     $depth = $1;
385   } else {
386     $depth = 1;
387   }
388   push @cust_main, $cust_main->referral_cust_main($depth);
389   $sortby=\*last_sort;
390 }
391
392 sub lastsearch {
393   my(%last_type);
394   foreach ( $cgi->param('last_type') ) {
395     $last_type{$_}++;
396   }
397
398   $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
399     or eidiot "Illegal last name";
400   my($last)=$1;
401
402   if ( $last_type{'Exact'}
403        && ! $last_type{'Fuzzy'} 
404      #  && ! $last_type{'Sound-alike'}
405   ) {
406
407     push @cust_main, qsearch('cust_main',{'last'=>$last});
408
409     push @cust_main, qsearch('cust_main',{'ship_last'=>$last})
410       if defined dbdef->table('cust_main')->column('ship_last');
411
412   } else {
413
414     &FS::cust_main::check_and_rebuild_fuzzyfiles;
415     my $all_last = &FS::cust_main::all_last;
416
417     my %last;
418     if ($last_type{'Fuzzy'}) { 
419       foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
420         $last{$_}++; 
421       }
422     }
423
424     #if ($last_type{'Sound-alike'}) {
425     #}
426
427     foreach ( keys %last ) {
428       push @cust_main, qsearch('cust_main',{'last'=>$_});
429       push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
430         if defined dbdef->table('cust_main')->column('ship_last');
431     }
432
433   }
434   $sortby=\*last_sort;
435 }
436
437 sub companysearch {
438
439   my(%company_type);
440   foreach ( $cgi->param('company_type') ) {
441     $company_type{$_}++ 
442   };
443
444   $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
445     or eidiot "Illegal company";
446   my($company)=$1;
447
448   if ( $company_type{'Exact'}
449        && ! $company_type{'Fuzzy'} 
450      #  && ! $company_type{'Sound-alike'}
451   ) {
452
453     push @cust_main, qsearch('cust_main',{'company'=>$company});
454
455     push @cust_main, qsearch('cust_main',{'ship_company'=>$company})
456       if defined dbdef->table('cust_main')->column('ship_last');
457
458   } else {
459
460     &FS::cust_main::check_and_rebuild_fuzzyfiles;
461     my $all_company = &FS::cust_main::all_company;
462
463     my %company;
464     if ($company_type{'Fuzzy'}) { 
465       foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
466         $company{$_}++;
467       }
468     }
469
470     #if ($company_type{'Sound-alike'}) {
471     #}
472
473     foreach ( keys %company ) {
474       push @cust_main, qsearch('cust_main',{'company'=>$_});
475       push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
476         if defined dbdef->table('cust_main')->column('ship_last');
477     }
478
479   }
480   $sortby=\*company_sort;
481
482 }
483 %>