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