don't start recurring billing when a start date hasn't been reached yet either.....
[freeside.git] / httemplate / view / cust_main / packages.html
1 % my $s = 0;
2 % if ( $curuser->access_right('Order customer package') ) { 
3   <% $s++ ? ' | ' : '' %>
4   <% include( '/elements/popup_link-cust_main.html',
5                 'action'      => $p. 'misc/order_pkg.html',
6                 'label'       => 'Order&nbsp;new&nbsp;package',
7                 'actionlabel' => 'Order new package',
8                 'color'       => '#333399',
9                 'cust_main'   => $cust_main,
10                 'closetext'   => 'Close',
11                 'width'       => 763,
12                 'height'      => 350,
13             )
14   %>
15 % } 
16
17 % if ( $curuser->access_right('One-time charge')
18 %        && $conf->config('payby-default') ne 'HIDE'
19 %      ) {
20   <% $s++ ? ' | ' : '' %>
21   <% include('one_time_charge_link.html', $cust_main) %>
22 % } 
23
24 % if ( $curuser->access_right('Bulk change customer packages') ) { 
25   <% $s++ ? ' | ' : '' %>
26   <A HREF="<% $p %>edit/cust_pkg.cgi?<% $cust_main->custnum %>">Bulk order and cancel packages</A> (preserves services)
27 % } 
28
29
30 <BR><BR>
31 % if ( @$packages ) {
32
33 Current packages
34 % } 
35 % if ( $cust_main->num_cancelled_pkgs ) {
36 %     if ( $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
37 %          || ( $conf->exists('hidecancelledpackages')
38 %               && ! $cgi->param('showcancelledpackages')
39 %             )
40 %        )
41 %     {
42 %       $cgi->param('showcancelledpackages', 1);
43 %
44
45   ( <a href="<% $cgi->self_url %>">show
46 %   } else {
47 %       $cgi->param('showcancelledpackages', 0);
48 %
49
50   ( <a href="<% $cgi->self_url %>">hide
51 %   } 
52
53  cancelled packages</a> )
54 % } 
55 % if ( $num_old_packages ) {
56 %   $cgi->param('showoldpackages', 1);
57     ( <a href="<% $cgi->self_url %>">show old packages</a> )
58 % } elsif ( $cgi->param('showoldpackages') ) {
59 %   $cgi->param('showoldpackages', 0);
60     ( <a href="<% $cgi->self_url %>">hide old packages</a> )
61 % }
62 % if ( @$packages ) { 
63
64 <% include('/elements/table-grid.html') %>
65 % my $bgcolor1 = '#eeeeee';
66 %   my $bgcolor2 = '#ffffff';
67 %   my $bgcolor = '';
68
69 <TR>
70   <TH CLASS="grid" BGCOLOR="#cccccc">Package</TH>
71   <TH CLASS="grid" BGCOLOR="#cccccc">Status</TH>
72 % if ( $show_location ) {
73   <TH CLASS="grid" BGCOLOR="#cccccc">Location</TH>
74 % }
75   <TH CLASS="grid" BGCOLOR="#cccccc">Services</TH>
76 </TR>
77
78 % #$FS::cust_pkg::DEBUG = 2;
79 % foreach my $cust_pkg (@$packages) {
80 %
81 %   if ( $bgcolor eq $bgcolor1 ) {
82 %     $bgcolor = $bgcolor2;
83 %   } else {
84 %     $bgcolor = $bgcolor1;
85 %   }
86 %
87 %   my %iopt = (
88 %     'bgcolor'  => $bgcolor,
89 %     'cust_pkg' => $cust_pkg,
90 %     'part_pkg' => $cust_pkg->part_pkg,
91 %     %conf_opt,
92 %   );
93 %
94
95     <!--pkgnum: <% $cust_pkg->pkgnum %>-->
96     <TR>
97       <% include('packages/package.html',  %iopt) %>
98       <% include('packages/status.html',   %iopt) %>
99 % if ( $show_location ) {
100       <% include('packages/location.html', %iopt) %>
101 % }
102       <% include('packages/services.html', %iopt) %>
103     </TR>
104
105 % }
106
107 </TABLE>
108
109 % } else {
110 <BR>
111 % } 
112
113 % if ( $cgi->param('fragment') =~ /^cust_pkg(\d+)$/ ) {
114   <SCRIPT>
115     // IE-specific hack.  other browsers listen to #fragments
116     // is this even working?  or is the #target redirection just working cause
117     // we set the URL params differently?
118     var el = document.getElementById( 'cust_pkg<% $1 %>' );
119     if ( el ) el.scrollIntoView(true);
120   </SCRIPT>
121 % }
122
123 <%init>
124
125 my( $cust_main ) = @_;
126 my $conf = new FS::Conf;
127
128 my $curuser = $FS::CurrentUser::CurrentUser;
129
130 my( $packages, $num_old_packages ) = get_packages($cust_main, $conf);
131
132 my $show_location = $conf->exists('cust_pkg-always_show_location')
133                         || ( grep $_->locationnum, @$packages ); # ? '1' : '0';
134
135 my $countrydefault = scalar($conf->config('countrydefault')) || 'US';
136 my %conf_opt = (
137   #for services.html and status.html
138   'cust_pkg-display_times'    => $conf->exists('cust_pkg-display_times'),
139
140   #for status.html
141   'cust_pkg-show_autosuspend' => $conf->exists('cust_pkg-show_autosuspend'),
142   #for status.html pkg-balances
143   'pkg-balances'              => $conf->exists('pkg-balances'),
144   'money_char'                => ( $conf->config('money_char') || '$' ),
145
146   #for location.html
147   'countrydefault'            => $countrydefault,
148   'statedefault'              => ( scalar($conf->config('statedefault'))
149                                   || ($countrydefault eq 'US' ? 'CA' : '') ),
150   #for services.html
151   'svc_external-skip_manual'  => $conf->exists('svc_external-skip_manual'),
152   'legacy_link'               => $conf->exists('legacy_link'),
153   'svc_broadband-manage_link' => $conf->config('svc_broadband-manage_link'),
154 );
155
156 #subroutines
157
158 sub get_packages {
159   my $cust_main = shift or return undef;
160   my $conf = shift;
161
162   my $method;
163   if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
164      || ( $conf->exists('hidecancelledpackages')
165            && ! $cgi->param('showcancelledpackages') )
166      )
167   {
168     $method = 'ncancelled_pkgs';
169   } else {
170     $method = 'all_pkgs';
171   }
172
173   my $cust_pkg_fields =
174     join(', ', map { "cust_pkg.$_ AS $_"          } fields('cust_pkg') );
175
176   my $part_pkg_fields =
177     join(', ', map { "part_pkg.$_ AS part_pkg_$_" } fields('part_pkg') );
178
179   my $group_by =
180     join(', ', map "cust_pkg.$_", fields('cust_pkg') ). ', '.
181     join(', ', map "part_pkg.$_", fields('part_pkg') );
182
183   my $num_svcs = '( SELECT COUNT(*) FROM cust_svc '.
184                  '    WHERE cust_svc.pkgnum = cust_pkg.pkgnum ) AS num_svcs';
185
186   my @packages = $cust_main->$method( {
187     'select'    => "$cust_pkg_fields, $part_pkg_fields, $num_svcs",
188     'addl_from' => 'LEFT JOIN part_pkg USING ( pkgpart )',
189   } );
190   my $num_old_packages = scalar(@packages);
191
192   foreach my $cust_pkg ( @packages ) {
193     my %hash = $cust_pkg->hash;
194     my %part_pkg = map  { /^part_pkg_(.+)$/ or die; ( $1 => $hash{$_} ); }
195                    grep { /^part_pkg_/ } keys %hash;
196     $cust_pkg->{'_pkgpart'} = new FS::part_pkg \%part_pkg;
197   }
198
199   unless ( $cgi->param('showoldpackages') ) {
200     my $years = $conf->config('cust_main-packages-years') || 2;
201     my $seconds = 31556926; #60*60*24*365.2422 is close enough
202     my $then = time - $seconds;
203
204     my %hide = ( 'cancelled'       => 'cancel',
205                  'one-time charge' => 'setup',
206                );
207   
208     @packages =
209       grep { !exists($hide{$_->status}) or $_->get($hide{$_->status}) > $then
210              or $_->num_svcs #don't hide packages w/services
211            }
212            @packages;
213   }
214
215   $num_old_packages -= scalar(@packages);
216
217   ( \@packages, $num_old_packages );
218 }
219
220 </%init>