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