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