page large customer package lists, RT#39822
[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
46 </STYLE>
47 % # activate rolldown buttons for hidden package blocks
48 <SCRIPT TYPE="text/javascript">
49 function toggle_rolldown() {
50   var up_arrow = <% decode_entities('&#x2b06') |js_string %>;
51   var dn_arrow = <% decode_entities('&#x2b07') |js_string %>;
52   var pkgnum = this.id.replace('rolldown_', '');
53   var hidden = document.getElementById('cust_pkg'+pkgnum+'_block');
54   if (hidden.style.display == 'none') {
55     hidden.style.display = '';
56     this.textContent = this.textContent.replace(dn_arrow, up_arrow);
57   } else {
58     hidden.style.display = 'none';
59     this.textContent = this.textContent.replace(up_arrow, dn_arrow);
60   }
61 }
62 <&| /elements/onload.js &>
63 var el;
64 % if ( $cgi->param('fragment') =~ /^cust_pkg(\d+)$/ ) {
65 % # IE-specific hack, but also unhide the row if it's in a hidden block
66 el = document.getElementById('cust_pkg<% $1 %>');
67 % }
68 var all_buttons = document.getElementsByClassName('rolldown_button');
69 for (var i = 0; i < all_buttons.length; i++) {
70   all_buttons[i].onclick = toggle_rolldown;
71   var block_id = all_buttons[i].id.replace('rolldown_', '');
72   if ( el && document.getElementById('cust_pkg'+block_id+'_block')
73                      .contains(el)
74      ) {
75     // then toggle it now
76     all_buttons[i].click();
77   }
78 }
79 if ( el ) el.scrollIntoView(true);
80 </&>
81 </SCRIPT>
82 <TABLE>
83   <TR>
84     <TD ALIGN="left" VALIGN="top">
85
86 % if ( $cust_main->num_cancelled_pkgs ) {
87 %     if ( $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
88 %          || ( $conf->exists('hidecancelledpackages')
89 %               && ! $cgi->param('showcancelledpackages')
90 %             )
91 %        )
92 %     {
93 %       my $prev = $cgi->param('showcancelledpackages');
94 %       $cgi->param('showcancelledpackages', 1);
95   ( <a href="<% $cgi->self_url %>"><% mt('show') |h %> 
96 %       $cgi->param('showcancelledpackages', $prev);
97 %   } else {
98 %       $cgi->param('showcancelledpackages', 0);
99   ( <a href="<% $cgi->self_url %>"><% mt('hide') |h %> 
100 %       $cgi->param('showcancelledpackages', 1);
101 %   } 
102
103  <% mt('cancelled packages') |h %></a> )
104 % } 
105
106 % if ( $cgi->param('showoldpackages') ) {
107 %   $cgi->param('showoldpackages', 0);
108     ( <a href="<% $cgi->self_url %>"><% mt('hide old packages') |h %></a> )
109 %   $cgi->param('showoldpackages', 1);
110 % } else {
111 %   $cgi->param('showoldpackages', 1);
112     ( <a href="<% $cgi->self_url %>"><% mt('show old packages') |h %></a> )
113 %   $cgi->param('showoldpackages', 0);
114 % }
115
116     </TD>
117
118   </TR>
119
120 % my $pager = include('/elements/pager.html',
121 %                       offset     => $offset,
122 %                       num_rows   => scalar(@packages),
123 %                       total      => $num_pkgs,
124 %                       maxrecords => $maxrecords,
125 %             );
126 % if ( $num_pkgs > $maxrecords ) {
127   <TR>
128     <TD COLSPAN=2>
129       <% $pager %>
130     </TD>
131   </TR>
132 % }
133
134   <TR>
135     <TD COLSPAN=2>
136
137 %     $opt{cust_main} = $cust_main;
138 %     $opt{packages}  = \@packages;
139 %     $opt{cust_location_cache} = {};
140 %     if ( $conf->exists('cust_pkg-group_by_location') ) {
141         <& locations.html, %opt &>
142 %     } else { # in this format, put all packages in one section
143         <& /elements/table-grid.html &>
144         <& packages/section.html, %opt &>
145         </TABLE>
146 %     }
147
148     </TD>
149   </TR>
150
151 % if ( $num_pkgs > $maxrecords ) {
152   <TR>
153     <TD COLSPAN=2>
154       <% $pager %>
155     </TD>
156   </TR>
157 % }
158
159 </TABLE>
160 <%init>
161
162 my $cust_main = shift;
163 my %opt = @_;
164 my $conf = new FS::Conf;
165
166 my $curuser = $FS::CurrentUser::CurrentUser;
167
168 my $countrydefault = scalar($conf->config('countrydefault')) || 'US';
169
170 my $hide_cancelled = 0;
171 if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
172    || ( $conf->exists('hidecancelledpackages')
173          && ! $cgi->param('showcancelledpackages') )
174    )
175 {
176   $hide_cancelled = 1;
177 }
178
179 my $cust_pkg_fields =
180   join(', ', map { "cust_pkg.$_ AS $_"          } fields('cust_pkg') );
181
182 my $part_pkg_fields =
183   join(', ', ( map { "part_pkg.$_ AS part_pkg_$_" } fields('part_pkg') ),
184              'setup_option.optionvalue AS part_pkg__setup_fee',
185              'recur_option.optionvalue AS part_pkg__recur_fee',
186       );
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 $addl_from = '
196   LEFT JOIN part_pkg USING ( pkgpart )
197   LEFT JOIN cust_pkg AS chgto   ON ( chgto.change_to_pkgnum = cust_pkg.pkgnum )
198   LEFT JOIN cust_pkg AS chgfrom ON ( chgfrom.change_pkgnum  = cust_pkg.pkgnum )
199 ';
200
201 my $extra_sql =
202   ' AND cust_pkg.main_pkgnum IS NULL '. # supplemental package of something else
203   ' AND chgto.pkgnum IS NULL '. # ordered, not-yet-active change target
204   ' AND chgfrom.pkgnum IS NULL ';    # canceled package changed into another
205
206 unless ( $cgi->param('showoldpackages') ) {
207   my $years = $conf->config('cust_main-packages-years') || 2;
208   my $then = time - $years * 31556926; #60*60*24*365.2422 is close enough
209
210   $extra_sql .= " AND (
211        ( part_pkg.freq  =  '0'
212            AND ( cust_pkg.setup  IS NULL OR cust_pkg.setup  > $then )
213        )
214     OR ( part_pkg.freq != '0'
215           AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel > $then )
216        )
217     OR EXISTS ( SELECT 1 FROM cust_svc WHERE cust_svc.pkgnum = cust_pkg.pkgnum )
218   )";
219 }
220
221 my $num_method = $hide_cancelled ? 'ncancelled_pkgs' : 'all_pkgs';
222 my $num_pkgs = $cust_main->$num_method({ 
223   'addl_from' => $addl_from,
224   'extra_sql' => $extra_sql,
225 });
226
227 my $maxrecords = 10;
228 my $offset = $cgi->param('offset') =~ /^(\d+)$/ ? $1 : 0;
229
230 my @packages = $cust_main->all_pkgs( {
231   'select'    => "$cust_pkg_fields, $part_pkg_fields, $num_svcs",
232   'addl_from' => $addl_from. "
233     LEFT JOIN part_pkg_option AS setup_option
234       ON (     cust_pkg.pkgpart = setup_option.pkgpart
235            AND setup_option.optionname = 'setup_fee' )
236     LEFT JOIN part_pkg_option AS recur_option
237       ON (     cust_pkg.pkgpart = recur_option.pkgpart
238            AND recur_option.optionname = 'recur_fee' )
239   ",
240   'extra_sql' => $extra_sql,
241   'order_by'  => "ORDER BY pkgnum ASC LIMIT $maxrecords OFFSET $offset",
242 } );
243
244 foreach my $cust_pkg ( @packages ) {
245
246   my %hash = $cust_pkg->hash;
247   my %part_pkg = map  { /^part_pkg_(.+)$/ or die; ( $1 => $hash{$_} ); }
248                  grep { /^part_pkg_/ } keys %hash;
249   $cust_pkg->{'_pkgpart'} = new FS::part_pkg \%part_pkg;
250
251   #arrayref of supplementals
252   $cust_pkg->set('_supplemental', [
253     qsearch('cust_pkg', { main_pkgnum=>$cust_pkg->pkgnum })
254   ] );
255
256   #for future changes
257   if ( $cust_pkg->change_to_pkgnum ) {
258     my $change_to =
259       qsearchs('cust_pkg', { pkgnum=>$cust_pkg->change_to_pkgnum });
260     $cust_pkg->set('change_to_pkg', $change_to);
261     $change_to->set('change_from_pkg', $cust_pkg);
262   }
263
264   #for past changes
265   setfrom($cust_pkg);
266
267   $cust_pkg->{'_cust_pkg_discount_active'} =
268    [ $cust_pkg->cust_pkg_discount_active ];
269
270 }
271
272 sub setfrom {
273   my $cust_pkg = shift;
274
275   if ( $cust_pkg->change_pkgnum ) {
276     my $changed_from =
277       qsearchs('cust_pkg', { pkgnum=>$cust_pkg->change_pkgnum });
278     $cust_pkg->set('changed_from_pkg', $changed_from);
279     $changed_from->set('changed_to_pkg', $cust_pkg);
280
281     setfrom($changed_from);
282
283   }
284
285 }
286
287 </%init>