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