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