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