optimize customer view when there's lots of packages; *really* avoid looking up any...
[freeside.git] / httemplate / view / cust_main / packages.html
1 <A NAME="cust_pkg"><FONT SIZE="+2">Packages</FONT></A><BR>
2
3 % my $s = 0;
4 % if ( $curuser->access_right('Order customer package') ) { 
5   <% $s++ ? ' | ' : '' %>
6   <% include( '/elements/popup_link-cust_main.html',
7                 'action'      => $p. 'misc/order_pkg.html',
8                 'label'       => 'Order&nbsp;new&nbsp;package',
9                 'actionlabel' => 'Order new package',
10                 'color'       => '#333399',
11                 'cust_main'   => $cust_main,
12                 'closetext'   => 'Close',
13                 'width'       => 763,
14             )
15   %>
16 % } 
17
18 % if ( $curuser->access_right('One-time charge')
19 %        && $conf->config('payby-default') ne 'HIDE'
20 %      ) {
21   <% $s++ ? ' | ' : '' %>
22   <% include('one_time_charge_link.html', $cust_main) %>
23 % } 
24
25 % if ( $curuser->access_right('Bulk change customer packages') ) { 
26   <% $s++ ? ' | ' : '' %>
27   <A HREF="<% $p %>edit/cust_pkg.cgi?<% $cust_main->custnum %>">Bulk order and cancel packages</A> (preserves services)
28 % } 
29
30
31 <BR><BR>
32 % if ( @$packages ) {
33
34 Current packages
35 % } 
36 % if ( $cust_main->num_cancelled_pkgs ) {
37 %     if ( $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
38 %          || ( $conf->exists('hidecancelledpackages')
39 %               && ! $cgi->param('showcancelledpackages')
40 %             )
41 %        )
42 %     {
43 %       $cgi->param('showcancelledpackages', 1);
44 %
45
46   ( <a href="<% $cgi->self_url %>">show
47 %   } else {
48 %       $cgi->param('showcancelledpackages', 0);
49 %
50
51   ( <a href="<% $cgi->self_url %>">hide
52 %   } 
53
54  cancelled packages</a> )
55 % } 
56 % if ( @$packages ) { 
57
58 <% include('/elements/table-grid.html') %>
59 % my $bgcolor1 = '#eeeeee';
60 %   my $bgcolor2 = '#ffffff';
61 %   my $bgcolor = '';
62
63 <TR>
64   <TH CLASS="grid" BGCOLOR="#cccccc">Package</TH>
65   <TH CLASS="grid" BGCOLOR="#cccccc">Status</TH>
66 % if ( $show_location ) {
67   <TH CLASS="grid" BGCOLOR="#cccccc">Location</TH>
68 % }
69   <TH CLASS="grid" BGCOLOR="#cccccc">Services</TH>
70 </TR>
71
72 % foreach my $cust_pkg (@$packages) {
73 %
74 %   if ( $bgcolor eq $bgcolor1 ) {
75 %     $bgcolor = $bgcolor2;
76 %   } else {
77 %     $bgcolor = $bgcolor1;
78 %   }
79 %
80 %   my %iopt = (
81 %     'bgcolor'  => $bgcolor,
82 %     'cust_pkg' => $cust_pkg,
83 %     'part_pkg' => $cust_pkg->part_pkg,
84 %     %conf_opt,
85 %   );
86
87     <!--pkgnum: <% $cust_pkg->pkgnum %>-->
88     <TR>
89       <% include('packages/package.html',  %iopt) %>
90       <% include('packages/status.html',   %iopt) %>
91 % if ( $show_location ) {
92       <% include('packages/location.html', %iopt) %>
93 % }
94       <% include('packages/services.html', %iopt) %>
95     </TR>
96
97 % }
98
99 </TABLE>
100
101 % } else {
102 <BR>
103 % } 
104
105 % if ( $cgi->param('fragment') =~ /^cust_pkg(\d+)$/ ) {
106   <SCRIPT>
107     // IE-specific hack.  other browsers listen to #fragments
108     // is this even working?  or is the #target redirection just working cause
109     // we set the URL params differently?
110     var el = document.getElementById( 'cust_pkg<% $1 %>' );
111     if ( el ) el.scrollIntoView(true);
112   </SCRIPT>
113 % }
114
115 <%init>
116
117 my( $cust_main ) = @_;
118 my $conf = new FS::Conf;
119
120 my $curuser = $FS::CurrentUser::CurrentUser;
121
122 my $packages = get_packages($cust_main, $conf);
123
124 my $show_location = $conf->exists('cust_pkg-always_show_location')
125                         || ( grep $_->locationnum, @$packages ); # ? '1' : '0';
126
127 my $countrydefault = scalar($conf->config('countrydefault')) || 'US';
128 my %conf_opt = (
129   #for services.html and status.html
130   'cust_pkg-display_times'    => $conf->exists('cust_pkg-display_times'),
131
132   #for status.html
133   'cust_pkg-show_autosuspend' => $conf->exists('cust_pkg-show_autosuspend'),
134
135   #for location.html
136   'countrydefault'            => $countrydefault,
137   'statedefault'              => ( scalar($conf->config('statedefault'))
138                                   || ($countrydefault eq 'US' ? 'CA' : '') ),
139   #for services.html
140   'svc_external-skip_manual'  => $conf->exists('svc_external-skip_manual'),
141   'legacy_link'               => $conf->exists('legacy_link'),
142 );
143
144 #subroutines
145
146 sub get_packages {
147   my $cust_main = shift or return undef;
148   my $conf = shift;
149   
150   my @packages = ();
151   my $method;
152   if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
153      || ( $conf->exists('hidecancelledpackages')
154            && ! $cgi->param('showcancelledpackages') )
155      )
156   {
157     $method = 'ncancelled_pkgs';
158   } else {
159     $method = 'all_pkgs';
160   }
161
162   [ $cust_main->$method() ];
163 }
164
165 </%init>