500632d5507e01deaa121189fad3844f6269b27e
[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
125 %     $opt{cust_main} = $cust_main;
126 %     $opt{packages}  = $packages;
127 %     $opt{cust_location_cache} = {};
128 %     if ( $conf->exists('cust_pkg-group_by_location') ) {
129         <& locations.html, %opt &>
130 %     } else { # in this format, put all packages in one section
131         <& /elements/table-grid.html &>
132         <& packages/section.html, %opt &>
133         </TABLE>
134 %     }
135
136     </TD>
137   </TR>
138
139 % if ( $cgi->param('fragment') =~ /^cust_pkg(\d+)$/ ) {
140   <SCRIPT>
141     // IE-specific hack.  other browsers listen to #fragments
142     // is this even working?  or is the #target redirection just working cause
143     // we set the URL params differently?
144     var el = document.getElementById( 'cust_pkg<% $1 %>' );
145     if ( el ) el.scrollIntoView(true);
146   </SCRIPT>
147 % }
148 </TABLE>
149 <%init>
150
151 my $cust_main = shift;
152 my %opt = @_;
153 my $conf = new FS::Conf;
154
155 my $curuser = $FS::CurrentUser::CurrentUser;
156
157 my( $packages, $num_old_packages ) = get_packages($cust_main, $conf);
158
159 my $countrydefault = scalar($conf->config('countrydefault')) || 'US';
160 #subroutines
161
162 sub get_packages {
163   my $cust_main = shift or return undef;
164   my $conf = shift;
165
166   my $method;
167   if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
168      || ( $conf->exists('hidecancelledpackages')
169            && ! $cgi->param('showcancelledpackages') )
170      )
171   {
172     $method = 'ncancelled_pkgs';
173   } else {
174     $method = 'all_pkgs';
175   }
176
177   my $cust_pkg_fields =
178     join(', ', map { "cust_pkg.$_ AS $_"          } fields('cust_pkg') );
179
180   my $part_pkg_fields =
181   join(', ', ( map { "part_pkg.$_ AS part_pkg_$_" } fields('part_pkg') ),
182              'setup_option.optionvalue AS part_pkg__setup_fee',
183              'recur_option.optionvalue AS part_pkg__recur_fee',
184       );
185
186   my $group_by =
187     join(', ', map "cust_pkg.$_", fields('cust_pkg') ). ', '.
188     join(', ', map "part_pkg.$_", fields('part_pkg') );
189
190   my $num_svcs = '( SELECT COUNT(*) FROM cust_svc '.
191                  '    WHERE cust_svc.pkgnum = cust_pkg.pkgnum ) AS num_svcs';
192
193   my @packages = $cust_main->$method( {
194     'select'    => "$cust_pkg_fields, $part_pkg_fields, $num_svcs",
195     'addl_from' => qq{
196       LEFT JOIN part_pkg USING ( pkgpart )
197       LEFT JOIN part_pkg_option AS setup_option
198         ON (     cust_pkg.pkgpart = setup_option.pkgpart
199              AND setup_option.optionname = 'setup_fee' )
200       LEFT JOIN part_pkg_option AS recur_option
201         ON (     cust_pkg.pkgpart = recur_option.pkgpart
202              AND recur_option.optionname = 'recur_fee' )
203     },
204   } );
205   my $num_old_packages = scalar(@packages);
206
207   my %change_to_from; # target pkgnum => current cust_pkg, for future changes
208
209   foreach my $cust_pkg ( @packages ) {
210     my %hash = $cust_pkg->hash;
211     my %part_pkg = map  { /^part_pkg_(.+)$/ or die; ( $1 => $hash{$_} ); }
212                    grep { /^part_pkg_/ } keys %hash;
213     $cust_pkg->{'_pkgpart'} = new FS::part_pkg \%part_pkg;
214     if ( $cust_pkg->change_to_pkgnum ) {
215       $change_to_from{$cust_pkg->change_to_pkgnum} = $cust_pkg;
216     }
217   }
218
219   if ( keys %change_to_from ) {
220     my @not_future_packages;
221     foreach my $cust_pkg (@packages) {
222       if ( exists( $change_to_from{$cust_pkg->pkgnum} ) ) {
223         my $change_from = $change_to_from{ $cust_pkg->pkgnum };
224         $cust_pkg->set('change_from_pkg', $change_from);
225         $change_from->set('change_to_pkg', $cust_pkg);
226       } else {
227         push @not_future_packages, $cust_pkg;
228       }
229     }
230     @packages = @not_future_packages;
231   }
232
233   unless ( $cgi->param('showoldpackages') ) {
234     my $years = $conf->config('cust_main-packages-years') || 2;
235     my $then = time - $years * 31556926; #60*60*24*365.2422 is close enough
236
237     my %hide = ( 'cancelled'       => 'cancel',
238                  'one-time charge' => 'setup',
239                );
240   
241     @packages =
242       grep { !exists($hide{$_->status}) or $_->get($hide{$_->status}) > $then
243              or $_->num_svcs #don't hide packages w/services
244            }
245            @packages;
246   }
247
248   $num_old_packages -= scalar(@packages);
249   
250   # don't include supplemental packages in this list; they'll be found from
251   # their main packages
252   # (as will change-target packages)
253   @packages = grep !$_->main_pkgnum, @packages;
254
255   foreach my $cust_pkg ( @packages ) {
256     $cust_pkg->{'_cust_pkg_discount_active'} =
257      [ $cust_pkg->cust_pkg_discount_active ];
258   }
259
260   ( \@packages, $num_old_packages );
261 }
262
263 </%init>