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 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') and $show_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     'show_location' => $show_location,
117  &>
118 </TABLE>
119 % }
120     </TD>
121   </TR>
122
123 % if ( $cgi->param('fragment') =~ /^cust_pkg(\d+)$/ ) {
124   <SCRIPT>
125     // IE-specific hack.  other browsers listen to #fragments
126     // is this even working?  or is the #target redirection just working cause
127     // we set the URL params differently?
128     var el = document.getElementById( 'cust_pkg<% $1 %>' );
129     if ( el ) el.scrollIntoView(true);
130   </SCRIPT>
131 % }
132 </TABLE>
133 <%init>
134
135 my $cust_main = shift;
136 my %opt = @_;
137 my $conf = new FS::Conf;
138
139 my $curuser = $FS::CurrentUser::CurrentUser;
140
141 my( $packages, $num_old_packages ) = get_packages($cust_main, $conf);
142
143
144 my $show_location = $conf->exists('cust_pkg-always_show_location') 
145   || (grep $_->locationnum ne $cust_main->ship_locationnum, @$packages);
146
147 my $countrydefault = scalar($conf->config('countrydefault')) || 'US';
148 #subroutines
149
150 sub get_packages {
151   my $cust_main = shift or return undef;
152   my $conf = shift;
153
154   my $method;
155   if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
156      || ( $conf->exists('hidecancelledpackages')
157            && ! $cgi->param('showcancelledpackages') )
158      )
159   {
160     $method = 'ncancelled_pkgs';
161   } else {
162     $method = 'all_pkgs';
163   }
164
165   my $cust_pkg_fields =
166     join(', ', map { "cust_pkg.$_ AS $_"          } fields('cust_pkg') );
167
168   my $part_pkg_fields =
169     join(', ', map { "part_pkg.$_ AS part_pkg_$_" } fields('part_pkg') );
170
171   my $group_by =
172     join(', ', map "cust_pkg.$_", fields('cust_pkg') ). ', '.
173     join(', ', map "part_pkg.$_", fields('part_pkg') );
174
175   my $num_svcs = '( SELECT COUNT(*) FROM cust_svc '.
176                  '    WHERE cust_svc.pkgnum = cust_pkg.pkgnum ) AS num_svcs';
177
178   my @packages = $cust_main->$method( {
179     'select'    => "$cust_pkg_fields, $part_pkg_fields, $num_svcs",
180     'addl_from' => 'LEFT JOIN part_pkg USING ( pkgpart )',
181   } );
182   my $num_old_packages = scalar(@packages);
183
184   foreach my $cust_pkg ( @packages ) {
185     my %hash = $cust_pkg->hash;
186     my %part_pkg = map  { /^part_pkg_(.+)$/ or die; ( $1 => $hash{$_} ); }
187                    grep { /^part_pkg_/ } keys %hash;
188     $cust_pkg->{'_pkgpart'} = new FS::part_pkg \%part_pkg;
189   }
190
191   unless ( $cgi->param('showoldpackages') ) {
192     my $years = $conf->config('cust_main-packages-years') || 2;
193     my $then = time - $years * 31556926; #60*60*24*365.2422 is close enough
194
195     my %hide = ( 'cancelled'       => 'cancel',
196                  'one-time charge' => 'setup',
197                );
198   
199     @packages =
200       grep { !exists($hide{$_->status}) or $_->get($hide{$_->status}) > $then
201              or $_->num_svcs #don't hide packages w/services
202            }
203            @packages;
204   }
205
206   $num_old_packages -= scalar(@packages);
207   
208   # don't include supplemental packages in this list; they'll be found from
209   # their main packages
210   @packages = grep !$_->main_pkgnum, @packages;
211
212   ( \@packages, $num_old_packages );
213 }
214
215 </%init>