future package unsuspend date, #14144
[freeside.git] / httemplate / view / cust_main / packages / status.html
1 <TD CLASS="inv" BGCOLOR="<% $bgcolor %>">
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 ( $cust_pkg->order_date ) {
7     <% pkg_status_row($cust_pkg, emt('Ordered'), 'order_date', %opt ) %>
8 % }
9
10 % if ( $cust_pkg->get('cancel') ) { #status: cancelled
11 %   my $cpr = $cust_pkg->last_cust_pkg_reason('cancel');
12
13     <% pkg_status_row($cust_pkg, emt('Cancelled'), 'cancel', 'color'=>'FF0000', %opt ) %>
14
15     <% pkg_status_row_colspan( $cust_pkg,
16          ( $cpr ? $cpr->reasontext. ' by '. $cpr->otaker : '' ), '',
17          'align'=>'right', 'color'=>'ff0000', 'size'=>'-2', 'colspan'=>$colspan,
18          %opt
19        )
20     %>
21
22 %   unless ( $cust_pkg->get('setup') ) { 
23
24         <% pkg_status_row_colspan( $cust_pkg, emt('Never billed'), '', 'colspan'=>$colspan, %opt, ) %>
25
26 %   } else { 
27
28        <% pkg_status_row( $cust_pkg, emt('Setup'), 'setup', %opt ) %>
29        <% pkg_status_row_changed( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
30        <% pkg_status_row_if( $cust_pkg, $last_bill_or_renewed, 'last_bill', %opt, curuser=>$curuser ) %>
31        <% pkg_status_row_if( $cust_pkg, emt('Suspended'), 'susp', %opt, curuser=>$curuser ) %>
32
33 %   } 
34 %
35 % } else { 
36 %
37 %   if ( $cust_pkg->get('susp') ) { #status: suspended
38 %     my $cpr = $cust_pkg->last_cust_pkg_reason('susp');
39
40     <% pkg_status_row( $cust_pkg, emt('Suspended'), 'susp', 'color'=>'FF9900', %opt ) %>
41
42     <% pkg_status_row_colspan( $cust_pkg,
43          ( $cpr ? $cpr->reasontext. ' by '. $cpr->otaker : '' ), '',
44          'align'=>'right', 'color'=>'FF9900', 'size'=>'-2', 'colspan'=>$colspan,
45          %opt,
46        )
47     %>
48
49     <% pkg_status_row_noauto( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
50
51     <% pkg_status_row_discount( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
52
53 %   unless ( $cust_pkg->get('setup') ) { 
54       <% pkg_status_row_colspan( $cust_pkg, emt('Never billed'), '', 'colspan'=>$colspan, %opt ) %>
55 %   } else { 
56       <% pkg_status_row($cust_pkg, emt('Setup'), 'setup', %opt ) %>
57 %   } 
58
59     <% pkg_status_row_changed( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
60     <% pkg_status_row_if( $cust_pkg, $last_bill_or_renewed, 'last_bill', %opt, curuser=>$curuser ) %>
61 %   if ( $part_pkg->option('suspend_bill', 1) ) {
62       <% pkg_status_row_if( $cust_pkg, emt('Next bill'), 'bill', %opt, curuser=>$curuser ) %>
63 %   }
64     <% pkg_status_row_if( $cust_pkg, emt('Will resume'), 'resume', %opt, curuser=>$curuser ) %>
65     <% pkg_status_row_if( $cust_pkg, emt('Expires'), 'expire', %opt, curuser=>$curuser ) %>
66     <% pkg_status_row_if( $cust_pkg, emt('Contract ends'), 'contract_end', %opt ) %>
67
68     <TR>
69       <TD COLSPAN=<%$colspan%>>
70         <FONT SIZE=-1>
71 %         if ( $curuser->access_right('Unsuspend customer package') ) { 
72             (&nbsp;<% pkg_unsuspend_link($cust_pkg) %>&nbsp;)
73             (&nbsp;<% pkg_resume_link($cust_pkg) %>&nbsp;)
74 %         }
75 %         if ( $curuser->access_right('Cancel customer package immediately') ) {
76             (&nbsp;<% pkg_cancel_link($cust_pkg) %>&nbsp;)
77 %         } 
78         </FONT>
79       </TD>
80     </TR>
81
82 %   } else { #status: active
83 %
84 %     unless ( $cust_pkg->get('setup') ) { #not setup
85 %
86 %       unless ( $part_pkg->freq ) {
87
88           <% pkg_status_row_colspan( $cust_pkg, emt('Not yet billed (one-time charge)'), '', 'colspan'=>$colspan, %opt ) %>
89
90           <% pkg_status_row_noauto( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
91
92           <% pkg_status_row_discount( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
93
94           <% pkg_status_row_if(
95                $cust_pkg,
96                ( $part_pkg->freq ? emt('Start billing') : emt('Bill on') ),
97                'start_date',
98                %opt
99              )
100           %>
101
102           <TR>
103             <TD COLSPAN=<%$colspan%>>
104               <FONT SIZE=-1>
105 %               if ( $curuser->access_right('Cancel customer package immediately') ) { 
106                   (&nbsp;<% pkg_cancel_link($cust_pkg) %>&nbsp;)
107 %               } 
108               </FONT>
109             </TD>
110           </TR>
111
112 %       } else { 
113
114           <% pkg_status_row_colspan($cust_pkg, emt("Not yet billed ($billed_or_prepaid [_1])", myfreq($part_pkg) ), '', 'colspan'=>$colspan, %opt ) %>
115
116           <% pkg_status_row_noauto( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
117
118           <% pkg_status_row_discount( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
119
120           <% pkg_status_row_if($cust_pkg, emt('Start billing'), 'start_date', %opt) %>
121
122 %       } 
123 %
124 %     } else { #setup
125 %
126 %       unless ( $part_pkg->freq ) { 
127
128           <% pkg_status_row_colspan($cust_pkg, emt('One-time charge'), '', 'colspan'=>$colspan, %opt ) %>
129
130           <% pkg_status_row($cust_pkg, emt('Billed'), 'setup', %opt) %>
131
132           <% pkg_status_row_noauto( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
133
134           <% pkg_status_row_discount( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
135
136 %       } else { 
137 %
138 %         my $num_cust_svc = $cust_pkg->num_cust_svc;
139 %         my $summarize = $opt{'cust_pkg-large_pkg_size'} > 0
140 %                           && $opt{'cust_pkg-large_pkg_size'} <= $num_cust_svc;
141 %
142 %         #overlimit process is expensive with many services, so skip w/summary
143 %         if ( !$summarize && scalar($cust_pkg->overlimit) ) {
144
145             <% pkg_status_row_colspan( $cust_pkg,
146                  emt('Overlimit'),
147                  $billed_or_prepaid. '&nbsp;'. myfreq($part_pkg),
148                  'color'=>'FFD000', 'colspan'=>$colspan,
149                  %opt
150                )
151             %>
152
153 %         } else {
154             <% pkg_status_row_colspan( $cust_pkg,
155                  emt('Active'),
156                  $billed_or_prepaid. '&nbsp;'. myfreq($part_pkg),
157                  'color'=>'00CC00', 'colspan'=>$colspan,
158                  %opt
159                )
160             %>
161 %         } 
162
163           <% pkg_status_row_noauto( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
164
165           <% pkg_status_row_discount( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
166
167           <% pkg_status_row($cust_pkg, emt('Setup'), 'setup', %opt) %>
168
169 %       } 
170 %
171 %     } 
172 %
173 %     if ( $opt{'cust_pkg-show_autosuspend'} ) {
174 %       my $autosuspend = pkg_autosuspend_time( $cust_pkg );
175 %       $cust_pkg->set('autosuspend', $autosuspend) if $autosuspend;
176 %     }
177
178       <% pkg_status_row_changed( $cust_pkg, %opt, 'colspan'=>$colspan ) %>
179       <% pkg_status_row_if( $cust_pkg, $last_bill_or_renewed, 'last_bill', %opt, curuser=>$curuser ) %>
180       <% pkg_status_row_if( $cust_pkg, $next_bill_or_prepaid_until, 'bill', %opt, curuser=>$curuser ) %>
181       <% pkg_status_row_if($cust_pkg, emt('Will automatically suspend by'), 'autosuspend', %opt) %>
182       <% pkg_status_row_if($cust_pkg, emt('Automatic suspension delayed until'), 'dundate', %opt) %>
183       <% pkg_status_row_if( $cust_pkg, emt('Will suspend on'), 'adjourn', %opt, curuser=>$curuser ) %>
184       <% pkg_status_row_if( $cust_pkg, emt('Will resume on'), 'resume', %opt, curuser=>$curuser ) %>
185       <% pkg_status_row_if( $cust_pkg, emt('Expires'), 'expire', %opt, curuser=>$curuser ) %>
186       <% pkg_status_row_if( $cust_pkg, emt('Contract ends'), 'contract_end', %opt ) %>
187
188 %     if ( $part_pkg->freq ) { 
189
190         <TR>
191           <TD COLSPAN=<%$colspan%>>
192             <FONT SIZE=-1>
193 %             if ( $curuser->access_right('Suspend customer package') ) { 
194                 (&nbsp;<% pkg_suspend_link($cust_pkg) %>&nbsp;)
195 %             } 
196 %             if ( $curuser->access_right('Suspend customer package later') ) { 
197                 (&nbsp;<% pkg_adjourn_link($cust_pkg) %>&nbsp;)
198 %             } 
199 %             if ( $curuser->access_right('Delay suspension events') ) { 
200                 (&nbsp;<% pkg_delay_link($cust_pkg) %>&nbsp;)
201 %             } 
202 %             if ( $curuser->access_right('Cancel customer package immediately') ) { 
203                 (&nbsp;<% pkg_cancel_link($cust_pkg) %>&nbsp;)
204 %             } 
205 %             if ( $curuser->access_right('Cancel customer package later') ) { 
206                 (&nbsp;<% pkg_expire_link($cust_pkg) %>&nbsp;)
207 %             } 
208
209             <FONT>
210           </TD>
211         </TR>
212 %     }
213 %
214 %   } 
215 % } 
216
217   </TABLE>
218 </TD>
219 <%init>
220
221 my %opt = @_;
222
223 my $bgcolor  = $opt{'bgcolor'};
224 my $cust_pkg = $opt{'cust_pkg'};
225 my $part_pkg = $opt{'part_pkg'};
226 my $curuser  = $FS::CurrentUser::CurrentUser;
227 my $colspan  = $opt{'cust_pkg-display_times'} ? 8 : 4;
228 my $width    = $opt{'cust_pkg-display_times'} ? '38%' : '56%';
229
230 #false laziness w/edit/REAL_cust_pkg.cgi
231 my( $billed_or_prepaid, $last_bill_or_renewed, $next_bill_or_prepaid_until );
232 unless ( $part_pkg->is_prepaid ) {
233   $billed_or_prepaid = 'billed';
234   $last_bill_or_renewed = 'Last&nbsp;bill';
235   $next_bill_or_prepaid_until = 'Next&nbsp;bill';
236 } else {
237   $billed_or_prepaid = 'prepaid';
238   $last_bill_or_renewed = 'Renewed';
239   $next_bill_or_prepaid_until = 'Prepaid&nbsp;until';
240 }
241
242 #subroutines
243
244 sub myfreq {
245   my $part_pkg = shift;
246   my $freq = $part_pkg->freq_pretty;
247   #$freq =~ s/ /&nbsp;/g;
248   $freq;
249 }
250
251 #false laziness w/package.html
252 sub pkg_link {
253   my($action, $label, $cust_pkg) = @_;
254   return '' unless $cust_pkg;
255   qq!<a href="$p$action.cgi?!. $cust_pkg->pkgnum. qq!">$label</a>!;
256 }
257
258 sub pkg_status_row {
259   my( $cust_pkg, $title, $field, %opt ) = @_;
260
261   my $color = $opt{'color'};
262
263   my $html = qq(<TR><TD WIDTH="<%$width%>" ALIGN="right">);
264   $html   .= qq(<FONT COLOR="#$color"><B>) if length($color);
265   $html   .= qq($title&nbsp;);
266   $html   .= qq(</B></FONT>) if length($color);
267
268   if ( $opt{'pkg_balances'} && ! $cust_pkg->{_printed_balance}++ ) { #kludge
269     $html .= ' (Balance:&nbsp;<B>'. $opt{'money_char'}.
270              $cust_pkg->cust_main->balance_pkgnum($cust_pkg->pkgnum).
271              '</B>)';
272   }
273
274   $html   .= qq(</TD>);
275   $html   .= pkg_datestr($cust_pkg, $field, %opt). '</TR>';
276
277   $html;
278 }
279
280 sub pkg_status_row_if {
281   my( $cust_pkg, $title, $field, %opt ) = @_;
282   
283   $title = '<FONT SIZE=-1>(&nbsp;'. pkg_unadjourn_link($cust_pkg). '&nbsp;)&nbsp;</FONT>'. $title
284     if ( $field eq 'adjourn' &&
285          $opt{curuser}->access_right('Suspend customer package later')
286        );
287
288   $title = '<FONT SIZE=-1>(&nbsp;'. pkg_unexpire_link($cust_pkg). '&nbsp;)&nbsp;</FONT>'. $title
289     if ( $field eq 'expire' &&
290          $opt{curuser}->access_right('Cancel customer package later')
291        );
292
293   $cust_pkg->get($field) ? pkg_status_row($cust_pkg, $title, $field, %opt) : '';
294 }
295
296 sub pkg_status_row_changed {
297   my( $cust_pkg, %opt ) = @_;
298
299   return '' unless $cust_pkg->change_date;
300
301   my $html =
302     pkg_status_row( $cust_pkg, emt('Package changed'), 'change_date', %opt );
303
304   my $old = $cust_pkg->old_cust_pkg;
305   if ( $old ) {
306     my $part_pkg = $old->part_pkg;
307     $html .= pkg_status_row_colspan( $cust_pkg, 
308                                      emt("Changed from [_1]: [_2]",
309                                             $cust_pkg->change_pkgnum,
310                                             $part_pkg->pkg_comment(nopartpkg => 1) ),
311                                      '',
312                                      'size'    => '-1',
313                                      'align'   => 'right',
314                                      'colspan' => $opt{'colspan'},
315                                    );
316   }
317
318   $html;
319 }
320
321 sub pkg_status_row_noauto {
322   my( $cust_pkg, %opt ) = @_;
323   my $part_pkg = $opt{'part_pkg'};
324   return '' unless $cust_pkg->no_auto || $part_pkg->no_auto;
325
326   #inefficient, should be passed in
327   my $cust_main = $cust_pkg->cust_main;
328
329   return '' unless $cust_main->payby =~ /^(CARD|CHEK)$/;
330   my $what = lc(FS::payby->shortname($cust_main->payby));
331
332   pkg_status_row_colspan( $cust_pkg, emt("No automatic $what charge"), '',
333                           'colspan' => $opt{'colspan'},
334                         );
335 }
336
337 sub pkg_status_row_discount {
338   my( $cust_pkg, %opt ) = @_;
339
340   my $html;
341
342   foreach my $cust_pkg_discount ( $cust_pkg->cust_pkg_discount_active ) {
343
344     my $discount = $cust_pkg_discount->discount;
345
346     my $label = '<B>'.emt('Discount').'</B>: '. $discount->description;
347     if ( $discount->months ) {
348       my $remaining = $discount->months - $cust_pkg_discount->months_used;
349       $remaining = sprintf('%.2f', $remaining) if $remaining =~ /\./;
350       $label .= emt("([_1] months remaining)",$remaining);
351     }
352
353     $label .= ' <FONT SIZE="-1">('.
354                 '<A HREF="../misc/delete-cust_pkg_discount.html?'.
355                   $cust_pkg_discount->pkgdiscountnum.
356                 '">'.emt('remove discount').'</A>)</FONT>';
357
358     $html .= pkg_status_row_colspan( $cust_pkg, $label, '',
359                                      'colspan' => $opt{'colspan'},
360                                    );
361
362   }
363
364   $html;
365 }
366
367 sub pkg_status_row_colspan {
368   my($cust_pkg, $title, $addl, %opt) = @_;
369
370   my $colspan  = $opt{'colspan'};
371
372   my $align = $opt{'align'} ? 'ALIGN="'. $opt{'align'}.'"' : '';
373   my $color = $opt{'color'} ? 'COLOR="#'.$opt{'color'}.'"' : '';
374   my $size  = $opt{'size'}  ? 'SIZE="'.  $opt{'size'}. '"' : '';
375
376   my $html = qq(<TR><TD COLSPAN=$colspan $align>);
377   $html   .= qq(<FONT $color $size>) if length($color) || $size;
378   $html   .= qq(<B>) if $color && !$size;
379   $html   .= $title;
380   $html   .= qq(</B>) if $color && !$size;
381   $html   .= qq(</FONT>) if length($color) || $size;
382   $html   .= ",&nbsp;$addl" if length($addl);
383
384   if ( $opt{'pkg-balances'} && ! $cust_pkg->{_printed_balance}++ ) { #kludge
385     $html .= ' (Balance:&nbsp;<B>'. $opt{'money_char'}.
386              $cust_pkg->cust_main->balance_pkgnum($cust_pkg->pkgnum).
387              '</B>)';
388   }
389
390   $html   .= qq(</TD></TR>);
391
392   $html;
393
394 }
395
396 sub pkg_datestr {
397   my($cust_pkg, $field, %opt) = @_ or return '';
398   return '&nbsp;' unless $cust_pkg->get($field);
399   my $format = '<TD align="left"><B>%b</B></TD>'.
400                '<TD align="right"><B>&nbsp;%o,</B></TD>'.
401                '<TD align="right"><B>&nbsp;%Y</B></TD>';
402   #$format .= '&nbsp;<FONT SIZE=-3>%l:%M:%S%P&nbsp;%z</FONT>'
403   $format .= '<TD ALIGN="right"><B>&nbsp;%l</TD>'.
404              '<TD ALIGN="center"><B>:</B></TD>'.
405              '<TD ALIGN="left"><B>%M</B></TD>'.
406              '<TD ALIGN="left"><B>&nbsp;%P</B></TD>'
407     if $opt{'cust_pkg-display_times'};
408   my $strip = time2str($format, $cust_pkg->get($field) );
409   $strip =~ s/ (\d)/$1/g;
410   $strip;
411 }
412
413 sub pkg_suspend_link {
414   include( '/elements/popup_link-cust_pkg.html',
415              'action'      => $p. 'misc/cancel_pkg.html?method=suspend',
416              'label'       => emt('Suspend now'),
417              'actionlabel' => emt('Suspend'),
418              'color'       => '#FF9900',
419              'cust_pkg'    => shift,
420          )
421 }
422
423 sub pkg_adjourn_link {
424   include( '/elements/popup_link-cust_pkg.html',
425              'action'      => $p. 'misc/cancel_pkg.html?method=adjourn',
426              'label'       => emt('Suspend later'),
427              'actionlabel' => emt('Adjourn'),
428              'color'       => '#CC6600',
429              'cust_pkg'    => shift,
430          )
431 }
432
433 sub pkg_delay_link  {
434   my($cust_pkg) = shift;
435   include( '/elements/popup_link-cust_pkg.html',
436              'action'      => $p. 'misc/delay_susp_pkg.html',
437              'label'       => ( $cust_pkg->dundate
438                                   ? emt('Edit suspension delay')
439                                   : emt('Delay suspend')
440                               ),
441              'actionlabel' => emt('Delay suspend for'),
442              'cust_pkg'    => $cust_pkg,
443          )
444 }
445
446 sub pkg_resume_link {
447   include( '/elements/popup_link-cust_pkg.html',
448              'action'      => $p. 'misc/cancel_pkg.html?method=resume',
449              'label'       => emt('Unsuspend later'),
450              'actionlabel' => emt('Resume'),
451              'color'       => '#00CC00',
452              'cust_pkg'    => shift,
453          )
454 }
455
456 sub pkg_unsuspend_link { pkg_link('misc/unsusp_pkg',    emt('Unsuspend now'), @_ ); }
457 sub pkg_unadjourn_link { pkg_link('misc/unadjourn_pkg', emt('Abort'),     @_ ); }
458 sub pkg_unexpire_link  { pkg_link('misc/unexpire_pkg',  emt('Abort'),     @_ ); }
459
460 sub pkg_cancel_link {
461   include( '/elements/popup_link-cust_pkg.html',
462              'action'      => $p. 'misc/cancel_pkg.html?method=cancel',
463              'label'       => emt('Cancel now'),
464              'actionlabel' => emt('Cancel'),
465              'color'       => '#ff0000',
466              'cust_pkg'    => shift,
467          )
468 }
469
470 sub pkg_expire_link {
471   include( '/elements/popup_link-cust_pkg.html',
472              'action'      => $p. 'misc/cancel_pkg.html?method=expire',
473              'label'       => emt('Cancel later'),
474              'actionlabel' => emt('Expire'),
475              'color'       => '#CC0000',
476              'cust_pkg'    => shift,
477          )
478 }
479
480 sub svc_recharge_link {
481   include( '/elements/popup_link-cust_svc.html',
482              'action'      => $p. 'misc/recharge_svc.html',
483              'label'       => emt('Recharge'),
484              'actionlabel' => emt('Recharge'),
485              'color'       => '#333399',
486              'cust_svc'    => shift,
487          )
488 }
489
490 sub pkg_autosuspend_time {
491   my $cust_pkg = shift or return '';
492   my $days = 7;
493   my $time = time;
494   my $pending_suspend = 0;
495   #this seems to be extremely inefficient...  and is slowing down all customer
496   #views
497   while ( $days > 0 &&
498           scalar(
499             grep { $_->part_event->action eq 'suspend' }
500             @{$cust_pkg->cust_main->due_cust_event( time => $time + 86400*$days,
501                                                     testonly => 1,
502                                                   ) }
503           )
504         )
505   {
506     $pending_suspend = 1;
507     $days--;
508   }
509
510   $pending_suspend ? time + ($days + 1) * 86400 : '';
511
512 }
513
514 </%init>