discounts, RT#6679
[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     $label .= ' ('. ( $discount->months - $cust_pkg_discount->months_used ).
304               ' months remaining)'
305       if $discount->months;
306
307     $label .= ' <FONT SIZE="-1">('.
308                 '<A HREF="../misc/delete-cust_pkg_discount.html?'.
309                   $cust_pkg_discount->pkgdiscountnum.
310                 '">remove&nbsp;discount</A>)</FONT>';
311
312     $html .= pkg_status_row_colspan( $cust_pkg, $label, '',
313                                      'colspan' => $opt{'colspan'},
314                                      #%opt,
315                                    );
316
317   }
318
319   $html;
320 }
321
322 sub pkg_status_row_colspan {
323   my($cust_pkg, $title, $addl, %opt) = @_;
324
325   my $colspan  = $opt{'colspan'};
326
327   my $align = $opt{'align'} ? 'ALIGN="'. $opt{'align'}.'"' : '';
328   my $color = $opt{'color'} ? 'COLOR="#'.$opt{'color'}.'"' : '';
329   my $size  = $opt{'size'}  ? 'SIZE="'.  $opt{'size'}. '"' : '';
330
331   my $html = qq(<TR><TD COLSPAN=$colspan $align>);
332   $html   .= qq(<FONT $color $size>) if length($color) || $size;
333   $html   .= qq(<B>) if $color && !$size;
334   $html   .= $title;
335   $html   .= qq(</B>) if $color && !$size;
336   $html   .= qq(</FONT>) if length($color) || $size;
337   $html   .= ",&nbsp;$addl" if length($addl);
338
339   if ( $opt{'pkg-balances'} && ! $cust_pkg->{_printed_balance}++ ) { #kludge
340     $html .= ' (Balance:&nbsp;<B>'. $opt{'money_char'}.
341              $cust_pkg->cust_main->balance_pkgnum($cust_pkg->pkgnum).
342              '</B>)';
343   }
344
345   $html   .= qq(</TD></TR>);
346
347   $html;
348
349 }
350
351 sub pkg_datestr {
352   my($cust_pkg, $field, %opt) = @_ or return '';
353   return '&nbsp;' unless $cust_pkg->get($field);
354   my $format = '<TD align="left"><B>%b</B></TD>'.
355                '<TD align="right"><B>&nbsp;%o,</B></TD>'.
356                '<TD align="right"><B>&nbsp;%Y</B></TD>';
357   #$format .= '&nbsp;<FONT SIZE=-3>%l:%M:%S%P&nbsp;%z</FONT>'
358   $format .= '<TD ALIGN="right"><B>&nbsp;%l</TD>'.
359              '<TD ALIGN="center"><B>:</B></TD>'.
360              '<TD ALIGN="left"><B>%M</B></TD>'.
361              '<TD ALIGN="left"><B>&nbsp;%P</B></TD>'
362     if $opt{'cust_pkg-display_times'};
363   my $strip = time2str($format, $cust_pkg->get($field) );
364   $strip =~ s/ (\d)/$1/g;
365   $strip;
366 }
367
368 sub pkg_suspend_link {
369   include( '/elements/popup_link-cust_pkg.html',
370              'action'      => $p. 'misc/cancel_pkg.html?method=suspend',
371              'label'       => 'Suspend&nbsp;now',
372              'actionlabel' => 'Suspend',
373              'color'       => '#FF9900',
374              'cust_pkg'    => shift,
375          )
376 }
377
378 sub pkg_adjourn_link {
379   include( '/elements/popup_link-cust_pkg.html',
380              'action'      => $p. 'misc/cancel_pkg.html?method=adjourn',
381              'label'       => 'Suspend&nbsp;later',
382              'actionlabel' => 'Adjourn',
383              'color'       => '#CC6600',
384              'cust_pkg'    => shift,
385          )
386 }
387
388 sub pkg_delay_link  {
389   include( '/elements/popup_link-cust_pkg.html',
390              'action'      => $p. 'misc/delay_susp_pkg.html',
391              'label'       => 'Delay&nbsp;suspend',
392              'actionlabel' => 'Delay suspend for',
393              'cust_pkg'    => shift,
394          )
395 }
396
397 sub pkg_unsuspend_link { pkg_link('misc/unsusp_pkg',    'Unsuspend', @_ ); }
398 sub pkg_unadjourn_link { pkg_link('misc/unadjourn_pkg', 'Abort',     @_ ); }
399 sub pkg_unexpire_link  { pkg_link('misc/unexpire_pkg',  'Abort',     @_ ); }
400
401 sub pkg_cancel_link {
402   include( '/elements/popup_link-cust_pkg.html',
403              'action'      => $p. 'misc/cancel_pkg.html?method=cancel',
404              'label'       => 'Cancel&nbsp;now',
405              'actionlabel' => 'Cancel',
406              'color'       => '#ff0000',
407              'cust_pkg'    => shift,
408          )
409 }
410
411 sub pkg_expire_link {
412   include( '/elements/popup_link-cust_pkg.html',
413              'action'      => $p. 'misc/cancel_pkg.html?method=expire',
414              'label'       => 'Cancel&nbsp;later',
415              'actionlabel' => 'Expire', #"Cancel package $num later"
416              'color'       => '#CC0000',
417              'cust_pkg'    => shift,
418          )
419 }
420
421 sub svc_recharge_link {
422   include( '/elements/popup_link-cust_svc.html',
423              'action'      => $p. 'misc/recharge_svc.html',
424              'label'       => 'Recharge',
425              'actionlabel' => 'Recharge',
426              'color'       => '#333399',
427              'cust_svc'    => shift,
428          )
429 }
430
431 sub pkg_autosuspend_time {
432   my $cust_pkg = shift or return '';
433   my $days = 7;
434   my $time = time;
435   my $pending_suspend = 0;
436   #this seems to be extremely inefficient...  and is slowing down all customer
437   #views
438   while ( $days > 0 &&
439           scalar(
440             grep { $_->part_event->action eq 'suspend' }
441             @{$cust_pkg->cust_main->due_cust_event( time => $time + 86400*$days,
442                                                     testonly => 1,
443                                                   ) }
444           )
445         )
446   {
447     $pending_suspend = 1;
448     $days--;
449   }
450
451   $pending_suspend ? time + ($days + 1) * 86400 : '';
452
453 }
454
455 </%init>