1 <& elements/search.html,
3 'name' => 'call detail records',
5 'count_query' => $count_query,
6 'count_addl' => $nototalminutes ? '' : [ $totalminutes_sub ],
7 'disable_total' => $topmode ? 1 : '',
11 'html_form' => qq!<FORM NAME="cdrForm" ACTION="$p/misc/cdr.cgi" METHOD="POST">!,
12 'html_foot' => $html_foot,
18 unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
20 my $edit_data = $FS::CurrentUser::CurrentUser->access_right('Edit rating data');
22 my $totalminutes_sub = sub {
24 sprintf("%.2f",$billsec/60) . ' total minutes';
27 my $conf = new FS::Conf;
29 my $title = 'Call Detail Records';
32 #process params for CDR search, populate $hashref...
33 # and fixup $count_query
41 my $str2time_sql = str2time_sql;
42 my $closing = str2time_sql_closing;
44 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
45 push @search, "$str2time_sql calldate $closing >= $beginning ",
46 "$str2time_sql calldate $closing <= $ending";
52 push @search, FS::UI::Web::parse_lt_gt($cgi, 'duration');
53 push @search, FS::UI::Web::parse_lt_gt($cgi, 'billsec');
55 #above here things just push @search
56 #below here things also have to define $hashref->{} or push @qsearch
57 my @qsearch = @search;
63 my $status = $cgi->param('freesidestatus');
64 if ( $status eq 'NULL' ) {
66 $title = "Unprocessed $title";
67 $hashref->{'freesidestatus'} = ''; # Record.pm will take care of it
68 push @search, "( freesidestatus IS NULL OR freesidestatus = '' )";
70 } elsif ( $status =~ /^([\w ]+)$/ ) {
72 if ( $status eq 'done' ) {
73 $title = "Processed $title";
75 elsif ( $status eq 'failed' ) {
76 $title = "Skipped $title";
78 $hashref->{'freesidestatus'} = $1;
79 push @search, "freesidestatus = '$1'";
87 if ( $cgi->param('cdrtypenum') =~ /^(\d+)$/ ) {
88 $hashref->{'cdrtypenum'} = $1;
89 push @search, "cdrtypenum = $1";
91 elsif ( $cgi->param('cdrtypenum') eq 'none' ) {
92 $hashref->{'cdrtypenum'} = '';
93 push @search, "cdrtypenum is null";
100 foreach my $param ( grep /^termpart\d+status$/, $cgi->param ) {
102 my $status = $cgi->param($param);
104 $param =~ /^termpart(\d+)status$/ or die 'guru meditation 54something';
108 if ( $status eq 'NULL' ) {
110 #false lazienss w/cdr_termination.pm (i should be a part_termination method)
112 "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
113 #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
115 "NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
117 } elsif ( $status =~ /^([\w ]+)$/ ) {
119 #false lazienss w/cdr_termination.pm (i should be a part_termination method)
121 "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart AND status = '$1' ) ";
122 #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
124 "EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
129 push @search, $search;
130 push @qsearch, $search;
136 # src/dest/charged_party/svcnum
139 my $phonenum = qr/^\s*([\d\-\+\ ]+)\s*$/;
141 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
142 $phonenum = qr/^\s*([\d\-\+\ A-Za-z]+)\s*$/;
143 $x = qr/[^\dA-Za-z]/;
146 if ( $cgi->param('src') =~ $phonenum ) {
147 ( my $src = $1 ) =~ s/$x//g;
148 $hashref->{'src'} = $src;
149 push @search, "src = '$src'";
152 if ( $cgi->param('dst') ) {
154 my @d = map { $_, "1$_" } split(/\s*,\s*/, $cgi->param('dst') );
156 my $search = 'dst IN ('. join(',', map dbh->quote($_), @d). ')';
158 push @search, $search;
159 push @qsearch, $search;
163 if ( $cgi->param('dcontext') =~ /^\s*(.+)\s*$/ ) {
165 $hashref->{'dcontext'} = $dcontext;
166 push @search, 'dcontext = '. dbh->quote($dcontext);
169 if ( $cgi->param('charged_party') ) {
171 my @cp = map { $_, "1$_" }
172 split(/\s*,\s*/, $cgi->param('charged_party') );
174 my $search = 'charged_party IN ('. join(',', map dbh->quote($_), @cp). ')';
176 push @search, $search;
177 push @qsearch, $search;
180 if ( $cgi->param('charged_party_or_src') ) {
182 my @cp = map { $_, "1$_" }
183 split(/\s*,\s*/, $cgi->param('charged_party_or_src') );
184 my $in = join(',', map dbh->quote($_), @cp);
186 my $search = "( charged_party IN ($in) OR src IN ($in) )";
188 push @search, $search;
189 push @qsearch, $search;
192 if ( $cgi->param('lastapp') =~ /^\s*(.+)\s*$/ ) {
194 $hashref->{'lastapp'} = $lastapp;
195 push @search, 'lastapp = '. dbh->quote($lastapp);
198 if ( $cgi->param('svcnum') =~ /^([\d, ]+)$/ ) {
200 my $search = "svcnum IN ($svcnum)";
201 push @search, $search;
202 push @qsearch, $search;
208 foreach my $field ('src_ip_addr','dst_ip_addr') {
209 if ( $cgi->param($field) ) {
210 my $search = FS::cdr->ip_addr_sql($field, $cgi->param($field));
211 push @search, $search;
212 push @qsearch, $search;
217 # cdrbatchnum (or legacy cdrbatch)
220 if ( $cgi->param('cdrbatch') ) {
223 qsearchs('cdr_batch', { 'cdrbatch' => scalar($cgi->param('cdrbatch')) } );
225 $hashref->{cdrbatchnum} = $cdr_batch->cdrbatchnum;
226 push @search, 'cdrbatchnum = '. $cdr_batch->cdrbatchnum;
228 die "unknown cdrbatch ". $cgi->param('cdrbatch');
231 } elsif ( $cgi->param('cdrbatchnum') ne '__ALL__' ) {
233 if ( $cgi->param('cdrbatchnum') eq '' ) {
234 my $search = "( cdrbatchnum IS NULL )";
235 push @qsearch, $search;
236 push @search, $search;
237 } elsif ( $cgi->param('cdrbatchnum') =~ /^(\d+)$/ ) {
238 $hashref->{cdrbatchnum} = $1;
239 push @search, "cdrbatchnum = $1";
248 if ( $cgi->param('acctid') =~ /\d/ ) {
249 my $acctid = $cgi->param('acctid');
250 $acctid =~ s/\r\n/\n/g; #browsers?
251 my @acctid = map { /^\s*(\d+)\s*$/ or die "guru meditation #4"; $1; }
252 grep { /^\s*(\d+)\s*$/ }
253 split(/\n/, $acctid);
255 my $search = 'acctid IN ( '. join(',', @acctid). ' )';
256 push @qsearch, $search;
257 push @search, $search;
265 my $nototalminutes = 0;
267 my $search = join(' AND ', @search);
268 $search = "WHERE $search" if $search;
270 my $count_query = "SELECT COUNT(*), sum(billsec) FROM cdr $search";
272 my $qsearch = join(' AND ', @qsearch);
273 $qsearch = ( scalar(keys %$hashref) ? ' AND ' : ' WHERE ' ) . $qsearch
276 my $query = { 'table' => 'cdr',
277 'hashref' => $hashref,
278 'extra_sql' => $qsearch,
279 'order_by' => 'ORDER BY calldate',
282 if ( $cgi->param('longest') =~ /^(\d+)$/ && $cgi->param('longest') > 0 ) {
283 $cgi->param('maxrecords',$1);
284 $count_query = "SELECT $1";
285 $query->{'order_by'} = 'ORDER BY billsec desc';
294 my %header = %{ FS::cdr->table_info->{'fields'} };
296 my @first = qw( acctid calldate clid charged_party src dst dcontext );
297 my %first = map { $_=>1 } @first;
299 my @fields = ( @first, grep !$first{$_}, fields('cdr') );
301 if ( $cgi->param('show') ) {
302 @fields = grep $cgi->param("show_$_"), @fields;
306 if ( exists($header{$_}) ) {
310 $header =~ s/\_/ /g; #//wtf
314 unshift @header, ' ';
316 my $date_sub_factory = sub {
320 my $date = shift->$column();
321 $date ? time2str( '%Y-%m-%d %T', $date ) : ''; #config time2str format?
326 #any other formatters?
327 map { $_ => &{ $date_sub_factory }($_) } qw( startdate answerdate enddate )
332 sub { $_[0]->svcnum ? [ $p.'view/svc_phone.cgi?', 'svcnum' ] : ''; },
335 @fields = map { exists($fields{$_}) ? $fields{$_} : $_ } @fields;
336 unshift @fields, sub {
337 return '' unless $edit_data;
339 my $acctid = $cdr->acctid;
340 qq!<INPUT NAME="acctid$acctid" TYPE="checkbox" VALUE="1">!;
343 my @links = ( map { exists($links{$_}) ? $links{$_} : '' } @fields );
352 if ( $cgi->param('top_dst') =~ /^(\d+)$/ && $cgi->param('top_dst') > 0 ) {
355 @header = ('Destination');
357 elsif ( $cgi->param('top_src') =~ /^(\d+)$/ && $cgi->param('top_src') > 0 ) {
360 @header = ('Source');
364 my $whichfield = $fields[0];
365 $query = { 'select' => "$whichfield, count(1) as cnt",
368 'extra_sql' => "$search
372 $count_query = "SELECT $topmode";
374 # this is weird, maybe we should change search.html
375 $cgi->param('maxrecords',$topmode);
378 my $link = "${p}search/cdr.html?".$cgi->query_string;
379 $link =~ s/;top_(dst|src)=(\d+|)//g;
380 $link =~ s/;maxrecords=(\d+|)//;
381 $link =~ s/;(src|dst)=(\d+|)//g;
382 $link = [ "$link;$whichfield=", sub { shift->$whichfield } ];
386 push @header, '# Calls';
391 my $html_foot = include('/search/elements/checkbox-foot.html',
393 { submit => "reprocess selected",
395 confirm => "Are you sure you want to reprocess the selected CDRs?" },
396 { submit => "delete selected",
398 confirm => "Are you sure you want to delete the selected CDRs?" },