allow exports to add links to customer view (#1407)
[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         <TD><% FS::UI::Web::svc_export_links($m, $part_svc, $cust_svc) %></TD>
285       </TR>
286
287       <TR>
288         <TD ALIGN="right" COLSPAN="3" VALIGN="top" STYLE="padding-bottom:1px;padding-top:0px"><FONT SIZE="-2" COLOR="#FFD000">
289
290             <% $cust_svc->overlimit ? "Overlimit: ". time2str('%b %o %Y' . ($conf->exists('cust_pkg-display_times') ? ' %l:%M %P' : ''), $cust_svc->overlimit) : '' %>
291           </FONT></TD>
292       </TR>
293
294       <TR>
295         <TD ALIGN="right" VALIGN="top" STYLE="padding-bottom:5px;padding-top:0px"><FONT SIZE="-2">
296
297 %         if ( $curuser->access_right('Recharge customer service')
298 %              && $part_svc->svcdb eq 'svc_acct'
299 %              && (    $cust_svc->svc_x->seconds    ne ''
300 %                   || $cust_svc->svc_x->upbytes    ne ''
301 %                   || $cust_svc->svc_x->downbytes  ne ''
302 %                   || $cust_svc->svc_x->totalbytes ne ''
303 %                 )
304 %         ) { 
305             (&nbsp;<%svc_recharge_link($cust_svc)%>&nbsp;)
306 %         } 
307           </FONT></TD>
308
309           <TD ALIGN="right" VALIGN="top" STYLE="padding-bottom:5px;padding-top:0px"><FONT SIZE="-2">
310
311 %         if ( $curuser->access_right('Unprovision customer service') ) { 
312             (&nbsp;<%svc_unprovision_link($cust_svc)%>&nbsp;)
313 %         } 
314           </FONT></TD>
315         </TR>
316 %   } 
317
318 %   if (    ! $cust_pkg->get('cancel')
319 %        && $curuser->access_right('Provision customer service') 
320 %        && $part_svc->num_avail
321 %      ) {
322
323       <TR>
324         <TD COLSPAN=3 ALIGN="center" STYLE="padding-bottom:4px;padding-top:0px">
325           <B><% svc_provision_link($cust_pkg, $part_svc, $conf, $curuser) %></B>
326         </TD>
327       </TR>
328
329 %   } 
330
331 % } 
332
333 </TABLE>
334 </TD>
335 % } #end display packages
336 %
337
338
339 </TABLE>
340 % } else { 
341
342 <BR>
343 % } 
344 <%init>
345
346 my( $cust_main ) = @_;
347 my $conf = new FS::Conf;
348
349 my $curuser = $FS::CurrentUser::CurrentUser;
350
351 my $packages = get_packages($cust_main, $conf);
352
353 my $colspan = $conf->exists('cust_pkg-display_times') ? 8 : 4;
354 my $width = $conf->exists('cust_pkg-display_times') ? '38%' : '56%';
355
356 sub pkg_status_row {
357   my( $cust_pkg, $title, $field, %opt ) = @_;
358
359   my $color = $opt{'color'};
360
361   my $html = qq(<TR><TD WIDTH="<%$width%>" ALIGN="right">);
362   $html   .= qq(<FONT COLOR="#$color"><B>) if length($color);
363   $html   .= qq($title&nbsp;);
364   $html   .= qq(</B></FONT>) if length($color);
365   $html   .= qq(</TD>);
366   $html   .= pkg_datestr($cust_pkg, $field, $conf).'</TR>';
367
368   $html;
369 }
370
371 sub pkg_status_row_if {
372   my( $cust_pkg, $title, $field, %opt ) = @_;
373   $cust_pkg->get($field) ? pkg_status_row(@_) : '';
374 }
375
376 sub pkg_status_row_changed {
377   my($cust_pkg) = @_;
378   return '' unless $cust_pkg->change_date;
379   my $html = pkg_status_row( $cust_pkg, 'Package&nbsp;changed', 'change_date' );
380   my $old = $cust_pkg->old_cust_pkg;
381   if ( $old ) {
382     my $part_pkg = $old->part_pkg;
383     my $label = 'Changed from '. $cust_pkg->change_pkgnum. ': '.
384                 $part_pkg->pkg. ' - '. $part_pkg->comment;
385     $html .= pkg_status_row_colspan( $label, '', size=>'-1', align=>'right' );
386   }
387   $html;
388 }
389
390 sub pkg_status_row_colspan {
391   my($title, $addl, %opt) = @_;
392
393   my $align = $opt{'align'} ? 'ALIGN="'. $opt{'align'}.'"' : '';
394   my $color = $opt{'color'} ? 'COLOR="#'.$opt{'color'}.'"' : '';
395   my $size  = $opt{'size'}  ? 'SIZE="'.  $opt{'size'}. '"' : '';
396
397   my $html = qq(<TR><TD COLSPAN=$colspan $align>);
398   $html   .= qq(<FONT $color $size>) if length($color) || $size;
399   $html   .= qq(<B>) if $color && !$size;
400   $html   .= $title;
401   $html   .= qq(</B>) if $color && !$size;
402   $html   .= qq(</FONT>) if length($color) || $size;
403   $html   .= ",&nbsp;$addl" if length($addl);
404   $html   .= qq(</TD></TR>);
405
406   $html;
407
408 }
409
410 </%init>
411 <%once>
412
413 #subroutines
414
415 sub get_packages {
416   my $cust_main = shift or return undef;
417   my $conf = shift;
418   
419   my @packages = ();
420   my $method;
421   if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
422      || ( $conf->exists('hidecancelledpackages')
423            && ! $cgi->param('showcancelledpackages') )
424      )
425   {
426     $method = 'ncancelled_pkgs';
427   } else {
428     $method = 'all_pkgs';
429   }
430
431   [ $cust_main->$method() ];
432 }
433   
434 sub svc_provision_link {
435   my ($cust_pkg, $part_svc, $conf, $curuser) = @_;
436   ( my $svc_nbsp = $part_svc->svc ) =~ s/\s+/&nbsp;/g;
437   my $num_avail = $part_svc->num_avail;
438   my $pkgnum_svcpart = "pkgnum=". $cust_pkg->pkgnum. ';'.
439                        "svcpart=". $part_svc->svcpart;
440   my $url;
441   if ( $part_svc->svcdb eq 'svc_external' #could be generalized
442        && $conf->exists('svc_external-skip_manual')
443   ) {
444     $url = "${p}edit/process/". $part_svc->svcdb. ".cgi?$pkgnum_svcpart";
445   } else {
446     $url = svc_url(
447                     'm'        => $m,
448                     'action'   => 'edit',
449                     'part_svc' => $part_svc, 
450                     'query'    => $pkgnum_svcpart,
451                   );
452     #$url = "${p}edit/$svcpart->{svcdb}.cgi?$pkgnum_svcpart";
453   }
454
455   my $link = qq!<A CLASS="provision" HREF="$url">!.
456              "Provision&nbsp;$svc_nbsp&nbsp;($num_avail)</A>";
457   if ( $conf->exists('legacy_link')
458        && $curuser->access_right('View/link unlinked services')
459      )
460   {
461     $link .= '<BR>'.
462              qq!<A CLASS="provision" HREF="${p}misc/link.cgi?!.
463              qq!$pkgnum_svcpart">!.
464             "Link&nbsp;to&nbsp;legacy&nbsp;$svc_nbsp&nbsp;($num_avail)</A>";
465   }
466   $link;
467 }
468
469 sub svc_unprovision_link {
470   my $cust_svc = shift or return '';
471   qq!<A HREF="javascript:areyousure('${p}misc/unprovision.cgi?!. $cust_svc->svcnum.
472   qq!', 'Permanently unprovision and delete this service?')">Unprovision</A>!;
473 }
474
475 sub pkg_datestr {
476   my($cust_pkg, $field, $conf) = @_ or return '';
477   return '&nbsp;' unless $cust_pkg->get($field);
478   my $format = '<TD align="left"><B>%b</B></TD>'.
479                '<TD align="right"><B>&nbsp;%o,</B></TD>'.
480                '<TD align="right"><B>&nbsp;%Y</B></TD>';
481   #$format .= '&nbsp;<FONT SIZE=-3>%l:%M:%S%P&nbsp;%z</FONT>'
482   $format .= '<TD ALIGN="right"><B>&nbsp;%l</TD>'.
483              '<TD ALIGN="center"><B>:</B></TD>'.
484              '<TD ALIGN="left"><B>%M</B></TD>'.
485              '<TD ALIGN="left"><B>&nbsp;%P</B></TD>'
486     if $conf->exists('cust_pkg-display_times');
487   my $strip = time2str($format, $cust_pkg->get($field) );
488   $strip =~ s/ (\d)/$1/g;
489   $strip;
490 }
491
492  sub pkg_change_link    { pkg_popup_link('misc/change_pkg.cgi?dummy=value',
493                                            'Change&nbsp;package',
494                                            'Change',
495                                            @_
496                                         );
497                         }
498
499 sub pkg_suspend_link   { pkg_popup_link( 'misc/cancel_pkg.html?method=suspend',
500                                          'Suspend&nbsp;now',
501                                          'Suspend',
502                                          @_
503                                        );
504                        }
505
506 sub pkg_unsuspend_link { pkg_link('misc/unsusp_pkg',    'Unsuspend',           @_ ); }
507 sub pkg_expire_link    { pkg_link('misc/expire_pkg',    'Cancel&nbsp;later',   @_ ); }
508 sub pkg_dates_link     { pkg_link('edit/REAL_cust_pkg', 'Edit&nbsp;dates',     @_ ); }
509
510 sub pkg_cancel_link    { pkg_popup_link( 'misc/cancel_pkg.html?method=cancel',
511                                          'Cancel&nbsp;now',
512                                          'Cancel',
513                                          @_
514                                        );
515                        }
516 sub pkg_adjourn_link   { pkg_popup_link( 'misc/cancel_pkg.html?method=adjourn',
517                                          'Suspend&nbsp;later',
518                                          'Adjourn',
519                                          @_
520                                        );
521                        }
522
523 sub pkg_expire_link    { pkg_popup_link( 'misc/cancel_pkg.html?method=expire',
524                                          'Cancel&nbsp;later',
525                                          'Expire', #"Cancel package $num later"
526                                          @_
527                                        );
528                        }
529
530 sub svc_recharge_link  { svc_popup_link( 'misc/recharge_svc.html',
531                                          'Recharge',
532                                          'Recharge',
533                                          @_
534                                        );
535                        }
536
537 sub pkg_link {
538   my($action, $label, $cust_pkg) = @_;
539   return '' unless $cust_pkg;
540   qq!<a href="$p$action.cgi?!. $cust_pkg->pkgnum. qq!">$label</a>!;
541 }
542
543 sub pkg_popup_link {
544   my($action, $label, $actionlabel, $cust_pkg) = @_;
545   $action .= '&pkgnum='. $cust_pkg->pkgnum;
546   $actionlabel .= ' package '. $cust_pkg->pkgnum;
547   popup_link($action, $label, $actionlabel, 768);
548 }
549
550 sub svc_popup_link {
551   my($action, $label, $actionlabel, $cust_svc) = @_;
552   $action .= '?svcnum='. $cust_svc->svcnum;
553   $actionlabel .= ' service '. $cust_svc->svcnum;
554   popup_link($action, $label, $actionlabel, 392);
555 }
556
557 sub popup_link {
558   my($action, $label, $actionlabel, $width) = @_;
559   $width ||= 540;
560   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>!;
561 }
562
563 sub pkg_customize_link {
564   my $cust_pkg = shift or return '';
565   my $custnum = $cust_pkg->custnum;
566   qq!<A HREF="${p}edit/part_pkg.cgi?!.
567     "keywords=$custnum;".
568     "clone=". $cust_pkg->part_pkg->pkgpart. ';'.
569     "pkgnum=". $cust_pkg->pkgnum.
570     qq!">Customize</A>!;
571 }
572
573 </%once>