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