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