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