add latitude/longitude to prospects, customers and package locations, RT#15539
[freeside.git] / httemplate / view / cust_main / packages.html
1 % my $s = 0;
2
3 % if ( $curuser->access_right('Qualify service') ) { 
4   <% $s++ ? ' | ' : '' %>
5   <& qual_link.html, $cust_main &>
6 % }
7
8 % if ( $curuser->access_right('Order customer package') ) { 
9   <% $s++ ? ' | ' : '' %>
10   <& order_pkg_link.html, $cust_main &>
11 % } 
12
13 % if ( $curuser->access_right('One-time charge')
14 %        && $conf->config('payby-default') ne 'HIDE'
15 %      ) {
16   <% $s++ ? ' | ' : '' %>
17   <& one_time_charge_link.html, $cust_main &>
18 % } 
19
20 % if ( $curuser->access_right('Bulk change customer packages') ) { 
21   <% $s++ ? ' | ' : '' %>
22   <A HREF="<% $p %>edit/cust_pkg.cgi?<% $cust_main->custnum %>"><% mt('Bulk order and cancel packages') |h %></A> (<% mt('preserves services') |h %>)
23 % } 
24
25 <BR><BR>
26
27 <TABLE>
28   <TR>
29     <TD ALIGN="left" VALIGN="top">
30
31 % if ( @$packages ) {
32
33 <% mt('Current packages') |h %> 
34 % } 
35 % if ( $cust_main->num_cancelled_pkgs ) {
36 %     if ( $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
37 %          || ( $conf->exists('hidecancelledpackages')
38 %               && ! $cgi->param('showcancelledpackages')
39 %             )
40 %        )
41 %     {
42 %       my $prev = $cgi->param('showcancelledpackages');
43 %       $cgi->param('showcancelledpackages', 1);
44   ( <a href="<% $cgi->self_url %>"><% mt('show') |h %> 
45 %       $cgi->param('showcancelledpackages', $prev);
46 %   } else {
47 %       $cgi->param('showcancelledpackages', 0);
48   ( <a href="<% $cgi->self_url %>"><% mt('hide') |h %> 
49 %       $cgi->param('showcancelledpackages', 1);
50 %   } 
51
52  <% mt('cancelled packages') |h %></a> )
53 % } 
54 % if ( $num_old_packages ) {
55 %   $cgi->param('showoldpackages', 1);
56     ( <a href="<% $cgi->self_url %>"><% mt('show old packages') |h %></a> )
57 % } elsif ( $cgi->param('showoldpackages') ) {
58 %   $cgi->param('showoldpackages', 0);
59     ( <a href="<% $cgi->self_url %>"><% mt('hide old packages') |h %></a> )
60 % }
61
62     </TD>
63     <TD ALIGN="right">
64       <A HREF="<%$p%>search/report_cust_pkg.html?custnum=<% $cust_main->custnum %>"><% mt('Package reports') |h %></A>
65 % if ( $curuser->access_right('Qualify service') ) { 
66     | <A HREF="<%$p%>search/qual.cgi?custnum=<% $cust_main->custnum %>"><% mt('View Qualifications') |h %></A>
67 % }
68       <BR>
69       <% mt('Service reports:') |h %> 
70         <A HREF="<%$p%>search/report_svc_acct.html?custnum=<% $cust_main->custnum %>"><% mt('accounts') |h %></A><BR>
71       <% mt('Usage reports:') |h %> 
72         <A HREF="<%$p%>search/report_cdr.html?custnum=<% $cust_main->custnum %>"><% mt('CDRs') |h %></A>
73     </TD>
74   </TR>
75
76   <TR>
77     <TD COLSPAN=2>
78 % if ( $conf->exists('cust_pkg-group_by_location') and $show_location ) {
79 <& locations.html,
80     'cust_main'     => $cust_main,
81     'packages'      => $packages,
82  &>
83 % }
84 % else {
85 % # in this format, put all packages in one section
86 <& /elements/table-grid.html &>
87 <& packages/section.html,
88     'cust_main'     => $cust_main,
89     'packages'      => $packages,
90     'show_location' => $show_location,
91  &>
92 </TABLE>
93 % }
94     </TD>
95   </TR>
96
97 % if ( $cgi->param('fragment') =~ /^cust_pkg(\d+)$/ ) {
98   <SCRIPT>
99     // IE-specific hack.  other browsers listen to #fragments
100     // is this even working?  or is the #target redirection just working cause
101     // we set the URL params differently?
102     var el = document.getElementById( 'cust_pkg<% $1 %>' );
103     if ( el ) el.scrollIntoView(true);
104   </SCRIPT>
105 % }
106 </TABLE>
107 <%init>
108
109 my $cust_main = shift;
110 my %opt = @_;
111 my $conf = new FS::Conf;
112
113 my $curuser = $FS::CurrentUser::CurrentUser;
114
115 my( $packages, $num_old_packages ) = get_packages($cust_main, $conf);
116
117
118 my $show_location = $conf->exists('cust_pkg-always_show_location') 
119                         || (grep $_->locationnum, @$packages); # ? '1' : '0';
120
121 my $countrydefault = scalar($conf->config('countrydefault')) || 'US';
122 #subroutines
123
124 sub get_packages {
125   my $cust_main = shift or return undef;
126   my $conf = shift;
127
128   my $method;
129   if (  $cgi->param('showcancelledpackages') eq '0' #see if it was set by me
130      || ( $conf->exists('hidecancelledpackages')
131            && ! $cgi->param('showcancelledpackages') )
132      )
133   {
134     $method = 'ncancelled_pkgs';
135   } else {
136     $method = 'all_pkgs';
137   }
138
139   my $cust_pkg_fields =
140     join(', ', map { "cust_pkg.$_ AS $_"          } fields('cust_pkg') );
141
142   my $part_pkg_fields =
143     join(', ', map { "part_pkg.$_ AS part_pkg_$_" } fields('part_pkg') );
144
145   my $group_by =
146     join(', ', map "cust_pkg.$_", fields('cust_pkg') ). ', '.
147     join(', ', map "part_pkg.$_", fields('part_pkg') );
148
149   my $num_svcs = '( SELECT COUNT(*) FROM cust_svc '.
150                  '    WHERE cust_svc.pkgnum = cust_pkg.pkgnum ) AS num_svcs';
151
152   my @packages = $cust_main->$method( {
153     'select'    => "$cust_pkg_fields, $part_pkg_fields, $num_svcs",
154     'addl_from' => 'LEFT JOIN part_pkg USING ( pkgpart )',
155   } );
156   my $num_old_packages = scalar(@packages);
157
158   foreach my $cust_pkg ( @packages ) {
159     my %hash = $cust_pkg->hash;
160     my %part_pkg = map  { /^part_pkg_(.+)$/ or die; ( $1 => $hash{$_} ); }
161                    grep { /^part_pkg_/ } keys %hash;
162     $cust_pkg->{'_pkgpart'} = new FS::part_pkg \%part_pkg;
163   }
164
165   unless ( $cgi->param('showoldpackages') ) {
166     my $years = $conf->config('cust_main-packages-years') || 2;
167     my $then = time - $years * 31556926; #60*60*24*365.2422 is close enough
168
169     my %hide = ( 'cancelled'       => 'cancel',
170                  'one-time charge' => 'setup',
171                );
172   
173     @packages =
174       grep { !exists($hide{$_->status}) or $_->get($hide{$_->status}) > $then
175              or $_->num_svcs #don't hide packages w/services
176            }
177            @packages;
178   }
179
180   $num_old_packages -= scalar(@packages);
181
182   ( \@packages, $num_old_packages );
183 }
184
185 </%init>