Will things ever be the same again?
[freeside.git] / httemplate / search / cust_main.cgi
1 %
2 %
3 %my $conf = new FS::Conf;
4 %my $maxrecords = $conf->config('maxsearchrecordsperpage');
5 %
6 %#my $cache;
7 %
8 %#my $monsterjoin = <<END;
9 %#cust_main left outer join (
10 %#  ( cust_pkg left outer join part_pkg using(pkgpart)
11 %#  ) left outer join (
12 %#    (
13 %#      (
14 %#        ( cust_svc left outer join part_svc using (svcpart)
15 %#        ) left outer join svc_acct using (svcnum)
16 %#      ) left outer join svc_domain using(svcnum)
17 %#    ) left outer join svc_forward using(svcnum)
18 %#  ) using (pkgnum)
19 %#) using (custnum)
20 %#END
21 %
22 %#my $monsterjoin = <<END;
23 %#cust_main left outer join (
24 %#  ( cust_pkg left outer join part_pkg using(pkgpart)
25 %#  ) left outer join (
26 %#    (
27 %#      (
28 %#        ( cust_svc left outer join part_svc using (svcpart)
29 %#        ) left outer join (
30 %#          svc_acct left outer join (
31 %#            select svcnum, domain, catchall from svc_domain
32 %#            ) as svc_acct_domsvc (
33 %#              svc_acct_svcnum, svc_acct_domain, svc_acct_catchall
34 %#          ) on svc_acct.domsvc = svc_acct_domsvc.svc_acct_svcnum
35 %#        ) using (svcnum)
36 %#      ) left outer join svc_domain using(svcnum)
37 %#    ) left outer join svc_forward using(svcnum)
38 %#  ) using (pkgnum)
39 %#) using (custnum)
40 %#END
41 %
42 %my $limit = '';
43 %$limit .= "LIMIT $maxrecords" if $maxrecords;
44 %
45 %my $offset = $cgi->param('offset') || 0;
46 %$limit .= " OFFSET $offset" if $offset;
47 %
48 %my $total = 0;
49 %
50 %my(@cust_main, $sortby, $orderby);
51 %my @select = ();
52 %my @addl_headers = ();
53 %my @addl_cols = ();
54 %if ( $cgi->param('browse')
55 %     || $cgi->param('otaker_on')
56 %     || $cgi->param('agentnum_on')
57 %) {
58 %
59 %  my %search = ();
60 %
61 %  if ( $cgi->param('browse') ) {
62 %    my $query = $cgi->param('browse');
63 %    if ( $query eq 'custnum' ) {
64 %      $sortby=\*custnum_sort;
65 %      $orderby = "ORDER BY custnum";
66 %    } elsif ( $query eq 'last' ) {
67 %      $sortby=\*last_sort;
68 %      $orderby = "ORDER BY LOWER(last || ' ' || first)";
69 %    } elsif ( $query eq 'company' ) {
70 %      $sortby=\*company_sort;
71 %      $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
72 %    } elsif ( $query eq 'tickets' ) {
73 %      $sortby = \*tickets_sort;
74 %      $orderby = "ORDER BY tickets DESC";
75 %      push @select, FS::TicketSystem->sql_num_customer_tickets. " as tickets";
76 %      push @addl_headers, 'Tickets';
77 %      push @addl_cols, 'tickets';
78 %    } else {
79 %      die "unknown browse field $query";
80 %    }
81 %  } else {
82 %    $sortby = \*last_sort; #??
83 %    $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
84 %  }
85 %
86 %  if ( $cgi->param('otaker_on') ) {
87 %    $cgi->param('otaker') =~ /^(\w{1,32})$/ or eidiot "Illegal otaker\n";
88 %    $search{otaker} = $1;
89 %  } elsif ( $cgi->param('agentnum_on') ) {
90 %    $cgi->param('agentnum') =~ /^(\d+)$/ or eidiot "Illegal agentnum\n";
91 %    $search{agentnum} = $1;
92 %#  } else {
93 %#    die "unknown query...";
94 %  }
95 %
96 %  my @qual = ();
97 %
98 %  my $ncancelled = '';
99 %
100 %  if (  $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
101 %       || ( $conf->exists('hidecancelledcustomers')
102 %             && ! $cgi->param('showcancelledcustomers') )
103 %     ) {
104 %    #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
105 %    push @qual, FS::cust_main->uncancel_sql;
106 %
107 %   }
108 %
109 %  push @qual, FS::cust_main->cancel_sql   if $cgi->param('cancelled');
110 %  push @qual, FS::cust_main->prospect_sql if $cgi->param('prospect');
111 %  push @qual, FS::cust_main->active_sql   if $cgi->param('active');
112 %  push @qual, FS::cust_main->inactive_sql if $cgi->param('inactive');
113 %  push @qual, FS::cust_main->susp_sql     if $cgi->param('suspended');
114 %
115 %  #EWWWWWW
116 %  my $qual = join(' AND ',
117 %            map { "$_ = ". dbh->quote($search{$_}) } keys %search );
118 %
119 %  my $addl_qual = join(' AND ', @qual);
120 %
121 %  #here is the agent virtualization
122 %  $addl_qual .= ( $addl_qual ? ' AND ' : '' ).
123 %                $FS::CurrentUser::CurrentUser->agentnums_sql;
124 %
125 %  if ( $addl_qual ) {
126 %    $qual .= ' AND ' if $qual;
127 %    $qual .= $addl_qual;
128 %  }
129 %    
130 %  $qual = " WHERE $qual" if $qual;
131 %  my $statement = "SELECT COUNT(*) FROM cust_main $qual";
132 %  my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
133 %  $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
134 %
135 %  $total = $sth->fetchrow_arrayref->[0];
136 %
137 %  if ( $addl_qual ) {
138 %    if ( %search ) {
139 %      $addl_qual = " AND $addl_qual";
140 %    } else {
141 %      $addl_qual = " WHERE $addl_qual";
142 %    }
143 %  }
144 %
145 %  my $select;
146 %  if ( @select ) {
147 %    $select = 'cust_main.*, '. join (', ', @select);
148 %  } else {
149 %    $select = '*';
150 %  }
151 %
152 %  @cust_main = qsearch('cust_main', \%search, $select,   
153 %                         "$addl_qual $orderby $limit" );
154 %
155 %#  foreach my $cust_main ( @just_cust_main ) {
156 %#
157 %#    my @one_cust_main;
158 %#    $FS::Record::DEBUG=1;
159 %#    ( $cache, @one_cust_main ) = jsearch(
160 %#      "$monsterjoin",
161 %#      { 'custnum' => $cust_main->custnum },
162 %#      '',
163 %#      '',
164 %#      'cust_main',
165 %#      'custnum',
166 %#    );
167 %#    push @cust_main, @one_cust_main;
168 %#  }
169 %
170 %} else {
171 %  @cust_main=();
172 %  $sortby = \*last_sort;
173 %
174 %  push @cust_main, @{&custnumsearch}
175 %    if $cgi->param('custnum_on') && $cgi->param('custnum_text');
176 %  push @cust_main, @{&cardsearch}
177 %    if $cgi->param('card_on') && $cgi->param('card');
178 %  push @cust_main, @{&lastsearch}
179 %    if $cgi->param('last_on') && $cgi->param('last_text');
180 %  push @cust_main, @{&companysearch}
181 %    if $cgi->param('company_on') && $cgi->param('company_text');
182 %  push @cust_main, @{&address2search}
183 %    if $cgi->param('address2_on') && $cgi->param('address2_text');
184 %  push @cust_main, @{&phonesearch}
185 %    if $cgi->param('phone_on') && $cgi->param('phone_text');
186 %  push @cust_main, @{&referralsearch}
187 %    if $cgi->param('referral_custnum');
188 %
189 %  if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
190 %    $sortby = \*company_sort;
191 %    push @cust_main, @{&companysearch};
192 %  }
193 %
194 %  if ( $cgi->param('search_cust') ) {
195 %    $sortby = \*company_sort;
196 %    $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
197 %    warn "smart searching for: ". $cgi->param('search_cust');
198 %    push @cust_main, smart_search( 'search' => $cgi->param('search_cust') );
199 %  }
200 %
201 %  @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
202 %    if ! $cgi->param('cancelled')
203 %       && (
204 %         $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
205 %         || ( $conf->exists('hidecancelledcustomers')
206 %               && ! $cgi->param('showcancelledcustomers') )
207 %       );
208 %
209 %  my %saw = ();
210 %  @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
211 %}
212 %
213 %my %all_pkgs;
214 %if ( $conf->exists('hidecancelledpackages' ) ) {
215 %  %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
216 %} else {
217 %  %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
218 %}
219 %#%all_pkgs = ();
220 %
221 %if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
222 %  if ( $cgi->param('quickpay') eq 'yes' ) {
223 %    print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
224 %  } else {
225 %    print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
226 %  }
227 %  #exit;
228 %} elsif ( scalar(@cust_main) == 0 ) {
229 %
230
231 <!-- mason kludge -->
232 %
233 %  eidiot "No matching customers found!\n";
234 %} else { 
235 %
236
237 <% include('/elements/header.html', "Customer Search Results", '' ) %>
238 % $total ||= scalar(@cust_main); 
239
240
241   <% $total %> matching customers found
242 %
243 %  #begin pager
244 %  my $pager = '';
245 %  if ( $total != scalar(@cust_main) && $maxrecords ) {
246 %    unless ( $offset == 0 ) {
247 %      $cgi->param('offset', $offset - $maxrecords);
248 %      $pager .= '<A HREF="'. $cgi->self_url.
249 %                '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
250 %    }
251 %    my $poff;
252 %    my $page;
253 %    for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
254 %      $page++;
255 %      if ( $offset == $poff ) {
256 %        $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
257 %      } else {
258 %        $cgi->param('offset', $poff);
259 %        $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
260 %      }
261 %    }
262 %    unless ( $offset + $maxrecords > $total ) {
263 %      $cgi->param('offset', $offset + $maxrecords);
264 %      $pager .= '<A HREF="'. $cgi->self_url.
265 %                '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
266 %    }
267 %  }
268 %  #end pager
269 %
270 %  unless ( $cgi->param('cancelled') ) {
271 %    if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
272 %         || ( $conf->exists('hidecancelledcustomers')
273 %              && ! $cgi->param('showcancelledcustomers')
274 %            )
275 %       ) {
276 %      $cgi->param('showcancelledcustomers', 1);
277 %      $cgi->param('offset', 0);
278 %      print qq!( <a href="!. $cgi->self_url. qq!">show!;
279 %    } else {
280 %      $cgi->param('showcancelledcustomers', 0);
281 %      $cgi->param('offset', 0);
282 %      print qq!( <a href="!. $cgi->self_url. qq!">hide!;
283 %    }
284 %    print ' cancelled customers</a> )';
285 %  }
286 %
287 %  if ( $cgi->param('referral_custnum') ) {
288 %    $cgi->param('referral_custnum') =~ /^(\d+)$/
289 %      or eidiot "Illegal referral_custnum\n";
290 %    my $referral_custnum = $1;
291 %    my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
292 %    print '<FORM METHOD="GET">'.
293 %          qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
294 %          'referrals of <A HREF="'. popurl(2).
295 %          "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
296 %          ( $cust_main->company
297 %            || $cust_main->last. ', '. $cust_main->first ).
298 %          '</A>';
299 %    print "\n",<<END;
300 %      <SCRIPT>
301 %      function changed(what) {
302 %        what.form.submit();
303 %      }
304 %      </SCRIPT>
305 %END
306 %    print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
307 %    my $max = 8; #config file
308 %    $cgi->param('referral_depth') =~ /^(\d*)$/ 
309 %      or eidiot "Illegal referral_depth";
310 %    my $referral_depth = $1;
311 %
312 %    foreach my $depth ( 1 .. $max ) {
313 %      print '<OPTION',
314 %            ' SELECTED'x($depth == $referral_depth),
315 %            ">$depth";
316 %    }
317 %    print "</SELECT> levels deep".
318 %          '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
319 %          '</FORM>';
320 %  }
321 %
322 %  my @custom_priorities = ();
323 %  if ( $conf->config('ticket_system-custom_priority_field')
324 %       && @{[ $conf->config('ticket_system-custom_priority_field-values') ]} ) {
325 %    @custom_priorities =
326 %      $conf->config('ticket_system-custom_priority_field-values');
327 %  }
328 %
329 %  print "<BR><BR>". $pager. include('/elements/table-grid.html'). <<END;
330 %      <TR>
331 %        <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
332 %        <TH CLASS="grid" BGCOLOR="#cccccc">(bill) name</TH>
333 %        <TH CLASS="grid" BGCOLOR="#cccccc">company</TH>
334 %END
335 %
336 %if ( defined dbdef->table('cust_main')->column('ship_last') ) {
337 %  print <<END;
338 %      <TH CLASS="grid" BGCOLOR="#cccccc">(service) name</TH>
339 %      <TH CLASS="grid" BGCOLOR="#cccccc">company</TH>
340 %END
341 %}
342 %
343 %foreach my $addl_header ( @addl_headers ) {
344 %  print '<TH CLASS="grid" BGCOLOR="#cccccc">'. "$addl_header</TH>";
345 %}
346 %
347 %print <<END;
348 %        <TH CLASS="grid" BGCOLOR="#cccccc">Packages</TH>
349 %        <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2>Services</TH>
350 %      </TR>
351 %END
352 %
353 %  my $bgcolor1 = '#eeeeee';
354 %  my $bgcolor2 = '#ffffff';
355 %  my $bgcolor;
356 %
357 %  my(%saw,$cust_main);
358 %  foreach $cust_main (
359 %    sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
360 %  ) {
361 %
362 %    if ( $bgcolor eq $bgcolor1 ) {
363 %      $bgcolor = $bgcolor2;
364 %    } else {
365 %      $bgcolor = $bgcolor1;
366 %    }
367 %
368 %    my($custnum,$last,$first,$company)=(
369 %      $cust_main->custnum,
370 %      $cust_main->getfield('last'),
371 %      $cust_main->getfield('first'),
372 %      $cust_main->company,
373 %    );
374 %
375 %    my(@lol_cust_svc);
376 %    my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
377 %    foreach ( @{$all_pkgs{$custnum}} ) {
378 %      #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
379 %      my @cust_svc = $_->cust_svc;
380 %      push @lol_cust_svc, \@cust_svc;
381 %      $rowspan += scalar(@cust_svc) || 1;
382 %    }
383 %
384 %    #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
385 %    my $view;
386 %    if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
387 %      $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
388 %    } else {
389 %      $view = $p. 'view/cust_main.cgi?'. $custnum;
390 %    }
391 %    my $pcompany = $company
392 %      ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
393 %      : '<FONT SIZE=-1>&nbsp;</FONT>';
394 %    
395
396
397     <TR>
398       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% $custnum %></FONT></A></TD>
399       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" %></FONT></A></TD>
400       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><% $pcompany %></TD>
401 %
402 %    if ( defined dbdef->table('cust_main')->column('ship_last') ) {
403 %      my($ship_last,$ship_first,$ship_company)=(
404 %        $cust_main->ship_last || $cust_main->getfield('last'),
405 %        $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
406 %        $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
407 %      );
408 %      my $pship_company = $ship_company
409 %        ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
410 %        : '<FONT SIZE=-1>&nbsp;</FONT>';
411 %      
412
413
414       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% "$ship_last, $ship_first" %></FONT></A></TD>
415       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><% $pship_company %></A></TD>
416 % }
417 %
418 %    foreach my $addl_col ( @addl_cols ) { 
419 % if ( $addl_col eq 'tickets' ) { 
420 % if ( @custom_priorities ) { 
421
422
423              <TD CLASS="inv" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %> ALIGN=right><FONT SIZE=-1>
424
425                <TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>
426 % foreach my $priority ( @custom_priorities, '' ) { 
427 %
428 %                    my $num =
429 %                      FS::TicketSystem->num_customer_tickets($custnum,$priority);
430 %                    my $ahref = '';
431 %                    $ahref= '<A HREF="'.
432 %                            FS::TicketSystem->href_customer_tickets($custnum,$priority).
433 %                            '">'
434 %                      if $num;
435 %                 
436
437         
438                  <TR>
439                    <TD ALIGN=right>
440                      <FONT SIZE=-1><% $ahref.$num %></A></FONT>
441                    </TD>
442                    <TD ALIGN=left>
443                      <FONT SIZE=-1><% $ahref %><% $priority || '<i>(none)</i>' %></A></FONT>
444                    </TD>
445                  </TR>
446 % } 
447
448
449              <TR>
450                <TH ALIGN=right STYLE="border-top: dashed 1px black">
451                <FONT SIZE=-1>
452 % } else { 
453
454
455           <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %> ALIGN=right><FONT SIZE=-1>
456 % } 
457 %
458 %           my $ahref = '';
459 %           $ahref = '<A HREF="'.
460 %                       FS::TicketSystem->href_customer_tickets($custnum).
461 %                       '">'
462 %             if $cust_main->get($addl_col);
463 %        
464
465
466         <% $ahref %><% $cust_main->get($addl_col) %></A>
467 % if ( @custom_priorities ) { 
468
469
470           </FONT></TH>
471             <TH ALIGN=left STYLE="border-top: dashed 1px black">
472               <FONT SIZE=-1><% ${ahref} %>Total</A><FONT>
473             </TH>
474           </TR>
475           </TABLE>
476 % } 
477
478
479         </FONT></TD>
480 % } else { 
481
482
483         <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %> ALIGN=right><FONT SIZE=-1>
484           <% $cust_main->get($addl_col) %>
485         </FONT></TD>
486 %
487 %      }
488 %    }
489 %
490 %    my($n1)='';
491 %    foreach ( @{$all_pkgs{$custnum}} ) {
492 %      my $pkgnum = $_->pkgnum;
493 %#      my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
494 %      my $part_pkg = $_->part_pkg;
495 %
496 %      my $pkg = $part_pkg->pkg;
497 %      my $comment = $part_pkg->comment;
498 %      my $pkgview = "${p}view/cust_main.cgi?$custnum#cust_pkg$pkgnum";
499 %      my @cust_svc = @{shift @lol_cust_svc};
500 %      #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
501 %      my $rowspan = scalar(@cust_svc) || 1;
502 %
503 %      print $n1, qq!<TD CLASS="grid" BGCOLOR="$bgcolor"  ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
504 %
505 %      my($n2)='';
506 %      foreach my $cust_svc ( @cust_svc ) {
507 %         my($label, $value, $svcdb) = $cust_svc->label;
508 %         my($svcnum) = $cust_svc->svcnum;
509 %         my($sview) = $p.'view';
510 %         print $n2,qq!<TD CLASS="grid" BGCOLOR="$bgcolor" ><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
511 %               qq!<TD CLASS="grid" BGCOLOR="$bgcolor" ><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
512 %         $n2="</TR><TR>";
513 %      }
514 %
515 %      unless ( @cust_svc ) {
516 %        print qq!<TD CLASS="grid" BGCOLOR="$bgcolor" COLSPAN=2>&nbsp;</TD>!;
517 %      }
518 %
519 %      #print qq!</TR><TR>\n!;
520 %      $n1="</TR><TR>";
521 %    }
522 %
523 %    unless ( @{$all_pkgs{$custnum}} ) {
524 %      print qq!<TD CLASS="grid" BGCOLOR="$bgcolor" COLSPAN=3>&nbsp;</TD>!;
525 %    }
526 %    
527 %    print "</TR>";
528 %  }
529 %
530 %  
531
532  
533   </TABLE><% $pager %>
534
535   <% include('/elements/footer.html') %>
536 % }
537 %
538 %#undef $cache; #does this help?
539 %
540 %#
541 %
542 %sub last_sort {
543 %  lc($a->getfield('last')) cmp lc($b->getfield('last'))
544 %  || lc($a->first) cmp lc($b->first);
545 %}
546 %
547 %sub company_sort {
548 %  return -1 if $a->company && ! $b->company;
549 %  return 1 if ! $a->company && $b->company;
550 %  lc($a->company) cmp lc($b->company)
551 %  || lc($a->getfield('last')) cmp lc($b->getfield('last'))
552 %  || lc($a->first) cmp lc($b->first);;
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 eidiot "Illegal customer number\n";
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 eidiot "Illegal card number\n";
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 eidiot "Illegal referral_custnum";
588 %  my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
589 %    or eidiot "Customer $1 not found";
590 %  my $depth;
591 %  if ( $cgi->param('referral_depth') ) {
592 %    $cgi->param('referral_depth') =~ /^(\d+)$/
593 %      or eidiot "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 eidiot "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->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 eidiot "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->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 eidiot "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 %                            { 'address2' => { 'op'    => 'ILIKE',
706 %                                              'value' => $address2 } } )
707 %    if defined dbdef->table('cust_main')->column('ship_last');
708 %
709 %  \@cust_main;
710 %}
711 %
712 %sub phonesearch {
713 %  my @cust_main;
714 %
715 %  my $phone = $cgi->param('phone_text');
716 %
717 %  #(no longer really) false laziness with Record::ut_phonen
718 %  #only works with US/CA numbers...
719 %  $phone =~ s/\D//g;
720 %  if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
721 %    $phone = "$1-$2-$3";
722 %    $phone .= " x$4" if $4;
723 %  } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
724 %    $phone = "$1-$2";
725 %  } elsif ( $phone =~ /^(\d{3,4})$/ ) {
726 %    $phone = $1;
727 %  } else {
728 %    eidiot gettext('illegal_phone'). ": $phone";
729 %  }
730 %
731 %  my @fields = qw(daytime night fax);
732 %  push @fields, qw(ship_daytime ship_night ship_fax)
733 %    if defined dbdef->table('cust_main')->column('ship_last');
734 %
735 %  for my $field ( @fields ) {
736 %    push @cust_main, qsearch ( 'cust_main', 
737 %                               { $field => { 'op'    => 'LIKE',
738 %                                             'value' => "%$phone%" } } );
739 %  }
740 %
741 %  \@cust_main;
742 %}
743 %
744 %
745