fix regression from table re-layout: don't display provision links for cancelled...
[freeside.git] / httemplate / view / cust_main / packages.html
1 %
2 %  my( $cust_main ) = @_;
3 %  my $conf = new FS::Conf;
4 %
5 %  my $curuser = $FS::CurrentUser::CurrentUser;
6 %
7 %  my $packages = get_packages($cust_main, $conf);
8 %
9
10
11 <A NAME="cust_pkg"><FONT SIZE="+2">Packages</FONT></A>
12 % if ( $curuser->access_right('Order customer package') ) { 
13
14   <% include('order_pkg.html', $cust_main ) %>
15 % } 
16 % if ( $curuser->access_right('One-time charge')
17 %        && $conf->config('payby-default') ne 'HIDE'
18 %      ) {
19 %
20
21   <% include('quick-charge.html', $cust_main ) %>
22 % } 
23 % if ( $curuser->access_right('Bulk change customer packages') ) { 
24
25   <A HREF="<% $p %>edit/cust_pkg.cgi?<% $cust_main->custnum %>">Bulk order and cancel packages</A> (preserves services)
26 % } 
27
28
29 <BR><BR>
30 % if ( @$packages ) { 
31
32 Current packages
33 % } 
34 % if ( $cust_main->num_cancelled_pkgs ) {
35 %     if ( $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
36 %          || ( $conf->exists('hidecancelledpackages')
37 %               && ! $cgi->param('showcancelledpackages')
38 %             )
39 %        )
40 %     {
41 %       $cgi->param('showcancelledpackages', 1);
42 %
43
44   ( <a href="<% $cgi->self_url %>">show
45 %   } else {
46 %       $cgi->param('showcancelledpackages', 0);
47 %
48
49   ( <a href="<% $cgi->self_url %>">hide
50 %   } 
51
52  cancelled packages</a> )
53 % } 
54 % if ( @$packages ) { 
55
56
57 <% include('/elements/table-grid.html') %>
58 % my $bgcolor1 = '#eeeeee';
59 %   my $bgcolor2 = '#ffffff';
60 %   my $bgcolor = '';
61 %
62
63
64 <TR>
65   <TH CLASS="grid" BGCOLOR="#cccccc">Package</TH>
66   <TH CLASS="grid" BGCOLOR="#cccccc">Status</TH>
67   <TH CLASS="grid" BGCOLOR="#cccccc">Services</TH>
68 </TR>
69 %
70 %foreach my $pkg (sort pkgsort_pkgnum_cancel @$packages) {
71 %
72 %  if ( $bgcolor eq $bgcolor1 ) {
73 %    $bgcolor = $bgcolor2;
74 %  } else {
75 %    $bgcolor = $bgcolor1;
76 %  }
77 %
78 %
79
80
81 <!--pkgnum: <%$pkg->{pkgnum}%>-->
82 <TR>
83   <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
84     <A NAME="cust_pkg<%$pkg->{pkgnum}%>"><%$pkg->{pkgnum}%></A>:
85     <%$pkg->{pkg}%> - <%$pkg->{comment}%><BR>
86     <FONT SIZE=-1>
87 % unless ( $pkg->{cancel} ) { 
88 % if ( $curuser->access_right('Change customer package') ) { 
89
90             (&nbsp;<%pkg_change_link($pkg)%>&nbsp;)
91 % } 
92 % if ( $curuser->access_right('Edit customer package dates') ) { 
93
94             (&nbsp;<%pkg_dates_link($pkg)%>&nbsp;)
95 % } 
96 % if ( $curuser->access_right('Customize customer package') ) { 
97
98             (&nbsp;<%pkg_customize_link($pkg,$cust_main->custnum)%>&nbsp;)
99 % } 
100 % } 
101
102     </FONT>
103   </TD>
104   <TD CLASS="inv" BGCOLOR="<% $bgcolor %>">
105     <TABLE CLASS="inv" BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="100%">
106 %
107 %  sub myfreq {
108 %    my $part_pkg = shift;
109 %    my $freq = $part_pkg->freq_pretty;
110 %    $freq =~ s/ /&nbsp;/g;
111 %    $freq;
112 %  }
113 %
114 %  #this should use cust_pkg->status and cust_pkg->statuscolor eventually
115 %
116 %  my $colspan = $conf->exists('cust_pkg-display_times') ? 8 : 4;
117 %  my $width = $conf->exists('cust_pkg-display_times') ? '38%' : '56%';
118 %
119 %  #false laziness w/edit/REAL_cust_pkg.cgi
120 %  my( $billed_or_prepaid, $last_bill_or_renewed, $next_bill_or_prepaid_until );
121 %  unless ( $pkg->{'part_pkg'}->is_prepaid ) {
122 %    $billed_or_prepaid = 'billed';
123 %    $last_bill_or_renewed = 'Last&nbsp;bill';
124 %    $next_bill_or_prepaid_until = 'Next&nbsp;bill';
125 %  } else {
126 %    $billed_or_prepaid = 'prepaid';
127 %    $last_bill_or_renewed = 'Renewed';
128 %    $next_bill_or_prepaid_until = 'Prepaid&nbsp;until';
129 %  }
130 %
131 %
132 % if ( $pkg->{cancel} ) { 
133  <!-- #status: cancelled -->
134
135   <TR>
136     <TD WIDTH="<%$width%>" ALIGN="right"><FONT COLOR="#ff0000"><B>Cancelled&nbsp;</B></FONT></TD>
137     <% pkg_datestr($pkg,'cancel',$conf) %>
138   </TR>
139 % unless ( $pkg->{setup} ) { 
140
141
142     <TR>
143       <TD COLSPAN=<%$colspan%>>Never billed</TD>
144     </TR>
145 % } else { 
146
147
148     <TR>
149       <TD WIDTH="<%$width%>" ALIGN="right">Setup&nbsp;</TD>
150       <% pkg_datestr($pkg, 'setup',$conf) %>
151     </TR>
152 % if ( $pkg->{'last_bill'} ) { 
153
154       <TR>
155         <TD WIDTH="<%$width%>" ALIGN="right"><% $last_bill_or_renewed %>&nbsp;</TD>
156         <% pkg_datestr($pkg, 'last_bill',$conf) %>
157       </TR>
158 % } 
159 % if ( $pkg->{'susp'} ) { 
160
161       <TR>
162         <TD WIDTH="<%$width%>" ALIGN="right">Suspended&nbsp;</TD>
163         <% pkg_datestr($pkg, 'susp',$conf) %>
164       </TR>
165 % } 
166 % } 
167 % } else { 
168 % if ( $pkg->{susp} ) { 
169  <!-- #status: suspended -->
170
171     <TR>
172       <TD WIDTH="<%$width%>" ALIGN="right"><FONT COLOR="#FF9900"><B>Suspended</B>&nbsp;</FONT></TD>
173       <% pkg_datestr($pkg,'susp',$conf) %>
174     </TR>
175 % unless ( $pkg->{setup} ) { 
176
177
178       <TR>
179         <TD COLSPAN=<%$colspan%>>Never billed</TD>
180       </TR>
181 % } else { 
182
183
184       <TR>
185         <TD WIDTH="<%$width%>" ALIGN="right">Setup&nbsp;</TD>
186         <% pkg_datestr($pkg, 'setup',$conf) %>
187       </TR>
188 % } 
189 % if ( $pkg->{'last_bill'} ) { 
190
191       <TR>
192         <TD WIDTH="<%$width%>" ALIGN="right"><% $last_bill_or_renewed %>&nbsp;</TD>
193         <% pkg_datestr($pkg, 'last_bill',$conf) %>
194       </TR>
195 % } 
196
197
198     <!-- # next bill ?? -->
199 % if ( $pkg->{'expire'} ) { 
200
201       <TR>
202         <TD WIDTH="<%$width%>" ALIGN="right">Expires&nbsp;</TD>
203         <% pkg_datestr($pkg, 'expire',$conf) %>
204       </TR>
205 % } 
206
207
208     <TR>
209       <TD COLSPAN=<%$colspan%>>
210         <FONT SIZE=-1>
211 % if ( $curuser->access_right('Unsuspend customer package') ) { 
212
213             (&nbsp;<% pkg_unsuspend_link($pkg) %>&nbsp;)
214 % } 
215 % if ( $curuser->access_right('Cancel customer package') ) { 
216
217             (&nbsp;<% pkg_cancel_link($pkg) %>&nbsp;)
218 % } 
219
220         </FONT>
221       </TD>
222     </TR>
223 % } else { 
224  <!-- #status: active -->
225 % unless ( $pkg->{setup} ) { 
226  <!-- #not setup -->
227 % unless ( $pkg->{'freq'} ) { 
228
229
230         <TR>
231           <TD COLSPAN=<%$colspan%>>Not&nbsp;yet&nbsp;billed&nbsp;(one-time&nbsp;charge)</TD>
232         </TR>
233
234         <TR>
235           <TD COLSPAN=<%$colspan%>>
236             <FONT SIZE=-1>
237 % if ( $curuser->access_right('Cancel customer package immediately') ) { 
238
239                 (&nbsp;<% pkg_cancel_link($pkg) %>&nbsp;)
240 % } 
241
242             </FONT>
243           </TD>
244         </TR>
245 % } else { 
246
247
248         <TR>
249           <TD COLSPAN=<%$colspan%>>Not&nbsp;yet&nbsp;billed&nbsp;(<% $billed_or_prepaid %>&nbsp;<% myfreq($pkg->{part_pkg}) %>)</TD>
250         </TR>
251 % } 
252 % } else { 
253  <!-- #setup -->
254 % unless ( $pkg->{freq} ) { 
255
256
257         <TR>
258           <TD COLSPAN=<%$colspan%>>One-time&nbsp;charge</TD>
259         </TR>
260
261         <TR>
262           <TD WIDTH="<%$width%>" ALIGN="right">Billed&nbsp;</TD>
263           <% pkg_datestr($pkg,'setup',$conf) %>
264         </TR>
265 % } else { 
266
267
268         <TR>
269           <TD COLSPAN=<%$colspan%>><FONT COLOR="#00CC00"><B>Active</B></FONT>,&nbsp;<% $billed_or_prepaid %>&nbsp;<% myfreq($pkg->{part_pkg}) %></TD>
270         </TR>
271
272         <TR>
273           <TD WIDTH="<%$width%>" ALIGN="right">Setup&nbsp;</TD>
274           <% pkg_datestr($pkg, 'setup',$conf) %>
275         </TR>
276 % } 
277 % } 
278 % if ( $pkg->{'last_bill'} ) { 
279
280       <TR>
281         <TD WIDTH="<%$width%>" ALIGN="right"><% $last_bill_or_renewed %>&nbsp;</TD>
282         <% pkg_datestr($pkg, 'last_bill',$conf) %>
283       </TR>
284 % } 
285 % if ( $pkg->{'next_bill'} ) { 
286
287       <TR>
288         <TD WIDTH="<%$width%>" ALIGN="right"><% $next_bill_or_prepaid_until %>&nbsp;</TD>
289         <% pkg_datestr($pkg, 'next_bill',$conf) %>
290       </TR>
291 % } 
292 % if ( $pkg->{'expire'} ) { 
293
294       <TR>
295         <TD WIDTH="<%$width%>" ALIGN="right">Expires&nbsp;</TD>
296         <% pkg_datestr($pkg, 'expire',$conf) %>
297       </TR>
298 % } 
299 % if ( $pkg->{freq} ) { 
300
301       <TR>
302         <TD COLSPAN=<%$colspan%>>
303           <FONT SIZE=-1>
304 % if ( $curuser->access_right('Suspend customer package') ) { 
305
306               (&nbsp;<% pkg_suspend_link($pkg) %>&nbsp;)
307 % } 
308 % if ( $curuser->access_right('Cancel customer package immediately') ) { 
309
310               (&nbsp;<% pkg_cancel_link($pkg) %>&nbsp;)
311 % } 
312 % if ( $curuser->access_right('Cancel customer package later') ) { 
313
314               (&nbsp;<% pkg_expire_link($pkg) %>&nbsp;)
315 % } 
316
317           <FONT>
318         </TD>
319       </TR>
320 % } 
321 % } 
322 % } 
323
324
325 </TABLE>
326 </TD>
327
328 <TD CLASS="inv" BGCOLOR="<% $bgcolor %>">
329   <TABLE CLASS="inv" BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="100%">
330
331 %  foreach my $svcpart (sort {$a->{svcpart} <=> $b->{svcpart}} @{$pkg->{svcparts}}) {
332
333 %    foreach my $service (@{$svcpart->{services}}) {
334
335       <TR>
336         <TD ALIGN="right" VALIGN="top" ROWSPAN=2><%svc_link($svcpart,$service)%></TD>
337         <TD STYLE="padding-bottom:0px"><B><%svc_label_link($svcpart,$service)%></B></TD>
338       </TR>
339
340 %      if ( $curuser->access_right('Unprovision customer service') ) { 
341
342         <TR>
343           <TD ALIGN="right" VALIGN="top" STYLE="padding-bottom:5px;padding-top:0px"><FONT SIZE="-2">(&nbsp;<%svc_unprovision_link($service)%>&nbsp;)</FONT></TD>
344         </TR>
345
346 %      } 
347 %   } 
348
349 %   if (    ! $pkg->{'cancel'}
350 %        && $curuser->access_right('Provision customer service') 
351 %        && $svcpart->{count} < $svcpart->{quantity}
352 %      ) {
353
354       <TR>
355         <TD COLSPAN=2 ALIGN="center" STYLE="padding-bottom:4px;padding-top:0px">
356           <B><% svc_provision_link($pkg, $svcpart, $conf, $curuser) %></B>
357         </TD>
358       </TR>
359
360 %   } 
361
362 % } 
363
364
365 </TABLE>
366 </TD>
367 % } #end display packages
368 %
369
370
371 </TABLE>
372 % } else { 
373
374 <BR>
375 % } 
376 %
377 %#subroutines
378 %
379 %sub get_packages {
380 %  my $cust_main = shift or return undef;
381 %  my $conf = shift;
382 %  
383 %  my @packages = ();
384 %  my $method;
385 %  if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
386 %     || ( $conf->exists('hidecancelledpackages')
387 %           && ! $cgi->param('showcancelledpackages') )
388 %     )
389 %  {
390 %    $method = 'ncancelled_pkgs';
391 %  } else {
392 %    $method = 'all_pkgs';
393 %  }
394 %  
395 %  foreach my $cust_pkg ( $cust_main->$method() ) {
396 %  
397 %    my $part_pkg = $cust_pkg->part_pkg;
398 %
399 %    my %pkg = ();
400 %
401 %    #to get back to the original object... should use it in the first place!!
402 %    $pkg{cust_pkg} = $cust_pkg;
403 %    $pkg{part_pkg} = $part_pkg;
404 %
405 %    $pkg{pkgnum} = $cust_pkg->pkgnum;
406 %    $pkg{pkg} = $part_pkg->pkg;
407 %    $pkg{pkgpart} = $part_pkg->pkgpart;
408 %    $pkg{comment} = $part_pkg->getfield('comment');
409 %    $pkg{freq} = $part_pkg->freq;
410 %    $pkg{setup} = $cust_pkg->getfield('setup');
411 %    $pkg{last_bill} = $cust_pkg->getfield('last_bill');
412 %    $pkg{next_bill} = $cust_pkg->getfield('bill');
413 %    $pkg{susp} = $cust_pkg->getfield('susp');
414 %    $pkg{expire} = $cust_pkg->getfield('expire');
415 %    $pkg{cancel} = $cust_pkg->getfield('cancel');
416 %
417 %  
418 %    my %svcparts = map {
419 %      $_->svcpart => {
420 %                       $_->part_svc->hash,
421 %                       'quantity' => $_->quantity,
422 %                       'count'    => $cust_pkg->num_cust_svc($_->svcpart),
423 %                       #'services' => [],
424 %                     };
425 %    } $part_pkg->pkg_svc;
426 %
427 %    foreach my $cust_svc ( $cust_pkg->cust_svc ) {
428 %      #warn "svcnum ". $cust_svc->svcnum. " / svcpart ". $cust_svc->svcpart. "\n";
429 %      my $svc = {
430 %        'svcnum' => $cust_svc->svcnum,
431 %        'label'  => ($cust_svc->label)[1],
432 %      };
433 %
434 %      #false laziness with above, to catch extraneous services.  whole
435 %      #damn thing should be OO...
436 %      my $svcpart = ( $svcparts{$cust_svc->svcpart} ||= {
437 %        $cust_svc->part_svc->hash,
438 %        'quantity' => 0,
439 %        'count'    => $cust_pkg->num_cust_svc($cust_svc->svcpart),
440 %        #'services' => [],
441 %      } );
442 %
443 %      push @{$svcpart->{services}}, $svc;
444 %
445 %    }
446 %
447 %    $pkg{svcparts} = [ values %svcparts ];
448 %
449 %    push @packages, \%pkg;
450 %  
451 %  }
452 %  
453 %  return \@packages;
454 %
455 %}
456 %
457 %sub svc_link {
458 %
459 %  my ($svcpart, $svc) = (shift,shift) or return '';
460 %  return qq!<A HREF="${p}view/$svcpart->{svcdb}.cgi?$svc->{svcnum}">$svcpart->{svc}</A>!;
461 %
462 %}
463 %
464 %sub svc_label_link {
465 %
466 %  my ($svcpart, $svc) = (shift,shift) or return '';
467 %  return qq!<A HREF="${p}view/$svcpart->{svcdb}.cgi?$svc->{svcnum}">$svc->{label}</A>!;
468 %
469 %}
470 %
471 %sub svc_provision_link {
472 %  my ($pkg, $svcpart, $conf, $curuser) = @_;
473 %  ( my $svc_nbsp = $svcpart->{svc} ) =~ s/\s+/&nbsp;/g;
474 %  my $num_left = $svcpart->{quantity} - $svcpart->{count};
475 %  my $pkgnum_svcpart = "pkgnum$pkg->{pkgnum}-svcpart$svcpart->{svcpart}";
476 %
477 %  my $url;
478 %  if ( $svcpart->{svcdb} eq 'svc_external'
479 %       && $conf->exists('svc_external-skip_manual')
480 %  ) {
481 %    $url = "${p}edit/process/$svcpart->{svcdb}.cgi?".
482 %           "pkgnum=$pkg->{pkgnum}&".
483 %           "svcpart=$svcpart->{svcpart}";
484 %  } else {
485 %    $url = "${p}edit/$svcpart->{svcdb}.cgi?$pkgnum_svcpart";
486 %  }
487 %
488 %  my $link = qq!<A CLASS="provision" HREF="$url">!.
489 %             "Provision&nbsp;$svc_nbsp&nbsp;($num_left)</A>";
490 %  if ( $conf->exists('legacy_link')
491 %       && $curuser->access_right('View/link unlinked services')
492 %     )
493 %  {
494 %    $link .= '<BR>'.
495 %             qq!<A CLASS="provision" HREF="${p}misc/link.cgi?!.
496 %             qq!$pkgnum_svcpart">!.
497 %            "Link&nbsp;to&nbsp;legacy&nbsp;$svc_nbsp&nbsp;($num_left)</A>";
498 %  }
499 %  $link;
500 %}
501 %
502 %sub svc_unprovision_link {
503 %  my $svc = shift or return '';
504 %  qq!<A HREF="javascript:areyousure('${p}misc/unprovision.cgi?$svc->{svcnum}',!.
505 %  qq!'Permanently unprovision and delete this service?')">Unprovision</A>!;
506 %}
507 %
508 %# This should be generalized to use config options to determine order.
509 %sub pkgsort_pkgnum_cancel {
510 %  if ($a->{cancel} and $b->{cancel}) {
511 %    return ($a->{pkgnum} <=> $b->{pkgnum});
512 %  } elsif ($a->{cancel} or $b->{cancel}) {
513 %    return (-1) if ($b->{cancel});
514 %    return (1) if ($a->{cancel});
515 %    return (0);
516 %  } else {
517 %    return($a->{pkgnum} <=> $b->{pkgnum});
518 %  }
519 %}
520 %
521 %sub pkg_datestr {
522 %  my($pkg, $field, $conf) = @_ or return '';
523 %  return '&nbsp;' unless $pkg->{$field};
524 %  my $format = '<TD align="left"><B>%b</B></TD>'.
525 %               '<TD align="right"><B>&nbsp;%o,</B></TD>'.
526 %               '<TD align="right"><B>&nbsp;%Y</B></TD>';
527 %  #$format .= '&nbsp;<FONT SIZE=-3>%l:%M:%S%P&nbsp;%z</FONT>'
528 %  $format .= '<TD ALIGN="right"><B>&nbsp;%l</TD>'.
529 %             '<TD ALIGN="center"><B>:</B></TD>'.
530 %             '<TD ALIGN="left"><B>%M</B></TD>'.
531 %             '<TD ALIGN="left"><B>&nbsp;%P</B></TD>'
532 %    if $conf->exists('cust_pkg-display_times');
533 %  ( my $strip = time2str($format, $pkg->{$field}) ) =~ s/ (\d)/$1/g;
534 %  $strip;
535 %}
536 %
537 %sub pkg_change_link {
538 %  my $pkg = shift or return '';
539 %  return qq!<a href="${p}misc/change_pkg.cgi?$pkg->{pkgnum}">!.
540 %         qq!Change&nbsp;package</a>!;
541 %}
542 %
543 %sub pkg_suspend_link {
544 %  my $pkg = shift or return '';
545 %  qq!<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('${p}misc/cancel_pkg.html?method=suspend&pkgnum=$pkg->{pkgnum}', 392, 336, 'suspend_pkg_popup' ), CAPTION, 'Suspend package $pkg->{pkgnum}', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK ); return false;">Suspend</A>!;
546 %}
547 %
548 %sub pkg_unsuspend_link {
549 %  my $pkg = shift or return '';
550 %  return qq!<a href="${p}misc/unsusp_pkg.cgi?$pkg->{pkgnum}">Unsuspend</a>!;
551 %}
552 %
553 %sub pkg_cancel_link {
554 %  my $pkg = shift or return '';
555 %
556 %  qq!<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('${p}misc/cancel_pkg.html?method=cancel&pkgnum=$pkg->{pkgnum}', 392, 336, 'cancel_pkg_popup' ), CAPTION, 'Cancel package $pkg->{pkgnum}', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK ); return false;">Cancel now</A>!;
557 %}
558 %
559 %sub pkg_expire_link {
560 %  my $pkg = shift or return '';
561 %  qq!<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('${p}misc/cancel_pkg.html?method=expire&pkgnum=$pkg->{pkgnum}', 392, 336, 'expire_pkg_popup' ), CAPTION, 'Expire package $pkg->{pkgnum}', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK ); return false;">Cancel later</A>!;
562 %}
563 %
564 %sub pkg_dates_link {
565 %  my $pkg = shift or return '';
566 %  qq!<A HREF="${p}edit/REAL_cust_pkg.cgi?$pkg->{pkgnum}">Edit&nbsp;dates</A>!;
567 %}
568 %
569 %sub pkg_customize_link {
570 %  my $pkg = shift or return '';
571 %  my $custnum = shift;
572 %  qq!<A HREF="${p}edit/part_pkg.cgi?keywords=$custnum;clone=$pkg->{pkgpart};!.
573 %  qq!pkgnum=$pkg->{pkgnum}">Customize</A>!;
574 %}
575 %
576 %
577