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