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