fix search of pending/billed CDRs to find src field too, RT#9640
[freeside.git] / httemplate / search / cdr.html
1 <% include( 'elements/search.html',
2                'title' => $title,
3                'name'  => 'call detail records',
4
5                'query' => { 'table'     => 'cdr',
6                             'hashref'   => $hashref,
7                             'extra_sql' => $qsearch,
8                             'order_by'  => 'ORDER BY calldate',
9                           },
10                'count_query' => $count_query,
11                'header' => [
12                              '', # checkbox column
13                              @header,
14                            ],
15                'fields' => [
16                              sub {
17                                return '' unless $edit_data;
18                                $areboxes = 1;
19                                my $cdr = shift;
20                                my $acctid = $cdr->acctid;
21                                qq!<INPUT NAME="acctid$acctid" TYPE="checkbox" VALUE="1">!;
22                              },
23                              @fields,       #XXX fill in some pretty-print
24                                             #processing, etc.
25                            ],
26                'links' => \@links,
27
28                'html_form'   => qq!<FORM NAME="cdrForm" ACTION="$p/misc/cdr.cgi" METHOD="POST">!,
29                #false laziness w/queue.html
30                'html_foot' => sub {
31                                 if ( $areboxes ) {
32                                   '<BR><INPUT TYPE="button" VALUE="select all" onClick="setAll(true)">'.
33                                   '<INPUT TYPE="button" VALUE="unselect all" onClick="setAll(false)">'.
34                                   qq!<BR><INPUT TYPE="submit" NAME="action" VALUE="reprocess selected" onClick="return confirm('Are you sure you want to reprocess the selected CDRs?')">!.
35                                   qq!<INPUT TYPE="submit" NAME="action" VALUE="delete selected" onClick="return confirm('Are you sure you want to delete the selected CDRs?')"><BR>!.
36                                   '<SCRIPT TYPE="text/javascript">'.
37                                   '  function setAll(setTo) { '.
38                                   '    theForm = document.cdrForm;'.
39                                   '    for (i=0,n=theForm.elements.length;i<n;i++)'.
40                                   '      if (theForm.elements[i].name.indexOf("acctid") != -1)'.
41                                   '        theForm.elements[i].checked = setTo;'.
42                                   '  }'.
43                                   '</SCRIPT>';
44                                 } else {
45                                   '';
46                                 }
47                               },
48
49              )
50 %>
51 <%init>
52
53 die "access denied"
54   unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
55
56 my $edit_data = $FS::CurrentUser::CurrentUser->access_right('Edit rating data');
57
58 my $areboxes = 0;
59
60 my $title = 'Call Detail Records';
61 my $hashref = {};
62
63 #process params for CDR search, populate $hashref...
64 # and fixup $count_query
65
66 my @search = ();
67
68 ###
69 # dates
70 ###
71
72 my $str2time_sql = str2time_sql;
73 my $closing      = str2time_sql_closing;
74
75 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
76 push @search, "$str2time_sql calldate $closing >= $beginning ",
77               "$str2time_sql calldate $closing <= $ending";
78
79 ###
80 # duration / billsec
81 ###
82
83 push @search, FS::UI::Web::parse_lt_gt($cgi, 'duration');
84 push @search, FS::UI::Web::parse_lt_gt($cgi, 'billsec');
85
86 #above here things just push @search
87 #below here things also have to define $hashref->{} or push @qsearch
88 my @qsearch = @search;
89
90 ###
91 # freesidestatus
92 ###
93
94 if ( $cgi->param('freesidestatus') eq 'NULL' ) {
95
96   $title = "Unprocessed $title";
97   $hashref->{'freesidestatus'} = ''; # Record.pm will take care of it
98   push @search, "( freesidestatus IS NULL OR freesidestatus = '' )";
99
100 } elsif ( $cgi->param('freesidestatus') =~ /^([\w ]+)$/ ) {
101
102   $title = "Processed $title";
103   $hashref->{'freesidestatus'} = $1;
104   push @search, "freesidestatus = '$1'";
105
106 }
107
108 ###
109 # termpartNstatus
110 ###
111
112 foreach my $param ( grep /^termpart\d+status$/, $cgi->param ) {
113
114   my $status = $cgi->param($param);
115
116   $param =~ /^termpart(\d+)status$/ or die 'guru meditation 54something';
117   my $termpart = $1;
118
119   my $search = '';
120   if ( $status eq 'NULL' ) {
121
122     #false lazienss w/cdr_termination.pm (i should be a part_termination method)
123     my $where_term =
124       "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
125     #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
126     $search =
127       "NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
128
129   } elsif ( $status =~ /^([\w ]+)$/ ) {
130
131     #false lazienss w/cdr_termination.pm (i should be a part_termination method)
132     my $where_term =
133       "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart AND status = '$1' ) ";
134     #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
135     $search =
136       "EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
137
138   }
139
140   if ( $search ) {
141     push @search,  $search;
142     push @qsearch, $search;
143   }
144
145 }
146
147 ###
148 # src/dest/charged_party
149 ###
150
151 if ( $cgi->param('src') =~ /^\s*([\d\-\+\ ]+)\s*$/ ) {
152   ( my $src = $1 ) =~ s/\D//g;
153   $hashref->{'src'} = $src;
154   push @search, "src = '$src'";
155 }
156
157 if ( $cgi->param('dst') =~ /^\s*([\d\-\+ ]+)\s*$/ ) {
158   ( my $dst = $1 ) =~ s/\D//g;
159   $hashref->{'dst'} = $dst;
160   push @search, "dst = '$dst'";
161 }
162
163 if ( $cgi->param('dcontext') =~ /^\s*(.+)\s*$/ ) {
164   my $dcontext = $1;
165   $hashref->{'dcontext'} = $dcontext;
166   push @search, "dcontext = '$dcontext'";
167 }
168
169 if ( $cgi->param('charged_party') =~ /^\s*([\d\-\+\ ]+)\s*$/ ) {
170   ( my $charged_party = $1 ) =~ s/\D//g;
171   #$hashref->{'charged_party'} = $charged_party;
172   #push @search, "charged_party = '$charged_party'";
173   #XXX countrycode
174
175   my $search = " (    charged_party IN ('$charged_party', '1$charged_party') )";
176
177   push @search,  $search;
178   push @qsearch, $search;
179 }
180
181 if ( $cgi->param('charged_party_or_src') =~ /^\s*([\d\-\+\ ]+)\s*$/ ) {
182   ( my $charged_party = $1 ) =~ s/\D//g;
183   #$hashref->{'charged_party'} = $charged_party;
184   #push @search, "charged_party = '$charged_party'";
185   #XXX countrycode
186
187   my $search = " (    charged_party IN ('$charged_party', '1$charged_party')
188                    OR src           IN ('$charged_party', '1$charged_party') )";
189
190   push @search,  $search;
191   push @qsearch, $search;
192 }
193
194
195 ###
196 # cdrbatchnum (or legacy cdrbatch)
197 ###
198
199 if ( $cgi->param('cdrbatch') ) {
200
201   my $cdr_batch =
202     qsearchs('cdr_batch', { 'cdrbatch' => scalar($cgi->param('cdrbatch')) } );
203   if ( $cdr_batch ) {
204     $hashref->{cdrbatchnum} = $cdr_batch->cdrbatchnum;
205     push @search, 'cdrbatchnum = '. $cdr_batch->cdrbatchnum;
206   } else {
207     die "unknown cdrbatch ". $cgi->param('cdrbatch');
208   }
209
210 } elsif ( $cgi->param('cdrbatchnum') ne '__ALL__' ) {
211
212   if ( $cgi->param('cdrbatchnum') eq '' ) {
213     my $search = "( cdrbatchnum IS NULL )";
214     push @qsearch, $search;
215     push @search,  $search;
216   } elsif ( $cgi->param('cdrbatchnum') =~ /^(\d+)$/ ) {
217     $hashref->{cdrbatchnum} = $1;
218     push @search, "cdrbatchnum = $1";
219   }
220
221 }
222
223 ###
224 # acctid
225 ###
226
227 if ( $cgi->param('acctid') =~ /\d/ ) {
228   my $acctid = $cgi->param('acctid');
229   $acctid =~ s/\r\n/\n/g; #browsers?
230   my @acctid = map  { /^\s*(\d+)\s*$/ or die "guru meditation #4"; $1; }
231                grep { /^\s*(\d+)\s*$/ }
232                split(/\n/, $acctid);
233   if ( @acctid ) {
234     my $search = 'acctid IN ( '. join(',', @acctid). ' )';
235     push @qsearch, $search;
236     push @search,  $search;
237   }
238 }
239
240 ###
241 # finish it up
242 ###
243
244 my $search = join(' AND ', @search);
245 $search = "WHERE $search" if $search;
246
247 my $count_query = "SELECT COUNT(*) FROM cdr $search";
248
249 my $qsearch = join(' AND ', @qsearch);
250 $qsearch = ( scalar(keys %$hashref) ? ' AND ' : ' WHERE ' ) . $qsearch
251   if $qsearch;
252
253 ###
254 # display fields
255 ###
256
257 my %header = %{ FS::cdr->table_info->{'fields'} };
258
259 my @first = qw( acctid calldate clid charged_party src dst dcontext );
260 my %first = map { $_=>1 } @first;
261
262 my @fields = ( @first, grep !$first{$_}, fields('cdr') );
263
264 if ( $cgi->param('show') ) {
265   @fields = grep $cgi->param("show_$_"), @fields;
266 }
267
268 my @header = map {
269                    if ( exists($header{$_}) ) {
270                      $header{$_};
271                    } else {
272                      my $header = $_;
273                      $header =~ s/\_/ /g; #//wtf
274                      ucfirst($header);
275                    }
276                  } @fields;
277
278 my $date_sub_factory = sub {
279   my $column = shift;
280   sub {
281     #my $cdr = shift;
282     my $date = shift->$column();
283     $date ? time2str( '%Y-%m-%d %T', $date ) : ''; #config time2str format?
284   };
285 };
286
287 my %fields = (
288   #any other formatters?
289   map { $_ => &{ $date_sub_factory }($_) } qw( startdate answerdate enddate )
290 );
291
292 my %links = (
293   'svcnum' =>
294     sub { $_[0]->svcnum ? [ $p.'view/svc_phone.cgi?', 'svcnum' ] : ''; },
295 );
296
297 @fields = map { exists($fields{$_}) ? $fields{$_} : $_ } @fields;
298
299               #checkbox column
300 my @links = ( '', map { exists($links{$_}) ? $links{$_} : '' } @fields );
301
302 </%init>