update unearned revenue report based on feedback from kevin
[freeside.git] / httemplate / search / cust_main.cgi
1 <%
2
3 my $conf = new FS::Conf;
4 my $maxrecords = $conf->config('maxsearchrecordsperpage');
5
6 #my $cache;
7
8 #my $monsterjoin = <<END;
9 #cust_main left outer join (
10 #  ( cust_pkg left outer join part_pkg using(pkgpart)
11 #  ) left outer join (
12 #    (
13 #      (
14 #        ( cust_svc left outer join part_svc using (svcpart)
15 #        ) left outer join svc_acct using (svcnum)
16 #      ) left outer join svc_domain using(svcnum)
17 #    ) left outer join svc_forward using(svcnum)
18 #  ) using (pkgnum)
19 #) using (custnum)
20 #END
21
22 #my $monsterjoin = <<END;
23 #cust_main left outer join (
24 #  ( cust_pkg left outer join part_pkg using(pkgpart)
25 #  ) left outer join (
26 #    (
27 #      (
28 #        ( cust_svc left outer join part_svc using (svcpart)
29 #        ) left outer join (
30 #          svc_acct left outer join (
31 #            select svcnum, domain, catchall from svc_domain
32 #            ) as svc_acct_domsvc (
33 #              svc_acct_svcnum, svc_acct_domain, svc_acct_catchall
34 #          ) on svc_acct.domsvc = svc_acct_domsvc.svc_acct_svcnum
35 #        ) 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 $limit = '';
43 $limit .= "LIMIT $maxrecords" if $maxrecords;
44
45 my $offset = $cgi->param('offset') || 0;
46 $limit .= " OFFSET $offset" if $offset;
47
48 my $total = 0;
49
50 my(@cust_main, $sortby, $orderby);
51 if ( $cgi->param('browse')
52      || $cgi->param('otaker_on')
53      || $cgi->param('agentnum_on')
54 ) {
55
56   my %search = ();
57   if ( $cgi->param('browse') ) {
58     my $query = $cgi->param('browse');
59     if ( $query eq 'custnum' ) {
60       $sortby=\*custnum_sort;
61       $orderby = "ORDER BY custnum";
62     } elsif ( $query eq 'last' ) {
63       $sortby=\*last_sort;
64       $orderby = "ORDER BY LOWER(last || ' ' || first)";
65     } elsif ( $query eq 'company' ) {
66       $sortby=\*company_sort;
67       $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
68     } else {
69       die "unknown browse field $query";
70     }
71   } else {
72     $sortby = \*last_sort; #??
73     $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
74     if ( $cgi->param('otaker_on') ) {
75       $cgi->param('otaker') =~ /^(\w{1,32})$/ or eidiot "Illegal otaker\n";
76       $search{otaker} = $1;
77     } elsif ( $cgi->param('agentnum_on') ) {
78       $cgi->param('agentnum') =~ /^(\d+)$/ or eidiot "Illegal agentnum\n";
79       $search{agentnum} = $1;
80     } else {
81       die "unknown query...";
82     }
83   }
84
85   my $ncancelled = '';
86
87   if ( driver_name eq 'mysql' ) {
88
89        my $sql = "CREATE TEMPORARY TABLE temp1_$$ TYPE=MYISAM
90                     SELECT cust_pkg.custnum,COUNT(*) as count
91                       FROM cust_pkg,cust_main
92                         WHERE cust_pkg.custnum = cust_main.custnum
93                               AND ( cust_pkg.cancel IS NULL
94                                     OR cust_pkg.cancel = 0 )
95                         GROUP BY cust_pkg.custnum";
96        my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
97        $sth->execute or die "Error executing \"$sql\": ". $sth->errstr;
98        $sql = "CREATE TEMPORARY TABLE temp2_$$ TYPE=MYISAM
99                  SELECT cust_pkg.custnum,COUNT(*) as count
100                    FROM cust_pkg,cust_main
101                      WHERE cust_pkg.custnum = cust_main.custnum
102                      GROUP BY cust_pkg.custnum";
103        $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
104        $sth->execute or die "Error executing \"$sql\": ". $sth->errstr;
105   }
106
107   if (  $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
108        || ( $conf->exists('hidecancelledcustomers')
109              && ! $cgi->param('showcancelledcustomers') )
110      ) {
111     #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
112     if ( driver_name eq 'mysql' ) {
113        $ncancelled = "
114           temp1_$$.custnum = cust_main.custnum
115                AND temp2_$$.custnum = cust_main.custnum
116                AND (temp1_$$.count > 0
117                        OR temp2_$$.count = 0 )
118        ";
119
120     } else {
121        $ncancelled = "
122           0 < ( SELECT COUNT(*) FROM cust_pkg
123                        WHERE cust_pkg.custnum = cust_main.custnum
124                          AND ( cust_pkg.cancel IS NULL
125                                OR cust_pkg.cancel = 0
126                              )
127                    )
128             OR 0 = ( SELECT COUNT(*) FROM cust_pkg
129                        WHERE cust_pkg.custnum = cust_main.custnum
130                    )
131        ";
132      }
133    }
134
135   my $cancelled = '';
136   if ( $cgi->param('cancelled') ) {
137     $cancelled = "
138       0 = ( SELECT COUNT(*) FROM cust_pkg
139                    WHERE cust_pkg.custnum = cust_main.custnum
140                       AND ( cust_pkg.cancel IS NULL
141                             OR cust_pkg.cancel = 0
142                           )
143           )
144         AND 0 < ( SELECT COUNT(*) FROM cust_pkg
145                     WHERE cust_pkg.custnum = cust_main.custnum
146                 )
147     ";
148   }
149
150   #EWWWWWW
151   my $qual = join(' AND ',
152             map { "$_ = ". dbh->quote($search{$_}) } keys %search );
153
154   if ( $cancelled ) {
155     $qual .= ' AND ' if $qual;
156     $qual .= $cancelled;
157   } elsif ( $ncancelled ) {
158     $qual .= ' AND ' if $qual;
159     $qual .= $ncancelled;
160   }
161     
162   $qual = " WHERE $qual" if $qual;
163   my $statement;
164   if ( driver_name eq 'mysql' ) {
165     $statement = "SELECT COUNT(*) FROM cust_main";
166     $statement .= ", temp1_$$, temp2_$$ $qual" if $qual;
167   } else {
168     $statement = "SELECT COUNT(*) FROM cust_main $qual";
169   }
170   my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
171   $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
172
173   $total = $sth->fetchrow_arrayref->[0];
174
175   my $rqual = $cancelled || $ncancelled;
176   if ( $rqual ) {
177     if ( %search ) {
178       $rqual = " AND $rqual";
179     } else {
180       $rqual = " WHERE $rqual";
181     }
182   }
183
184   my @just_cust_main;
185   if ( driver_name eq 'mysql' ) {
186     @just_cust_main = qsearch('cust_main', \%search, 'cust_main.*',
187                               ",temp1_$$,temp2_$$ $rqual $orderby $limit");
188   } else {
189     @just_cust_main = qsearch('cust_main', \%search, '',   
190                               "$rqual $orderby $limit" );
191   }
192   if ( driver_name eq 'mysql' ) {
193     my $sql = "DROP TABLE temp1_$$,temp2_$$;";
194     my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
195     $sth->execute or die "Error executing \"$sql\": ". $sth->errstr;
196   }
197   @cust_main = @just_cust_main;
198
199 #  foreach my $cust_main ( @just_cust_main ) {
200 #
201 #    my @one_cust_main;
202 #    $FS::Record::DEBUG=1;
203 #    ( $cache, @one_cust_main ) = jsearch(
204 #      "$monsterjoin",
205 #      { 'custnum' => $cust_main->custnum },
206 #      '',
207 #      '',
208 #      'cust_main',
209 #      'custnum',
210 #    );
211 #    push @cust_main, @one_cust_main;
212 #  }
213
214 } else {
215   @cust_main=();
216   $sortby = \*last_sort;
217
218   push @cust_main, @{&custnumsearch}
219     if $cgi->param('custnum_on') && $cgi->param('custnum_text');
220   push @cust_main, @{&cardsearch}
221     if $cgi->param('card_on') && $cgi->param('card');
222   push @cust_main, @{&lastsearch}
223     if $cgi->param('last_on') && $cgi->param('last_text');
224   push @cust_main, @{&companysearch}
225     if $cgi->param('company_on') && $cgi->param('company_text');
226   push @cust_main, @{&address2search}
227     if $cgi->param('address2_on') && $cgi->param('address2_text');
228   push @cust_main, @{&phonesearch}
229     if $cgi->param('phone_on') && $cgi->param('phone_text');
230   push @cust_main, @{&referralsearch}
231     if $cgi->param('referral_custnum');
232
233   if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
234     $sortby = \*company_sort;
235     push @cust_main, @{&companysearch};
236   }
237
238   @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
239     if ! $cgi->param('cancelled')
240        && (
241          $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
242          || ( $conf->exists('hidecancelledcustomers')
243                && ! $cgi->param('showcancelledcustomers') )
244        );
245
246   my %saw = ();
247   @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
248 }
249
250 my %all_pkgs;
251 if ( $conf->exists('hidecancelledpackages' ) ) {
252   %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
253 } else {
254   %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
255 }
256 #%all_pkgs = ();
257
258 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
259   if ( $cgi->param('quickpay') eq 'yes' ) {
260     print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
261   } else {
262     print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
263   }
264   #exit;
265 } elsif ( scalar(@cust_main) == 0 ) {
266 %>
267 <!-- mason kludge -->
268 <%
269   eidiot "No matching customers found!\n";
270 } else { 
271 %>
272 <!-- mason kludge -->
273 <%
274
275   $total ||= scalar(@cust_main);
276   print header("Customer Search Results",menubar(
277     'Main Menu', popurl(2)
278   )), "$total matching customers found ";
279
280   #begin pager
281   my $pager = '';
282   if ( $total != scalar(@cust_main) && $maxrecords ) {
283     unless ( $offset == 0 ) {
284       $cgi->param('offset', $offset - $maxrecords);
285       $pager .= '<A HREF="'. $cgi->self_url.
286                 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
287     }
288     my $poff;
289     my $page;
290     for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
291       $page++;
292       if ( $offset == $poff ) {
293         $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
294       } else {
295         $cgi->param('offset', $poff);
296         $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
297       }
298     }
299     unless ( $offset + $maxrecords > $total ) {
300       $cgi->param('offset', $offset + $maxrecords);
301       $pager .= '<A HREF="'. $cgi->self_url.
302                 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
303     }
304   }
305   #end pager
306
307   unless ( $cgi->param('cancelled') ) {
308     if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
309          || ( $conf->exists('hidecancelledcustomers')
310               && ! $cgi->param('showcancelledcustomers')
311             )
312        ) {
313       $cgi->param('showcancelledcustomers', 1);
314       $cgi->param('offset', 0);
315       print qq!( <a href="!. $cgi->self_url. qq!">show!;
316     } else {
317       $cgi->param('showcancelledcustomers', 0);
318       $cgi->param('offset', 0);
319       print qq!( <a href="!. $cgi->self_url. qq!">hide!;
320     }
321     print ' cancelled customers</a> )';
322   }
323   if ( $cgi->param('referral_custnum') ) {
324     $cgi->param('referral_custnum') =~ /^(\d+)$/
325       or eidiot "Illegal referral_custnum\n";
326     my $referral_custnum = $1;
327     my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
328     print '<FORM METHOD=POST>'.
329           qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
330           'referrals of <A HREF="'. popurl(2).
331           "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
332           ( $cust_main->company
333             || $cust_main->last. ', '. $cust_main->first ).
334           '</A>';
335     print "\n",<<END;
336       <SCRIPT>
337       function changed(what) {
338         what.form.submit();
339       }
340       </SCRIPT>
341 END
342     print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
343     my $max = 8; #config file
344     $cgi->param('referral_depth') =~ /^(\d*)$/ 
345       or eidiot "Illegal referral_depth";
346     my $referral_depth = $1;
347
348     foreach my $depth ( 1 .. $max ) {
349       print '<OPTION',
350             ' SELECTED'x($depth == $referral_depth),
351             ">$depth";
352     }
353     print "</SELECT> levels deep".
354           '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
355           '</FORM>';
356   }
357
358   print "<BR><BR>". $pager. &table(). <<END;
359       <TR>
360         <TH></TH>
361         <TH>(bill) name</TH>
362         <TH>company</TH>
363 END
364
365 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
366   print <<END;
367       <TH>(service) name</TH>
368       <TH>company</TH>
369 END
370 }
371
372 print <<END;
373         <TH>Packages</TH>
374         <TH COLSPAN=2>Services</TH>
375       </TR>
376 END
377
378   my(%saw,$cust_main);
379   my $p = popurl(2);
380   foreach $cust_main (
381     sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
382   ) {
383     my($custnum,$last,$first,$company)=(
384       $cust_main->custnum,
385       $cust_main->getfield('last'),
386       $cust_main->getfield('first'),
387       $cust_main->company,
388     );
389
390     my(@lol_cust_svc);
391     my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
392     foreach ( @{$all_pkgs{$custnum}} ) {
393       #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
394       my @cust_svc = $_->cust_svc;
395       push @lol_cust_svc, \@cust_svc;
396       $rowspan += scalar(@cust_svc) || 1;
397     }
398
399     #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
400     my $view;
401     if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
402       $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
403     } else {
404       $view = $p. 'view/cust_main.cgi?'. $custnum;
405     }
406     my $pcompany = $company
407       ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
408       : '<FONT SIZE=-1>&nbsp;</FONT>';
409     print <<END;
410     <TR>
411       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
412       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
413       <TD ROWSPAN=$rowspan>$pcompany</TD>
414 END
415     if ( defined dbdef->table('cust_main')->column('ship_last') ) {
416       my($ship_last,$ship_first,$ship_company)=(
417         $cust_main->ship_last || $cust_main->getfield('last'),
418         $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
419         $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
420       );
421       my $pship_company = $ship_company
422         ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
423         : '<FONT SIZE=-1>&nbsp;</FONT>';
424       print <<END;
425       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
426       <TD ROWSPAN=$rowspan>$pship_company</A></TD>
427 END
428     }
429
430     my($n1)='';
431     foreach ( @{$all_pkgs{$custnum}} ) {
432       my $pkgnum = $_->pkgnum;
433 #      my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
434       my $part_pkg = $_->part_pkg;
435
436       my $pkg = $part_pkg->pkg;
437       my $comment = $part_pkg->comment;
438       my $pkgview = "${p}view/cust_main.cgi?$custnum#cust_pkg$pkgnum";
439       my @cust_svc = @{shift @lol_cust_svc};
440       #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
441       my $rowspan = scalar(@cust_svc) || 1;
442
443       print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
444       my($n2)='';
445       foreach my $cust_svc ( @cust_svc ) {
446          my($label, $value, $svcdb) = $cust_svc->label;
447          my($svcnum) = $cust_svc->svcnum;
448          my($sview) = $p.'view';
449          print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
450                qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
451          $n2="</TR><TR>";
452       }
453       #print qq!</TR><TR>\n!;
454       $n1="</TR><TR>";
455     }
456     print "</TR>";
457   }
458  
459   print "</TABLE>$pager</BODY></HTML>";
460
461 }
462
463 #undef $cache; #does this help?
464
465 #
466
467 sub last_sort {
468   lc($a->getfield('last')) cmp lc($b->getfield('last'))
469   || lc($a->first) cmp lc($b->first);
470 }
471
472 sub company_sort {
473   return -1 if $a->company && ! $b->company;
474   return 1 if ! $a->company && $b->company;
475   lc($a->company) cmp lc($b->company)
476   || lc($a->getfield('last')) cmp lc($b->getfield('last'))
477   || lc($a->first) cmp lc($b->first);;
478 }
479
480 sub custnum_sort {
481   $a->getfield('custnum') <=> $b->getfield('custnum');
482 }
483
484 sub custnumsearch {
485
486   my $custnum = $cgi->param('custnum_text');
487   $custnum =~ s/\D//g;
488   $custnum =~ /^(\d{1,23})$/ or eidiot "Illegal customer number\n";
489   $custnum = $1;
490   
491   [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
492 }
493
494 sub cardsearch {
495
496   my($card)=$cgi->param('card');
497   $card =~ s/\D//g;
498   $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
499   my($payinfo)=$1;
500
501   [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
502     qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'DCRD'})
503   ];
504 }
505
506 sub referralsearch {
507   $cgi->param('referral_custnum') =~ /^(\d+)$/
508     or eidiot "Illegal referral_custnum";
509   my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
510     or eidiot "Customer $1 not found";
511   my $depth;
512   if ( $cgi->param('referral_depth') ) {
513     $cgi->param('referral_depth') =~ /^(\d+)$/
514       or eidiot "Illegal referral_depth";
515     $depth = $1;
516   } else {
517     $depth = 1;
518   }
519   [ $cust_main->referral_cust_main($depth) ];
520 }
521
522 sub lastsearch {
523   my(%last_type);
524   my @cust_main;
525   foreach ( $cgi->param('last_type') ) {
526     $last_type{$_}++;
527   }
528
529   $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
530     or eidiot "Illegal last name";
531   my($last)=$1;
532
533   if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
534     push @cust_main, qsearch( 'cust_main',
535                               { 'last' => { 'op'    => 'ILIKE',
536                                             'value' => $last    } } );
537
538     push @cust_main, qsearch( 'cust_main',
539                               { 'ship_last' => { 'op'    => 'ILIKE',
540                                                  'value' => $last    } } )
541       if defined dbdef->table('cust_main')->column('ship_last');
542   }
543
544   if ( $last_type{'Substring'} || $last_type{'All'} ) {
545
546     push @cust_main, qsearch( 'cust_main',
547                               { 'last' => { 'op'    => 'ILIKE',
548                                             'value' => "%$last%" } } );
549
550     push @cust_main, qsearch( 'cust_main',
551                               { 'ship_last' => { 'op'    => 'ILIKE',
552                                                  'value' => "%$last%" } } )
553       if defined dbdef->table('cust_main')->column('ship_last');
554
555   }
556
557   if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
558
559     &FS::cust_main::check_and_rebuild_fuzzyfiles;
560     my $all_last = &FS::cust_main::all_last;
561
562     my %last;
563     if ( $last_type{'Fuzzy'} || $last_type{'All'} ) { 
564       foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
565         $last{$_}++; 
566       }
567     }
568
569     #if ($last_type{'Sound-alike'}) {
570     #}
571
572     foreach ( keys %last ) {
573       push @cust_main, qsearch('cust_main',{'last'=>$_});
574       push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
575         if defined dbdef->table('cust_main')->column('ship_last');
576     }
577
578   }
579
580   \@cust_main;
581 }
582
583 sub companysearch {
584
585   my(%company_type);
586   my @cust_main;
587   foreach ( $cgi->param('company_type') ) {
588     $company_type{$_}++ 
589   };
590
591   $cgi->param('company_text') =~
592     /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
593       or eidiot "Illegal company";
594   my $company = $1;
595
596   if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
597     push @cust_main, qsearch( 'cust_main',
598                               { 'company' => { 'op'    => 'ILIKE',
599                                                'value' => $company } } );
600
601     push @cust_main, qsearch( 'cust_main',
602                               { 'ship_company' => { 'op'    => 'ILIKE',
603                                                     'value' => $company } } )
604       if defined dbdef->table('cust_main')->column('ship_last');
605   }
606
607   if ( $company_type{'Substring'} || $company_type{'All'} ) {
608
609     push @cust_main, qsearch( 'cust_main',
610                               { 'company' => { 'op'    => 'ILIKE',
611                                                'value' => "%$company%" } } );
612
613     push @cust_main, qsearch( 'cust_main',
614                               { 'ship_company' => { 'op'    => 'ILIKE',
615                                                     'value' => "%$company%" } })
616       if defined dbdef->table('cust_main')->column('ship_last');
617
618   }
619
620   if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
621
622     &FS::cust_main::check_and_rebuild_fuzzyfiles;
623     my $all_company = &FS::cust_main::all_company;
624
625     my %company;
626     if ( $company_type{'Fuzzy'} || $company_type{'All'} ) { 
627       foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
628         $company{$_}++;
629       }
630     }
631
632     #if ($company_type{'Sound-alike'}) {
633     #}
634
635     foreach ( keys %company ) {
636       push @cust_main, qsearch('cust_main',{'company'=>$_});
637       push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
638         if defined dbdef->table('cust_main')->column('ship_last');
639     }
640
641   }
642
643   \@cust_main;
644 }
645
646 sub address2search {
647   my @cust_main;
648
649   $cgi->param('address2_text') =~
650     /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
651       or eidiot "Illegal address2";
652   my $address2 = $1;
653
654   push @cust_main, qsearch( 'cust_main',
655                             { 'address2' => { 'op'    => 'ILIKE',
656                                               'value' => $address2 } } );
657   push @cust_main, qsearch( 'cust_main',
658                             { 'address2' => { 'op'    => 'ILIKE',
659                                               'value' => $address2 } } )
660     if defined dbdef->table('cust_main')->column('ship_last');
661
662   \@cust_main;
663 }
664
665 sub phonesearch {
666   my @cust_main;
667
668   my $phone = $cgi->param('phone_text');
669
670   #(no longer really) false laziness with Record::ut_phonen
671   #only works with US/CA numbers...
672   $phone =~ s/\D//g;
673   if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
674     $phone = "$1-$2-$3";
675     $phone .= " x$4" if $4;
676   } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
677     $phone = "$1-$2";
678   } elsif ( $phone =~ /^(\d{3,4})$/ ) {
679     $phone = $1;
680   } else {
681     eidiot gettext('illegal_phone'). ": $phone";
682   }
683
684   my @fields = qw(daytime night fax);
685   push @fields, qw(ship_daytime ship_night ship_fax)
686     if defined dbdef->table('cust_main')->column('ship_last');
687
688   for my $field ( @fields ) {
689     push @cust_main, qsearch ( 'cust_main', 
690                                { $field => { 'op'    => 'LIKE',
691                                              'value' => "%$phone%" } } );
692   }
693
694   \@cust_main;
695 }
696
697 %>