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') ) {
173 my @d = map { $_, "1$_" } split(/\s*,\s*/, $cgi->param('dst') );
175 my $search = 'dst IN ('. join(',', map dbh->quote($_), @d). ')';
177 push @search, $search;
178 push @qsearch, $search;
182 if ( $cgi->param('dcontext') =~ /^\s*(.+)\s*$/ ) {
184 $hashref->{'dcontext'} = $dcontext;
185 push @search, "dcontext = '$dcontext'";
188 if ( $cgi->param('charged_party') ) {
190 my @cp = map { $_, "1$_" }
191 split(/\s*,\s*/, $cgi->param('charged_party') );
193 my $search = 'charged_party IN ('. join(',', map dbh->quote($_), @cp). ')';
195 push @search, $search;
196 push @qsearch, $search;
199 if ( $cgi->param('charged_party_or_src') ) {
201 my @cp = map { $_, "1$_" }
202 split(/\s*,\s*/, $cgi->param('charged_party_or_src') );
203 my $in = join(',', map dbh->quote($_), @cp);
205 my $search = "( charged_party IN ($in) OR src IN ($in) )";
207 push @search, $search;
208 push @qsearch, $search;
211 if ( $cgi->param('svcnum') =~ /^([\d, ]+)$/ ) {
213 my $search = "svcnum IN ($svcnum)";
214 push @search, $search;
215 push @qsearch, $search;
219 # cdrbatchnum (or legacy cdrbatch)
222 if ( $cgi->param('cdrbatch') ) {
225 qsearchs('cdr_batch', { 'cdrbatch' => scalar($cgi->param('cdrbatch')) } );
227 $hashref->{cdrbatchnum} = $cdr_batch->cdrbatchnum;
228 push @search, 'cdrbatchnum = '. $cdr_batch->cdrbatchnum;
230 die "unknown cdrbatch ". $cgi->param('cdrbatch');
233 } elsif ( $cgi->param('cdrbatchnum') ne '__ALL__' ) {
235 if ( $cgi->param('cdrbatchnum') eq '' ) {
236 my $search = "( cdrbatchnum IS NULL )";
237 push @qsearch, $search;
238 push @search, $search;
239 } elsif ( $cgi->param('cdrbatchnum') =~ /^(\d+)$/ ) {
240 $hashref->{cdrbatchnum} = $1;
241 push @search, "cdrbatchnum = $1";
250 if ( $cgi->param('acctid') =~ /\d/ ) {
251 my $acctid = $cgi->param('acctid');
252 $acctid =~ s/\r\n/\n/g; #browsers?
253 my @acctid = map { /^\s*(\d+)\s*$/ or die "guru meditation #4"; $1; }
254 grep { /^\s*(\d+)\s*$/ }
255 split(/\n/, $acctid);
257 my $search = 'acctid IN ( '. join(',', @acctid). ' )';
258 push @qsearch, $search;
259 push @search, $search;
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
280 my %header = %{ FS::cdr->table_info->{'fields'} };
282 my @first = qw( acctid calldate clid charged_party src dst dcontext );
283 my %first = map { $_=>1 } @first;
285 my @fields = ( @first, grep !$first{$_}, fields('cdr') );
287 if ( $cgi->param('show') ) {
288 @fields = grep $cgi->param("show_$_"), @fields;
292 if ( exists($header{$_}) ) {
296 $header =~ s/\_/ /g; #//wtf
301 my $date_sub_factory = sub {
305 my $date = shift->$column();
306 $date ? time2str( '%Y-%m-%d %T', $date ) : ''; #config time2str format?
311 #any other formatters?
312 map { $_ => &{ $date_sub_factory }($_) } qw( startdate answerdate enddate )
317 sub { $_[0]->svcnum ? [ $p.'view/svc_phone.cgi?', 'svcnum' ] : ''; },
320 @fields = map { exists($fields{$_}) ? $fields{$_} : $_ } @fields;
323 my @links = ( '', map { exists($links{$_}) ? $links{$_} : '' } @fields );