link new tax report to cust_credit_bill_pkg for credits, RT#12332
[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 = '$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('svcnum') =~ /^([\d, ]+)$/ ) {
212   my $svcnum = $1;
213   my $search = "svcnum IN ($svcnum)";
214   push @search,  $search;
215   push @qsearch, $search;
216 }
217
218 ###
219 # cdrbatchnum (or legacy cdrbatch)
220 ###
221
222 if ( $cgi->param('cdrbatch') ) {
223
224   my $cdr_batch =
225     qsearchs('cdr_batch', { 'cdrbatch' => scalar($cgi->param('cdrbatch')) } );
226   if ( $cdr_batch ) {
227     $hashref->{cdrbatchnum} = $cdr_batch->cdrbatchnum;
228     push @search, 'cdrbatchnum = '. $cdr_batch->cdrbatchnum;
229   } else {
230     die "unknown cdrbatch ". $cgi->param('cdrbatch');
231   }
232
233 } elsif ( $cgi->param('cdrbatchnum') ne '__ALL__' ) {
234
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";
242   }
243
244 }
245
246 ###
247 # acctid
248 ###
249
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);
256   if ( @acctid ) {
257     my $search = 'acctid IN ( '. join(',', @acctid). ' )';
258     push @qsearch, $search;
259     push @search,  $search;
260   }
261 }
262
263 ###
264 # finish it up
265 ###
266
267 my $search = join(' AND ', @search);
268 $search = "WHERE $search" if $search;
269
270 my $count_query = "SELECT COUNT(*), sum(billsec) FROM cdr $search";
271
272 my $qsearch = join(' AND ', @qsearch);
273 $qsearch = ( scalar(keys %$hashref) ? ' AND ' : ' WHERE ' ) . $qsearch
274   if $qsearch;
275
276 ###
277 # display fields
278 ###
279
280 my %header = %{ FS::cdr->table_info->{'fields'} };
281
282 my @first = qw( acctid calldate clid charged_party src dst dcontext );
283 my %first = map { $_=>1 } @first;
284
285 my @fields = ( @first, grep !$first{$_}, fields('cdr') );
286
287 if ( $cgi->param('show') ) {
288   @fields = grep $cgi->param("show_$_"), @fields;
289 }
290
291 my @header = map {
292                    if ( exists($header{$_}) ) {
293                      $header{$_};
294                    } else {
295                      my $header = $_;
296                      $header =~ s/\_/ /g; #//wtf
297                      ucfirst($header);
298                    }
299                  } @fields;
300
301 my $date_sub_factory = sub {
302   my $column = shift;
303   sub {
304     #my $cdr = shift;
305     my $date = shift->$column();
306     $date ? time2str( '%Y-%m-%d %T', $date ) : ''; #config time2str format?
307   };
308 };
309
310 my %fields = (
311   #any other formatters?
312   map { $_ => &{ $date_sub_factory }($_) } qw( startdate answerdate enddate )
313 );
314
315 my %links = (
316   'svcnum' =>
317     sub { $_[0]->svcnum ? [ $p.'view/svc_phone.cgi?', 'svcnum' ] : ''; },
318 );
319
320 @fields = map { exists($fields{$_}) ? $fields{$_} : $_ } @fields;
321
322               #checkbox column
323 my @links = ( '', map { exists($links{$_}) ? $links{$_} : '' } @fields );
324
325 </%init>