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