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