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