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