add display of total billed minutes on CDR report, RT12344
[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') =~ $phonenum ) {
172   ( my $dst = $1 ) =~ s/$x//g;
173   $hashref->{'dst'} = $dst;
174   push @search, "dst = '$dst'";
175 }
176
177 if ( $cgi->param('dcontext') =~ /^\s*(.+)\s*$/ ) {
178   my $dcontext = $1;
179   $hashref->{'dcontext'} = $dcontext;
180   push @search, "dcontext = '$dcontext'";
181 }
182
183 if ( $cgi->param('charged_party') ) {
184
185   my @cp = map { $_, "1$_" }
186              split(/\s*,\s*/, $cgi->param('charged_party') );
187   
188   my $search = 'charged_party IN ('. join(',', map dbh->quote($_), @cp). ')';
189
190   push @search,  $search;
191   push @qsearch, $search;
192 }
193
194 if ( $cgi->param('charged_party_or_src') ) {
195
196   my @cp = map { $_, "1$_" }
197              split(/\s*,\s*/, $cgi->param('charged_party_or_src') );
198   my $in = join(',', map dbh->quote($_), @cp);
199
200   my $search = "( charged_party IN ($in) OR src IN ($in) )";
201
202   push @search,  $search;
203   push @qsearch, $search;
204 }
205
206 if ( $cgi->param('svcnum') =~ /^([\d, ]+)$/ ) {
207   my $svcnum = $1;
208   my $search = "svcnum IN ($svcnum)";
209   push @search,  $search;
210   push @qsearch, $search;
211 }
212
213 ###
214 # cdrbatchnum (or legacy cdrbatch)
215 ###
216
217 if ( $cgi->param('cdrbatch') ) {
218
219   my $cdr_batch =
220     qsearchs('cdr_batch', { 'cdrbatch' => scalar($cgi->param('cdrbatch')) } );
221   if ( $cdr_batch ) {
222     $hashref->{cdrbatchnum} = $cdr_batch->cdrbatchnum;
223     push @search, 'cdrbatchnum = '. $cdr_batch->cdrbatchnum;
224   } else {
225     die "unknown cdrbatch ". $cgi->param('cdrbatch');
226   }
227
228 } elsif ( $cgi->param('cdrbatchnum') ne '__ALL__' ) {
229
230   if ( $cgi->param('cdrbatchnum') eq '' ) {
231     my $search = "( cdrbatchnum IS NULL )";
232     push @qsearch, $search;
233     push @search,  $search;
234   } elsif ( $cgi->param('cdrbatchnum') =~ /^(\d+)$/ ) {
235     $hashref->{cdrbatchnum} = $1;
236     push @search, "cdrbatchnum = $1";
237   }
238
239 }
240
241 ###
242 # acctid
243 ###
244
245 if ( $cgi->param('acctid') =~ /\d/ ) {
246   my $acctid = $cgi->param('acctid');
247   $acctid =~ s/\r\n/\n/g; #browsers?
248   my @acctid = map  { /^\s*(\d+)\s*$/ or die "guru meditation #4"; $1; }
249                grep { /^\s*(\d+)\s*$/ }
250                split(/\n/, $acctid);
251   if ( @acctid ) {
252     my $search = 'acctid IN ( '. join(',', @acctid). ' )';
253     push @qsearch, $search;
254     push @search,  $search;
255   }
256 }
257
258 ###
259 # finish it up
260 ###
261
262 my $search = join(' AND ', @search);
263 $search = "WHERE $search" if $search;
264
265 my $count_query = "SELECT COUNT(*), sum(billsec) FROM cdr $search";
266
267 my $qsearch = join(' AND ', @qsearch);
268 $qsearch = ( scalar(keys %$hashref) ? ' AND ' : ' WHERE ' ) . $qsearch
269   if $qsearch;
270
271 ###
272 # display fields
273 ###
274
275 my %header = %{ FS::cdr->table_info->{'fields'} };
276
277 my @first = qw( acctid calldate clid charged_party src dst dcontext );
278 my %first = map { $_=>1 } @first;
279
280 my @fields = ( @first, grep !$first{$_}, fields('cdr') );
281
282 if ( $cgi->param('show') ) {
283   @fields = grep $cgi->param("show_$_"), @fields;
284 }
285
286 my @header = map {
287                    if ( exists($header{$_}) ) {
288                      $header{$_};
289                    } else {
290                      my $header = $_;
291                      $header =~ s/\_/ /g; #//wtf
292                      ucfirst($header);
293                    }
294                  } @fields;
295
296 my $date_sub_factory = sub {
297   my $column = shift;
298   sub {
299     #my $cdr = shift;
300     my $date = shift->$column();
301     $date ? time2str( '%Y-%m-%d %T', $date ) : ''; #config time2str format?
302   };
303 };
304
305 my %fields = (
306   #any other formatters?
307   map { $_ => &{ $date_sub_factory }($_) } qw( startdate answerdate enddate )
308 );
309
310 my %links = (
311   'svcnum' =>
312     sub { $_[0]->svcnum ? [ $p.'view/svc_phone.cgi?', 'svcnum' ] : ''; },
313 );
314
315 @fields = map { exists($fields{$_}) ? $fields{$_} : $_ } @fields;
316
317               #checkbox column
318 my @links = ( '', map { exists($links{$_}) ? $links{$_} : '' } @fields );
319
320 </%init>