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