1 <% include( 'elements/search.html',
3 'name' => 'call detail records',
5 'query' => { 'table' => 'cdr',
7 'extra_sql' => $qsearch,
8 'order_by' => 'ORDER BY calldate',
10 'count_query' => $count_query,
11 'count_addl' => [ $totalminutes_sub ],
18 return '' unless $edit_data;
21 my $acctid = $cdr->acctid;
22 qq!<INPUT NAME="acctid$acctid" TYPE="checkbox" VALUE="1">!;
24 @fields, #XXX fill in some pretty-print
29 'html_form' => qq!<FORM NAME="cdrForm" ACTION="$p/misc/cdr.cgi" METHOD="POST">!,
30 #false laziness w/queue.html
33 '<BR><INPUT TYPE="button" VALUE="select all" onClick="setAll(true)">'.
34 '<INPUT TYPE="button" VALUE="unselect all" onClick="setAll(false)">'.
35 qq!<BR><INPUT TYPE="submit" NAME="action" VALUE="reprocess selected" onClick="return confirm('Are you sure you want to reprocess the selected CDRs?')">!.
36 qq!<INPUT TYPE="submit" NAME="action" VALUE="delete selected" onClick="return confirm('Are you sure you want to delete the selected CDRs?')"><BR>!.
37 '<SCRIPT TYPE="text/javascript">'.
38 ' function setAll(setTo) { '.
39 ' theForm = document.cdrForm;'.
40 ' for (i=0,n=theForm.elements.length;i<n;i++)'.
41 ' if (theForm.elements[i].name.indexOf("acctid") != -1)'.
42 ' theForm.elements[i].checked = setTo;'.
54 unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
56 my $edit_data = $FS::CurrentUser::CurrentUser->access_right('Edit rating data');
58 my $totalminutes_sub = sub {
60 sprintf("%.2f",$billsec/60) . ' total minutes';
63 my $conf = new FS::Conf;
67 my $title = 'Call Detail Records';
70 #process params for CDR search, populate $hashref...
71 # and fixup $count_query
79 my $str2time_sql = str2time_sql;
80 my $closing = str2time_sql_closing;
82 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
83 push @search, "$str2time_sql calldate $closing >= $beginning ",
84 "$str2time_sql calldate $closing <= $ending";
90 push @search, FS::UI::Web::parse_lt_gt($cgi, 'duration');
91 push @search, FS::UI::Web::parse_lt_gt($cgi, 'billsec');
93 #above here things just push @search
94 #below here things also have to define $hashref->{} or push @qsearch
95 my @qsearch = @search;
101 if ( $cgi->param('freesidestatus') eq 'NULL' ) {
103 $title = "Unprocessed $title";
104 $hashref->{'freesidestatus'} = ''; # Record.pm will take care of it
105 push @search, "( freesidestatus IS NULL OR freesidestatus = '' )";
107 } elsif ( $cgi->param('freesidestatus') =~ /^([\w ]+)$/ ) {
109 $title = "Processed $title";
110 $hashref->{'freesidestatus'} = $1;
111 push @search, "freesidestatus = '$1'";
119 foreach my $param ( grep /^termpart\d+status$/, $cgi->param ) {
121 my $status = $cgi->param($param);
123 $param =~ /^termpart(\d+)status$/ or die 'guru meditation 54something';
127 if ( $status eq 'NULL' ) {
129 #false lazienss w/cdr_termination.pm (i should be a part_termination method)
131 "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
132 #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
134 "NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
136 } elsif ( $status =~ /^([\w ]+)$/ ) {
138 #false lazienss w/cdr_termination.pm (i should be a part_termination method)
140 "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart AND status = '$1' ) ";
141 #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
143 "EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
148 push @search, $search;
149 push @qsearch, $search;
155 # src/dest/charged_party/svcnum
158 my $phonenum = qr/^\s*([\d\-\+\ ]+)\s*$/;
160 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
161 $phonenum = qr/^\s*([\d\-\+\ A-Za-z]+)\s*$/;
162 $x = qr/[^\dA-Za-z]/;
165 if ( $cgi->param('src') =~ $phonenum ) {
166 ( my $src = $1 ) =~ s/$x//g;
167 $hashref->{'src'} = $src;
168 push @search, "src = '$src'";
171 if ( $cgi->param('dst') =~ $phonenum ) {
172 ( my $dst = $1 ) =~ s/$x//g;
173 $hashref->{'dst'} = $dst;
174 push @search, "dst = '$dst'";
177 if ( $cgi->param('dcontext') =~ /^\s*(.+)\s*$/ ) {
179 $hashref->{'dcontext'} = $dcontext;
180 push @search, "dcontext = '$dcontext'";
183 if ( $cgi->param('charged_party') ) {
185 my @cp = map { $_, "1$_" }
186 split(/\s*,\s*/, $cgi->param('charged_party') );
188 my $search = 'charged_party IN ('. join(',', map dbh->quote($_), @cp). ')';
190 push @search, $search;
191 push @qsearch, $search;
194 if ( $cgi->param('charged_party_or_src') ) {
196 my @cp = map { $_, "1$_" }
197 split(/\s*,\s*/, $cgi->param('charged_party_or_src') );
198 my $in = join(',', map dbh->quote($_), @cp);
200 my $search = "( charged_party IN ($in) OR src IN ($in) )";
202 push @search, $search;
203 push @qsearch, $search;
206 if ( $cgi->param('svcnum') =~ /^([\d, ]+)$/ ) {
208 my $search = "svcnum IN ($svcnum)";
209 push @search, $search;
210 push @qsearch, $search;
214 # cdrbatchnum (or legacy cdrbatch)
217 if ( $cgi->param('cdrbatch') ) {
220 qsearchs('cdr_batch', { 'cdrbatch' => scalar($cgi->param('cdrbatch')) } );
222 $hashref->{cdrbatchnum} = $cdr_batch->cdrbatchnum;
223 push @search, 'cdrbatchnum = '. $cdr_batch->cdrbatchnum;
225 die "unknown cdrbatch ". $cgi->param('cdrbatch');
228 } elsif ( $cgi->param('cdrbatchnum') ne '__ALL__' ) {
230 if ( $cgi->param('cdrbatchnum') eq '' ) {
231 my $search = "( cdrbatchnum IS NULL )";
232 push @qsearch, $search;
233 push @search, $search;
234 } elsif ( $cgi->param('cdrbatchnum') =~ /^(\d+)$/ ) {
235 $hashref->{cdrbatchnum} = $1;
236 push @search, "cdrbatchnum = $1";
245 if ( $cgi->param('acctid') =~ /\d/ ) {
246 my $acctid = $cgi->param('acctid');
247 $acctid =~ s/\r\n/\n/g; #browsers?
248 my @acctid = map { /^\s*(\d+)\s*$/ or die "guru meditation #4"; $1; }
249 grep { /^\s*(\d+)\s*$/ }
250 split(/\n/, $acctid);
252 my $search = 'acctid IN ( '. join(',', @acctid). ' )';
253 push @qsearch, $search;
254 push @search, $search;
262 my $search = join(' AND ', @search);
263 $search = "WHERE $search" if $search;
265 my $count_query = "SELECT COUNT(*), sum(billsec) FROM cdr $search";
267 my $qsearch = join(' AND ', @qsearch);
268 $qsearch = ( scalar(keys %$hashref) ? ' AND ' : ' WHERE ' ) . $qsearch
275 my %header = %{ FS::cdr->table_info->{'fields'} };
277 my @first = qw( acctid calldate clid charged_party src dst dcontext );
278 my %first = map { $_=>1 } @first;
280 my @fields = ( @first, grep !$first{$_}, fields('cdr') );
282 if ( $cgi->param('show') ) {
283 @fields = grep $cgi->param("show_$_"), @fields;
287 if ( exists($header{$_}) ) {
291 $header =~ s/\_/ /g; #//wtf
296 my $date_sub_factory = sub {
300 my $date = shift->$column();
301 $date ? time2str( '%Y-%m-%d %T', $date ) : ''; #config time2str format?
306 #any other formatters?
307 map { $_ => &{ $date_sub_factory }($_) } qw( startdate answerdate enddate )
312 sub { $_[0]->svcnum ? [ $p.'view/svc_phone.cgi?', 'svcnum' ] : ''; },
315 @fields = map { exists($fields{$_}) ? $fields{$_} : $_ } @fields;
318 my @links = ( '', map { exists($links{$_}) ? $links{$_} : '' } @fields );