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