push out event triggered suspensions
[freeside.git] / httemplate / view / cust_main / packages.html
1 <A NAME="cust_pkg"><FONT SIZE="+2">Packages</FONT></A><BR>
2
3 % if ( $curuser->access_right('One-time charge') ) {
4
5 <SCRIPT TYPE="text/javascript">
6
7 function taxproductmagic(which) {
8   var str = '';
9   var elements = which.form.elements;
10   for (var i = 0; i<elements.length; i++) {
11     if (elements[i].name == 'taxproductnum'){
12       document.getElementById('taxproductnum').value = elements[i].value;
13       continue;
14     }
15     if (elements[i].name == 'taxproductnum_description'){
16       continue;
17     }
18     if (str.length){str += ';';}
19     str += elements[i].name + '=' + escape(elements[i].value);
20   }
21   document.getElementById('charge_storage').value = str;
22   cClick();
23   overlib( OLiframeContent('<% $p %>/browse/part_pkg_taxproduct.cgi?_type=select&id=taxproductnum&onclick=taxproductquickchargemagic&taxproductnum='+document.getElementById('taxproductnum').value, 1000, 400, 'tax_product_popup'), CAPTION, 'Select product', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK);
24 }
25
26 function taxproductquickchargemagic() {
27   var str = document.getElementById('charge_storage').value;
28   if (str.length){str += ';';}
29   str += 'magic=taxproductnum;taxproductnum=';
30   str += escape(document.getElementById('taxproductnum').value);
31   cClick();
32   overlib( OLiframeContent('<% $p %>/edit/quick-charge.html?'+str, 545, 336, 'One-time charge'), CAPTION, 'One-time charge', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK, BGCOLOR, '#333399', CGCOLOR, '#333399', CLOSETEXT, 'Close');
33
34 }
35
36 function taxoverridemagic(which) {
37   var str = '';
38   var elements = which.ownerDocument.QuickChargeForm.elements;
39   for (var i = 0; i<elements.length; i++) {
40     if (elements[i].name == 'tax_override'){
41       document.getElementById('tax_override').value = elements[i].value;
42       continue;
43     }
44     if (str.length){str += ';';}
45     str += elements[i].name + '=' + escape(elements[i].value);
46   }
47   document.getElementById('charge_storage').value = str;
48   cClick();
49   overlib( OLiframeContent('<% $p %>/edit/part_pkg_taxoverride.html?element_name=tax_override;onclick=taxoverridequickchargemagic;selected='+document.getElementById('tax_override').value, 1100, 600, 'tax_product_popup'), CAPTION, 'Edit product tax overrides', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK);
50 }
51
52 function taxoverridequickchargemagic() {
53   var str = document.getElementById('charge_storage').value;
54   if (str.length){str += ';';}
55   str += 'magic=taxoverride;tax_override=';
56   str += document.getElementById('tax_override').value;
57   cClick();
58   overlib( OLiframeContent('<% $p %>/edit/quick-charge.html?'+str, 545, 336, 'One-time charge'), CAPTION, 'One-time charge', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK, BGCOLOR, '#333399', CGCOLOR, '#333399', CLOSETEXT, 'Close');
59
60 }
61
62 </SCRIPT>
63 <FORM NAME='quickcharge'>
64   <INPUT NAME="taxproductnum"  ID="taxproductnum"  TYPE="hidden">
65   <INPUT NAME="tax_override"   ID="tax_override"   TYPE="hidden">
66   <INPUT NAME="charge_storage" ID="charge_storage" TYPE="hidden">
67   <INPUT NAME="taxproductnum_description"  ID="taxproductnum_description" TYPE="hidden">
68 </FORM>
69 % } 
70
71 % my $s = 0;
72 % if ( $curuser->access_right('Order customer package') ) { 
73   <% $s++ ? ' | ' : '' %>
74   <% order_pkg_link($cust_main) %>
75 % } 
76
77 % if ( $curuser->access_right('One-time charge')
78 %        && $conf->config('payby-default') ne 'HIDE'
79 %      ) {
80 %
81   <% $s++ ? ' | ' : '' %>
82   <% include('/elements/popup_link.html',
83      { 
84        'action'      => $p. 'edit/quick-charge.html?custnum='. $cust_main->custnum,
85        'label'       => 'One-time charge',
86        'actionlabel' => 'One-time charge',
87        'color'       => '#333399',
88        'width'       => 763,
89      })
90   %>
91 % } 
92
93 % if ( $curuser->access_right('Bulk change customer packages') ) { 
94   <% $s++ ? ' | ' : '' %>
95   <A HREF="<% $p %>edit/cust_pkg.cgi?<% $cust_main->custnum %>">Bulk order and cancel packages</A> (preserves services)
96 % } 
97
98
99 <BR><BR>
100 % if ( @$packages ) { 
101
102 Current packages
103 % } 
104 % if ( $cust_main->num_cancelled_pkgs ) {
105 %     if ( $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
106 %          || ( $conf->exists('hidecancelledpackages')
107 %               && ! $cgi->param('showcancelledpackages')
108 %             )
109 %        )
110 %     {
111 %       $cgi->param('showcancelledpackages', 1);
112 %
113
114   ( <a href="<% $cgi->self_url %>">show
115 %   } else {
116 %       $cgi->param('showcancelledpackages', 0);
117 %
118
119   ( <a href="<% $cgi->self_url %>">hide
120 %   } 
121
122  cancelled packages</a> )
123 % } 
124 % if ( @$packages ) { 
125
126
127 <% include('/elements/table-grid.html') %>
128 % my $bgcolor1 = '#eeeeee';
129 %   my $bgcolor2 = '#ffffff';
130 %   my $bgcolor = '';
131
132 <TR>
133   <TH CLASS="grid" BGCOLOR="#cccccc">Package</TH>
134   <TH CLASS="grid" BGCOLOR="#cccccc">Status</TH>
135   <TH CLASS="grid" BGCOLOR="#cccccc">Services</TH>
136 </TR>
137
138 %foreach my $cust_pkg (@$packages) {
139 %
140 %  my $part_pkg = $cust_pkg->part_pkg;
141 %
142 %  if ( $bgcolor eq $bgcolor1 ) {
143 %    $bgcolor = $bgcolor2;
144 %  } else {
145 %    $bgcolor = $bgcolor1;
146 %  }
147
148
149 <!--pkgnum: <% $cust_pkg->pkgnum %>-->
150 <TR>
151
152   <TD CLASS="inv" BGCOLOR="<% $bgcolor %>">
153     <TABLE CLASS="inv" BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="100%">
154       <TR>
155         <TD COLSPAN=2>
156           <A NAME="cust_pkg<% $cust_pkg->pkgnum %>"
157              ID  ="cust_pkg<% $cust_pkg->pkgnum %>"
158           ><% $curuser->option('show_pkgnum') ? $cust_pkg->pkgnum.': ' : '' %><% $part_pkg->pkg %></A>
159           - 
160           <% $part_pkg->comment %>
161         </TD>
162       </TR>
163
164 %   if ( $cust_pkg->quantity > 1 ) {
165       <TR>
166         <TD COLSPAN=2>
167           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Quantity: 
168           <B><% $cust_pkg->quantity %></B>
169         </TD>
170       </TR>
171 %   }
172
173       <TR>
174         <TD COLSPAN=2>
175
176           <FONT SIZE=-1>
177
178 % unless ( $cust_pkg->get('cancel') ) { 
179 %   my $br = 0;
180 %   if ( $curuser->access_right('Change customer package') ) { $br=1;
181
182             (&nbsp;<%pkg_change_link($cust_pkg)%>&nbsp;)
183 %   } 
184 %   if ( $curuser->access_right('Edit customer package dates') ) { $br=1;
185
186             (&nbsp;<%pkg_dates_link($cust_pkg)%>&nbsp;)
187 %   } 
188 %   if ( $curuser->access_right('Customize customer package') ) { $br=1;
189
190             (&nbsp;<%pkg_customize_link($cust_pkg,$cust_main->custnum)%>&nbsp;)
191 %   } 
192     <% $br ? '<BR>' : '' %>
193 % } 
194
195 % if ( $cust_pkg->num_cust_event
196 %      && (    $curuser->access_right('Billing event reports')
197 %           || $curuser->access_right('View customer billing events')
198 %         )
199 %    ) {
200     (&nbsp;<%pkg_event_link($cust_pkg)%>&nbsp;)
201 % }
202
203           </FONT>
204
205         </TD>
206       </TR>
207
208 %   my $editi = $curuser->access_right('Edit customer package invoice details');
209 %   my $editc = $curuser->access_right('Edit customer package comments');
210 %
211 %   if ( $cust_pkg->cust_pkg_detail('I') || $cust_pkg->cust_pkg_detail('C')
212 %        || $editi || $editc                                                ) {
213 %
214 %     my $editlink = $p. 'edit/cust_pkg_detail?pkgnum='. $cust_pkg->pkgnum.
215 %                    ';detailtype=';
216
217       <TR>
218
219 %       if ( $cust_pkg->cust_pkg_detail('I') ) { 
220           <TD VALIGN="top">
221             <% include('/elements/table-grid.html') %>
222               <TR>
223                 <TH BGCOLOR="#dddddd" STYLE="border-bottom: dashed 1px black; padding-bottom: 1px">
224                   <FONT SIZE="-1">
225                     Invoice details
226 %                   if ( $editi && ! $cust_pkg->get('cancel') ) {
227                       (<% include('/elements/popup_link.html', { 
228                                     'action'      => $editlink. 'I',
229                                     'label'       => 'edit',
230                                     'actionlabel' => 'Edit invoice details',
231                                     'color'       => '#333399',
232                                     'width'       => 763,
233                                  })
234                        %>)
235 %                   }
236                   </FONT>
237                 </TH>
238               </TR>
239 %             foreach my $cust_pkg_detail ( $cust_pkg->cust_pkg_detail('I') ) {
240                 <TR>
241                   <TD><FONT SIZE="-1">&nbsp;-&nbsp;<% $cust_pkg_detail->detail |h %></FONT></TD>
242                 </TR>
243 %             }
244             </TABLE>
245           </TD>
246 %       } else {
247           <TD>
248 %           if ( $editi && ! $cust_pkg->get('cancel') ) {
249               <FONT SIZE="-1">
250                 (&nbsp;<% include('/elements/popup_link.html', { 
251                                'action'      => $editlink. 'I',
252                                'label'       => 'Add&nbsp;invoice&nbsp;details',
253                                'actionlabel' => 'Add invoice details',
254                                'color'       => '#333399',
255                                'width'       => 763,
256                             })
257                   %>&nbsp;)
258               </FONT>
259 %           }
260           </TD>
261 %       }
262
263 %       if ( $cust_pkg->cust_pkg_detail('C') ) { 
264           <TD VALIGN="top">
265             <% include('/elements/table-grid.html') %>
266               <TR>
267                 <TH BGCOLOR="#dddddd" STYLE="border-bottom: dashed 1px black; padding-bottom: 1px">
268                   <FONT SIZE="-1">
269                     Comments
270 %                   if ( $editc ) {
271                       (<% include('/elements/popup_link.html', { 
272                                     'action'      => $editlink. 'C',
273                                     'label'       => 'edit',
274                                     'actionlabel' => 'Edit comments',
275                                     'color'       => '#333399',
276                                     'width'       => 763,
277                                  })
278                        %>)
279 %                   }
280                   </FONT>
281                 </TH>
282               </TR>
283 %             foreach my $cust_pkg_detail ( $cust_pkg->cust_pkg_detail('C') ) {
284                 <TR>
285                   <TD><FONT SIZE="-1">&nbsp;-&nbsp;<% $cust_pkg_detail->detail |h %></FONT></TD>
286                 </TR>
287 %             }
288             </TABLE>
289           </TD>
290 %       } else {
291           <TD>
292 %           if ( $editc ) {
293               <FONT SIZE="-1">
294                 (&nbsp;<% include('/elements/popup_link.html', { 
295                                'action'      => $editlink. 'C',
296                                'label'       => 'Add&nbsp;comments',
297                                'actionlabel' => 'Add comments',
298                                'color'       => '#333399',
299                                'width'       => 763,
300                             })
301                   %>&nbsp;)
302               </FONT>
303 %           }
304           </TD>
305 %       }
306
307       </TR>
308 %   }
309
310     </TABLE>
311
312   </TD>
313
314   <TD CLASS="inv" BGCOLOR="<% $bgcolor %>">
315     <TABLE CLASS="inv" BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="100%">
316 %
317 %  sub myfreq {
318 %    my $part_pkg = shift;
319 %    my $freq = $part_pkg->freq_pretty;
320 %    $freq =~ s/ /&nbsp;/g;
321 %    $freq;
322 %  }
323 %
324 %  #this should use cust_pkg->status and cust_pkg->statuscolor eventually
325 %  #my $colspan = $conf->exists('cust_pkg-display_times') ? 8 : 4;
326 %  #my $width = $conf->exists('cust_pkg-display_times') ? '38%' : '56%';
327 %
328 %  #false laziness w/edit/REAL_cust_pkg.cgi
329 %  my( $billed_or_prepaid, $last_bill_or_renewed, $next_bill_or_prepaid_until );
330 %  unless ( $part_pkg->is_prepaid ) {
331 %    $billed_or_prepaid = 'billed';
332 %    $last_bill_or_renewed = 'Last&nbsp;bill';
333 %    $next_bill_or_prepaid_until = 'Next&nbsp;bill';
334 %  } else {
335 %    $billed_or_prepaid = 'prepaid';
336 %    $last_bill_or_renewed = 'Renewed';
337 %    $next_bill_or_prepaid_until = 'Prepaid&nbsp;until';
338 %  }
339 %
340 %
341 % if ( $cust_pkg->get('cancel') ) { #status: cancelled
342 %   my $cpr = $cust_pkg->last_cust_pkg_reason('cancel');
343
344     <% pkg_status_row($cust_pkg, 'Cancelled', 'cancel', 'color'=>'FF0000', conf=>$conf ) %>
345
346     <% pkg_status_row_colspan(
347          ( $cpr ? $cpr->reasontext. ' by '. $cpr->otaker : '' ), '',
348          'align' => 'right', 'color' => 'ff0000', 'size' => '-2',
349        )
350     %>
351
352 %   unless ( $cust_pkg->get('setup') ) { 
353
354         <% pkg_status_row_colspan('Never billed') %>
355
356 %   } else { 
357
358        <% pkg_status_row( $cust_pkg, 'Setup', 'setup', conf=>$conf ) %>
359        <% pkg_status_row_changed( $cust_pkg, conf=>$conf ) %>
360        <% pkg_status_row_if( $cust_pkg, $last_bill_or_renewed, 'last_bill', conf=>$conf, curuser=>$curuser ) %>
361        <% pkg_status_row_if( $cust_pkg, 'Suspended', 'susp', conf=>$conf, curuser=>$curuser ) %>
362
363 %   } 
364 %
365 % } else { 
366 %
367 %   if ( $cust_pkg->get('susp') ) { #status: suspended
368 %     my $cpr = $cust_pkg->last_cust_pkg_reason('susp');
369
370     <% pkg_status_row( $cust_pkg, 'Suspended', 'susp', 'color'=>'FF9900', conf=>$conf ) %>
371
372     <% pkg_status_row_colspan(
373          ( $cpr ? $cpr->reasontext. ' by '. $cpr->otaker : '' ), '',
374          'align' => 'right', 'color' => 'FF9900', 'size' => '-2',
375        )
376     %>
377
378 %   unless ( $cust_pkg->get('setup') ) { 
379       <% pkg_status_row_colspan('Never billed') %>
380 %   } else { 
381       <% pkg_status_row($cust_pkg, 'Setup', 'setup', conf=>$conf ) %>
382 %   } 
383
384     <% pkg_status_row_changed( $cust_pkg, conf=>$conf ) %>
385     <% pkg_status_row_if( $cust_pkg, $last_bill_or_renewed, 'last_bill', conf=>$conf, curuser=>$curuser ) %>
386 %   # pkg_status_row($cust_pkg, 'Next bill', 'bill', conf=>$conf)
387     <% pkg_status_row_if( $cust_pkg, 'Expires', 'expire', conf=>$conf, curuser=>$curuser ) %>
388
389     <TR>
390       <TD COLSPAN=<%$colspan%>>
391         <FONT SIZE=-1>
392 %         if ( $curuser->access_right('Unsuspend customer package') ) { 
393             (&nbsp;<% pkg_unsuspend_link($cust_pkg) %>&nbsp;)
394 %         } 
395 %         if ( $curuser->access_right('Cancel customer package immediately') ) {
396             (&nbsp;<% pkg_cancel_link($cust_pkg) %>&nbsp;)
397 %         } 
398         </FONT>
399       </TD>
400     </TR>
401
402 %   } else { #status: active
403 %
404 %     unless ( $cust_pkg->get('setup') ) { #not setup
405 %
406 %       unless ( $part_pkg->freq ) { 
407
408           <% pkg_status_row_colspan('Not&nbsp;yet&nbsp;billed&nbsp;(one-time&nbsp;charge)') %>
409
410           <TR>
411             <TD COLSPAN=<%$colspan%>>
412               <FONT SIZE=-1>
413 %               if ( $curuser->access_right('Cancel customer package immediately') ) { 
414                   (&nbsp;<% pkg_cancel_link($cust_pkg) %>&nbsp;)
415 %               } 
416               </FONT>
417             </TD>
418           </TR>
419
420 %       } else { 
421
422          <% pkg_status_row_colspan("Not&nbsp;yet&nbsp;billed&nbsp;($billed_or_prepaid&nbsp;". myfreq($part_pkg). ')' ) %>
423
424 %       } 
425 %
426 %     } else { #setup
427 %
428 %       unless ( $part_pkg->freq ) { 
429
430           <% pkg_status_row_colspan('One-time&nbsp;charge') %>
431
432           <% pkg_status_row($cust_pkg, 'Billed', 'setup', conf=>$conf) %>
433
434 %       } else { 
435 %
436 %         if (scalar($cust_pkg->overlimit)) {
437
438             <% pkg_status_row_colspan(
439                  'Overlimit',
440                  $billed_or_prepaid. '&nbsp;'. myfreq($part_pkg),
441                  'color' => 'FFD000',
442                )
443             %>
444
445 %         } else {
446             <% pkg_status_row_colspan(
447                  'Active',
448                  $billed_or_prepaid. '&nbsp;'. myfreq($part_pkg),
449                  'color' => '00CC00',
450                )
451             %>
452 %         } 
453
454           <% pkg_status_row($cust_pkg, 'Setup', 'setup', conf=>$conf) %>
455
456 %       } 
457 %
458 %     } 
459 %   my $autosuspend = pkg_autosuspend_time( $cust_pkg );
460 %   $cust_pkg->set('autosuspend', $autosuspend) if $autosuspend;
461
462       <% pkg_status_row_changed( $cust_pkg, conf=>$conf ) %>
463       <% pkg_status_row_if( $cust_pkg, $last_bill_or_renewed, 'last_bill', conf=>$conf, curuser=>$curuser ) %>
464       <% pkg_status_row_if( $cust_pkg, $next_bill_or_prepaid_until, 'bill', conf=>$conf, curuser=>$curuser ) %>
465       <% pkg_status_row_if($cust_pkg, 'Will automatically suspend by', 'autosuspend', conf=>$conf) %>
466       <% pkg_status_row_if( $cust_pkg, 'Will suspend on', 'adjourn', conf=>$conf, curuser=>$curuser ) %>
467       <% pkg_status_row_if( $cust_pkg, 'Expires', 'expire', conf=>$conf, curuser=>$curuser ) %>
468
469 %     if ( $part_pkg->freq ) { 
470
471         <TR>
472           <TD COLSPAN=<%$colspan%>>
473             <FONT SIZE=-1>
474 %             if ( $curuser->access_right('Suspend customer package') ) { 
475                 (&nbsp;<% pkg_suspend_link($cust_pkg) %>&nbsp;)
476 %             } 
477 %             if ( $curuser->access_right('Suspend customer package later') ) { 
478                 (&nbsp;<% pkg_adjourn_link($cust_pkg) %>&nbsp;)
479 %             } 
480 %             if ( $curuser->access_right('Delay suspension events') ) { 
481                 (&nbsp;<% pkg_delay_link($cust_pkg) %>&nbsp;)
482 %             } 
483 %             if ( $curuser->access_right('Cancel customer package immediately') ) { 
484                 (&nbsp;<% pkg_cancel_link($cust_pkg) %>&nbsp;)
485 %             } 
486 %             if ( $curuser->access_right('Cancel customer package later') ) { 
487                 (&nbsp;<% pkg_expire_link($cust_pkg) %>&nbsp;)
488 %             } 
489
490             <FONT>
491           </TD>
492         </TR>
493 %     }
494 %
495 %   } 
496 % } 
497
498 </TABLE>
499 </TD>
500
501 <TD CLASS="inv" BGCOLOR="<% $bgcolor %>">
502   <TABLE CLASS="inv" BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="100%">
503
504 %  #foreach my $svcpart (sort {$a->{svcpart} <=> $b->{svcpart}} @{$pkg->{svcparts}}) {
505 %  foreach my $part_svc ( $cust_pkg->part_svc ) {
506
507 %    #foreach my $service (@{$svcpart->{services}}) {
508 %    foreach my $cust_svc ( @{ $part_svc->cust_pkg_svc } ) {
509
510       <TR>
511         <TD ALIGN="right" VALIGN="top"><% FS::UI::Web::svc_link($m, $part_svc, $cust_svc) %></TD>
512         <TD STYLE="padding-bottom:0px"><B><% FS::UI::Web::svc_label_link($m, $part_svc, $cust_svc) %></B></TD>
513         <TD><% FS::UI::Web::svc_export_links($m, $part_svc, $cust_svc) %></TD>
514       </TR>
515
516       <TR>
517         <TD ALIGN="right" COLSPAN="3" VALIGN="top" STYLE="padding-bottom:1px;padding-top:0px"><FONT SIZE="-2" COLOR="#FFD000">
518
519             <% $cust_svc->overlimit ? "Overlimit: ". time2str('%b %o %Y' . ($conf->exists('cust_pkg-display_times') ? ' %l:%M %P' : ''), $cust_svc->overlimit) : '' %>
520           </FONT></TD>
521       </TR>
522
523       <TR>
524         <TD ALIGN="right" VALIGN="top" STYLE="padding-bottom:5px;padding-top:0px"><FONT SIZE="-2">
525
526 %         if ( $curuser->access_right('Recharge customer service')
527 %              && $part_svc->svcdb eq 'svc_acct'
528 %              && (    $cust_svc->svc_x->seconds    ne ''
529 %                   || $cust_svc->svc_x->upbytes    ne ''
530 %                   || $cust_svc->svc_x->downbytes  ne ''
531 %                   || $cust_svc->svc_x->totalbytes ne ''
532 %                 )
533 %         ) { 
534             (&nbsp;<%svc_recharge_link($cust_svc)%>&nbsp;)
535 %         } 
536           </FONT></TD>
537
538           <TD ALIGN="right" VALIGN="top" STYLE="padding-bottom:5px;padding-top:0px"><FONT SIZE="-2">
539
540 %         if ( $curuser->access_right('Unprovision customer service') ) { 
541             (&nbsp;<%svc_unprovision_link($cust_svc)%>&nbsp;)
542 %         } 
543           </FONT></TD>
544         </TR>
545 %   } 
546
547 %   if (    ! $cust_pkg->get('cancel')
548 %        && $curuser->access_right('Provision customer service') 
549 %        && $part_svc->num_avail
550 %      ) {
551
552       <TR>
553         <TD COLSPAN=3 ALIGN="center" STYLE="padding-bottom:4px;padding-top:0px">
554           <B><% svc_provision_link($cust_pkg, $part_svc, $conf, $curuser) %></B>
555         </TD>
556       </TR>
557
558 %   } 
559
560 % } 
561
562 </TABLE>
563 </TD>
564 % } #end display packages
565 %
566
567
568 </TABLE>
569 % } else { 
570
571 <BR>
572 % } 
573 % if ( $cgi->param('fragment') =~ /^cust_pkg(\d+)$/ ) {
574   <SCRIPT>
575     // IE-specific hack.  other browsers listen to #fragments
576     // is this even working?  or is the #target redirection just working cause
577     // we set the URL params differently?
578     var el = document.getElementById( 'cust_pkg<% $1 %>' );
579     if ( el ) el.scrollIntoView(true);
580   </SCRIPT>
581 % }
582 <%init>
583
584 my( $cust_main ) = @_;
585 my $conf = new FS::Conf;
586
587 my $curuser = $FS::CurrentUser::CurrentUser;
588
589 my $packages = get_packages($cust_main, $conf);
590
591 my $colspan = $conf->exists('cust_pkg-display_times') ? 8 : 4;
592 my $width = $conf->exists('cust_pkg-display_times') ? '38%' : '56%';
593
594 sub pkg_status_row {
595   my( $cust_pkg, $title, $field, %opt ) = @_;
596
597   my $color = $opt{'color'};
598
599   my $html = qq(<TR><TD WIDTH="<%$width%>" ALIGN="right">);
600   $html   .= qq(<FONT COLOR="#$color"><B>) if length($color);
601   $html   .= qq($title&nbsp;);
602   $html   .= qq(</B></FONT>) if length($color);
603   $html   .= qq(</TD>);
604   $html   .= pkg_datestr($cust_pkg, $field, $opt{conf}).'</TR>';
605
606   $html;
607 }
608
609 sub pkg_status_row_if {
610   my( $cust_pkg, $title, $field, %opt ) = @_;
611   
612   $title = '<FONT SIZE=-1>(&nbsp;'. pkg_unadjourn_link($cust_pkg). '&nbsp;)&nbsp;</FONT>'. $title
613     if ( $field eq 'adjourn' &&
614          $opt{curuser}->access_right('Suspend customer package later')
615        );
616
617   $title = '<FONT SIZE=-1>(&nbsp;'. pkg_unexpire_link($cust_pkg). '&nbsp;)&nbsp;</FONT>'. $title
618     if ( $field eq 'expire' &&
619          $opt{curuser}->access_right('Cancel customer package later')
620        );
621
622   $cust_pkg->get($field) ? pkg_status_row($cust_pkg, $title, $field, %opt) : '';
623 }
624
625 sub pkg_status_row_changed {
626   my( $cust_pkg, %opt ) = @_;
627   return '' unless $cust_pkg->change_date;
628   my $html = pkg_status_row( $cust_pkg, 'Package&nbsp;changed', 'change_date', conf=>$opt{'conf'} );
629   my $old = $cust_pkg->old_cust_pkg;
630   if ( $old ) {
631     my $part_pkg = $old->part_pkg;
632     my $label = 'Changed from '. $cust_pkg->change_pkgnum. ': '.
633                 $part_pkg->pkg. ' - '. $part_pkg->comment;
634     $html .= pkg_status_row_colspan( $label, '', size=>'-1', align=>'right' );
635   }
636   $html;
637 }
638
639 sub pkg_status_row_colspan {
640   my($title, $addl, %opt) = @_;
641
642   my $align = $opt{'align'} ? 'ALIGN="'. $opt{'align'}.'"' : '';
643   my $color = $opt{'color'} ? 'COLOR="#'.$opt{'color'}.'"' : '';
644   my $size  = $opt{'size'}  ? 'SIZE="'.  $opt{'size'}. '"' : '';
645
646   my $html = qq(<TR><TD COLSPAN=$colspan $align>);
647   $html   .= qq(<FONT $color $size>) if length($color) || $size;
648   $html   .= qq(<B>) if $color && !$size;
649   $html   .= $title;
650   $html   .= qq(</B>) if $color && !$size;
651   $html   .= qq(</FONT>) if length($color) || $size;
652   $html   .= ",&nbsp;$addl" if length($addl);
653   $html   .= qq(</TD></TR>);
654
655   $html;
656
657 }
658
659 #subroutines
660
661 sub get_packages {
662   my $cust_main = shift or return undef;
663   my $conf = shift;
664   
665   my @packages = ();
666   my $method;
667   if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
668      || ( $conf->exists('hidecancelledpackages')
669            && ! $cgi->param('showcancelledpackages') )
670      )
671   {
672     $method = 'ncancelled_pkgs';
673   } else {
674     $method = 'all_pkgs';
675   }
676
677   [ $cust_main->$method() ];
678 }
679
680 sub svc_provision_link {
681   my ($cust_pkg, $part_svc, $conf, $curuser) = @_;
682   ( my $svc_nbsp = $part_svc->svc ) =~ s/\s+/&nbsp;/g;
683   my $num_avail = $part_svc->num_avail;
684   my $pkgnum_svcpart = "pkgnum=". $cust_pkg->pkgnum. ';'.
685                        "svcpart=". $part_svc->svcpart;
686   my $url;
687   if ( $part_svc->svcdb eq 'svc_external' #could be generalized
688        && $conf->exists('svc_external-skip_manual')
689   ) {
690     $url = "${p}edit/process/". $part_svc->svcdb. ".cgi?$pkgnum_svcpart";
691   } else {
692     $url = svc_url(
693                     'm'        => $m,
694                     'action'   => 'edit',
695                     'part_svc' => $part_svc, 
696                     'query'    => $pkgnum_svcpart,
697                   );
698     #$url = "${p}edit/$svcpart->{svcdb}.cgi?$pkgnum_svcpart";
699   }
700
701   my $link = qq!<A CLASS="provision" HREF="$url">!.
702              "Provision&nbsp;$svc_nbsp&nbsp;($num_avail)</A>";
703   if ( $conf->exists('legacy_link')
704        && $curuser->access_right('View/link unlinked services')
705      )
706   {
707     $link .= '<BR>'.
708              qq!<A CLASS="provision" HREF="${p}misc/link.cgi?!.
709              qq!$pkgnum_svcpart">!.
710             "Link&nbsp;to&nbsp;legacy&nbsp;$svc_nbsp&nbsp;($num_avail)</A>";
711   }
712   $link;
713 }
714
715 sub svc_unprovision_link {
716   my $cust_svc = shift or return '';
717   qq!<A HREF="javascript:areyousure('${p}misc/unprovision.cgi?!. $cust_svc->svcnum.
718   qq!', 'Permanently unprovision and delete this service?')">Unprovision</A>!;
719 }
720
721 sub pkg_datestr {
722   my($cust_pkg, $field, $conf) = @_ or return '';
723   return '&nbsp;' unless $cust_pkg->get($field);
724   my $format = '<TD align="left"><B>%b</B></TD>'.
725                '<TD align="right"><B>&nbsp;%o,</B></TD>'.
726                '<TD align="right"><B>&nbsp;%Y</B></TD>';
727   #$format .= '&nbsp;<FONT SIZE=-3>%l:%M:%S%P&nbsp;%z</FONT>'
728   $format .= '<TD ALIGN="right"><B>&nbsp;%l</TD>'.
729              '<TD ALIGN="center"><B>:</B></TD>'.
730              '<TD ALIGN="left"><B>%M</B></TD>'.
731              '<TD ALIGN="left"><B>&nbsp;%P</B></TD>'
732     if $conf->exists('cust_pkg-display_times');
733   my $strip = time2str($format, $cust_pkg->get($field) );
734   $strip =~ s/ (\d)/$1/g;
735   $strip;
736 }
737
738 sub pkg_change_link { include( '/elements/popup_link-cust_pkg.html',
739                                { 'action'      => $p. 'misc/change_pkg.cgi?dummy=value',
740                                  'label'       => 'Change&nbsp;package',
741                                  'actionlabel' => 'Change',
742                                  'cust_pkg'    => shift,
743                                }
744                              )
745                      }
746
747 sub pkg_suspend_link { include( '/elements/popup_link-cust_pkg.html',
748                                 { 'action'      => $p. 'misc/cancel_pkg.html?method=suspend',
749                                   'label'       => 'Suspend&nbsp;now',
750                                   'actionlabel' => 'Suspend',
751                                   'color'       => '#FF9900',
752                                   'cust_pkg'    => shift,
753                                 }
754                               )
755                      }
756
757
758 sub pkg_adjourn_link { include( '/elements/popup_link-cust_pkg.html',
759                                 { 'action'      => $p. 'misc/cancel_pkg.html?method=adjourn',
760                                   'label'       => 'Suspend&nbsp;later',
761                                   'actionlabel' => 'Adjourn',
762                                   'color'       => '#CC6600',
763                                   'cust_pkg'    => shift,
764                                 }
765                               )
766                      }
767
768 sub pkg_delay_link   { include( '/elements/popup_link-cust_pkg.html',
769                                 { 'action'      => $p. 'misc/delay_susp_pkg.html',
770                                   'label'       => 'Delay&nbsp;suspend',
771                                   'actionlabel' => 'Delay suspend for',
772                                   'cust_pkg'    => shift,
773                                 }
774                               )
775                      }
776
777 sub pkg_unsuspend_link { pkg_link('misc/unsusp_pkg',    'Unsuspend',           @_ ); }
778 sub pkg_dates_link     { pkg_link('edit/REAL_cust_pkg', 'Edit&nbsp;dates',     @_ ); }
779 sub pkg_unadjourn_link { pkg_link('misc/unadjourn_pkg', 'Abort',               @_ ); }
780 sub pkg_unexpire_link  { pkg_link('misc/unexpire_pkg',  'Abort',               @_ ); }
781
782 sub pkg_cancel_link { include( '/elements/popup_link-cust_pkg.html',
783                                { 'action'      => $p. 'misc/cancel_pkg.html?method=cancel',
784                                  'label'       => 'Cancel&nbsp;now',
785                                  'actionlabel' => 'Cancel',
786                                  'color'       => '#ff0000',
787                                  'cust_pkg'    => shift,
788                                }
789                              )
790                     }
791
792 sub pkg_expire_link { include( '/elements/popup_link-cust_pkg.html',
793                                { 'action'      => $p. 'misc/cancel_pkg.html?method=expire',
794                                  'label'       => 'Cancel&nbsp;later',
795                                  'actionlabel' => 'Expire', #"Cancel package $num later"
796                                  'color'       => '#CC0000',
797                                  'cust_pkg'    => shift,
798                                }
799                              )
800                     }
801
802 sub svc_recharge_link { include( '/elements/popup_link-cust_svc.html',
803                                  { 'action'      => $p. 'misc/recharge_svc.html',
804                                    'label'       => 'Recharge',
805                                    'actionlabel' => 'Recharge',
806                                    'color'       => '#333399',
807                                    'cust_svc'    => shift,
808                                  }
809                                )
810                       }
811
812 sub order_pkg_link { include( '/elements/popup_link-cust_main.html',
813                               { 'action'      => $p. 'misc/order_pkg.html',
814                                 'label'       => 'Order&nbsp;new&nbsp;package',
815                                 'actionlabel' => 'Order new package',
816                                 'color'       => '#333399',
817                                 'cust_main'   => shift,
818                                 'closetext'   => 'Close',
819                               }
820                             )
821                    }
822
823 sub pkg_event_link {
824   my($cust_pkg) = @_;
825   qq!<a href="${p}search/cust_event.html?pkgnum=!. $cust_pkg->pkgnum. qq!">!.
826   'View package events'.
827   '</a>';
828 }
829
830 sub pkg_link {
831   my($action, $label, $cust_pkg) = @_;
832   return '' unless $cust_pkg;
833   qq!<a href="$p$action.cgi?!. $cust_pkg->pkgnum. qq!">$label</a>!;
834 }
835
836 sub pkg_customize_link {
837   my $cust_pkg = shift or return '';
838   my $custnum = $cust_pkg->custnum;
839   qq!<A HREF="${p}edit/part_pkg.cgi?!.
840     "clone=". $cust_pkg->part_pkg->pkgpart. ';'.
841     "pkgnum=". $cust_pkg->pkgnum.
842     qq!">Customize</A>!;
843 }
844
845 sub pkg_autosuspend_time {
846   my $cust_pkg = shift or return '';
847   my $days = 7;
848   my $time = time;
849   my $pending_suspend = 0;
850   while ( $days > 0 &&
851           scalar(
852             grep { $_->part_event->action eq 'suspend' }
853             @{$cust_pkg->cust_main->due_cust_event( time => $time + 86400*$days,
854                                                     testonly => 1,
855                                                   ) }
856           )
857         )
858   {
859     $pending_suspend = 1;
860     $days--;
861   }
862
863   $pending_suspend ? time + ($days + 1) * 86400 : '';
864
865 }
866 </%init>