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