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