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