optimizations for large package lists, RT#28526
[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 table.hiddenrows {
26   width: 80%;
27   margin-left: 100px;
28   border: 1px solid #7E0079;
29   background-color: #cccccc;
30 }
31
32 .hiddenrows td {
33   text-align: center;
34 }
35
36 .rolldown_button {
37   min-width: 80px;
38   margin-left: 100px;
39   min-height: 20px;
40   background-color: #efefef;
41   border: 1px solid #7e0079;
42   z-index: 1;
43   text-align: center;
44 }
45 </STYLE>
46 % # activate rolldown buttons for hidden package blocks
47 <SCRIPT TYPE="text/javascript">
48 function toggle_rolldown() {
49   var up_arrow = <% decode_entities('&#x2b06') |js_string %>;
50   var dn_arrow = <% decode_entities('&#x2b07') |js_string %>;
51   var pkgnum = this.id.replace('rolldown_', '');
52   var hidden = document.getElementById('cust_pkg'+pkgnum+'_block');
53   if (hidden.style.display == 'none') {
54     hidden.style.display = '';
55     this.textContent = this.textContent.replace(dn_arrow, up_arrow);
56   } else {
57     hidden.style.display = 'none';
58     this.textContent = this.textContent.replace(up_arrow, dn_arrow);
59   }
60 }
61 <&| /elements/onload.js &>
62 var el;
63 % if ( $cgi->param('fragment') =~ /^cust_pkg(\d+)$/ ) {
64 % # IE-specific hack, but also unhide the row if it's in a hidden block
65 el = document.getElementById('cust_pkg<% $1 %>');
66 % }
67 var all_buttons = document.getElementsByClassName('rolldown_button');
68 for (var i = 0; i < all_buttons.length; i++) {
69   all_buttons[i].onclick = toggle_rolldown;
70   var block_id = all_buttons[i].id.replace('rolldown_', '');
71   if ( el && document.getElementById('cust_pkg'+block_id+'_block')
72                      .contains(el)
73      ) {
74     // then toggle it now
75     all_buttons[i].click();
76   }
77 }
78 if ( el ) el.scrollIntoView(true);
79 </&>
80 </SCRIPT>
81 % unless ( $opt{no_links} ) {
82
83 %   my $s = 0;
84
85 % if ( $curuser->access_right('Qualify service') ) { 
86   <% $s++ ? ' | ' : '' %>
87   <& qual_link.html, $cust_main &>
88 % }
89
90 % if ( $curuser->access_right('Order customer package') ) { 
91   <% $s++ ? ' | ' : '' %>
92   <& /elements/order_pkg_link.html, 'cust_main'=>$cust_main &>
93 % } 
94
95 % if ( $curuser->access_right('One-time charge')
96 %        && $conf->config('payby-default') ne 'HIDE'
97 %      ) {
98   <% $s++ ? ' | ' : '' %>
99   <& one_time_charge_link.html, $cust_main &>
100 % } 
101
102 % if ( $curuser->access_right('Bulk move customer services') ) { 
103   <% $s++ ? ' | ' : '' %>
104
105   <& /elements/popup_link-cust_main.html,
106                'label'       => emt('Move services between packages'),
107                'action'      => "${p}edit/bulk-cust_svc-pkgnum.html",
108                'cust_main'   => $cust_main,
109                'actionlabel' => emt('Move services'),
110                'width'       => 968, #763,
111                'height'      => 575,
112   &>
113
114 % } 
115
116 % if ( $curuser->access_right('Bulk change customer packages') ) { 
117   <% $s++ ? ' | ' : '' %>
118   <A HREF="<% $p %>edit/cust_pkg.cgi?<% $cust_main->custnum %>"><% mt('Bulk order and cancel packages') |h %></A>
119 % } 
120
121 <BR><BR>
122
123 % } # unless $opt{no_links}
124
125 <TABLE>
126   <TR>
127     <TD ALIGN="left" VALIGN="top">
128
129 % if ( $cust_main->num_cancelled_pkgs ) {
130 %     if ( $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
131 %          || ( $conf->exists('hidecancelledpackages')
132 %               && ! $cgi->param('showcancelledpackages')
133 %             )
134 %        )
135 %     {
136 %       my $prev = $cgi->param('showcancelledpackages');
137 %       $cgi->param('showcancelledpackages', 1);
138   ( <a href="<% $cgi->self_url %>"><% mt('show') |h %> 
139 %       $cgi->param('showcancelledpackages', $prev);
140 %   } else {
141 %       $cgi->param('showcancelledpackages', 0);
142   ( <a href="<% $cgi->self_url %>"><% mt('hide') |h %> 
143 %       $cgi->param('showcancelledpackages', 1);
144 %   } 
145
146  <% mt('cancelled packages') |h %></a> )
147 % } 
148 % if ( $num_old_packages ) {
149 %   $cgi->param('showoldpackages', 1);
150     ( <a href="<% $cgi->self_url %>"><% mt('show old packages') |h %></a> )
151 % } elsif ( $cgi->param('showoldpackages') ) {
152 %   $cgi->param('showoldpackages', 0);
153     ( <a href="<% $cgi->self_url %>"><% mt('hide old packages') |h %></a> )
154 % }
155
156     </TD>
157
158 %   unless ( $opt{no_links} ) {
159
160     <TD ALIGN="right">
161       <A HREF="<%$p%>search/report_cust_pkg.html?custnum=<% $cust_main->custnum %>"><% mt('Package reports') |h %></A>
162 % if ( $curuser->access_right('Qualify service') ) { 
163     | <A HREF="<%$p%>search/qual.cgi?custnum=<% $cust_main->custnum %>"><% mt('View Qualifications') |h %></A>
164 % }
165       <BR>
166       <% mt('Service reports:') |h %> 
167         <A HREF="<%$p%>search/report_svc_acct.html?custnum=<% $cust_main->custnum %>"><% mt('accounts') |h %></A><BR>
168       <% mt('Usage reports:') |h %> 
169         <A HREF="<%$p%>search/report_cdr.html?custnum=<% $cust_main->custnum %>"><% mt('CDRs') |h %></A>
170     </TD>
171
172 %   } # unless $opt{no_links}
173
174   </TR>
175
176   <TR>
177     <TD COLSPAN=2>
178
179 %     $opt{cust_main} = $cust_main;
180 %     $opt{packages}  = \@packages;
181 %     $opt{cust_location_cache} = {};
182 %     if ( $conf->exists('cust_pkg-group_by_location') ) {
183         <& locations.html, %opt &>
184 %     } else { # in this format, put all packages in one section
185         <& /elements/table-grid.html &>
186         <& packages/section.html, %opt &>
187         </TABLE>
188 %     }
189
190     </TD>
191   </TR>
192
193 </TABLE>
194 <%init>
195
196 my $cust_main = shift;
197 my %opt = @_;
198 my $conf = new FS::Conf;
199
200 my $curuser = $FS::CurrentUser::CurrentUser;
201
202 my $countrydefault = scalar($conf->config('countrydefault')) || 'US';
203
204 my $hide_changed = $curuser->option('hide_package_changes');
205
206 my $hide_cancelled = 0;
207 if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
208    || ( $conf->exists('hidecancelledpackages')
209          && ! $cgi->param('showcancelledpackages') )
210    )
211 {
212   $hide_cancelled = 1;
213 }
214
215 my $cust_pkg_fields =
216   join(', ', map { "cust_pkg.$_ AS $_"          } fields('cust_pkg') );
217
218 my $part_pkg_fields =
219   join(', ', ( map { "part_pkg.$_ AS part_pkg_$_" } fields('part_pkg') ),
220              'setup_option.optionvalue AS part_pkg__setup_fee',
221              'recur_option.optionvalue AS part_pkg__recur_fee',
222       );
223
224 my $group_by =
225   join(', ', map "cust_pkg.$_", fields('cust_pkg') ). ', '.
226   join(', ', map "part_pkg.$_", fields('part_pkg') );
227
228 my $num_svcs = '( SELECT COUNT(*) FROM cust_svc '.
229                '    WHERE cust_svc.pkgnum = cust_pkg.pkgnum ) AS num_svcs';
230
231 # don't exclude cancelled packages at this stage
232 my @packages = $cust_main->all_pkgs( {
233   'select'    => "$cust_pkg_fields, $part_pkg_fields, $num_svcs",
234   'addl_from' => qq{
235     LEFT JOIN part_pkg USING ( pkgpart )
236     LEFT JOIN part_pkg_option AS setup_option
237       ON (     cust_pkg.pkgpart = setup_option.pkgpart
238            AND setup_option.optionname = 'setup_fee' )
239     LEFT JOIN part_pkg_option AS recur_option
240       ON (     cust_pkg.pkgpart = recur_option.pkgpart
241            AND recur_option.optionname = 'recur_fee' )
242   },
243 } );
244
245 my %change_to_from; # target pkgnum => current cust_pkg, for future changes
246 my %changed_from; # old pkgnum => new cust_pkg, for past changes
247
248 foreach my $cust_pkg ( @packages ) {
249   my %hash = $cust_pkg->hash;
250   my %part_pkg = map  { /^part_pkg_(.+)$/ or die; ( $1 => $hash{$_} ); }
251                  grep { /^part_pkg_/ } keys %hash;
252   $cust_pkg->{'_pkgpart'} = new FS::part_pkg \%part_pkg;
253   if ( $cust_pkg->change_to_pkgnum ) {
254     $change_to_from{$cust_pkg->change_to_pkgnum} = $cust_pkg;
255   }
256   if ( $cust_pkg->change_pkgnum ) {
257     $changed_from{$cust_pkg->change_pkgnum} = $cust_pkg;
258   }
259 }
260
261 # filter out hidden package changes
262 if ( keys %change_to_from or keys %changed_from ) {
263   my @displayable_packages;
264   foreach my $cust_pkg (@packages) {
265     if ( exists( $change_to_from{$cust_pkg->pkgnum} ) ) {
266       # $cust_pkg is an ordered, not-yet-active package change target
267       my $change_from = $change_to_from{ $cust_pkg->pkgnum };
268       $cust_pkg->set('change_from_pkg', $change_from);
269       $change_from->set('change_to_pkg', $cust_pkg);
270     } elsif ( exists( $changed_from{$cust_pkg->pkgnum} ) ) {
271       # $cust_pkg is a canceled package changed into another packge
272       my $changed_to = $changed_from{$cust_pkg->pkgnum};
273       if ( ( $hide_changed eq 'all' ) or
274            ( $hide_changed eq 'location' 
275               and $changed_to->pkgpart  == $cust_pkg->pkgpart
276               and $changed_to->refnum   == $cust_pkg->refnum
277               and $changed_to->quantity == $cust_pkg->quantity )
278       ) {
279         # then we're hiding it
280         $cust_pkg->set('changed_to_pkg', $changed_to);
281         $changed_to->set('changed_from_pkg', $cust_pkg);
282       } else { # show it anyway
283         push @displayable_packages, $cust_pkg;
284       }
285     } else {
286       push @displayable_packages, $cust_pkg;
287     }
288   }
289   @packages = @displayable_packages;
290 }
291
292 # filter all cancelled packages if the user wants
293 if ( $hide_cancelled ) {
294   @packages = grep { !$_->get('cancel') } @packages;
295 }
296
297 # filter out 'old' packages
298 my $num_old_packages = scalar(@packages);
299
300 unless ( $cgi->param('showoldpackages') ) {
301   my $years = $conf->config('cust_main-packages-years') || 2;
302   my $then = time - $years * 31556926; #60*60*24*365.2422 is close enough
303
304   my %hide = ( 'cancelled'       => 'cancel',
305                'one-time charge' => 'setup',
306              );
307
308   @packages =
309     grep { !exists($hide{$_->status}) or $_->get($hide{$_->status}) > $then
310            or $_->num_svcs #don't hide packages w/services
311          }
312          @packages;
313 }
314
315 $num_old_packages -= scalar(@packages);
316
317 # don't include supplemental packages in this list; they'll be found from
318 # their main packages
319 # (as will change-target packages)
320 @packages = grep !$_->main_pkgnum, @packages;
321
322 foreach my $cust_pkg ( @packages ) {
323   $cust_pkg->{'_cust_pkg_discount_active'} =
324    [ $cust_pkg->cust_pkg_discount_active ];
325 }
326
327 </%init>