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