Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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 <TABLE>
82   <TR>
83     <TD ALIGN="left" VALIGN="top">
84
85 % if ( $cust_main->num_cancelled_pkgs ) {
86 %     if ( $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
87 %          || ( $conf->exists('hidecancelledpackages')
88 %               && ! $cgi->param('showcancelledpackages')
89 %             )
90 %        )
91 %     {
92 %       my $prev = $cgi->param('showcancelledpackages');
93 %       $cgi->param('showcancelledpackages', 1);
94   ( <a href="<% $cgi->self_url %>"><% mt('show') |h %> 
95 %       $cgi->param('showcancelledpackages', $prev);
96 %   } else {
97 %       $cgi->param('showcancelledpackages', 0);
98   ( <a href="<% $cgi->self_url %>"><% mt('hide') |h %> 
99 %       $cgi->param('showcancelledpackages', 1);
100 %   } 
101
102  <% mt('cancelled packages') |h %></a> )
103 % } 
104 % if ( $num_old_packages ) {
105 %   $cgi->param('showoldpackages', 1);
106     ( <a href="<% $cgi->self_url %>"><% mt('show old packages') |h %></a> )
107 % } elsif ( $cgi->param('showoldpackages') ) {
108 %   $cgi->param('showoldpackages', 0);
109     ( <a href="<% $cgi->self_url %>"><% mt('hide old packages') |h %></a> )
110 % }
111
112     </TD>
113
114   </TR>
115
116   <TR>
117     <TD COLSPAN=2>
118
119 %     $opt{cust_main} = $cust_main;
120 %     $opt{packages}  = \@packages;
121 %     $opt{cust_location_cache} = {};
122 %     if ( $conf->exists('cust_pkg-group_by_location') ) {
123         <& locations.html, %opt &>
124 %     } else { # in this format, put all packages in one section
125         <& /elements/table-grid.html &>
126         <& packages/section.html, %opt &>
127         </TABLE>
128 %     }
129
130     </TD>
131   </TR>
132
133 </TABLE>
134 <%init>
135
136 my $cust_main = shift;
137 my %opt = @_;
138 my $conf = new FS::Conf;
139
140 my $curuser = $FS::CurrentUser::CurrentUser;
141
142 my $countrydefault = scalar($conf->config('countrydefault')) || 'US';
143
144 my $hide_cancelled = 0;
145 if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
146    || ( $conf->exists('hidecancelledpackages')
147          && ! $cgi->param('showcancelledpackages') )
148    )
149 {
150   $hide_cancelled = 1;
151 }
152
153 my $cust_pkg_fields =
154   join(', ', map { "cust_pkg.$_ AS $_"          } fields('cust_pkg') );
155
156 my $part_pkg_fields =
157   join(', ', ( map { "part_pkg.$_ AS part_pkg_$_" } fields('part_pkg') ),
158              'setup_option.optionvalue AS part_pkg__setup_fee',
159              'recur_option.optionvalue AS part_pkg__recur_fee',
160       );
161
162 my $group_by =
163   join(', ', map "cust_pkg.$_", fields('cust_pkg') ). ', '.
164   join(', ', map "part_pkg.$_", fields('part_pkg') );
165
166 my $num_svcs = '( SELECT COUNT(*) FROM cust_svc '.
167                '    WHERE cust_svc.pkgnum = cust_pkg.pkgnum ) AS num_svcs';
168
169 # don't exclude cancelled packages at this stage
170 my @packages = $cust_main->all_pkgs( {
171   'select'    => "$cust_pkg_fields, $part_pkg_fields, $num_svcs",
172   'addl_from' => qq{
173     LEFT JOIN part_pkg USING ( pkgpart )
174     LEFT JOIN part_pkg_option AS setup_option
175       ON (     cust_pkg.pkgpart = setup_option.pkgpart
176            AND setup_option.optionname = 'setup_fee' )
177     LEFT JOIN part_pkg_option AS recur_option
178       ON (     cust_pkg.pkgpart = recur_option.pkgpart
179            AND recur_option.optionname = 'recur_fee' )
180   },
181 } );
182
183 my $is_anything_hidden = 0; # optimization
184
185 my %change_to_from; # target pkgnum => current cust_pkg, for future changes
186 my %changed_from; # old pkgnum => new cust_pkg, for past changes
187 my %supplementals_of; # main pkgnum => arrayref of supplementals
188
189 foreach my $cust_pkg ( @packages ) {
190   my %hash = $cust_pkg->hash;
191   my %part_pkg = map  { /^part_pkg_(.+)$/ or die; ( $1 => $hash{$_} ); }
192                  grep { /^part_pkg_/ } keys %hash;
193   $cust_pkg->{'_pkgpart'} = new FS::part_pkg \%part_pkg;
194   if ( $cust_pkg->change_to_pkgnum ) {
195     $change_to_from{$cust_pkg->change_to_pkgnum} = $cust_pkg;
196     $is_anything_hidden = 1;
197   }
198   if ( $cust_pkg->change_pkgnum ) {
199     $changed_from{$cust_pkg->change_pkgnum} = $cust_pkg;
200     $is_anything_hidden = 1;
201   }
202   if ( $cust_pkg->main_pkgnum ) {
203     $supplementals_of{$cust_pkg->main_pkgnum} ||= [];
204     push @{ $supplementals_of{$cust_pkg->main_pkgnum} }, $cust_pkg;
205     $is_anything_hidden = 1;
206   }
207 }
208
209 # filter out hidden package changes
210 if ( $is_anything_hidden ) {
211   my @displayable_packages;
212   foreach my $cust_pkg (@packages) {
213
214     # if this package has any supplemental packages, it should remember them
215     $cust_pkg->set('_supplemental', $supplementals_of{$cust_pkg->pkgnum});
216
217     if ( $cust_pkg->main_pkgnum ) {
218
219       # it's a supplemental package of something else, and shouldn't be on the
220       # root list
221
222     } elsif ( exists( $change_to_from{$cust_pkg->pkgnum} ) ) {
223
224       # $cust_pkg is an ordered, not-yet-active package change target
225       my $change_from = $change_to_from{ $cust_pkg->pkgnum };
226       $cust_pkg->set('change_from_pkg', $change_from);
227       $change_from->set('change_to_pkg', $cust_pkg);
228
229     } elsif ( exists( $changed_from{$cust_pkg->pkgnum} ) ) {
230
231       # $cust_pkg is a canceled package changed into another packge
232       # hide it under the destination package's history
233       my $changed_to = $changed_from{$cust_pkg->pkgnum};
234       $cust_pkg->set('changed_to_pkg', $changed_to);
235       $changed_to->set('changed_from_pkg', $cust_pkg);
236
237     } else {
238
239       push @displayable_packages, $cust_pkg;
240
241     }
242
243   }
244   @packages = @displayable_packages;
245 }
246
247 # filter all cancelled packages if the user wants
248 if ( $hide_cancelled ) {
249   @packages = grep { !$_->get('cancel') } @packages;
250 }
251
252 # filter out 'old' packages
253 my $num_old_packages = scalar(@packages);
254
255 unless ( $cgi->param('showoldpackages') ) {
256   my $years = $conf->config('cust_main-packages-years') || 2;
257   my $then = time - $years * 31556926; #60*60*24*365.2422 is close enough
258
259   my %hide = ( 'cancelled'       => 'cancel',
260                'one-time charge' => 'setup',
261              );
262
263   @packages =
264     grep { !exists($hide{$_->status}) or $_->get($hide{$_->status}) > $then
265            or $_->num_svcs #don't hide packages w/services
266          }
267          @packages;
268 }
269
270 $num_old_packages -= scalar(@packages);
271
272 # don't include supplemental packages in this list; they'll be found from
273 # their main packages
274 # (as will change-target packages)
275 ####@packages = grep !$_->main_pkgnum, @packages;
276
277 foreach my $cust_pkg ( @packages ) {
278   $cust_pkg->{'_cust_pkg_discount_active'} =
279    [ $cust_pkg->cust_pkg_discount_active ];
280 }
281
282 </%init>