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