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