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