This commit was manufactured by cvs2svn to create branch
[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 $conf = new FS::Conf;
59
60 my $areboxes = 0;
61
62 my $title = 'Call Detail Records';
63 my $hashref = {};
64
65 #process params for CDR search, populate $hashref...
66 # and fixup $count_query
67
68 my @search = ();
69
70 ###
71 # dates
72 ###
73
74 my $str2time_sql = str2time_sql;
75 my $closing      = str2time_sql_closing;
76
77 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
78 push @search, "$str2time_sql calldate $closing >= $beginning ",
79               "$str2time_sql calldate $closing <= $ending";
80
81 ###
82 # duration / billsec
83 ###
84
85 push @search, FS::UI::Web::parse_lt_gt($cgi, 'duration');
86 push @search, FS::UI::Web::parse_lt_gt($cgi, 'billsec');
87
88 #above here things just push @search
89 #below here things also have to define $hashref->{} or push @qsearch
90 my @qsearch = @search;
91
92 ###
93 # freesidestatus
94 ###
95
96 if ( $cgi->param('freesidestatus') eq 'NULL' ) {
97
98   $title = "Unprocessed $title";
99   $hashref->{'freesidestatus'} = ''; # Record.pm will take care of it
100   push @search, "( freesidestatus IS NULL OR freesidestatus = '' )";
101
102 } elsif ( $cgi->param('freesidestatus') =~ /^([\w ]+)$/ ) {
103
104   $title = "Processed $title";
105   $hashref->{'freesidestatus'} = $1;
106   push @search, "freesidestatus = '$1'";
107
108 }
109
110 ###
111 # termpartNstatus
112 ###
113
114 foreach my $param ( grep /^termpart\d+status$/, $cgi->param ) {
115
116   my $status = $cgi->param($param);
117
118   $param =~ /^termpart(\d+)status$/ or die 'guru meditation 54something';
119   my $termpart = $1;
120
121   my $search = '';
122   if ( $status eq 'NULL' ) {
123
124     #false lazienss w/cdr_termination.pm (i should be a part_termination method)
125     my $where_term =
126       "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
127     #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
128     $search =
129       "NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
130
131   } elsif ( $status =~ /^([\w ]+)$/ ) {
132
133     #false lazienss w/cdr_termination.pm (i should be a part_termination method)
134     my $where_term =
135       "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart AND status = '$1' ) ";
136     #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
137     $search =
138       "EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
139
140   }
141
142   if ( $search ) {
143     push @search,  $search;
144     push @qsearch, $search;
145   }
146
147 }
148
149 ###
150 # src/dest/charged_party/svcnum
151 ###
152
153 my $phonenum = qr/^\s*([\d\-\+\ ]+)\s*$/;
154 my $x = qr/\D/;
155 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
156   $phonenum = qr/^\s*([\d\-\+\ A-Za-z]+)\s*$/;
157   $x = qr/[^\dA-Za-z]/;
158 }
159
160 if ( $cgi->param('src') =~ $phonenum ) {
161   ( my $src = $1 ) =~ s/$x//g;
162   $hashref->{'src'} = $src;
163   push @search, "src = '$src'";
164 }
165
166 if ( $cgi->param('dst') =~ $phonenum ) {
167   ( my $dst = $1 ) =~ s/$x//g;
168   $hashref->{'dst'} = $dst;
169   push @search, "dst = '$dst'";
170 }
171
172 if ( $cgi->param('dcontext') =~ /^\s*(.+)\s*$/ ) {
173   my $dcontext = $1;
174   $hashref->{'dcontext'} = $dcontext;
175   push @search, "dcontext = '$dcontext'";
176 }
177
178 if ( $cgi->param('charged_party') ) {
179
180   my @cp = map { $_, "1$_" }
181              split(/\s*,\s*/, $cgi->param('charged_party') );
182   
183   my $search = 'charged_party IN ('. join(',', map dbh->quote($_), @cp). ')';
184
185   push @search,  $search;
186   push @qsearch, $search;
187 }
188
189 if ( $cgi->param('charged_party_or_src') ) {
190
191   my @cp = map { $_, "1$_" }
192              split(/\s*,\s*/, $cgi->param('charged_party_or_src') );
193   my $in = join(',', map dbh->quote($_), @cp);
194
195   my $search = "( charged_party IN ($in) OR src IN ($in) )";
196
197   push @search,  $search;
198   push @qsearch, $search;
199 }
200
201 if ( $cgi->param('svcnum') =~ /^([\d, ]+)$/ ) {
202   my $svcnum = $1;
203   my $search = "svcnum IN ($svcnum)";
204   push @search,  $search;
205   push @qsearch, $search;
206 }
207
208 ###
209 # cdrbatchnum (or legacy cdrbatch)
210 ###
211
212 if ( $cgi->param('cdrbatch') ) {
213
214   my $cdr_batch =
215     qsearchs('cdr_batch', { 'cdrbatch' => scalar($cgi->param('cdrbatch')) } );
216   if ( $cdr_batch ) {
217     $hashref->{cdrbatchnum} = $cdr_batch->cdrbatchnum;
218     push @search, 'cdrbatchnum = '. $cdr_batch->cdrbatchnum;
219   } else {
220     die "unknown cdrbatch ". $cgi->param('cdrbatch');
221   }
222
223 } elsif ( $cgi->param('cdrbatchnum') ne '__ALL__' ) {
224
225   if ( $cgi->param('cdrbatchnum') eq '' ) {
226     my $search = "( cdrbatchnum IS NULL )";
227     push @qsearch, $search;
228     push @search,  $search;
229   } elsif ( $cgi->param('cdrbatchnum') =~ /^(\d+)$/ ) {
230     $hashref->{cdrbatchnum} = $1;
231     push @search, "cdrbatchnum = $1";
232   }
233
234 }
235
236 ###
237 # acctid
238 ###
239
240 if ( $cgi->param('acctid') =~ /\d/ ) {
241   my $acctid = $cgi->param('acctid');
242   $acctid =~ s/\r\n/\n/g; #browsers?
243   my @acctid = map  { /^\s*(\d+)\s*$/ or die "guru meditation #4"; $1; }
244                grep { /^\s*(\d+)\s*$/ }
245                split(/\n/, $acctid);
246   if ( @acctid ) {
247     my $search = 'acctid IN ( '. join(',', @acctid). ' )';
248     push @qsearch, $search;
249     push @search,  $search;
250   }
251 }
252
253 ###
254 # finish it up
255 ###
256
257 my $search = join(' AND ', @search);
258 $search = "WHERE $search" if $search;
259
260 my $count_query = "SELECT COUNT(*) FROM cdr $search";
261
262 my $qsearch = join(' AND ', @qsearch);
263 $qsearch = ( scalar(keys %$hashref) ? ' AND ' : ' WHERE ' ) . $qsearch
264   if $qsearch;
265
266 ###
267 # display fields
268 ###
269
270 my %header = %{ FS::cdr->table_info->{'fields'} };
271
272 my @first = qw( acctid calldate clid charged_party src dst dcontext );
273 my %first = map { $_=>1 } @first;
274
275 my @fields = ( @first, grep !$first{$_}, fields('cdr') );
276
277 if ( $cgi->param('show') ) {
278   @fields = grep $cgi->param("show_$_"), @fields;
279 }
280
281 my @header = map {
282                    if ( exists($header{$_}) ) {
283                      $header{$_};
284                    } else {
285                      my $header = $_;
286                      $header =~ s/\_/ /g; #//wtf
287                      ucfirst($header);
288                    }
289                  } @fields;
290
291 my $date_sub_factory = sub {
292   my $column = shift;
293   sub {
294     #my $cdr = shift;
295     my $date = shift->$column();
296     $date ? time2str( '%Y-%m-%d %T', $date ) : ''; #config time2str format?
297   };
298 };
299
300 my %fields = (
301   #any other formatters?
302   map { $_ => &{ $date_sub_factory }($_) } qw( startdate answerdate enddate )
303 );
304
305 my %links = (
306   'svcnum' =>
307     sub { $_[0]->svcnum ? [ $p.'view/svc_phone.cgi?', 'svcnum' ] : ''; },
308 );
309
310 @fields = map { exists($fields{$_}) ? $fields{$_} : $_ } @fields;
311
312               #checkbox column
313 my @links = ( '', map { exists($links{$_}) ? $links{$_} : '' } @fields );
314
315 </%init>