merge NG auth, RT#21563
[freeside.git] / httemplate / view / cust_main / packages.html
1 <STYLE TYPE="text/css">
2 td.package {
3   vertical-align: top;
4   border-width: 0;
5   border-style: solid;
6   border-color: #bbbbff;
7 }
8 table.package {
9   border: none;
10   padding: 0;
11   border-spacing: 0;
12   width: 100%;
13 }
14 table.usage {
15   border: 1px solid black;
16   margin: auto;
17   width: 60%;
18   border-spacing: 0px;
19 }
20 .shared > * {
21   background-color: #ffffaa;
22 }
23 .row0 { background-color: #eeeeee; }
24 .row1 { background-color: #ffffff; }
25
26 </STYLE>
27 % my $s = 0;
28
29 % if ( $curuser->access_right('Qualify service') ) { 
30   <% $s++ ? ' | ' : '' %>
31   <& qual_link.html, $cust_main &>
32 % }
33
34 % if ( $curuser->access_right('Order customer package') ) { 
35   <% $s++ ? ' | ' : '' %>
36   <& /elements/order_pkg_link.html, 'cust_main'=>$cust_main &>
37 % } 
38
39 % if ( $curuser->access_right('One-time charge')
40 %        && $conf->config('payby-default') ne 'HIDE'
41 %      ) {
42   <% $s++ ? ' | ' : '' %>
43   <& one_time_charge_link.html, $cust_main &>
44 % } 
45
46 % if ( $curuser->access_right('Bulk change customer packages') ) { 
47   <% $s++ ? ' | ' : '' %>
48   <A HREF="<% $p %>edit/cust_pkg.cgi?<% $cust_main->custnum %>"><% mt('Bulk order and cancel packages') |h %></A> (<% mt('preserves services') |h %>)
49 % } 
50
51 <BR><BR>
52
53 <TABLE>
54   <TR>
55     <TD ALIGN="left" VALIGN="top">
56
57 % if ( @$packages ) {
58
59 <% mt('Current packages') |h %> 
60 % } 
61 % if ( $cust_main->num_cancelled_pkgs ) {
62 %     if ( $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
63 %          || ( $conf->exists('hidecancelledpackages')
64 %               && ! $cgi->param('showcancelledpackages')
65 %             )
66 %        )
67 %     {
68 %       my $prev = $cgi->param('showcancelledpackages');
69 %       $cgi->param('showcancelledpackages', 1);
70   ( <a href="<% $cgi->self_url %>"><% mt('show') |h %> 
71 %       $cgi->param('showcancelledpackages', $prev);
72 %   } else {
73 %       $cgi->param('showcancelledpackages', 0);
74   ( <a href="<% $cgi->self_url %>"><% mt('hide') |h %> 
75 %       $cgi->param('showcancelledpackages', 1);
76 %   } 
77
78  <% mt('cancelled packages') |h %></a> )
79 % } 
80 % if ( $num_old_packages ) {
81 %   $cgi->param('showoldpackages', 1);
82     ( <a href="<% $cgi->self_url %>"><% mt('show old packages') |h %></a> )
83 % } elsif ( $cgi->param('showoldpackages') ) {
84 %   $cgi->param('showoldpackages', 0);
85     ( <a href="<% $cgi->self_url %>"><% mt('hide old packages') |h %></a> )
86 % }
87
88     </TD>
89     <TD ALIGN="right">
90       <A HREF="<%$p%>search/report_cust_pkg.html?custnum=<% $cust_main->custnum %>"><% mt('Package reports') |h %></A>
91 % if ( $curuser->access_right('Qualify service') ) { 
92     | <A HREF="<%$p%>search/qual.cgi?custnum=<% $cust_main->custnum %>"><% mt('View Qualifications') |h %></A>
93 % }
94       <BR>
95       <% mt('Service reports:') |h %> 
96         <A HREF="<%$p%>search/report_svc_acct.html?custnum=<% $cust_main->custnum %>"><% mt('accounts') |h %></A><BR>
97       <% mt('Usage reports:') |h %> 
98         <A HREF="<%$p%>search/report_cdr.html?custnum=<% $cust_main->custnum %>"><% mt('CDRs') |h %></A>
99     </TD>
100   </TR>
101
102   <TR>
103     <TD COLSPAN=2>
104 % if ( $conf->exists('cust_pkg-group_by_location') ) {
105 <& locations.html,
106     'cust_main'     => $cust_main,
107     'packages'      => $packages,
108  &>
109 % }
110 % else {
111 % # in this format, put all packages in one section
112 <& /elements/table-grid.html &>
113 <& packages/section.html,
114     'cust_main'     => $cust_main,
115     'packages'      => $packages,
116  &>
117 </TABLE>
118 % }
119     </TD>
120   </TR>
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 </TABLE>
132 <%init>
133
134 my $cust_main = shift;
135 my %opt = @_;
136 my $conf = new FS::Conf;
137
138 my $curuser = $FS::CurrentUser::CurrentUser;
139
140 my( $packages, $num_old_packages ) = get_packages($cust_main, $conf);
141
142 my $countrydefault = scalar($conf->config('countrydefault')) || 'US';
143 #subroutines
144
145 sub get_packages {
146   my $cust_main = shift or return undef;
147   my $conf = shift;
148
149   my $method;
150   if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
151      || ( $conf->exists('hidecancelledpackages')
152            && ! $cgi->param('showcancelledpackages') )
153      )
154   {
155     $method = 'ncancelled_pkgs';
156   } else {
157     $method = 'all_pkgs';
158   }
159
160   my $cust_pkg_fields =
161     join(', ', map { "cust_pkg.$_ AS $_"          } fields('cust_pkg') );
162
163   my $part_pkg_fields =
164     join(', ', map { "part_pkg.$_ AS part_pkg_$_" } fields('part_pkg') );
165
166   my $group_by =
167     join(', ', map "cust_pkg.$_", fields('cust_pkg') ). ', '.
168     join(', ', map "part_pkg.$_", fields('part_pkg') );
169
170   my $num_svcs = '( SELECT COUNT(*) FROM cust_svc '.
171                  '    WHERE cust_svc.pkgnum = cust_pkg.pkgnum ) AS num_svcs';
172
173   my @packages = $cust_main->$method( {
174     'select'    => "$cust_pkg_fields, $part_pkg_fields, $num_svcs",
175     'addl_from' => 'LEFT JOIN part_pkg USING ( pkgpart )',
176   } );
177   my $num_old_packages = scalar(@packages);
178
179   foreach my $cust_pkg ( @packages ) {
180     my %hash = $cust_pkg->hash;
181     my %part_pkg = map  { /^part_pkg_(.+)$/ or die; ( $1 => $hash{$_} ); }
182                    grep { /^part_pkg_/ } keys %hash;
183     $cust_pkg->{'_pkgpart'} = new FS::part_pkg \%part_pkg;
184   }
185
186   unless ( $cgi->param('showoldpackages') ) {
187     my $years = $conf->config('cust_main-packages-years') || 2;
188     my $then = time - $years * 31556926; #60*60*24*365.2422 is close enough
189
190     my %hide = ( 'cancelled'       => 'cancel',
191                  'one-time charge' => 'setup',
192                );
193   
194     @packages =
195       grep { !exists($hide{$_->status}) or $_->get($hide{$_->status}) > $then
196              or $_->num_svcs #don't hide packages w/services
197            }
198            @packages;
199   }
200
201   $num_old_packages -= scalar(@packages);
202   
203   # don't include supplemental packages in this list; they'll be found from
204   # their main packages
205   @packages = grep !$_->main_pkgnum, @packages;
206
207   ( \@packages, $num_old_packages );
208 }
209
210 </%init>