add USPS-unvalidated customer addresses report, RT12402
[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 %    } elsif ( $query eq 'uspsunvalid' ) {
87 %       $search{'country'} = 'US';
88 %       $sortby=\*custnum_sort;
89 %       $orderby = "ORDER BY custnum";
90 %    } else {
91 %      die "unknown browse field $query";
92 %    }
93 %  } else {
94 %    $sortby = \*last_sort; #??
95 %    $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
96 %  }
97 %
98 %  if ( $cgi->param('otaker_on') ) {
99 %    die "access denied"
100 %      unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
101 %    $cgi->param('otaker') =~ /^(\w{1,32})$/ or errorpage("Illegal otaker");
102 %    $search{otaker} = $1;
103 %  } elsif ( $cgi->param('agentnum_on') ) {
104 %    $cgi->param('agentnum') =~ /^(\d+)$/ or errorpage("Illegal agentnum");
105 %    $search{agentnum} = $1;
106 %#  } else {
107 %#    die "unknown query...";
108 %  }
109 %
110 %  my @qual = ();
111 %
112 %  my $ncancelled = '';
113 %
114 %  if (  $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
115 %       || ( $conf->exists('hidecancelledcustomers')
116 %             && ! $cgi->param('showcancelledcustomers') )
117 %     ) {
118 %    #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
119 %    push @qual, FS::cust_main->uncancel_sql;
120 %
121 %   }
122 %
123 %  push @qual, FS::cust_main->cancel_sql   if $cgi->param('cancelled');
124 %  push @qual, FS::cust_main->prospect_sql if $cgi->param('prospect');
125 %  push @qual, FS::cust_main->active_sql   if $cgi->param('active');
126 %  push @qual, FS::cust_main->inactive_sql if $cgi->param('inactive');
127 %  push @qual, FS::cust_main->susp_sql     if $cgi->param('suspended');
128 %
129 %  #EWWWWWW
130 %  my $qual = join(' AND ',
131 %            map { "$_ = ". dbh->quote($search{$_}) } keys %search );
132 %
133 %  my $addl_qual = join(' AND ', @qual);
134 %
135 %  #here is the agent virtualization
136 %  $addl_qual .= ( $addl_qual ? ' AND ' : '' ).
137 %                $FS::CurrentUser::CurrentUser->agentnums_sql;
138 %
139 %  if ( $cgi->param('browse') && $cgi->param('browse') eq 'uspsunvalid' ) {
140 %       $addl_qual .= ' AND ( length(zip) < 9 OR upper(address1) != address1 OR upper(city) != city ) ';
141 %  }
142 %
143 %  if ( $addl_qual ) {
144 %    $qual .= ' AND ' if $qual;
145 %    $qual .= $addl_qual;
146 %  }
147 %    
148 %  $qual = " WHERE $qual" if $qual;
149 %  my $statement = "SELECT COUNT(*) FROM cust_main $qual";
150 %  my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
151 %  $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
152 %
153 %  $total = $sth->fetchrow_arrayref->[0];
154 %
155 %  if ( $addl_qual ) {
156 %    if ( %search ) {
157 %      $addl_qual = " AND $addl_qual";
158 %    } else {
159 %      $addl_qual = " WHERE $addl_qual";
160 %    }
161 %  }
162 %
163 %  my $select;
164 %  if ( @select ) {
165 %    $select = 'cust_main.*, '. join (', ', @select);
166 %  } else {
167 %    $select = '*';
168 %  }
169 %
170 %  @cust_main = qsearch('cust_main', \%search, $select,   
171 %                         "$addl_qual $orderby $limit" );
172 %
173 %#  foreach my $cust_main ( @just_cust_main ) {
174 %#
175 %#    my @one_cust_main;
176 %#    $FS::Record::DEBUG=1;
177 %#    ( $cache, @one_cust_main ) = jsearch(
178 %#      "$monsterjoin",
179 %#      { 'custnum' => $cust_main->custnum },
180 %#      '',
181 %#      '',
182 %#      'cust_main',
183 %#      'custnum',
184 %#    );
185 %#    push @cust_main, @one_cust_main;
186 %#  }
187 %
188 %} else {
189 %  @cust_main=();
190 %  $sortby = \*last_sort;
191 %
192 %  push @cust_main, @{&custnumsearch}
193 %    if $cgi->param('custnum_on') && $cgi->param('custnum_text');
194 %  push @cust_main, @{&cardsearch}
195 %    if $cgi->param('card_on') && $cgi->param('card');
196 %  push @cust_main, @{&lastsearch}
197 %    if $cgi->param('last_on') && $cgi->param('last_text');
198 %  push @cust_main, @{&companysearch}
199 %    if $cgi->param('company_on') && $cgi->param('company_text');
200 %  push @cust_main, @{&address2search}
201 %    if $cgi->param('address2_on') && $cgi->param('address2_text');
202 %  push @cust_main, @{&phonesearch}
203 %    if $cgi->param('phone_on') && $cgi->param('phone_text');
204 %  push @cust_main, @{&referralsearch}
205 %    if $cgi->param('referral_custnum');
206 %
207 %  if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
208 %    $sortby = \*company_sort;
209 %    push @cust_main, @{&companysearch};
210 %  }
211 %
212 %  if ( $cgi->param('search_cust') ) {
213 %    $sortby = \*company_sort;
214 %    $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
215 %    push @cust_main, smart_search( 'search' => $cgi->param('search_cust'),
216 %                                   'no_fuzzy_on_exact' => 1, #pref?
217 %                                 );
218 %  }
219 %
220 %  @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
221 %    if ! $cgi->param('cancelled')
222 %       && (
223 %         $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
224 %         || ( $conf->exists('hidecancelledcustomers')
225 %               && ! $cgi->param('showcancelledcustomers') )
226 %       );
227 %
228 %  my %saw = ();
229 %  @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
230 %}
231 %
232 %my %all_pkgs;
233 %if ( $conf->exists('hidecancelledpackages' ) ) {
234 %  %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
235 %} else {
236 %  %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
237 %}
238 %#%all_pkgs = ();
239 %
240 %if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
241 %  if ( $cgi->param('quickpay') eq 'yes' ) {
242 %    print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
243 %  } else {
244 %    print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
245 %  }
246 %  #exit;
247 %} elsif ( scalar(@cust_main) == 0 ) {
248 %
249
250 <!-- mason kludge -->
251 %
252 %  errorpage("No matching customers found!");
253 %} else { 
254 %
255
256 <% include('/elements/header.html', "Customer Search Results", '' ) %>
257 % $total ||= scalar(@cust_main); 
258
259
260   <% $total %> matching customers found
261
262 % my $pager = include( '/elements/pager.html',
263 %                        'offset'     => $offset,
264 %                        'num_rows'   => scalar(@cust_main),
265 %                        'total'      => $total,
266 %                        'maxrecords' => $maxrecords,
267 %                    );
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 ' canceled customers</a> )';
284 %  }
285 %
286 %  if ( $cgi->param('referral_custnum') ) {
287 %    $cgi->param('referral_custnum') =~ /^(\d+)$/
288 %      or errorpage("Illegal referral_custnum");
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 errorpage("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">Status</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 %    my $status = $cust_main->status;
396 %    my $statuscol = $cust_main->statuscolor;
397
398     <TR>
399       <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% $cust_main->display_custnum %></FONT></A></TD>
400       <TD CLASS="grid" ALIGN="center" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><FONT SIZE="-1" COLOR="#<% $statuscol %>"><B><% ucfirst($status) %></B></FONT></TD>
401       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" %></FONT></A></TD>
402       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><% $pcompany %></TD>
403 %
404 %    if ( defined dbdef->table('cust_main')->column('ship_last') ) {
405 %      my($ship_last,$ship_first,$ship_company)=(
406 %        $cust_main->ship_last || $cust_main->getfield('last'),
407 %        $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
408 %        $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
409 %      );
410 %      my $pship_company = $ship_company
411 %        ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
412 %        : '<FONT SIZE=-1>&nbsp;</FONT>';
413 %      
414
415
416       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% "$ship_last, $ship_first" %></FONT></A></TD>
417       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><% $pship_company %></A></TD>
418 % }
419 %
420 %    foreach my $addl_col ( @addl_cols ) { 
421 % if ( $addl_col eq 'tickets' ) { 
422 % if ( @custom_priorities ) { 
423
424
425              <TD CLASS="inv" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %> ALIGN=right><FONT SIZE=-1>
426
427                <TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>
428 % foreach my $priority ( @custom_priorities, '' ) { 
429 %
430 %                    my $num =
431 %                      FS::TicketSystem->num_customer_tickets($custnum,$priority);
432 %                    my $ahref = '';
433 %                    $ahref= '<A HREF="'.
434 %                            FS::TicketSystem->href_customer_tickets($custnum,$priority).
435 %                            '">'
436 %                      if $num;
437 %                 
438
439         
440                  <TR>
441                    <TD ALIGN=right>
442                      <FONT SIZE=-1><% $ahref.$num %></A></FONT>
443                    </TD>
444                    <TD ALIGN=left>
445                      <FONT SIZE=-1><% $ahref %><% $priority || '<i>(none)</i>' %></A></FONT>
446                    </TD>
447                  </TR>
448 % } 
449
450
451              <TR>
452                <TH ALIGN=right STYLE="border-top: dashed 1px black">
453                <FONT SIZE=-1>
454 % } else { 
455
456
457           <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %> ALIGN=right><FONT SIZE=-1>
458 % } 
459 %
460 %           my $ahref = '';
461 %           $ahref = '<A HREF="'.
462 %                       FS::TicketSystem->href_customer_tickets($custnum).
463 %                       '">'
464 %             if $cust_main->get($addl_col);
465 %        
466
467
468         <% $ahref %><% $cust_main->get($addl_col) %></A>
469 % if ( @custom_priorities ) { 
470
471
472           </FONT></TH>
473             <TH ALIGN=left STYLE="border-top: dashed 1px black">
474               <FONT SIZE=-1><% ${ahref} %>Total</A><FONT>
475             </TH>
476           </TR>
477           </TABLE>
478 % } 
479
480
481         </FONT></TD>
482 % } else { 
483
484
485         <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %> ALIGN=right><FONT SIZE=-1>
486           <% $cust_main->get($addl_col) %>
487         </FONT></TD>
488 %
489 %      }
490 %    }
491 %
492 %    my($n1)='';
493 %    foreach ( @{$all_pkgs{$custnum}} ) {
494 %      my $pkgnum = $_->pkgnum;
495 %#      my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
496 %      my $part_pkg = $_->part_pkg;
497 %
498 %      my $pkg_comment = $part_pkg->pkg_comment(nopkgpart => 1);
499 %      my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
500 %                   ? ''
501 %                   : ';show=packages';
502 %      my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
503 %      my $pkgview = "${p}view/cust_main.cgi?custnum=$custnum$show;fragment=$frag#$frag";
504 %      my @cust_svc = @{shift @lol_cust_svc};
505 %      #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
506 %      my $rowspan = scalar(@cust_svc) || 1;
507 %
508 %      print $n1, qq!<TD CLASS="grid" BGCOLOR="$bgcolor"  ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg_comment</FONT></A></TD>!;
509 %
510 %      my($n2)='';
511 %      foreach my $cust_svc ( @cust_svc ) {
512 %         my($label, $value, $svcdb) = $cust_svc->label;
513 %         my($svcnum) = $cust_svc->svcnum;
514 %         my($sview) = $p.'view';
515 %         print $n2,
516 %           qq!<TD CLASS="grid" BGCOLOR="$bgcolor" >!. FS::UI::Web::svc_link($m, $cust_svc->part_svc, $cust_svc) . qq!</TD> !.
517 %           qq!<TD CLASS="grid" BGCOLOR="$bgcolor" >!. FS::UI::Web::svc_label_link($m, $cust_svc->part_svc, $cust_svc) . qq!</TD> !;
518 %         $n2="</TR><TR>";
519 %      }
520 %
521 %      unless ( @cust_svc ) {
522 %        print qq!<TD CLASS="grid" BGCOLOR="$bgcolor" COLSPAN=2>&nbsp;</TD>!;
523 %      }
524 %
525 %      #print qq!</TR><TR>\n!;
526 %      $n1="</TR><TR>";
527 %    }
528 %
529 %    unless ( @{$all_pkgs{$custnum}} ) {
530 %      print qq!<TD CLASS="grid" BGCOLOR="$bgcolor" COLSPAN=3>&nbsp;</TD>!;
531 %    }
532 %    
533 %    print "</TR>";
534 %  }
535 %
536 %  
537
538  
539   </TABLE><% $pager %>
540
541   <% include('/elements/footer.html') %>
542 % }
543 %
544 %#undef $cache; #does this help?
545 %
546 %#
547 %
548 %sub last_sort {
549 %  lc($a->getfield('last')) cmp lc($b->getfield('last'))
550 %  || lc($a->first) cmp lc($b->first);
551 %}
552 %
553 %sub company_sort {
554 %  return -1 if $a->company && ! $b->company;
555 %  return 1 if ! $a->company && $b->company;
556 %  lc($a->company) cmp lc($b->company)
557 %  || lc($a->getfield('last')) cmp lc($b->getfield('last'))
558 %  || lc($a->first) cmp lc($b->first);;
559 %}
560 %
561 %sub display_custnum_sort {
562 %  $a->display_custnum <=> $b->display_custnum;
563 %}
564 %
565 %sub custnum_sort {
566 %  $a->getfield('custnum') <=> $b->getfield('custnum');
567 %}
568 %
569 %sub tickets_sort {
570 %  $b->getfield('tickets') <=> $a->getfield('tickets');
571 %}
572 %
573 %sub custnumsearch {
574 %
575 %  my $custnum = $cgi->param('custnum_text');
576 %  $custnum =~ s/\D//g;
577 %  $custnum =~ /^(\d{1,23})$/ or errorpage("Illegal customer number");
578 %  $custnum = $1;
579 %  
580 %  [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
581 %}
582 %
583 %sub cardsearch {
584 %
585 %  my($card)=$cgi->param('card');
586 %  $card =~ s/\D//g;
587 %  $card =~ /^(\d{13,16})$/ or errorpage("Illegal card number");
588 %  my($payinfo)=$1;
589 %
590 %  [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
591 %    qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'DCRD'})
592 %  ];
593 %}
594 %
595 %sub referralsearch {
596 %  $cgi->param('referral_custnum') =~ /^(\d+)$/
597 %    or errorpage("Illegal referral_custnum");
598 %  my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
599 %    or errorpage("Customer $1 not found");
600 %  my $depth;
601 %  if ( $cgi->param('referral_depth') ) {
602 %    $cgi->param('referral_depth') =~ /^(\d+)$/
603 %      or errorpage("Illegal referral_depth");
604 %    $depth = $1;
605 %  } else {
606 %    $depth = 1;
607 %  }
608 %  [ $cust_main->referral_cust_main($depth) ];
609 %}
610 %
611 %sub lastsearch {
612 %  my(%last_type);
613 %  my @cust_main;
614 %  foreach ( $cgi->param('last_type') ) {
615 %    $last_type{$_}++;
616 %  }
617 %
618 %  $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
619 %    or errorpage("Illegal last name");
620 %  my($last)=$1;
621 %
622 %  if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
623 %    push @cust_main, qsearch( 'cust_main',
624 %                              { 'last' => { 'op'    => 'ILIKE',
625 %                                            'value' => $last    } } );
626 %
627 %    push @cust_main, qsearch( 'cust_main',
628 %                              { 'ship_last' => { 'op'    => 'ILIKE',
629 %                                                 'value' => $last    } } )
630 %      if defined dbdef->table('cust_main')->column('ship_last');
631 %  }
632 %
633 %  if ( $last_type{'Substring'} || $last_type{'All'} ) {
634 %
635 %    push @cust_main, qsearch( 'cust_main',
636 %                              { 'last' => { 'op'    => 'ILIKE',
637 %                                            'value' => "%$last%" } } );
638 %
639 %    push @cust_main, qsearch( 'cust_main',
640 %                              { 'ship_last' => { 'op'    => 'ILIKE',
641 %                                                 'value' => "%$last%" } } )
642 %      if defined dbdef->table('cust_main')->column('ship_last');
643 %
644 %  }
645 %
646 %  if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
647 %    push @cust_main, FS::cust_main::Search->fuzzy_search( { 'last' => $last } );
648 %  }
649 %
650 %  #if ($last_type{'Sound-alike'}) {
651 %  #}
652 %
653 %  \@cust_main;
654 %}
655 %
656 %sub companysearch {
657 %
658 %  my(%company_type);
659 %  my @cust_main;
660 %  foreach ( $cgi->param('company_type') ) {
661 %    $company_type{$_}++ 
662 %  };
663 %
664 %  $cgi->param('company_text') =~
665 %    /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
666 %      or errorpage("Illegal company");
667 %  my $company = $1;
668 %
669 %  if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
670 %    push @cust_main, qsearch( 'cust_main',
671 %                              { 'company' => { 'op'    => 'ILIKE',
672 %                                               'value' => $company } } );
673 %
674 %    push @cust_main, qsearch( 'cust_main',
675 %                              { 'ship_company' => { 'op'    => 'ILIKE',
676 %                                                    'value' => $company } } )
677 %      if defined dbdef->table('cust_main')->column('ship_last');
678 %  }
679 %
680 %  if ( $company_type{'Substring'} || $company_type{'All'} ) {
681 %
682 %    push @cust_main, qsearch( 'cust_main',
683 %                              { 'company' => { 'op'    => 'ILIKE',
684 %                                               'value' => "%$company%" } } );
685 %
686 %    push @cust_main, qsearch( 'cust_main',
687 %                              { 'ship_company' => { 'op'    => 'ILIKE',
688 %                                                    'value' => "%$company%" } })
689 %      if defined dbdef->table('cust_main')->column('ship_last');
690 %
691 %  }
692 %
693 %  if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
694 %    push @cust_main, FS::cust_main::Search->fuzzy_search( { 'company' => $company } );
695 %  }
696 %
697 %  if ($company_type{'Sound-alike'}) {
698 %  }
699 %
700 %  \@cust_main;
701 %}
702 %
703 %sub address2search {
704 %  my @cust_main;
705 %
706 %  $cgi->param('address2_text') =~
707 %    /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
708 %      or errorpage("Illegal address2");
709 %  my $address2 = $1;
710 %
711 %  push @cust_main, qsearch( 'cust_main',
712 %                            { 'address2' => { 'op'    => 'ILIKE',
713 %                                              'value' => $address2 } } );
714 %  push @cust_main, qsearch( 'cust_main',
715 %                            { 'ship_address2' => { 'op'    => 'ILIKE',
716 %                                                   'value' => $address2 } } );
717 %
718 %  \@cust_main;
719 %}
720 %
721 %sub phonesearch {
722 %  my @cust_main;
723 %
724 %  my $phone = $cgi->param('phone_text');
725 %
726 %  #(no longer really) false laziness with Record::ut_phonen
727 %  #only works with US/CA numbers...
728 %  $phone =~ s/\D//g;
729 %  if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
730 %    $phone = "$1-$2-$3";
731 %    $phone .= " x$4" if $4;
732 %  } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
733 %    $phone = "$1-$2";
734 %  } elsif ( $phone =~ /^(\d{3,4})$/ ) {
735 %    $phone = $1;
736 %  } else {
737 %    errorpage(gettext('illegal_phone'). ": $phone");
738 %  }
739 %
740 %  my @fields = qw(daytime night fax);
741 %  push @fields, qw(ship_daytime ship_night ship_fax)
742 %    if defined dbdef->table('cust_main')->column('ship_last');
743 %
744 %  for my $field ( @fields ) {
745 %    push @cust_main, qsearch ( 'cust_main', 
746 %                               { $field => { 'op'    => 'LIKE',
747 %                                             'value' => "%$phone%" } } );
748 %  }
749 %
750 %  \@cust_main;
751 %}