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