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