clean up package list UI: put actions in a dropdown
[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 %
35 % } else { # not canceled
36 %
37 %   if ( $cust_pkg->get('susp') ) { #suspended or on hold
38 %
39 %     #if ( $cust_pkg->order_date eq $cust_pkg->get('susp') ) # inconsistent with FS::cust_pkg::status
40 %     if ( ! $cust_pkg->setup ) { #status: on hold
41
42         <% pkg_status_row( $cust_pkg, emt('On Hold'), '', 'color'=>'7E0079', %opt ) %>
43
44 %     } else { #status: suspended
45 %       my ($cpr,$susplabel);
46 %       if ($cust_pkg->is_status_delay_cancel(%opt)) {
47 %         $cpr = $cust_pkg->last_cust_pkg_reason('expire');
48 %         $susplabel = 'Suspended (Cancelled)';
49 %       } else {
50 %         $cpr = $cust_pkg->last_cust_pkg_reason('susp');
51 %         $susplabel = 'Suspended';
52 %       }
53         <% pkg_status_row( $cust_pkg, emt($susplabel), 'susp', 'color'=>'FF9900', %opt ) %>
54         <% pkg_reason_row( $cust_pkg, $cpr, 'color' => 'FF9900', %opt ) %>
55
56 %     }
57
58     <% pkg_status_row_noauto( $cust_pkg, %opt ) %>
59
60     <% pkg_status_row_separate_bill( $cust_pkg, %opt ) %>
61
62     <% pkg_status_row_discount( $cust_pkg, %opt ) %>
63
64 %   if ( $cust_pkg->order_date ne $cust_pkg->get('susp') ) { # not on hold
65 %     if ( $cust_pkg->get('setup') ) {
66         <% pkg_status_row($cust_pkg, emt('Setup'), 'setup', %opt ) %>
67 %     } else {
68         <% pkg_status_row_colspan( $cust_pkg, emt('Never billed'), '', %opt ) %>
69 %     }
70 %   }
71
72     <% pkg_status_row_if($cust_pkg, emt('Un-cancelled'), 'uncancel', %opt ) %>
73
74     <% pkg_status_row_changed( $cust_pkg, %opt ) %>
75     <% pkg_status_row_if( $cust_pkg, $last_bill_or_renewed, 'last_bill', %opt, curuser=>$curuser ) %>
76 %   if ( $cust_pkg->option('suspend_bill', 1)
77 %        || ( $part_pkg->option('suspend_bill', 1)
78 %               && ! $cust_pkg->option('no_suspend_bill',1)
79 %           )
80 %      )
81 %   {
82       <% pkg_status_row_if( $cust_pkg, emt('Next bill'), 'bill', %opt, curuser=>$curuser ) %>
83 %   }
84     <% pkg_status_row_if( $cust_pkg, emt('Will resume'), 'resume', %opt, curuser=>$curuser ) %>
85     <% pkg_status_row_expire($cust_pkg, %opt, curuser=>$curuser) %>
86     <% pkg_status_row_if( $cust_pkg, emt('Contract ends'), 'contract_end', %opt ) %>
87
88 %   } else { #status: active
89 %
90 %     if ( $change_from ) { # future change
91 %
92           <% pkg_status_row_colspan( $cust_pkg, emt('Waiting for package change'), '', %opt ) %>
93           <% pkg_status_row_if( $cust_pkg,
94                              emt('Will be activated on'),
95                              'start_date',
96                              %opt ) %>
97 %
98 %     } elsif ( ! $cust_pkg->get('setup') ) { # not setup
99 %
100 %       unless ( $part_pkg->freq ) { # one-time charge
101
102           <% pkg_status_row_colspan( $cust_pkg, emt('Not yet billed (one-time charge)'), '', %opt ) %>
103
104           <% pkg_status_row_noauto( $cust_pkg, %opt ) %>
105
106           <% pkg_status_row_separate_bill( $cust_pkg, %opt ) %>
107
108           <% pkg_status_row_discount( $cust_pkg, %opt ) %>
109
110           <% pkg_status_row_if(
111                $cust_pkg,
112                ( $part_pkg->freq ? emt('Start billing') : emt('Bill on') ),
113                'start_date',
114                %opt
115              )
116           %>
117
118           <% pkg_status_row_if($cust_pkg, emt('Un-cancelled'), 'uncancel', %opt ) %>
119
120 %       } else { # recurring package, not yet billed
121
122           <% pkg_status_row_colspan($cust_pkg, emt("Not yet billed ($billed_or_prepaid [_1])", myfreq($part_pkg) ), '', %opt ) %>
123
124           <% pkg_status_row_noauto( $cust_pkg, %opt ) %>
125
126           <% pkg_status_row_separate_bill( $cust_pkg, %opt ) %>
127
128           <% pkg_status_row_discount( $cust_pkg, %opt ) %>
129
130           <% pkg_status_row_if($cust_pkg, emt('Start billing'), 'start_date', %opt) %>
131           <% pkg_status_row_if($cust_pkg, emt('Un-cancelled'), 'uncancel', %opt ) %>
132
133 %       } 
134 %
135 %     } else { #setup
136 %
137 %       unless ( $part_pkg->freq ) { 
138
139           <% pkg_status_row_colspan($cust_pkg, emt('One-time charge'), '', %opt ) %>
140
141           <% pkg_status_row($cust_pkg, emt('Billed'), 'setup', %opt) %>
142
143           <% pkg_status_row_noauto( $cust_pkg, %opt ) %>
144
145           <% pkg_status_row_separate_bill( $cust_pkg, %opt ) %>
146
147           <% pkg_status_row_discount( $cust_pkg, %opt ) %>
148
149           <% pkg_status_row_if($cust_pkg, emt('Un-cancelled'), 'uncancel', %opt ) %>
150
151 %       } else { # recurring package
152 %
153 %         my $num_cust_svc = $cust_pkg->num_cust_svc;
154 %         my $summarize = $opt{'cust_pkg-large_pkg_size'} > 0
155 %                           && $opt{'cust_pkg-large_pkg_size'} <= $num_cust_svc;
156 %
157 %         #overlimit process is expensive with many services, so skip w/summary
158 %         if ( !$summarize && scalar($cust_pkg->overlimit) ) {
159
160             <% pkg_status_row_colspan( $cust_pkg,
161                  emt('Overlimit'),
162                  $billed_or_prepaid. '&nbsp;'. myfreq($part_pkg),
163                  'color'=>'FFD000',
164                  %opt
165                )
166             %>
167
168 %         } else {
169             <% pkg_status_row_colspan( $cust_pkg,
170                  emt('Active'),
171                  $billed_or_prepaid. '&nbsp;'. myfreq($part_pkg),
172                  'color'=>'00CC00',
173                  %opt
174                )
175             %>
176 %         } 
177
178           <% pkg_status_row_noauto( $cust_pkg, %opt ) %>
179
180           <% pkg_status_row_separate_bill( $cust_pkg, %opt ) %>
181
182           <% pkg_status_row_discount( $cust_pkg, %opt ) %>
183
184           <% pkg_status_row($cust_pkg, emt('Setup'), 'setup', %opt) %>
185
186           <% pkg_status_row_if($cust_pkg, emt('Un-cancelled'), 'uncancel', %opt ) %>
187
188 %       } 
189 %
190 %     }
191 %
192 %     if ( $opt{'cust_pkg-show_autosuspend'} ) {
193 %       my $autosuspend = pkg_autosuspend_time( $cust_pkg );
194 %       $cust_pkg->set('autosuspend', $autosuspend) if $autosuspend;
195 %     }
196
197       <% pkg_status_row_changed( $cust_pkg, %opt ) %>
198       <% pkg_status_row_if( $cust_pkg, $last_bill_or_renewed, 'last_bill', %opt, curuser=>$curuser ) %>
199       <% pkg_status_row_if( $cust_pkg, $next_bill_or_prepaid_until, 'bill', %opt, curuser=>$curuser ) %>
200       <% pkg_status_row_if($cust_pkg, emt('Will automatically suspend by'), 'autosuspend', %opt) %>
201       <% pkg_status_row_if($cust_pkg, emt('Automatic suspension delayed until'), 'dundate', %opt) %>
202       <% pkg_status_row_if( $cust_pkg, emt('Will suspend on'), 'adjourn', %opt, curuser=>$curuser ) %>
203       <% pkg_status_row_if( $cust_pkg, emt('Will resume on'), 'resume', %opt, curuser=>$curuser ) %>
204       <% pkg_status_row_expire($cust_pkg, %opt, curuser=>$curuser) %>
205       <% pkg_status_row_if( $cust_pkg, emt('Contract ends'), 'contract_end', %opt ) %>
206
207
208 %   } 
209 % } 
210
211   </TABLE>
212 </TD>
213 <%init>
214
215 my %opt = @_;
216
217 my $bgcolor  = $opt{'bgcolor'};
218 my $cust_pkg = $opt{'cust_pkg'};
219 my $part_pkg = $opt{'part_pkg'};
220 my $curuser  = $FS::CurrentUser::CurrentUser;
221 my $width    = $opt{'cust_pkg-display_times'} ? '38%' : '56%';
222 my $supplemental = $opt{'supplemental'};
223 my $change_from  = $opt{'change_from'};
224
225 $opt{colspan}  = $opt{'cust_pkg-display_times'} ? 8 : 4;
226
227 #false laziness w/edit/REAL_cust_pkg.cgi
228 my( $billed_or_prepaid, $last_bill_or_renewed, $next_bill_or_prepaid_until );
229 unless ( $part_pkg->is_prepaid ) {
230   $billed_or_prepaid = 'billed';
231   $last_bill_or_renewed = 'Last&nbsp;bill';
232   $next_bill_or_prepaid_until = 'Next&nbsp;bill';
233 } else {
234   $billed_or_prepaid = 'prepaid';
235   $last_bill_or_renewed = 'Renewed';
236   $next_bill_or_prepaid_until = 'Prepaid&nbsp;until';
237 }
238
239 #subroutines
240
241 sub myfreq {
242   my $part_pkg = shift;
243   my $freq = $part_pkg->freq_pretty;
244   #$freq =~ s/ /&nbsp;/g;
245   $freq;
246 }
247
248 #false laziness w/package.html
249 sub pkg_link {
250   my($action, $label, $cust_pkg) = @_;
251   return '' unless $cust_pkg;
252   qq!<a href="$p$action.cgi?!. $cust_pkg->pkgnum. qq!">$label</a>!;
253 }
254
255 sub pkg_status_row {
256   my( $cust_pkg, $title, $field, %opt ) = @_;
257
258   if ( $field and $cust_pkg->main_pkgnum ) {
259     # for supplemental packages, we mostly only show these if they're 
260     # different from the main package
261     my $main_pkg = $cust_pkg-> main_pkg;
262     if (    $main_pkg->get($field) ne $cust_pkg->get($field)
263         # with some exceptions
264         or  $field eq 'bill'
265         or  $field eq 'last_bill'
266         or  $field eq 'setup'
267         or  $field eq 'susp'
268         or  $field eq 'cancel'
269       ) {
270       # handle it normally
271     } else {
272       return '';
273     }
274   }
275
276   my $color = $opt{'color'};
277
278   my $html = qq(<TR><TD WIDTH="$width" ALIGN="right">);
279   $html   .= qq(<FONT COLOR="#$color"><B>) if length($color);
280   $html   .= qq($title&nbsp;);
281   $html   .= qq(</B></FONT>) if length($color);
282
283   if ( $opt{'pkg_balances'} && ! $cust_pkg->{_printed_balance}++ ) { #kludge
284     $html .= ' (Balance:&nbsp;<B>'. $opt{'money_char'}.
285              $cust_pkg->cust_main->balance_pkgnum($cust_pkg->pkgnum).
286              '</B>)';
287   }
288
289   $html   .= qq(</TD>);
290   $html   .= pkg_datestr($cust_pkg, $field, %opt). '</TR>';
291
292   $html;
293 }
294
295 sub pkg_status_row_if {
296   my( $cust_pkg, $title, $field, %opt ) = @_;
297   
298   $title = '<FONT SIZE=-1>(&nbsp;'. pkg_unadjourn_link($cust_pkg). '&nbsp;)&nbsp;</FONT>'. $title
299     if ( $field eq 'adjourn' &&
300          $opt{curuser}->access_right('Suspend customer package later')
301        );
302
303   $cust_pkg->get($field) ? pkg_status_row($cust_pkg, $title, $field, %opt) : '';
304 }
305
306 sub pkg_status_row_expire {
307   my $cust_pkg = shift;
308   my %opt = @_;
309   return unless $cust_pkg->get('expire');
310
311   my $title;
312
313   if ( $cust_pkg->get('change_to_pkg') ) {
314     if ( $cust_pkg->change_to_pkg->pkgpart != $cust_pkg->pkgpart ) {
315       $title = mt('Will change to <b>[_1]</b> on',
316                  $cust_pkg->change_to_pkg->part_pkg->pkg);
317     } elsif ( $cust_pkg->change_to_pkg->locationnum != $cust_pkg->locationnum )
318     {
319       $title = mt('Will <b>change location</b> on');
320     } elsif (( $cust_pkg->change_to_pkg->quantity != $cust_pkg->quantity ) ||
321              ( $cust_pkg->change_to_pkg->contract_end != $cust_pkg->contract_end ))
322     {
323       $title = mt('Will change on');
324     } else {
325       # FS::cust_pkg->change_later should have prevented this, but 
326       # just so that we can display _something_
327       $title = '<font color="#ff0000">Unknown package change</font>';
328     }
329
330   } else {
331
332     $title = emt('Expires');
333     if ( $opt{curuser}->access_right('Cancel customer package later')) {
334       $title = '<FONT SIZE=-1>(&nbsp;'. pkg_unexpire_link($cust_pkg). '&nbsp;)&nbsp;</FONT>'. $title;
335     }
336
337   }
338
339   pkg_status_row( $cust_pkg, $title, 'expire', %opt );
340 }
341
342 sub pkg_status_row_changed {
343   my( $cust_pkg, %opt ) = @_;
344
345   return '' unless $cust_pkg->change_date;
346
347   my $html =
348     pkg_status_row( $cust_pkg, emt('Changed'), 'change_date', %opt );
349
350   my $old = $cust_pkg->old_cust_pkg;
351   if ( $old ) {
352     my $part_pkg = $old->part_pkg;
353     $html .= pkg_status_row_colspan(
354       $cust_pkg, 
355 #      emt("Changed from [_1]: [_2]",
356 #             $cust_pkg->change_pkgnum,
357 #             $part_pkg->pkg_comment(cust_pkg=>$old, nopartpkg=>1)
358 #         ),
359       '',
360       '',
361       'size'    => '-1',
362       'align'   => 'right',
363     );
364   }
365
366   $html;
367 }
368
369 sub pkg_status_row_detached {
370   my( $cust_pkg, %opt ) = @_;
371
372   return '' unless $cust_pkg->change_custnum;
373
374   my $html = '';
375
376   my $cust_main = $cust_pkg->change_cust_main;
377   if ( $cust_main ) {
378
379     my $cust_link = '<A HREF="cust_main.cgi?'.  $cust_pkg->change_custnum. '">'.
380                       encode_entities( $cust_main->name ).
381                     '</A>';
382
383     my $what = $opt{'pkg_attached'} ? 'Attached' : 'Detached';
384
385     $html .= pkg_status_row_colspan( $cust_pkg, 
386                                      emt("$what to customer #[_1]: ",
387                                             $cust_pkg->change_custnum
388                                         ).
389                                        $cust_link,
390                                      '',
391                                      'size'    => '-1',
392                                      'align'   => 'right',
393                                      'colspan' => 4,
394                                    );
395   }
396
397   $html;
398 }
399
400 sub pkg_status_row_noauto {
401   my( $cust_pkg, %opt ) = @_;
402
403   return '' unless ( $cust_pkg->no_auto || $opt{'part_pkg'}->no_auto )
404                 && $opt{'has_cust_payby_auto'};
405
406   pkg_status_row_colspan( $cust_pkg, emt("No automatic charge"), '');
407 }
408
409 sub pkg_status_row_separate_bill {
410   my $cust_pkg = shift;
411   return '' unless $cust_pkg->separate_bill;
412   pkg_status_row_colspan( $cust_pkg, emt("Invoiced separately") );
413 }
414
415 sub pkg_status_row_discount {
416   my( $cust_pkg, %opt ) = @_;
417
418   my $html;
419
420   if ( $cust_pkg->waive_setup ) {
421     my $label = '<SPAN STYLE="font-size: small;font-weight: bold">' .
422                  emt('Setup fee waived') .
423                  '</SPAN>';
424     $html .= pkg_status_row_colspan( $cust_pkg, $label, '', %opt );
425   }
426
427   foreach my $cust_pkg_discount (@{ $cust_pkg->{_cust_pkg_discount_active} }) {
428
429     my $discount = $cust_pkg_discount->discount;
430
431     my $label = '<SPAN STYLE="font-size: small"><B>';
432     if ( $cust_pkg_discount->setuprecur eq 'setup' ) {
433       $label .= emt('Setup Discount');
434     } else {
435       $label .= emt('Recurring Discount');
436     }
437     $label .= '</B>: '. $discount->description;
438     if ( $discount->months > 0 and $cust_pkg_discount->months_used > 0 ) {
439       my $remaining = $discount->months - $cust_pkg_discount->months_used;
440       $remaining = sprintf('%.2f', $remaining) if $remaining =~ /\./;
441       $label .= <br> . emt(" ([quant,_1,month,months] remaining)",$remaining);
442     }
443     $label .= '</SPAN>';
444
445     #$label .= ' <FONT SIZE="-1">('.
446     #            '<A HREF="../misc/delete-cust_pkg_discount.html?'.
447     #              $cust_pkg_discount->pkgdiscountnum.
448     #            '">'.emt('remove discount').'</A>)</FONT>';
449
450     $html .= pkg_status_row_colspan( $cust_pkg, $label, '', %opt );
451
452   }
453
454   $html;
455 }
456
457 sub pkg_reason_row {
458   my ($cust_pkg, $cpr, %opt) = @_;
459   return '' if $cust_pkg->main_pkgnum;
460
461   my $reasontext = '';
462   $reasontext = $cpr->reasontext . ' by ' . $cpr->otaker if $cpr;
463   pkg_status_row_colspan( $cust_pkg, $reasontext, '',
464     'align'=>'right', 'size'=>'-2', %opt
465   );
466 }
467
468 sub pkg_status_row_colspan {
469   my($cust_pkg, $title, $addl, %opt) = @_;
470
471   my $colspan  = $opt{'colspan'};
472
473   my $align = $opt{'align'} ? 'ALIGN="'. $opt{'align'}.'"' : '';
474   my $color = $opt{'color'} ? 'COLOR="#'.$opt{'color'}.'"' : '';
475   my $size  = $opt{'size'}  ? 'SIZE="'.  $opt{'size'}. '"' : '';
476
477   my $html = qq(<TR><TD COLSPAN=$colspan $align>);
478   $html   .= qq(<FONT $color $size>) if length($color) || $size;
479   $html   .= qq(<B>) if $color && !$size;
480   $html   .= $title;
481   $html   .= qq(</B>) if $color && !$size;
482   $html   .= qq(</FONT>) if length($color) || $size;
483   $html   .= ",&nbsp;$addl" if length($addl);
484
485   if ( $opt{'pkg-balances'} && ! $cust_pkg->{_printed_balance}++ ) { #kludge
486     $html .= ' (Balance:&nbsp;<B>'. $opt{'money_char'}.
487              $cust_pkg->cust_main->balance_pkgnum($cust_pkg->pkgnum).
488              '</B>)';
489   }
490
491   $html   .= qq(</TD></TR>);
492
493   $html;
494
495 }
496
497 sub pkg_datestr {
498   my($cust_pkg, $field, %opt) = @_ or return '';
499   return '&nbsp;' unless $cust_pkg->get($field);
500   my $format = '<TD align="left"><B>%b</B></TD>'.
501                '<TD align="right"><B>&nbsp;%o,</B></TD>'.
502                '<TD align="right"><B>&nbsp;%Y</B></TD>';
503   #$format .= '&nbsp;<FONT SIZE=-3>%l:%M:%S%P&nbsp;%z</FONT>'
504   $format .= '<TD ALIGN="right"><B>&nbsp;%l</TD>'.
505              '<TD ALIGN="center"><B>:</B></TD>'.
506              '<TD ALIGN="left"><B>%M</B></TD>'.
507              '<TD ALIGN="left"><B>&nbsp;%P</B></TD>'
508     if $opt{'cust_pkg-display_times'};
509   my $strip = time2str($format, $cust_pkg->get($field) );
510   $strip =~ s/ (\d)/$1/g;
511   $strip;
512 }
513
514 sub pkg_unadjourn_link { pkg_link('misc/unadjourn_pkg', emt('Abort'),     @_ ); }
515 sub pkg_unexpire_link  { pkg_link('misc/unexpire_pkg',  emt('Abort'),     @_ ); }
516
517 sub svc_recharge_link {
518   include( '/elements/popup_link-cust_svc.html',
519              'action'      => $p. 'misc/recharge_svc.html',
520              'label'       => emt('Recharge'),
521              'actionlabel' => emt('Recharge'),
522              'color'       => '#333399',
523              'cust_svc'    => shift,
524          )
525 }
526
527 sub pkg_autosuspend_time {
528   my $cust_pkg = shift or return '';
529   my $days = 7;
530   my $time = time;
531   my $pending_suspend = 0;
532   #this seems to be extremely inefficient...  and is slowing down all customer
533   #views
534   while ( $days > 0 &&
535           scalar(
536             grep { $_->part_event->action eq 'suspend' }
537             @{$cust_pkg->cust_main->due_cust_event( time => $time + 86400*$days,
538                                                     testonly => 1,
539                                                   ) }
540           )
541         )
542   {
543     $pending_suspend = 1;
544     $days--;
545   }
546
547   $pending_suspend ? time + ($days + 1) * 86400 : '';
548
549 }
550
551 </%init>