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