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