1 <% include( '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 #false laziness w/queue.html
15 '<BR><INPUT TYPE="button" VALUE="select all" onClick="setAll(true)">'.
16 '<INPUT TYPE="button" VALUE="unselect all" onClick="setAll(false)">'.
17 qq!<BR><INPUT TYPE="submit" NAME="action" VALUE="reprocess selected" onClick="return confirm('Are you sure you want to reprocess the selected CDRs?')">!.
18 qq!<INPUT TYPE="submit" NAME="action" VALUE="delete selected" onClick="return confirm('Are you sure you want to delete the selected CDRs?')"><BR>!.
19 '<SCRIPT TYPE="text/javascript">'.
20 ' function setAll(setTo) { '.
21 ' theForm = document.cdrForm;'.
22 ' for (i=0,n=theForm.elements.length;i<n;i++)'.
23 ' if (theForm.elements[i].name.indexOf("acctid") != -1)'.
24 ' theForm.elements[i].checked = setTo;'.
36 unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
38 my $edit_data = $FS::CurrentUser::CurrentUser->access_right('Edit rating data');
40 my $totalminutes_sub = sub {
42 sprintf("%.2f",$billsec/60) . ' total minutes';
45 my $conf = new FS::Conf;
49 my $title = 'Call Detail Records';
52 #process params for CDR search, populate $hashref...
53 # and fixup $count_query
61 my $str2time_sql = str2time_sql;
62 my $closing = str2time_sql_closing;
64 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
65 push @search, "$str2time_sql calldate $closing >= $beginning ",
66 "$str2time_sql calldate $closing <= $ending";
72 push @search, FS::UI::Web::parse_lt_gt($cgi, 'duration');
73 push @search, FS::UI::Web::parse_lt_gt($cgi, 'billsec');
75 #above here things just push @search
76 #below here things also have to define $hashref->{} or push @qsearch
77 my @qsearch = @search;
83 if ( $cgi->param('freesidestatus') eq 'NULL' ) {
85 $title = "Unprocessed $title";
86 $hashref->{'freesidestatus'} = ''; # Record.pm will take care of it
87 push @search, "( freesidestatus IS NULL OR freesidestatus = '' )";
89 } elsif ( $cgi->param('freesidestatus') =~ /^([\w ]+)$/ ) {
91 $title = "Processed $title";
92 $hashref->{'freesidestatus'} = $1;
93 push @search, "freesidestatus = '$1'";
101 foreach my $param ( grep /^termpart\d+status$/, $cgi->param ) {
103 my $status = $cgi->param($param);
105 $param =~ /^termpart(\d+)status$/ or die 'guru meditation 54something';
109 if ( $status eq 'NULL' ) {
111 #false lazienss w/cdr_termination.pm (i should be a part_termination method)
113 "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
114 #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
116 "NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
118 } elsif ( $status =~ /^([\w ]+)$/ ) {
120 #false lazienss w/cdr_termination.pm (i should be a part_termination method)
122 "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart AND status = '$1' ) ";
123 #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
125 "EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
130 push @search, $search;
131 push @qsearch, $search;
137 # src/dest/charged_party/svcnum
140 my $phonenum = qr/^\s*([\d\-\+\ ]+)\s*$/;
142 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
143 $phonenum = qr/^\s*([\d\-\+\ A-Za-z]+)\s*$/;
144 $x = qr/[^\dA-Za-z]/;
147 if ( $cgi->param('src') =~ $phonenum ) {
148 ( my $src = $1 ) =~ s/$x//g;
149 $hashref->{'src'} = $src;
150 push @search, "src = '$src'";
153 if ( $cgi->param('dst') ) {
155 my @d = map { $_, "1$_" } split(/\s*,\s*/, $cgi->param('dst') );
157 my $search = 'dst IN ('. join(',', map dbh->quote($_), @d). ')';
159 push @search, $search;
160 push @qsearch, $search;
164 if ( $cgi->param('dcontext') =~ /^\s*(.+)\s*$/ ) {
166 $hashref->{'dcontext'} = $dcontext;
167 push @search, "dcontext = '$dcontext'";
170 if ( $cgi->param('charged_party') ) {
172 my @cp = map { $_, "1$_" }
173 split(/\s*,\s*/, $cgi->param('charged_party') );
175 my $search = 'charged_party IN ('. join(',', map dbh->quote($_), @cp). ')';
177 push @search, $search;
178 push @qsearch, $search;
181 if ( $cgi->param('charged_party_or_src') ) {
183 my @cp = map { $_, "1$_" }
184 split(/\s*,\s*/, $cgi->param('charged_party_or_src') );
185 my $in = join(',', map dbh->quote($_), @cp);
187 my $search = "( charged_party IN ($in) OR src IN ($in) )";
189 push @search, $search;
190 push @qsearch, $search;
193 if ( $cgi->param('svcnum') =~ /^([\d, ]+)$/ ) {
195 my $search = "svcnum IN ($svcnum)";
196 push @search, $search;
197 push @qsearch, $search;
201 # cdrbatchnum (or legacy cdrbatch)
204 if ( $cgi->param('cdrbatch') ) {
207 qsearchs('cdr_batch', { 'cdrbatch' => scalar($cgi->param('cdrbatch')) } );
209 $hashref->{cdrbatchnum} = $cdr_batch->cdrbatchnum;
210 push @search, 'cdrbatchnum = '. $cdr_batch->cdrbatchnum;
212 die "unknown cdrbatch ". $cgi->param('cdrbatch');
215 } elsif ( $cgi->param('cdrbatchnum') ne '__ALL__' ) {
217 if ( $cgi->param('cdrbatchnum') eq '' ) {
218 my $search = "( cdrbatchnum IS NULL )";
219 push @qsearch, $search;
220 push @search, $search;
221 } elsif ( $cgi->param('cdrbatchnum') =~ /^(\d+)$/ ) {
222 $hashref->{cdrbatchnum} = $1;
223 push @search, "cdrbatchnum = $1";
232 if ( $cgi->param('acctid') =~ /\d/ ) {
233 my $acctid = $cgi->param('acctid');
234 $acctid =~ s/\r\n/\n/g; #browsers?
235 my @acctid = map { /^\s*(\d+)\s*$/ or die "guru meditation #4"; $1; }
236 grep { /^\s*(\d+)\s*$/ }
237 split(/\n/, $acctid);
239 my $search = 'acctid IN ( '. join(',', @acctid). ' )';
240 push @qsearch, $search;
241 push @search, $search;
249 my $nototalminutes = 0;
251 my $search = join(' AND ', @search);
252 $search = "WHERE $search" if $search;
254 my $count_query = "SELECT COUNT(*), sum(billsec) FROM cdr $search";
256 my $qsearch = join(' AND ', @qsearch);
257 $qsearch = ( scalar(keys %$hashref) ? ' AND ' : ' WHERE ' ) . $qsearch
260 my $query = { 'table' => 'cdr',
261 'hashref' => $hashref,
262 'extra_sql' => $qsearch,
263 'order_by' => 'ORDER BY calldate',
266 if ( $cgi->param('longest') =~ /^(\d+)$/ && $cgi->param('longest') > 0 ) {
267 $cgi->param('maxrecords',$1);
268 $count_query = "SELECT $1";
269 $query->{'order_by'} = 'ORDER BY billsec desc';
278 my %header = %{ FS::cdr->table_info->{'fields'} };
280 my @first = qw( acctid calldate clid charged_party src dst dcontext );
281 my %first = map { $_=>1 } @first;
283 my @fields = ( @first, grep !$first{$_}, fields('cdr') );
285 if ( $cgi->param('show') ) {
286 @fields = grep $cgi->param("show_$_"), @fields;
290 if ( exists($header{$_}) ) {
294 $header =~ s/\_/ /g; #//wtf
298 unshift @header, ' ';
300 my $date_sub_factory = sub {
304 my $date = shift->$column();
305 $date ? time2str( '%Y-%m-%d %T', $date ) : ''; #config time2str format?
310 #any other formatters?
311 map { $_ => &{ $date_sub_factory }($_) } qw( startdate answerdate enddate )
316 sub { $_[0]->svcnum ? [ $p.'view/svc_phone.cgi?', 'svcnum' ] : ''; },
319 @fields = map { exists($fields{$_}) ? $fields{$_} : $_ } @fields;
320 unshift @fields, sub {
321 return '' unless $edit_data;
324 my $acctid = $cdr->acctid;
325 qq!<INPUT NAME="acctid$acctid" TYPE="checkbox" VALUE="1">!;
328 my @links = ( '', map { exists($links{$_}) ? $links{$_} : '' } @fields );
337 if ( $cgi->param('top_dst') =~ /^(\d+)$/ && $cgi->param('top_dst') > 0 ) {
340 @header = ('Destination');
342 elsif ( $cgi->param('top_src') =~ /^(\d+)$/ && $cgi->param('top_src') > 0 ) {
345 @header = ('Source');
349 my $whichfield = $fields[0];
350 $query = { 'select' => "$whichfield, count(1) as cnt",
353 'extra_sql' => "$search
357 $count_query = "SELECT $topmode";
359 # this is weird, maybe we should change search.html
360 $cgi->param('maxrecords',$topmode);
363 my $link = "${p}search/cdr.html?".$cgi->query_string;
364 $link =~ s/;top_(dst|src)=(\d+|)//g;
365 $link =~ s/;maxrecords=(\d+|)//;
366 $link =~ s/;(src|dst)=(\d+|)//g;
367 $link = [ "$link;$whichfield=", sub { shift->$whichfield } ];
371 push @header, '# Calls';