large package display in customer/package search, #13364
[freeside.git] / httemplate / search / svc_phone.cgi
1 <% include( 'elements/search.html',
2                  'title'             => "Phone number search results",
3                  'name'              => 'phone numbers',
4                  'query'             => $sql_query,
5                  'count_query'       => $count_query,
6                  'redirect'          => $redirect,
7                  'header'            => [ '#',
8                                           'Service',
9                                           'Country code',
10                                           'Phone number',
11                                           @header,
12                                           FS::UI::Web::cust_header(),
13                                         ],
14                  'fields'            => [ 'svcnum',
15                                           'svc',
16                                           'countrycode',
17                                           'phonenum',
18                                           @fields,
19                                           \&FS::UI::Web::cust_fields,
20                                         ],
21                  'links'             => [ $link,
22                                           $link,
23                                           $link,
24                                           $link,
25                                           ( map '', @header ),
26                                           ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
27                                                 FS::UI::Web::cust_header()
28                                           ),
29                                         ],
30                  'align' => 'rlrr'.
31                             join('', map 'r', @header).
32                             FS::UI::Web::cust_aligns(),
33                  'color' => [ 
34                               '',
35                               '',
36                               '',
37                               '',
38                               ( map '', @header ),
39                               FS::UI::Web::cust_colors(),
40                             ],
41                  'style' => [ 
42                               '',
43                               '',
44                               '',
45                               '',
46                               ( map '', @header ),
47                               FS::UI::Web::cust_styles(),
48                             ],
49               )
50 %>
51 <%init>
52
53 die "access denied"
54   unless $FS::CurrentUser::CurrentUser->access_right('List services');
55
56 my $conf = new FS::Conf;
57
58 my @select = ();
59 my %svc_phone = ();
60 my @extra_sql = ();
61 my $orderby = 'ORDER BY svcnum';
62
63 my @header = ();
64 my @fields = ();
65 my $link = [ "${p}view/svc_phone.cgi?", 'svcnum' ];
66 my $redirect = $link;
67
68 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
69
70   push @extra_sql, 'pkgnum IS NULL'
71     if $cgi->param('magic') eq 'unlinked';
72
73   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
74     my $sortby = $1;
75     $orderby = "ORDER BY $sortby";
76   }
77
78   if ( $cgi->param('usage_total') ) {
79
80     my($beginning,$ending) = FS::UI::Web::parse_beginning_ending($cgi, 'usage');
81
82     $redirect = '';
83
84     #my $and_date = " AND startdate >= $beginning AND startdate <= $ending ";
85     my $and_date = " AND enddate >= $beginning AND enddate <= $ending ";
86
87     my $fromwhere = " FROM cdr WHERE cdr.svcnum = svc_phone.svcnum $and_date";
88
89     #more efficient to join against cdr just once... this will do for now
90     push @select, map { " ( SELECT SUM($_) $fromwhere ) AS $_ " }
91                       qw( billsec rated_price );
92
93     my $money_char = $conf->config('money_char') || '$';
94
95     push @header, 'Minutes', 'Billed';
96     push @fields, 
97       sub { sprintf('%.3f', shift->get('billsec') / 60 ); },
98       sub { $money_char. sprintf('%.2f', shift->get('rated_price') ); };
99
100     #XXX and termination... (this needs a config to turn on, not by default)
101     if ( 1 ) { # $conf->exists('cdr-termination_hack') { #}
102
103       my $f_w =
104         " FROM cdr_termination LEFT JOIN cdr USING ( acctid ) ".
105         " WHERE cdr.carrierid = CAST(svc_phone.phonenum AS BIGINT) ". # XXX connectone-specific, has to match svc_external.id :/
106         $and_date;
107
108       push @select,
109         " ( SELECT SUM(billsec) $f_w ) AS term_billsec ",
110         " ( SELECT SUM(cdr_termination.rated_price) $f_w ) AS term_rated_price";
111
112       push @header, 'Term Min', 'Term Billed';
113       push @fields,
114         sub { sprintf('%.3f', shift->get('term_billsec') / 60 ); },
115         sub { $money_char. sprintf('%.2f', shift->get('rated_price') ); };
116
117     }
118                  
119
120   }
121
122 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
123   push @extra_sql, "svcpart = $1";
124 } else {
125   $cgi->param('phonenum') =~ /^([\d\- ]+)$/; 
126   ( $svc_phone{'phonenum'} = $1 ) =~ s/\D//g;
127 }
128
129 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
130                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
131                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
132                 ' LEFT JOIN cust_main USING ( custnum ) ';
133
134 #here is the agent virtualization
135 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql(
136                    'null_right' => 'View/link unlinked services'
137                  );
138
139 my $extra_sql = '';
140 if ( @extra_sql ) {
141   $extra_sql = ( keys(%svc_phone) ? ' AND ' : ' WHERE ' ).
142                join(' AND ', @extra_sql );
143 }
144
145 my $count_query = "SELECT COUNT(*) FROM svc_phone $addl_from ";
146 if ( keys %svc_phone ) {
147   $count_query .= ' WHERE '.
148                     join(' AND ', map "$_ = ". dbh->quote($svc_phone{$_}),
149                                       keys %svc_phone
150                         );
151 }
152 $count_query .= $extra_sql;
153
154 my $sql_query = {
155   'table'     => 'svc_phone',
156   'hashref'   => \%svc_phone,
157   'select'    => join(', ',
158                    'svc_phone.*',
159                    'part_svc.svc',
160                    @select,
161                    'cust_main.custnum',
162                    FS::UI::Web::cust_sql_fields(),
163                  ),
164   'extra_sql' => $extra_sql,
165   'order_by'  => $orderby,
166   'addl_from' => $addl_from,
167 };
168
169 #smaller false laziness w/svc_*.cgi here
170 my $link_cust = sub {
171   my $svc_x = shift;
172   $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
173 };
174
175 </%init>