show reasons
[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   <TR>
140     <TD WIDTH="<%$width%>" ALIGN="right"><FONT COLOR="#ff0000" SIZE="-2">
141       <% $pkg->{reason} %>
142     </FONT></TD>
143   </TR>
144 % unless ( $pkg->{setup} ) { 
145
146
147     <TR>
148       <TD COLSPAN=<%$colspan%>>Never billed</TD>
149     </TR>
150 % } else { 
151
152
153     <TR>
154       <TD WIDTH="<%$width%>" ALIGN="right">Setup&nbsp;</TD>
155       <% pkg_datestr($pkg, 'setup',$conf) %>
156     </TR>
157 % if ( $pkg->{'last_bill'} ) { 
158
159       <TR>
160         <TD WIDTH="<%$width%>" ALIGN="right"><% $last_bill_or_renewed %>&nbsp;</TD>
161         <% pkg_datestr($pkg, 'last_bill',$conf) %>
162       </TR>
163 % } 
164 % if ( $pkg->{'susp'} ) { 
165
166       <TR>
167         <TD WIDTH="<%$width%>" ALIGN="right">Suspended&nbsp;</TD>
168         <% pkg_datestr($pkg, 'susp',$conf) %>
169       </TR>
170 % } 
171 % } 
172 % } else { 
173 % if ( $pkg->{susp} ) { 
174  <!-- #status: suspended -->
175
176     <TR>
177       <TD WIDTH="<%$width%>" ALIGN="right"><FONT COLOR="#FF9900"><B>Suspended</B>&nbsp;</FONT></TD>
178       <% pkg_datestr($pkg,'susp',$conf) %>
179     </TR>
180     <TR>
181       <TD WIDTH="<%$width%>" ALIGN="right"><FONT COLOR="#FF9900" SIZE="-2">
182         <% $pkg->{reason} %>
183       </FONT></TD>
184     </TR>
185 % unless ( $pkg->{setup} ) { 
186
187
188       <TR>
189         <TD COLSPAN=<%$colspan%>>Never billed</TD>
190       </TR>
191 % } else { 
192
193
194       <TR>
195         <TD WIDTH="<%$width%>" ALIGN="right">Setup&nbsp;</TD>
196         <% pkg_datestr($pkg, 'setup',$conf) %>
197       </TR>
198 % } 
199 % if ( $pkg->{'last_bill'} ) { 
200
201       <TR>
202         <TD WIDTH="<%$width%>" ALIGN="right"><% $last_bill_or_renewed %>&nbsp;</TD>
203         <% pkg_datestr($pkg, 'last_bill',$conf) %>
204       </TR>
205 % } 
206
207
208     <!-- # next bill ?? -->
209 % if ( $pkg->{'expire'} ) { 
210
211       <TR>
212         <TD WIDTH="<%$width%>" ALIGN="right">Expires&nbsp;</TD>
213         <% pkg_datestr($pkg, 'expire',$conf) %>
214       </TR>
215 % } 
216
217
218     <TR>
219       <TD COLSPAN=<%$colspan%>>
220         <FONT SIZE=-1>
221 % if ( $curuser->access_right('Unsuspend customer package') ) { 
222
223             (&nbsp;<% pkg_unsuspend_link($pkg) %>&nbsp;)
224 % } 
225 % if ( $curuser->access_right('Cancel customer package') ) { 
226
227             (&nbsp;<% pkg_cancel_link($pkg) %>&nbsp;)
228 % } 
229
230         </FONT>
231       </TD>
232     </TR>
233 % } else { 
234  <!-- #status: active -->
235 % unless ( $pkg->{setup} ) { 
236  <!-- #not setup -->
237 % unless ( $pkg->{'freq'} ) { 
238
239
240         <TR>
241           <TD COLSPAN=<%$colspan%>>Not&nbsp;yet&nbsp;billed&nbsp;(one-time&nbsp;charge)</TD>
242         </TR>
243
244         <TR>
245           <TD COLSPAN=<%$colspan%>>
246             <FONT SIZE=-1>
247 % if ( $curuser->access_right('Cancel customer package immediately') ) { 
248
249                 (&nbsp;<% pkg_cancel_link($pkg) %>&nbsp;)
250 % } 
251
252             </FONT>
253           </TD>
254         </TR>
255 % } else { 
256
257
258         <TR>
259           <TD COLSPAN=<%$colspan%>>Not&nbsp;yet&nbsp;billed&nbsp;(<% $billed_or_prepaid %>&nbsp;<% myfreq($pkg->{part_pkg}) %>)</TD>
260         </TR>
261 % } 
262 % } else { 
263  <!-- #setup -->
264 % unless ( $pkg->{freq} ) { 
265
266
267         <TR>
268           <TD COLSPAN=<%$colspan%>>One-time&nbsp;charge</TD>
269         </TR>
270
271         <TR>
272           <TD WIDTH="<%$width%>" ALIGN="right">Billed&nbsp;</TD>
273           <% pkg_datestr($pkg,'setup',$conf) %>
274         </TR>
275 % } else { 
276
277
278         <TR>
279           <TD COLSPAN=<%$colspan%>><FONT COLOR="#00CC00"><B>Active</B></FONT>,&nbsp;<% $billed_or_prepaid %>&nbsp;<% myfreq($pkg->{part_pkg}) %></TD>
280         </TR>
281
282         <TR>
283           <TD WIDTH="<%$width%>" ALIGN="right">Setup&nbsp;</TD>
284           <% pkg_datestr($pkg, 'setup',$conf) %>
285         </TR>
286 % } 
287 % } 
288 % if ( $pkg->{'last_bill'} ) { 
289
290       <TR>
291         <TD WIDTH="<%$width%>" ALIGN="right"><% $last_bill_or_renewed %>&nbsp;</TD>
292         <% pkg_datestr($pkg, 'last_bill',$conf) %>
293       </TR>
294 % } 
295 % if ( $pkg->{'next_bill'} ) { 
296
297       <TR>
298         <TD WIDTH="<%$width%>" ALIGN="right"><% $next_bill_or_prepaid_until %>&nbsp;</TD>
299         <% pkg_datestr($pkg, 'next_bill',$conf) %>
300       </TR>
301 % } 
302 % if ( $pkg->{'expire'} ) { 
303
304       <TR>
305         <TD WIDTH="<%$width%>" ALIGN="right">Expires&nbsp;</TD>
306         <% pkg_datestr($pkg, 'expire',$conf) %>
307       </TR>
308 % } 
309 % if ( $pkg->{freq} ) { 
310
311       <TR>
312         <TD COLSPAN=<%$colspan%>>
313           <FONT SIZE=-1>
314 % if ( $curuser->access_right('Suspend customer package') ) { 
315
316               (&nbsp;<% pkg_suspend_link($pkg) %>&nbsp;)
317 % } 
318 % if ( $curuser->access_right('Cancel customer package immediately') ) { 
319
320               (&nbsp;<% pkg_cancel_link($pkg) %>&nbsp;)
321 % } 
322 % if ( $curuser->access_right('Cancel customer package later') ) { 
323
324               (&nbsp;<% pkg_expire_link($pkg) %>&nbsp;)
325 % } 
326
327           <FONT>
328         </TD>
329       </TR>
330 % } 
331 % } 
332 % } 
333
334
335 </TABLE>
336 </TD>
337
338 <TD CLASS="inv" BGCOLOR="<% $bgcolor %>">
339   <TABLE CLASS="inv" BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="100%">
340
341 %  foreach my $svcpart (sort {$a->{svcpart} <=> $b->{svcpart}} @{$pkg->{svcparts}}) {
342
343 %    foreach my $service (@{$svcpart->{services}}) {
344
345       <TR>
346         <TD ALIGN="right" VALIGN="top" ROWSPAN=2><%svc_link($svcpart,$service)%></TD>
347         <TD STYLE="padding-bottom:0px"><B><%svc_label_link($svcpart,$service)%></B></TD>
348       </TR>
349
350 %      if ( $curuser->access_right('Unprovision customer service') ) { 
351
352         <TR>
353           <TD ALIGN="right" VALIGN="top" STYLE="padding-bottom:5px;padding-top:0px"><FONT SIZE="-2">(&nbsp;<%svc_unprovision_link($service)%>&nbsp;)</FONT></TD>
354         </TR>
355
356 %      } 
357 %   } 
358
359 %   if (    ! $pkg->{'cancel'}
360 %        && $curuser->access_right('Provision customer service') 
361 %        && $svcpart->{count} < $svcpart->{quantity}
362 %      ) {
363
364       <TR>
365         <TD COLSPAN=2 ALIGN="center" STYLE="padding-bottom:4px;padding-top:0px">
366           <B><% svc_provision_link($pkg, $svcpart, $conf, $curuser) %></B>
367         </TD>
368       </TR>
369
370 %   } 
371
372 % } 
373
374
375 </TABLE>
376 </TD>
377 % } #end display packages
378 %
379
380
381 </TABLE>
382 % } else { 
383
384 <BR>
385 % } 
386 %
387 %#subroutines
388 %
389 %sub get_packages {
390 %  my $cust_main = shift or return undef;
391 %  my $conf = shift;
392 %  
393 %  my @packages = ();
394 %  my $method;
395 %  if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
396 %     || ( $conf->exists('hidecancelledpackages')
397 %           && ! $cgi->param('showcancelledpackages') )
398 %     )
399 %  {
400 %    $method = 'ncancelled_pkgs';
401 %  } else {
402 %    $method = 'all_pkgs';
403 %  }
404 %  
405 %  foreach my $cust_pkg ( $cust_main->$method() ) {
406 %  
407 %    my $part_pkg = $cust_pkg->part_pkg;
408 %
409 %    my %pkg = ();
410 %
411 %    #to get back to the original object... should use it in the first place!!
412 %    $pkg{cust_pkg} = $cust_pkg;
413 %    $pkg{part_pkg} = $part_pkg;
414 %
415 %    $pkg{pkgnum} = $cust_pkg->pkgnum;
416 %    $pkg{pkg} = $part_pkg->pkg;
417 %    $pkg{pkgpart} = $part_pkg->pkgpart;
418 %    $pkg{comment} = $part_pkg->getfield('comment');
419 %    $pkg{freq} = $part_pkg->freq;
420 %    $pkg{setup} = $cust_pkg->getfield('setup');
421 %    $pkg{last_bill} = $cust_pkg->getfield('last_bill');
422 %    $pkg{next_bill} = $cust_pkg->getfield('bill');
423 %    $pkg{susp} = $cust_pkg->getfield('susp');
424 %    $pkg{expire} = $cust_pkg->getfield('expire');
425 %    $pkg{cancel} = $cust_pkg->getfield('cancel');
426 %    $pkg{reason} = $cust_pkg->last_reason->reason if $cust_pkg->last_reason;
427 %
428 %  
429 %    my %svcparts = map {
430 %      $_->svcpart => {
431 %                       $_->part_svc->hash,
432 %                       'quantity' => $_->quantity,
433 %                       'count'    => $cust_pkg->num_cust_svc($_->svcpart),
434 %                       #'services' => [],
435 %                     };
436 %    } $part_pkg->pkg_svc;
437 %
438 %    foreach my $cust_svc ( $cust_pkg->cust_svc ) {
439 %      #warn "svcnum ". $cust_svc->svcnum. " / svcpart ". $cust_svc->svcpart. "\n";
440 %      my $svc = {
441 %        'svcnum' => $cust_svc->svcnum,
442 %        'label'  => ($cust_svc->label)[1],
443 %      };
444 %
445 %      #false laziness with above, to catch extraneous services.  whole
446 %      #damn thing should be OO...
447 %      my $svcpart = ( $svcparts{$cust_svc->svcpart} ||= {
448 %        $cust_svc->part_svc->hash,
449 %        'quantity' => 0,
450 %        'count'    => $cust_pkg->num_cust_svc($cust_svc->svcpart),
451 %        #'services' => [],
452 %      } );
453 %
454 %      push @{$svcpart->{services}}, $svc;
455 %
456 %    }
457 %
458 %    $pkg{svcparts} = [ values %svcparts ];
459 %
460 %    push @packages, \%pkg;
461 %  
462 %  }
463 %  
464 %  return \@packages;
465 %
466 %}
467 %
468 %sub svc_link {
469 %
470 %  my ($svcpart, $svc) = (shift,shift) or return '';
471 %  return qq!<A HREF="${p}view/$svcpart->{svcdb}.cgi?$svc->{svcnum}">$svcpart->{svc}</A>!;
472 %
473 %}
474 %
475 %sub svc_label_link {
476 %
477 %  my ($svcpart, $svc) = (shift,shift) or return '';
478 %  return qq!<A HREF="${p}view/$svcpart->{svcdb}.cgi?$svc->{svcnum}">$svc->{label}</A>!;
479 %
480 %}
481 %
482 %sub svc_provision_link {
483 %  my ($pkg, $svcpart, $conf, $curuser) = @_;
484 %  ( my $svc_nbsp = $svcpart->{svc} ) =~ s/\s+/&nbsp;/g;
485 %  my $num_left = $svcpart->{quantity} - $svcpart->{count};
486 %  my $pkgnum_svcpart = "pkgnum$pkg->{pkgnum}-svcpart$svcpart->{svcpart}";
487 %
488 %  my $url;
489 %  if ( $svcpart->{svcdb} eq 'svc_external'
490 %       && $conf->exists('svc_external-skip_manual')
491 %  ) {
492 %    $url = "${p}edit/process/$svcpart->{svcdb}.cgi?".
493 %           "pkgnum=$pkg->{pkgnum}&".
494 %           "svcpart=$svcpart->{svcpart}";
495 %  } else {
496 %    $url = "${p}edit/$svcpart->{svcdb}.cgi?$pkgnum_svcpart";
497 %  }
498 %
499 %  my $link = qq!<A CLASS="provision" HREF="$url">!.
500 %             "Provision&nbsp;$svc_nbsp&nbsp;($num_left)</A>";
501 %  if ( $conf->exists('legacy_link')
502 %       && $curuser->access_right('View/link unlinked services')
503 %     )
504 %  {
505 %    $link .= '<BR>'.
506 %             qq!<A CLASS="provision" HREF="${p}misc/link.cgi?!.
507 %             qq!$pkgnum_svcpart">!.
508 %            "Link&nbsp;to&nbsp;legacy&nbsp;$svc_nbsp&nbsp;($num_left)</A>";
509 %  }
510 %  $link;
511 %}
512 %
513 %sub svc_unprovision_link {
514 %  my $svc = shift or return '';
515 %  qq!<A HREF="javascript:areyousure('${p}misc/unprovision.cgi?$svc->{svcnum}',!.
516 %  qq!'Permanently unprovision and delete this service?')">Unprovision</A>!;
517 %}
518 %
519 %# This should be generalized to use config options to determine order.
520 %sub pkgsort_pkgnum_cancel {
521 %  if ($a->{cancel} and $b->{cancel}) {
522 %    return ($a->{pkgnum} <=> $b->{pkgnum});
523 %  } elsif ($a->{cancel} or $b->{cancel}) {
524 %    return (-1) if ($b->{cancel});
525 %    return (1) if ($a->{cancel});
526 %    return (0);
527 %  } else {
528 %    return($a->{pkgnum} <=> $b->{pkgnum});
529 %  }
530 %}
531 %
532 %sub pkg_datestr {
533 %  my($pkg, $field, $conf) = @_ or return '';
534 %  return '&nbsp;' unless $pkg->{$field};
535 %  my $format = '<TD align="left"><B>%b</B></TD>'.
536 %               '<TD align="right"><B>&nbsp;%o,</B></TD>'.
537 %               '<TD align="right"><B>&nbsp;%Y</B></TD>';
538 %  #$format .= '&nbsp;<FONT SIZE=-3>%l:%M:%S%P&nbsp;%z</FONT>'
539 %  $format .= '<TD ALIGN="right"><B>&nbsp;%l</TD>'.
540 %             '<TD ALIGN="center"><B>:</B></TD>'.
541 %             '<TD ALIGN="left"><B>%M</B></TD>'.
542 %             '<TD ALIGN="left"><B>&nbsp;%P</B></TD>'
543 %    if $conf->exists('cust_pkg-display_times');
544 %  ( my $strip = time2str($format, $pkg->{$field}) ) =~ s/ (\d)/$1/g;
545 %  $strip;
546 %}
547 %
548 %sub pkg_change_link {
549 %  my $pkg = shift or return '';
550 %  return qq!<a href="${p}misc/change_pkg.cgi?$pkg->{pkgnum}">!.
551 %         qq!Change&nbsp;package</a>!;
552 %}
553 %
554 %sub pkg_suspend_link {
555 %  my $pkg = shift or return '';
556 %  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>!;
557 %}
558 %
559 %sub pkg_unsuspend_link {
560 %  my $pkg = shift or return '';
561 %  return qq!<a href="${p}misc/unsusp_pkg.cgi?$pkg->{pkgnum}">Unsuspend</a>!;
562 %}
563 %
564 %sub pkg_cancel_link {
565 %  my $pkg = shift or return '';
566 %
567 %  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>!;
568 %}
569 %
570 %sub pkg_expire_link {
571 %  my $pkg = shift or return '';
572 %  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>!;
573 %}
574 %
575 %sub pkg_dates_link {
576 %  my $pkg = shift or return '';
577 %  qq!<A HREF="${p}edit/REAL_cust_pkg.cgi?$pkg->{pkgnum}">Edit&nbsp;dates</A>!;
578 %}
579 %
580 %sub pkg_customize_link {
581 %  my $pkg = shift or return '';
582 %  my $custnum = shift;
583 %  qq!<A HREF="${p}edit/part_pkg.cgi?keywords=$custnum;clone=$pkg->{pkgpart};!.
584 %  qq!pkgnum=$pkg->{pkgnum}">Customize</A>!;
585 %}
586 %
587 %
588