740d1544a46f2f288263c7c73293e24ced0032a5
[freeside.git] / httemplate / view / cust_main / packages.html
1 % my $s = 0;
2
3 % if ( $curuser->access_right('Qualify service') ) { 
4   <% $s++ ? ' | ' : '' %>
5   <& qual_link.html, $cust_main &>
6 % }
7
8 % if ( $curuser->access_right('Order customer package') ) { 
9   <% $s++ ? ' | ' : '' %>
10   <& order_pkg_link.html, $cust_main &>
11 % } 
12
13 % if ( $curuser->access_right('One-time charge')
14 %        && $conf->config('payby-default') ne 'HIDE'
15 %      ) {
16   <% $s++ ? ' | ' : '' %>
17   <& one_time_charge_link.html, $cust_main &>
18 % } 
19
20 % if ( $curuser->access_right('Bulk change customer packages') ) { 
21   <% $s++ ? ' | ' : '' %>
22   <A HREF="<% $p %>edit/cust_pkg.cgi?<% $cust_main->custnum %>"><% mt('Bulk order and cancel packages') |h %></A> (<% mt('preserves services') |h %>)
23 % } 
24
25 <BR><BR>
26
27 <TABLE>
28   <TR>
29     <TD ALIGN="left">
30
31 % if ( @$packages ) {
32
33 <% mt('Current packages') |h %> 
34 % } 
35 % if ( $cust_main->num_cancelled_pkgs ) {
36 %     if ( $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
37 %          || ( $conf->exists('hidecancelledpackages')
38 %               && ! $cgi->param('showcancelledpackages')
39 %             )
40 %        )
41 %     {
42 %       my $prev = $cgi->param('showcancelledpackages');
43 %       $cgi->param('showcancelledpackages', 1);
44   ( <a href="<% $cgi->self_url %>"><% mt('show') |h %> 
45 %       $cgi->param('showcancelledpackages', $prev);
46 %   } else {
47 %       $cgi->param('showcancelledpackages', 0);
48   ( <a href="<% $cgi->self_url %>"><% mt('hide') |h %> 
49 %       $cgi->param('showcancelledpackages', 1);
50 %   } 
51
52  <% mt('cancelled packages') |h %></a> )
53 % } 
54 % if ( $num_old_packages ) {
55 %   $cgi->param('showoldpackages', 1);
56     ( <a href="<% $cgi->self_url %>"><% mt('show old packages') |h %></a> )
57 % } elsif ( $cgi->param('showoldpackages') ) {
58 %   $cgi->param('showoldpackages', 0);
59     ( <a href="<% $cgi->self_url %>"><% mt('hide old packages') |h %></a> )
60 % }
61
62     </TD>
63     <TD ALIGN="right">
64       <A HREF="<%$p%>search/report_cust_pkg.html?custnum=<% $cust_main->custnum %>"><% mt('Package reports') |h %></A>
65 % if ( $curuser->access_right('Qualify service') ) { 
66     | <A HREF="<%$p%>search/qual.cgi?custnum=<% $cust_main->custnum %>"><% mt('View Qualifications') |h %></A>
67 % }
68       <BR>
69       <% mt('Service reports:') |h %> 
70         <A HREF="<%$p%>search/report_svc_acct.html?custnum=<% $cust_main->custnum %>"><% mt('accounts') |h %></A><BR>
71       <% mt('Usage reports:') |h %> 
72         <A HREF="<%$p%>search/report_cdr.html?custnum=<% $cust_main->custnum %>"><% mt('CDRs') |h %></A>
73     </TD>
74   </TR>
75
76   <TR>
77     <TD COLSPAN=2>
78 % if ( $conf->exists('cust_pkg-group_by_location') and $show_location ) {
79 <& locations.html,
80     'cust_main'     => $cust_main,
81     'packages'      => $packages,
82  &>
83 % }
84 % else {
85 % # in this format, put all packages in one section
86 <& /elements/table-grid.html &>
87 <& packages/section.html,
88     'packages'      => $packages,
89     'show_location' => $show_location,
90  &>
91 </TABLE>
92 % }
93     </TD>
94   </TR>
95
96 % if ( $cgi->param('fragment') =~ /^cust_pkg(\d+)$/ ) {
97   <SCRIPT>
98     // IE-specific hack.  other browsers listen to #fragments
99     // is this even working?  or is the #target redirection just working cause
100     // we set the URL params differently?
101     var el = document.getElementById( 'cust_pkg<% $1 %>' );
102     if ( el ) el.scrollIntoView(true);
103   </SCRIPT>
104 % }
105 </TABLE>
106 <%init>
107
108 my $cust_main = shift;
109 my %opt = @_;
110 my $conf = new FS::Conf;
111
112 my $curuser = $FS::CurrentUser::CurrentUser;
113
114 my( $packages, $num_old_packages ) = get_packages($cust_main, $conf);
115
116
117 my $show_location = $conf->exists('cust_pkg-always_show_location') 
118                         || (grep $_->locationnum, @$packages); # ? '1' : '0';
119
120 my $countrydefault = scalar($conf->config('countrydefault')) || 'US';
121 #subroutines
122
123 sub get_packages {
124   my $cust_main = shift or return undef;
125   my $conf = shift;
126
127   my $method;
128   if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
129      || ( $conf->exists('hidecancelledpackages')
130            && ! $cgi->param('showcancelledpackages') )
131      )
132   {
133     $method = 'ncancelled_pkgs';
134   } else {
135     $method = 'all_pkgs';
136   }
137
138   my $cust_pkg_fields =
139     join(', ', map { "cust_pkg.$_ AS $_"          } fields('cust_pkg') );
140
141   my $part_pkg_fields =
142     join(', ', map { "part_pkg.$_ AS part_pkg_$_" } fields('part_pkg') );
143
144   my $group_by =
145     join(', ', map "cust_pkg.$_", fields('cust_pkg') ). ', '.
146     join(', ', map "part_pkg.$_", fields('part_pkg') );
147
148   my $num_svcs = '( SELECT COUNT(*) FROM cust_svc '.
149                  '    WHERE cust_svc.pkgnum = cust_pkg.pkgnum ) AS num_svcs';
150
151   my @packages = $cust_main->$method( {
152     'select'    => "$cust_pkg_fields, $part_pkg_fields, $num_svcs",
153     'addl_from' => 'LEFT JOIN part_pkg USING ( pkgpart )',
154   } );
155   my $num_old_packages = scalar(@packages);
156
157   foreach my $cust_pkg ( @packages ) {
158     my %hash = $cust_pkg->hash;
159     my %part_pkg = map  { /^part_pkg_(.+)$/ or die; ( $1 => $hash{$_} ); }
160                    grep { /^part_pkg_/ } keys %hash;
161     $cust_pkg->{'_pkgpart'} = new FS::part_pkg \%part_pkg;
162   }
163
164   unless ( $cgi->param('showoldpackages') ) {
165     my $years = $conf->config('cust_main-packages-years') || 2;
166     my $then = time - $years * 31556926; #60*60*24*365.2422 is close enough
167
168     my %hide = ( 'cancelled'       => 'cancel',
169                  'one-time charge' => 'setup',
170                );
171   
172     @packages =
173       grep { !exists($hide{$_->status}) or $_->get($hide{$_->status}) > $then
174              or $_->num_svcs #don't hide packages w/services
175            }
176            @packages;
177   }
178
179   $num_old_packages -= scalar(@packages);
180
181   ( \@packages, $num_old_packages );
182 }
183
184 </%init>