minor UI fix for future package change + advance billing, #37870
[freeside.git] / httemplate / view / cust_main / packages / status.html
1 <TD CLASS="inv" BGCOLOR="<% $bgcolor %>" VALIGN="top">
2   <TABLE CLASS="inv" BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="100%">
3
4 %#this should use cust_pkg->status and cust_pkg->statuscolor eventually
5
6 % if ( $supplemental ) {
7     <% pkg_status_row_colspan($cust_pkg, emt('Supplemental'), '', 'color' => '7777FF', %opt) %>
8 % } elsif ( $cust_pkg->order_date ) {
9     <% pkg_status_row($cust_pkg, emt('Ordered'), 'order_date', %opt ) %>
10 % }
11
12 % if ( $cust_pkg->get('cancel') ) { #status: cancelled
13 %   my $cpr = $cust_pkg->last_cust_pkg_reason('cancel');
14
15     <% pkg_status_row($cust_pkg, emt('Cancelled'), 'cancel', 'color'=>'FF0000', %opt ) %>
16
17     <% pkg_status_row_detached($cust_pkg, %opt) %>
18
19     <% pkg_reason_row($cust_pkg, $cpr, color => 'ff0000', %opt) %>
20
21 %   unless ( $cust_pkg->get('setup') ) { 
22
23         <% pkg_status_row_colspan( $cust_pkg, emt('Never billed'), '', %opt, ) %>
24
25 %   } else { 
26
27        <% pkg_status_row( $cust_pkg, emt('Setup'), 'setup', %opt ) %>
28        <% pkg_status_row_changed( $cust_pkg, %opt ) %>
29        <% pkg_status_row_if( $cust_pkg, $last_bill_or_renewed, 'last_bill', %opt, curuser=>$curuser ) %>
30        <% pkg_status_row_if( $cust_pkg, emt('Suspended'), 'susp', %opt, curuser=>$curuser ) %>
31
32 %   } 
33 %
34 %   if ( $part_pkg->freq && !$supplemental && !$cust_pkg->change_custnum ) { #?
35
36       <TR>
37         <TD COLSPAN=<%$opt{colspan}%>>
38           <FONT SIZE=-1>
39 %           if ( $curuser->access_right('Un-cancel customer package') && ! $opt{no_links} ) { 
40               (&nbsp;<% pkg_uncancel_link($cust_pkg) %>&nbsp;)
41 %           } 
42           <FONT>
43         </TD>
44       </TR>
45 %   }
46 %
47 % } else { # not canceled
48 %
49 %   if ( $cust_pkg->get('susp') ) { #suspended or on hold
50 %
51 %     #if ( $cust_pkg->order_date eq $cust_pkg->get('susp') ) # inconsistent with FS::cust_pkg::status
52 %     if ( ! $cust_pkg->setup ) { #status: on hold
53
54         <% pkg_status_row( $cust_pkg, emt('On Hold'), '', 'color'=>'7E0079', %opt ) %>
55
56 %     } else { #status: suspended
57 %       my ($cpr,$susplabel);
58 %       if ($cust_pkg->is_status_delay_cancel(%opt)) {
59 %         $cpr = $cust_pkg->last_cust_pkg_reason('expire');
60 %         $susplabel = 'Suspended (Cancelled)';
61 %       } else {
62 %         $cpr = $cust_pkg->last_cust_pkg_reason('susp');
63 %         $susplabel = 'Suspended';
64 %       }
65         <% pkg_status_row( $cust_pkg, emt($susplabel), 'susp', 'color'=>'FF9900', %opt ) %>
66         <% pkg_reason_row( $cust_pkg, $cpr, 'color' => 'FF9900', %opt ) %>
67
68 %     }
69
70     <% pkg_status_row_noauto( $cust_pkg, %opt ) %>
71
72     <% pkg_status_row_separate_bill( $cust_pkg, %opt ) %>
73
74     <% pkg_status_row_discount( $cust_pkg, %opt ) %>
75
76 %   if ( $cust_pkg->order_date ne $cust_pkg->get('susp') ) { # not on hold
77 %     if ( $cust_pkg->get('setup') ) {
78         <% pkg_status_row($cust_pkg, emt('Setup'), 'setup', %opt ) %>
79 %     } else {
80         <% pkg_status_row_colspan( $cust_pkg, emt('Never billed'), '', %opt ) %>
81 %     }
82 %   }
83
84     <% pkg_status_row_if($cust_pkg, emt('Un-cancelled'), 'uncancel', %opt ) %>
85
86     <% pkg_status_row_changed( $cust_pkg, %opt ) %>
87     <% pkg_status_row_if( $cust_pkg, $last_bill_or_renewed, 'last_bill', %opt, curuser=>$curuser ) %>
88 %   if ( $cust_pkg->option('suspend_bill', 1)
89 %        || ( $part_pkg->option('suspend_bill', 1)
90 %               && ! $cust_pkg->option('no_suspend_bill',1)
91 %           )
92 %      )
93 %   {
94       <% pkg_status_row_if( $cust_pkg, emt('Next bill'), 'bill', %opt, curuser=>$curuser ) %>
95 %   }
96     <% pkg_status_row_if( $cust_pkg, emt('Will resume'), 'resume', %opt, curuser=>$curuser ) %>
97     <% pkg_status_row_expire($cust_pkg, %opt, curuser=>$curuser) %>
98     <% pkg_status_row_if( $cust_pkg, emt('Contract ends'), 'contract_end', %opt ) %>
99
100 %   # Status changes for suspended packages: can unsuspend, future-unsuspend,
101 %   # or future-change. If this package is a future change or is supplemental
102 %   # disable them all.
103 %   if ( !$supplemental && ! $opt{no_links} && !$change_from ) {
104       <TR>
105         <TD COLSPAN=<%$opt{colspan}%>>
106           <FONT SIZE=-1>
107 %           if ( $cust_pkg->change_to_pkgnum ) {
108 %               # then you can modify the package change
109 %               if ( $curuser->access_right('Change customer package') ) {
110                 (&nbsp;<% pkg_change_now_link($cust_pkg) %>&nbsp;)
111                 (&nbsp;<% pkg_change_later_link($cust_pkg) %>&nbsp;)
112                 (&nbsp;<% pkg_unchange_link($cust_pkg) %>&nbsp;)
113                 <BR>
114 %               }
115 %           }
116 %           if ( $curuser->access_right('Unsuspend customer package') ) { 
117 %             if ( $cust_pkg->order_date eq $cust_pkg->get('susp') ) { #on hold
118                 (&nbsp;<% pkg_unhold_link($cust_pkg) %>&nbsp;)
119 %             } else {
120                 (&nbsp;<% pkg_unsuspend_link($cust_pkg) %>&nbsp;)
121                 (&nbsp;<% pkg_resume_link($cust_pkg) %>&nbsp;)
122 %             }
123 %           }
124 %           if ( !$cust_pkg->change_to_pkgnum and
125 %                $curuser->access_right('Cancel customer package immediately')
126 %           ) {
127               (&nbsp;<% pkg_cancel_link($cust_pkg) %>&nbsp;)
128 %           } 
129           </FONT>
130         </TD>
131       </TR>
132 %     }
133 %
134 %   } else { #status: active
135 %
136 %     if ( $change_from ) { # future change
137 %
138           <% pkg_status_row_colspan( $cust_pkg, emt('Waiting for package change'), '', %opt ) %>
139           <% pkg_status_row_if( $cust_pkg,
140                              emt('Will be activated on'),
141                              'start_date',
142                              %opt ) %>
143 %
144 %     } elsif ( ! $cust_pkg->get('setup') ) { # not setup
145 %
146 %       unless ( $part_pkg->freq ) { # one-time charge
147
148           <% pkg_status_row_colspan( $cust_pkg, emt('Not yet billed (one-time charge)'), '', %opt ) %>
149
150           <% pkg_status_row_noauto( $cust_pkg, %opt ) %>
151
152           <% pkg_status_row_separate_bill( $cust_pkg, %opt ) %>
153
154           <% pkg_status_row_discount( $cust_pkg, %opt ) %>
155
156           <% pkg_status_row_if(
157                $cust_pkg,
158                ( $part_pkg->freq ? emt('Start billing') : emt('Bill on') ),
159                'start_date',
160                %opt
161              )
162           %>
163
164           <% pkg_status_row_if($cust_pkg, emt('Un-cancelled'), 'uncancel', %opt ) %>
165
166 %         if ( !$supplemental && ! $opt{no_links} ) {
167           <TR>
168             <TD COLSPAN=<%$opt{colspan}%>>
169               <FONT SIZE=-1>
170 %               if ( $curuser->access_right('Cancel customer package immediately') ) { 
171                   (&nbsp;<% pkg_cancel_link($cust_pkg) %>&nbsp;)
172 %               } 
173               </FONT>
174             </TD>
175           </TR>
176 %         }
177
178 %       } else { 
179
180           <% pkg_status_row_colspan($cust_pkg, emt("Not yet billed ($billed_or_prepaid [_1])", myfreq($part_pkg) ), '', %opt ) %>
181
182           <% pkg_status_row_noauto( $cust_pkg, %opt ) %>
183
184           <% pkg_status_row_separate_bill( $cust_pkg, %opt ) %>
185
186           <% pkg_status_row_discount( $cust_pkg, %opt ) %>
187
188           <% pkg_status_row_if($cust_pkg, emt('Start billing'), 'start_date', %opt) %>
189           <% pkg_status_row_if($cust_pkg, emt('Un-cancelled'), 'uncancel', %opt ) %>
190
191 %       } 
192 %
193 %     } else { #setup
194 %
195 %       unless ( $part_pkg->freq ) { 
196
197           <% pkg_status_row_colspan($cust_pkg, emt('One-time charge'), '', %opt ) %>
198
199           <% pkg_status_row($cust_pkg, emt('Billed'), 'setup', %opt) %>
200
201           <% pkg_status_row_noauto( $cust_pkg, %opt ) %>
202
203           <% pkg_status_row_separate_bill( $cust_pkg, %opt ) %>
204
205           <% pkg_status_row_discount( $cust_pkg, %opt ) %>
206
207           <% pkg_status_row_if($cust_pkg, emt('Un-cancelled'), 'uncancel', %opt ) %>
208
209 %       } else { # recurring package
210 %
211 %         my $num_cust_svc = $cust_pkg->num_cust_svc;
212 %         my $summarize = $opt{'cust_pkg-large_pkg_size'} > 0
213 %                           && $opt{'cust_pkg-large_pkg_size'} <= $num_cust_svc;
214 %
215 %         #overlimit process is expensive with many services, so skip w/summary
216 %         if ( !$summarize && scalar($cust_pkg->overlimit) ) {
217
218             <% pkg_status_row_colspan( $cust_pkg,
219                  emt('Overlimit'),
220                  $billed_or_prepaid. '&nbsp;'. myfreq($part_pkg),
221                  'color'=>'FFD000',
222                  %opt
223                )
224             %>
225
226 %         } else {
227             <% pkg_status_row_colspan( $cust_pkg,
228                  emt('Active'),
229                  $billed_or_prepaid. '&nbsp;'. myfreq($part_pkg),
230                  'color'=>'00CC00',
231                  %opt
232                )
233             %>
234 %         } 
235
236           <% pkg_status_row_noauto( $cust_pkg, %opt ) %>
237
238           <% pkg_status_row_separate_bill( $cust_pkg, %opt ) %>
239
240           <% pkg_status_row_discount( $cust_pkg, %opt ) %>
241
242           <% pkg_status_row($cust_pkg, emt('Setup'), 'setup', %opt) %>
243
244           <% pkg_status_row_if($cust_pkg, emt('Un-cancelled'), 'uncancel', %opt ) %>
245
246 %       } 
247 %
248 %     }
249 %
250 %     if ( $opt{'cust_pkg-show_autosuspend'} ) {
251 %       my $autosuspend = pkg_autosuspend_time( $cust_pkg );
252 %       $cust_pkg->set('autosuspend', $autosuspend) if $autosuspend;
253 %     }
254
255       <% pkg_status_row_changed( $cust_pkg, %opt ) %>
256       <% pkg_status_row_if( $cust_pkg, $last_bill_or_renewed, 'last_bill', %opt, curuser=>$curuser ) %>
257       <% pkg_status_row_if( $cust_pkg, $next_bill_or_prepaid_until, 'bill', %opt, curuser=>$curuser ) %>
258       <% pkg_status_row_if($cust_pkg, emt('Will automatically suspend by'), 'autosuspend', %opt) %>
259       <% pkg_status_row_if($cust_pkg, emt('Automatic suspension delayed until'), 'dundate', %opt) %>
260       <% pkg_status_row_if( $cust_pkg, emt('Will suspend on'), 'adjourn', %opt, curuser=>$curuser ) %>
261       <% pkg_status_row_if( $cust_pkg, emt('Will resume on'), 'resume', %opt, curuser=>$curuser ) %>
262       <% pkg_status_row_expire($cust_pkg, %opt, curuser=>$curuser) %>
263       <% pkg_status_row_if( $cust_pkg, emt('Contract ends'), 'contract_end', %opt ) %>
264
265 %     # Status changes for active recurring packages. If it has a future
266 %     # package change scheduled, let that be modified. If it's supplemental,
267 %     # then that's the only allowed action. Otherwise allow suspend, future
268 %     # suspend, do-not-suspend, and immediate and future cancel.
269 %     if ( $part_pkg->freq and ! $opt{no_links} ) { 
270
271         <TR>
272           <TD COLSPAN=<%$opt{colspan}%>>
273             <FONT SIZE=-1>
274 % # action links
275 %           if ( $change_from ) {
276 %               # nothing
277 %           } elsif ( $cust_pkg->change_to_pkgnum ) {
278 %               # then you can modify the package change
279 %               if ( $curuser->access_right('Change customer package') ) {
280                 (&nbsp;<% pkg_change_now_link($cust_pkg) %>&nbsp;)
281                 (&nbsp;<% pkg_change_later_link($cust_pkg) %>&nbsp;)
282                 (&nbsp;<% pkg_unchange_link($cust_pkg) %>&nbsp;)
283                 <BR>
284 %               }
285 %           }
286
287 %           if ( !$supplemental and !$change_from ) {
288 %           # suspension actions--always available for a package that's
289 %           # independently active
290 %             if ( $curuser->access_right('Suspend customer package') ) {
291                 (&nbsp;<% pkg_suspend_link($cust_pkg) %>&nbsp;)
292 %             }
293 %             if ( $curuser->access_right('Suspend customer package later') ) {
294                 (&nbsp;<% pkg_adjourn_link($cust_pkg) %>&nbsp;)
295 %             }
296 %             if ( $curuser->access_right('Delay suspension events') ) { 
297                 (&nbsp;<% pkg_delay_link($cust_pkg) %>&nbsp;)
298 %             }
299 %
300 %             if ( $change_from or $cust_pkg->change_to_pkgnum ) {
301 %                 # you can't cancel the package while in this state
302 %             } else { # the normal case: links to cancel the package
303                 <BR>
304 %               if ( $curuser->access_right('Cancel customer package immediately') ) {
305                   (&nbsp;<% pkg_cancel_link($cust_pkg) %>&nbsp;)
306 %               }
307 %               if ( $curuser->access_right('Cancel customer package later') ) {
308                   (&nbsp;<% pkg_expire_link($cust_pkg) %>&nbsp;)
309 %               }
310 %             }
311 %           }
312
313             <FONT>
314           </TD>
315         </TR>
316 %     }
317 %
318 %   } 
319 % } 
320
321   </TABLE>
322 </TD>
323 <%init>
324
325 my %opt = @_;
326
327 my $bgcolor  = $opt{'bgcolor'};
328 my $cust_pkg = $opt{'cust_pkg'};
329 my $part_pkg = $opt{'part_pkg'};
330 my $curuser  = $FS::CurrentUser::CurrentUser;
331 my $width    = $opt{'cust_pkg-display_times'} ? '38%' : '56%';
332 my $supplemental = $opt{'supplemental'};
333 my $change_from  = $opt{'change_from'};
334
335 $opt{colspan}  = $opt{'cust_pkg-display_times'} ? 8 : 4;
336
337 #false laziness w/edit/REAL_cust_pkg.cgi
338 my( $billed_or_prepaid, $last_bill_or_renewed, $next_bill_or_prepaid_until );
339 unless ( $part_pkg->is_prepaid ) {
340   $billed_or_prepaid = 'billed';
341   $last_bill_or_renewed = 'Last&nbsp;bill';
342   $next_bill_or_prepaid_until = 'Next&nbsp;bill';
343 } else {
344   $billed_or_prepaid = 'prepaid';
345   $last_bill_or_renewed = 'Renewed';
346   $next_bill_or_prepaid_until = 'Prepaid&nbsp;until';
347 }
348
349 #subroutines
350
351 sub myfreq {
352   my $part_pkg = shift;
353   my $freq = $part_pkg->freq_pretty;
354   #$freq =~ s/ /&nbsp;/g;
355   $freq;
356 }
357
358 #false laziness w/package.html
359 sub pkg_link {
360   my($action, $label, $cust_pkg) = @_;
361   return '' unless $cust_pkg;
362   qq!<a href="$p$action.cgi?!. $cust_pkg->pkgnum. qq!">$label</a>!;
363 }
364
365 sub pkg_status_row {
366   my( $cust_pkg, $title, $field, %opt ) = @_;
367
368   if ( $field and $cust_pkg->main_pkgnum ) {
369     # for supplemental packages, we mostly only show these if they're 
370     # different from the main package
371     my $main_pkg = $cust_pkg-> main_pkg;
372     if (    $main_pkg->get($field) ne $cust_pkg->get($field)
373         # with some exceptions
374         or  $field eq 'bill'
375         or  $field eq 'last_bill'
376         or  $field eq 'setup'
377         or  $field eq 'susp'
378         or  $field eq 'cancel'
379       ) {
380       # handle it normally
381     } else {
382       return '';
383     }
384   }
385
386   my $color = $opt{'color'};
387
388   my $html = qq(<TR><TD WIDTH="$width" ALIGN="right">);
389   $html   .= qq(<FONT COLOR="#$color"><B>) if length($color);
390   $html   .= qq($title&nbsp;);
391   $html   .= qq(</B></FONT>) if length($color);
392
393   if ( $opt{'pkg_balances'} && ! $cust_pkg->{_printed_balance}++ ) { #kludge
394     $html .= ' (Balance:&nbsp;<B>'. $opt{'money_char'}.
395              $cust_pkg->cust_main->balance_pkgnum($cust_pkg->pkgnum).
396              '</B>)';
397   }
398
399   $html   .= qq(</TD>);
400   $html   .= pkg_datestr($cust_pkg, $field, %opt). '</TR>';
401
402   $html;
403 }
404
405 sub pkg_status_row_if {
406   my( $cust_pkg, $title, $field, %opt ) = @_;
407   
408   $title = '<FONT SIZE=-1>(&nbsp;'. pkg_unadjourn_link($cust_pkg). '&nbsp;)&nbsp;</FONT>'. $title
409     if ( $field eq 'adjourn' &&
410          $opt{curuser}->access_right('Suspend customer package later')
411        );
412
413   $cust_pkg->get($field) ? pkg_status_row($cust_pkg, $title, $field, %opt) : '';
414 }
415
416 sub pkg_status_row_expire {
417   my $cust_pkg = shift;
418   my %opt = @_;
419   return unless $cust_pkg->get('expire');
420
421   my $title;
422
423   if ( $cust_pkg->get('change_to_pkg') ) {
424     if ( $cust_pkg->change_to_pkg->pkgpart != $cust_pkg->pkgpart ) {
425       $title = mt('Will change to <b>[_1]</b> on',
426                  $cust_pkg->change_to_pkg->part_pkg->pkg);
427     } elsif ( $cust_pkg->change_to_pkg->locationnum != $cust_pkg->locationnum )
428     {
429       $title = mt('Will <b>change location</b> on');
430     } elsif (( $cust_pkg->change_to_pkg->quantity != $cust_pkg->quantity ) ||
431              ( $cust_pkg->change_to_pkg->contract_end != $cust_pkg->contract_end ))
432     {
433       $title = mt('Will change on');
434     } else {
435       # FS::cust_pkg->change_later should have prevented this, but 
436       # just so that we can display _something_
437       $title = '<font color="#ff0000">Unknown package change</font>';
438     }
439
440   } else {
441
442     $title = emt('Expires');
443     if ( $opt{curuser}->access_right('Cancel customer package later')) {
444       $title = '<FONT SIZE=-1>(&nbsp;'. pkg_unexpire_link($cust_pkg). '&nbsp;)&nbsp;</FONT>'. $title;
445     }
446
447   }
448
449   pkg_status_row( $cust_pkg, $title, 'expire', %opt );
450 }
451
452 sub pkg_status_row_changed {
453   my( $cust_pkg, %opt ) = @_;
454
455   return '' unless $cust_pkg->change_date;
456
457   my $html =
458     pkg_status_row( $cust_pkg, emt('Package changed'), 'change_date', %opt );
459
460   my $old = $cust_pkg->old_cust_pkg;
461   if ( $old ) {
462     my $part_pkg = $old->part_pkg;
463     $html .= pkg_status_row_colspan(
464       $cust_pkg, 
465 #      emt("Changed from [_1]: [_2]",
466 #             $cust_pkg->change_pkgnum,
467 #             $part_pkg->pkg_comment(cust_pkg=>$old, nopartpkg=>1)
468 #         ),
469       '',
470       '',
471       'size'    => '-1',
472       'align'   => 'right',
473     );
474   }
475
476   $html;
477 }
478
479 sub pkg_status_row_detached {
480   my( $cust_pkg, %opt ) = @_;
481
482   return '' unless $cust_pkg->change_custnum;
483
484   my $html = '';
485
486   my $cust_main = $cust_pkg->change_cust_main;
487   if ( $cust_main ) {
488
489     my $cust_link = '<A HREF="cust_main.cgi?'.  $cust_pkg->change_custnum. '">'.
490                       encode_entities( $cust_main->name ).
491                     '</A>';
492
493     my $what = $opt{'pkg_attached'} ? 'Attached' : 'Detached';
494
495     $html .= pkg_status_row_colspan( $cust_pkg, 
496                                      emt("$what to customer #[_1]: ",
497                                             $cust_pkg->change_custnum
498                                         ).
499                                        $cust_link,
500                                      '',
501                                      'size'    => '-1',
502                                      'align'   => 'right',
503                                      'colspan' => 4,
504                                    );
505   }
506
507   $html;
508 }
509
510 sub pkg_status_row_noauto {
511   my( $cust_pkg, %opt ) = @_;
512
513   return '' unless ( $cust_pkg->no_auto || $opt{'part_pkg'}->no_auto )
514                 && $opt{'has_cust_payby_auto'};
515
516   pkg_status_row_colspan( $cust_pkg, emt("No automatic charge"), '');
517 }
518
519 sub pkg_status_row_separate_bill {
520   my $cust_pkg = shift;
521   return '' unless $cust_pkg->separate_bill;
522   pkg_status_row_colspan( $cust_pkg, emt("Invoiced separately") );
523 }
524
525 sub pkg_status_row_discount {
526   my( $cust_pkg, %opt ) = @_;
527
528   my $html;
529
530   if ( $cust_pkg->waive_setup ) {
531     my $label = '<SPAN STYLE="font-size: small;font-weight: bold">' .
532                  emt('Setup fee waived') .
533                  '</SPAN>';
534     $html .= pkg_status_row_colspan( $cust_pkg, $label, '', %opt );
535   }
536
537   foreach my $cust_pkg_discount (@{ $cust_pkg->{_cust_pkg_discount_active} }) {
538
539     my $discount = $cust_pkg_discount->discount;
540
541     my $label = '<SPAN STYLE="font-size: small"><B>';
542     if ( $cust_pkg_discount->setuprecur eq 'setup' ) {
543       $label .= emt('Setup Discount');
544     } else {
545       $label .= emt('Recurring Discount');
546     }
547     $label .= '</B>: '. $discount->description;
548     if ( $discount->months > 0 and $cust_pkg_discount->months_used > 0 ) {
549       my $remaining = $discount->months - $cust_pkg_discount->months_used;
550       $remaining = sprintf('%.2f', $remaining) if $remaining =~ /\./;
551       $label .= <br> . emt(" ([quant,_1,month,months] remaining)",$remaining);
552     }
553     $label .= '</SPAN>';
554
555     #$label .= ' <FONT SIZE="-1">('.
556     #            '<A HREF="../misc/delete-cust_pkg_discount.html?'.
557     #              $cust_pkg_discount->pkgdiscountnum.
558     #            '">'.emt('remove discount').'</A>)</FONT>';
559
560     $html .= pkg_status_row_colspan( $cust_pkg, $label, '', %opt );
561
562   }
563
564   $html;
565 }
566
567 sub pkg_reason_row {
568   my ($cust_pkg, $cpr, %opt) = @_;
569   return '' if $cust_pkg->main_pkgnum;
570
571   my $reasontext = '';
572   $reasontext = $cpr->reasontext . ' by ' . $cpr->otaker if $cpr;
573   pkg_status_row_colspan( $cust_pkg, $reasontext, '',
574     'align'=>'right', 'size'=>'-2', %opt
575   );
576 }
577
578 sub pkg_status_row_colspan {
579   my($cust_pkg, $title, $addl, %opt) = @_;
580
581   my $colspan  = $opt{'colspan'};
582
583   my $align = $opt{'align'} ? 'ALIGN="'. $opt{'align'}.'"' : '';
584   my $color = $opt{'color'} ? 'COLOR="#'.$opt{'color'}.'"' : '';
585   my $size  = $opt{'size'}  ? 'SIZE="'.  $opt{'size'}. '"' : '';
586
587   my $html = qq(<TR><TD COLSPAN=$colspan $align>);
588   $html   .= qq(<FONT $color $size>) if length($color) || $size;
589   $html   .= qq(<B>) if $color && !$size;
590   $html   .= $title;
591   $html   .= qq(</B>) if $color && !$size;
592   $html   .= qq(</FONT>) if length($color) || $size;
593   $html   .= ",&nbsp;$addl" if length($addl);
594
595   if ( $opt{'pkg-balances'} && ! $cust_pkg->{_printed_balance}++ ) { #kludge
596     $html .= ' (Balance:&nbsp;<B>'. $opt{'money_char'}.
597              $cust_pkg->cust_main->balance_pkgnum($cust_pkg->pkgnum).
598              '</B>)';
599   }
600
601   $html   .= qq(</TD></TR>);
602
603   $html;
604
605 }
606
607 sub pkg_datestr {
608   my($cust_pkg, $field, %opt) = @_ or return '';
609   return '&nbsp;' unless $cust_pkg->get($field);
610   my $format = '<TD align="left"><B>%b</B></TD>'.
611                '<TD align="right"><B>&nbsp;%o,</B></TD>'.
612                '<TD align="right"><B>&nbsp;%Y</B></TD>';
613   #$format .= '&nbsp;<FONT SIZE=-3>%l:%M:%S%P&nbsp;%z</FONT>'
614   $format .= '<TD ALIGN="right"><B>&nbsp;%l</TD>'.
615              '<TD ALIGN="center"><B>:</B></TD>'.
616              '<TD ALIGN="left"><B>%M</B></TD>'.
617              '<TD ALIGN="left"><B>&nbsp;%P</B></TD>'
618     if $opt{'cust_pkg-display_times'};
619   my $strip = time2str($format, $cust_pkg->get($field) );
620   $strip =~ s/ (\d)/$1/g;
621   $strip;
622 }
623
624 sub pkg_suspend_link {
625   include( '/elements/popup_link-cust_pkg.html',
626              'action'      => $p. 'misc/cancel_pkg.html?method=suspend',
627              'label'       => emt('Suspend now'),
628              'actionlabel' => emt('Suspend'),
629              'color'       => '#FF9900',
630              'cust_pkg'    => shift,
631              'height'      => 420,
632          )
633 }
634
635 sub pkg_adjourn_link {
636   include( '/elements/popup_link-cust_pkg.html',
637              'action'      => $p. 'misc/cancel_pkg.html?method=adjourn',
638              'label'       => emt('Suspend later'),
639              'actionlabel' => emt('Adjourn'),
640              'color'       => '#CC6600',
641              'cust_pkg'    => shift,
642              'height'      => 445,
643          )
644 }
645
646 sub pkg_delay_link  {
647   my($cust_pkg) = shift;
648   include( '/elements/popup_link-cust_pkg.html',
649              'action'      => $p. 'misc/delay_susp_pkg.html',
650              'label'       => ( $cust_pkg->dundate
651                                   ? emt('Edit suspension delay')
652                                   : emt('Delay suspend')
653                               ),
654              'actionlabel' => emt('Delay suspend for'),
655              'cust_pkg'    => $cust_pkg,
656          )
657 }
658
659 sub pkg_resume_link {
660   include( '/elements/popup_link-cust_pkg.html',
661              'action'      => $p. 'misc/cancel_pkg.html?method=resume',
662              'label'       => emt('Unsuspend later'),
663              'actionlabel' => emt('Resume'),
664              'color'       => '#00CC00',
665              'cust_pkg'    => shift,
666          )
667 }
668
669 sub pkg_unhold_link {
670   include( '/elements/popup_link-cust_pkg.html',
671              'action'      => $p. 'misc/unhold_pkg.html?',
672              'label'       => emt('Start billing'),
673              'actionlabel' => emt('Start billing'),
674              'color'       => '#00CC00',
675              'width'       => 510,
676              'height'      => 310,
677              'cust_pkg'    => shift,
678          )
679 }
680
681 sub pkg_unsuspend_link { pkg_link('misc/unsusp_pkg',    emt('Unsuspend now'), @_ ); }
682 sub pkg_unadjourn_link { pkg_link('misc/unadjourn_pkg', emt('Abort'),     @_ ); }
683 sub pkg_unexpire_link  { pkg_link('misc/unexpire_pkg',  emt('Abort'),     @_ ); }
684 sub pkg_unchange_link  { pkg_link('misc/do_not_change_pkg',  emt('Abort change'),     @_ ); }
685 sub pkg_change_now_link  { pkg_link('misc/change_pkg_now',  emt('Change now'),     @_ ); }
686
687 sub pkg_cancel_link {
688   include( '/elements/popup_link-cust_pkg.html',
689              'action'      => $p. 'misc/cancel_pkg.html?method=cancel',
690              'label'       => emt('Cancel now'),
691              'actionlabel' => emt('Cancel'),
692              'color'       => '#ff0000',
693              'cust_pkg'    => shift,
694          )
695 }
696
697 sub pkg_uncancel_link {
698   include( '/elements/popup_link-cust_pkg.html',
699              'action'      => $p. 'misc/cancel_pkg.html?method=uncancel',
700              'label'       => emt('Un-cancel'),
701              'actionlabel' => emt('Un-cancel'),
702              #'color'       =>  #?
703              'cust_pkg'    => shift,
704          )
705 }
706
707 sub pkg_expire_link {
708   include( '/elements/popup_link-cust_pkg.html',
709              'action'      => $p. 'misc/cancel_pkg.html?method=expire',
710              'label'       => emt('Cancel later'),
711              'actionlabel' => emt('Expire'),
712              'color'       => '#CC0000',
713              'cust_pkg'    => shift,
714          )
715 }
716
717 sub pkg_change_later_link {
718   my $cust_pkg = shift;
719   include( '/elements/popup_link-cust_pkg.html',
720     'action'      => $p . 'misc/change_pkg.cgi?',
721     'label'       => emt('Reschedule'),
722     'actionlabel' => emt('Edit scheduled change for'),
723     'cust_pkg'    => $cust_pkg,
724     'width'       => 960,
725     'height'      => 490,
726   )
727 }
728
729 sub svc_recharge_link {
730   include( '/elements/popup_link-cust_svc.html',
731              'action'      => $p. 'misc/recharge_svc.html',
732              'label'       => emt('Recharge'),
733              'actionlabel' => emt('Recharge'),
734              'color'       => '#333399',
735              'cust_svc'    => shift,
736          )
737 }
738
739 sub pkg_autosuspend_time {
740   my $cust_pkg = shift or return '';
741   my $days = 7;
742   my $time = time;
743   my $pending_suspend = 0;
744   #this seems to be extremely inefficient...  and is slowing down all customer
745   #views
746   while ( $days > 0 &&
747           scalar(
748             grep { $_->part_event->action eq 'suspend' }
749             @{$cust_pkg->cust_main->due_cust_event( time => $time + 86400*$days,
750                                                     testonly => 1,
751                                                   ) }
752           )
753         )
754   {
755     $pending_suspend = 1;
756     $days--;
757   }
758
759   $pending_suspend ? time + ($days + 1) * 86400 : '';
760
761 }
762
763 </%init>