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