fix package links in customer search in 2.1, RT#14091
[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 %      my $rows = 0;
381 %      foreach my $part_svc ( $cust_pkg->part_svc ) {
382 %        my $svcpart = $part_svc->svcpart;
383 %        my $num_cust_svc = $cust_pkg->num_cust_svc($svcpart);
384 %        if ( $large_pkg_size > 0 and $num_cust_svc >= $large_pkg_size ) {
385 %          # don't retrieve the cust_svc records, just stash the 
386 %          # part_svc and num_cust_svc for later
387 %          $cust_svc_by_svcpart{$svcpart} = 
388 %            [ 'summarize', $part_svc, $num_cust_svc ];
389 %          $rows += 2;
390 %        }
391 %        elsif ( $num_cust_svc ) {
392 %          $cust_svc_by_svcpart{$svcpart} = [ $cust_pkg->cust_svc($svcpart) ];
393 %          $rows += $num_cust_svc;
394 %        } #if summarize
395 %      } #foreach $part_svc
396 %      $rows ||= 1; # in case the package has no services
397 %      push @all_cust_svc, \%cust_svc_by_svcpart;
398 %      push @pkg_rowspans, $rows;
399 %    } #foreach $cust_pkg
400 %    my $rowspan = List::Util::sum(@pkg_rowspans) || 1;
401 %
402 %    my $view;
403 %    if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
404 %      $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
405 %    } else {
406 %      $view = $p. 'view/cust_main.cgi?'. $custnum;
407 %    }
408 %    my $pcompany = $company
409 %      ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
410 %      : '<FONT SIZE=-1>&nbsp;</FONT>';
411 %    
412 %    my $status = $cust_main->status;
413 %    my $statuscol = $cust_main->statuscolor;
414
415     <TR>
416       <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>><A HREF="<% $view %>"><FONT SIZE=-1><% $cust_main->display_custnum %></FONT></A></TD>
417       <TD CLASS="grid" ALIGN="center" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>><FONT SIZE="-1" COLOR="#<% $statuscol %>"><B><% ucfirst($status) %></B></FONT></TD>
418       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>><A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" %></FONT></A></TD>
419       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>><% $pcompany %></TD>
420 %
421 %    if ( defined dbdef->table('cust_main')->column('ship_last') ) {
422 %      my($ship_last,$ship_first,$ship_company)=(
423 %        $cust_main->ship_last || $cust_main->getfield('last'),
424 %        $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
425 %        $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
426 %      );
427 %      my $pship_company = $ship_company
428 %        ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
429 %        : '<FONT SIZE=-1>&nbsp;</FONT>';
430 %      
431
432
433       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>><A HREF="<% $view %>"><FONT SIZE=-1><% "$ship_last, $ship_first" %></FONT></A></TD>
434       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %>><% $pship_company %></A></TD>
435 % }
436 %
437 %    foreach my $addl_col ( @addl_cols ) { 
438 % if ( $addl_col eq 'tickets' ) { 
439 % if ( @custom_priorities ) { 
440
441
442              <TD CLASS="inv" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right><FONT SIZE=-1>
443
444                <TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>
445 % foreach my $priority ( @custom_priorities, '' ) { 
446 %
447 %                    my $num =
448 %                      FS::TicketSystem->num_customer_tickets($custnum,$priority);
449 %                    my $ahref = '';
450 %                    $ahref= '<A HREF="'.
451 %                            FS::TicketSystem->href_customer_tickets($custnum,$priority).
452 %                            '">'
453 %                      if $num;
454 %                 
455
456         
457                  <TR>
458                    <TD ALIGN=right>
459                      <FONT SIZE=-1><% $ahref.$num %></A></FONT>
460                    </TD>
461                    <TD ALIGN=left>
462                      <FONT SIZE=-1><% $ahref %><% $priority || '<i>(none)</i>' %></A></FONT>
463                    </TD>
464                  </TR>
465 % } 
466
467
468              <TR>
469                <TH ALIGN=right STYLE="border-top: dashed 1px black">
470                <FONT SIZE=-1>
471 % } else { 
472
473
474           <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right><FONT SIZE=-1>
475 % } 
476 %
477 %           my $ahref = '';
478 %           $ahref = '<A HREF="'.
479 %                       FS::TicketSystem->href_customer_tickets($custnum).
480 %                       '">'
481 %             if $cust_main->get($addl_col);
482 %        
483
484
485         <% $ahref %><% $cust_main->get($addl_col) %></A>
486 % if ( @custom_priorities ) { 
487
488
489           </FONT></TH>
490             <TH ALIGN=left STYLE="border-top: dashed 1px black">
491               <FONT SIZE=-1><% ${ahref} %>Total</A><FONT>
492             </TH>
493           </TR>
494           </TABLE>
495 % } 
496
497
498         </FONT></TD>
499 % } else { 
500
501
502         <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan %> ALIGN=right><FONT SIZE=-1>
503           <% $cust_main->get($addl_col) %>
504         </FONT></TD>
505 %
506 %      }
507 %    }
508 %
509 %    my($n1)='';
510 %    foreach ( @{$all_pkgs{$custnum}} ) {
511 %      my $pkgnum = $_->pkgnum;
512 %#      my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
513 %      my $part_pkg = $_->part_pkg;
514 %
515 %      my $pkg_comment = $part_pkg->pkg_comment(nopkgpart => 1);
516 %      my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
517 %                   ? ''
518 %                   : ';show=packages';
519 %      my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
520 %      my $pkgview = "${p}view/cust_main.cgi?custnum=$custnum$show;fragment=$frag#$frag";
521 %      # cust_svc stuff, built earlier
522 %      my %cust_svc_by_svcpart = %{ shift @all_cust_svc };
523 %      my $pkg_rowspan = shift @pkg_rowspans;
524
525        <% $n1 %><TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN="<% $pkg_rowspan %>">
526       <A HREF="<% $pkgview %>"><FONT SIZE=-1><% $pkg_comment %></FONT></A></TD>
527
528 %      my $n2 = '';
529 %      my $td = '<TD CLASS="grid" BGCOLOR="'.$bgcolor.'">';
530 %
531 %      foreach my $svcpart ( sort keys %cust_svc_by_svcpart ) { #sort order?
532 %        my $these = $cust_svc_by_svcpart{$svcpart};
533 %        if ( $these->[0] eq 'summarize' ) {
534 %           my $part_svc = $these->[1];
535 %           my $num_cust_svc = $these->[2];
536        <% $n2 %>
537 %        # summarize
538 %        # link opens a new search for this pkgnum/svcnum combo
539 %        my $href = $p.'search/cust_pkg_svc.html?svcpart='.$svcpart.
540 %                  ';pkgnum='.$pkgnum;
541          <% $td %>
542            <A HREF="<% $href %>"><% $part_svc->svc %></A>
543          </TD>
544          <% $td %>
545            <A HREF="<% $href %>"><B>(view all <% $num_cust_svc %>)</B></A>
546          </TD>
547        </TR><TR>
548          <% $td %></TD>
549          <% $td %><& /elements/search-cust_svc.html,
550                    'svcpart' => $svcpart,
551                    'pkgnum'  => $pkgnum,
552                    'svcdb'   => $part_svc->svcdb,
553                    &></TD>
554 %          $n2="</TR><TR>";
555 %        }
556 %        elsif ( scalar @$these ) { # do not summarize
557 %          foreach my $cust_svc ( @$these ) {
558          <% $n2 %>
559            <% $td %>
560              <% FS::UI::Web::svc_link($m, $cust_svc->part_svc, $cust_svc) %>
561            </TD>
562            <% $td %>
563              <% FS::UI::Web::svc_label_link($m, $cust_svc->part_svc, $cust_svc) %>
564            </TD>
565 %            $n2 = "</TR><TR>";
566 %          } # foreach $cust_svc
567 %        }
568 %      } # foreach $svcpart
569 %
570 %      unless ( %cust_svc_by_svcpart ) {
571          <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" COLSPAN=2>&nbsp;</TD>
572 %      }
573 %
574 %      #print qq!</TR><TR>\n!;
575 %      $n1="</TR><TR>";
576 %    }
577 %
578 %    unless ( @{$all_pkgs{$custnum}} ) {
579 %      print qq!<TD CLASS="grid" BGCOLOR="$bgcolor" COLSPAN=3>&nbsp;</TD>!;
580 %    }
581 %    
582 %    print "</TR>";
583 %  }
584 %
585 %  
586
587  
588   </TABLE><% $pager %>
589
590   <% include('/elements/footer.html') %>
591 % }
592 %
593 %#undef $cache; #does this help?
594 %
595 %#
596 %
597 %sub last_sort {
598 %  lc($a->getfield('last')) cmp lc($b->getfield('last'))
599 %  || lc($a->first) cmp lc($b->first);
600 %}
601 %
602 %sub company_sort {
603 %  return -1 if $a->company && ! $b->company;
604 %  return 1 if ! $a->company && $b->company;
605 %  lc($a->company) cmp lc($b->company)
606 %  || lc($a->getfield('last')) cmp lc($b->getfield('last'))
607 %  || lc($a->first) cmp lc($b->first);;
608 %}
609 %
610 %sub display_custnum_sort {
611 %  $a->display_custnum <=> $b->display_custnum;
612 %}
613 %
614 %sub custnum_sort {
615 %  $a->getfield('custnum') <=> $b->getfield('custnum');
616 %}
617 %
618 %sub tickets_sort {
619 %  $b->getfield('tickets') <=> $a->getfield('tickets');
620 %}
621 %
622 %sub custnumsearch {
623 %
624 %  my $custnum = $cgi->param('custnum_text');
625 %  $custnum =~ s/\D//g;
626 %  $custnum =~ /^(\d{1,23})$/ or errorpage("Illegal customer number");
627 %  $custnum = $1;
628 %  
629 %  [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
630 %}
631 %
632 %sub cardsearch {
633 %
634 %  my($card)=$cgi->param('card');
635 %  $card =~ s/\D//g;
636 %  $card =~ /^(\d{13,16})$/ or errorpage("Illegal card number");
637 %  my($payinfo)=$1;
638 %
639 %  [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
640 %    qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'DCRD'})
641 %  ];
642 %}
643 %
644 %sub referralsearch {
645 %  $cgi->param('referral_custnum') =~ /^(\d+)$/
646 %    or errorpage("Illegal referral_custnum");
647 %  my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
648 %    or errorpage("Customer $1 not found");
649 %  my $depth;
650 %  if ( $cgi->param('referral_depth') ) {
651 %    $cgi->param('referral_depth') =~ /^(\d+)$/
652 %      or errorpage("Illegal referral_depth");
653 %    $depth = $1;
654 %  } else {
655 %    $depth = 1;
656 %  }
657 %  [ $cust_main->referral_cust_main($depth) ];
658 %}
659 %
660 %sub lastsearch {
661 %  my(%last_type);
662 %  my @cust_main;
663 %  foreach ( $cgi->param('last_type') ) {
664 %    $last_type{$_}++;
665 %  }
666 %
667 %  $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
668 %    or errorpage("Illegal last name");
669 %  my($last)=$1;
670 %
671 %  if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
672 %    push @cust_main, qsearch( 'cust_main',
673 %                              { 'last' => { 'op'    => 'ILIKE',
674 %                                            'value' => $last    } } );
675 %
676 %    push @cust_main, qsearch( 'cust_main',
677 %                              { 'ship_last' => { 'op'    => 'ILIKE',
678 %                                                 'value' => $last    } } )
679 %      if defined dbdef->table('cust_main')->column('ship_last');
680 %  }
681 %
682 %  if ( $last_type{'Substring'} || $last_type{'All'} ) {
683 %
684 %    push @cust_main, qsearch( 'cust_main',
685 %                              { 'last' => { 'op'    => 'ILIKE',
686 %                                            'value' => "%$last%" } } );
687 %
688 %    push @cust_main, qsearch( 'cust_main',
689 %                              { 'ship_last' => { 'op'    => 'ILIKE',
690 %                                                 'value' => "%$last%" } } )
691 %      if defined dbdef->table('cust_main')->column('ship_last');
692 %
693 %  }
694 %
695 %  if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
696 %    push @cust_main, FS::cust_main::Search->fuzzy_search( { 'last' => $last } );
697 %  }
698 %
699 %  #if ($last_type{'Sound-alike'}) {
700 %  #}
701 %
702 %  \@cust_main;
703 %}
704 %
705 %sub companysearch {
706 %
707 %  my(%company_type);
708 %  my @cust_main;
709 %  foreach ( $cgi->param('company_type') ) {
710 %    $company_type{$_}++ 
711 %  };
712 %
713 %  $cgi->param('company_text') =~
714 %    /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
715 %      or errorpage("Illegal company");
716 %  my $company = $1;
717 %
718 %  if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
719 %    push @cust_main, qsearch( 'cust_main',
720 %                              { 'company' => { 'op'    => 'ILIKE',
721 %                                               'value' => $company } } );
722 %
723 %    push @cust_main, qsearch( 'cust_main',
724 %                              { 'ship_company' => { 'op'    => 'ILIKE',
725 %                                                    'value' => $company } } )
726 %      if defined dbdef->table('cust_main')->column('ship_last');
727 %  }
728 %
729 %  if ( $company_type{'Substring'} || $company_type{'All'} ) {
730 %
731 %    push @cust_main, qsearch( 'cust_main',
732 %                              { 'company' => { 'op'    => 'ILIKE',
733 %                                               'value' => "%$company%" } } );
734 %
735 %    push @cust_main, qsearch( 'cust_main',
736 %                              { 'ship_company' => { 'op'    => 'ILIKE',
737 %                                                    'value' => "%$company%" } })
738 %      if defined dbdef->table('cust_main')->column('ship_last');
739 %
740 %  }
741 %
742 %  if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
743 %    push @cust_main, FS::cust_main::Search->fuzzy_search( { 'company' => $company } );
744 %  }
745 %
746 %  if ($company_type{'Sound-alike'}) {
747 %  }
748 %
749 %  \@cust_main;
750 %}
751 %
752 %sub address2search {
753 %  my @cust_main;
754 %
755 %  $cgi->param('address2_text') =~
756 %    /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
757 %      or errorpage("Illegal address2");
758 %  my $address2 = $1;
759 %
760 %  push @cust_main, qsearch( 'cust_main',
761 %                            { 'address2' => { 'op'    => 'ILIKE',
762 %                                              'value' => $address2 } } );
763 %  push @cust_main, qsearch( 'cust_main',
764 %                            { 'ship_address2' => { 'op'    => 'ILIKE',
765 %                                                   'value' => $address2 } } );
766 %
767 %  \@cust_main;
768 %}
769 %
770 %sub phonesearch {
771 %  my @cust_main;
772 %
773 %  my $phone = $cgi->param('phone_text');
774 %
775 %  #(no longer really) false laziness with Record::ut_phonen
776 %  #only works with US/CA numbers...
777 %  $phone =~ s/\D//g;
778 %  if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
779 %    $phone = "$1-$2-$3";
780 %    $phone .= " x$4" if $4;
781 %  } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
782 %    $phone = "$1-$2";
783 %  } elsif ( $phone =~ /^(\d{3,4})$/ ) {
784 %    $phone = $1;
785 %  } else {
786 %    errorpage(gettext('illegal_phone'). ": $phone");
787 %  }
788 %
789 %  my @fields = qw(daytime night fax);
790 %  push @fields, qw(ship_daytime ship_night ship_fax)
791 %    if defined dbdef->table('cust_main')->column('ship_last');
792 %
793 %  for my $field ( @fields ) {
794 %    push @cust_main, qsearch ( 'cust_main', 
795 %                               { $field => { 'op'    => 'LIKE',
796 %                                             'value' => "%$phone%" } } );
797 %  }
798 %
799 %  \@cust_main;
800 %}