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