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