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