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