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