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