d1f68c5c67f8244fb48d07df02952c332ea2c31b
[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 # cdrbatch
183 ###
184
185 if ( $cgi->param('cdrbatch') ne '__ALL__' ) {
186   if ( $cgi->param('cdrbatch') eq '' ) {
187     my $search = "( cdrbatch IS NULL OR cdrbatch = '' )";
188     push @qsearch, $search;
189     push @search,  $search;
190   } else {
191     $hashref->{cdrbatch} = $cgi->param('cdrbatch');
192     push @search, 'cdrbatch = '. dbh->quote($cgi->param('cdrbatch'));
193   }
194 }
195
196 ###
197 # acctid
198 ###
199
200 if ( $cgi->param('acctid') =~ /\d/ ) {
201   my $acctid = $cgi->param('acctid');
202   $acctid =~ s/\r\n/\n/g; #browsers?
203   my @acctid = map  { /^\s*(\d+)\s*$/ or die "guru meditation #4"; $1; }
204                grep { /^\s*(\d+)\s*$/ }
205                split(/\n/, $acctid);
206   if ( @acctid ) {
207     my $search = 'acctid IN ( '. join(',', @acctid). ' )';
208     push @qsearch, $search;
209     push @search,  $search;
210   }
211 }
212
213 ###
214 # finish it up
215 ###
216
217 my $search = join(' AND ', @search);
218 $search = "WHERE $search" if $search;
219
220 my $count_query = "SELECT COUNT(*) FROM cdr $search";
221
222 my $qsearch = join(' AND ', @qsearch);
223 $qsearch = ( scalar(keys %$hashref) ? ' AND ' : ' WHERE ' ) . $qsearch
224   if $qsearch;
225
226 ###
227 # display fields
228 ###
229
230 my %header = %{ FS::cdr->table_info->{'fields'} };
231
232 my @first = qw( acctid calldate clid charged_party src dst dcontext );
233 my %first = map { $_=>1 } @first;
234
235 my @fields = ( @first, grep !$first{$_}, fields('cdr') );
236
237 if ( $cgi->param('show') ) {
238   @fields = grep $cgi->param("show_$_"), @fields;
239 }
240
241 my @header = map {
242                    if ( exists($header{$_}) ) {
243                      $header{$_};
244                    } else {
245                      my $header = $_;
246                      $header =~ s/\_/ /g; #//wtf
247                      ucfirst($header);
248                    }
249                  } @fields;
250
251 my $date_sub_factory = sub {
252   my $column = shift;
253   sub {
254     #my $cdr = shift;
255     my $date = shift->$column();
256     $date ? time2str( '%Y-%m-%d %T', $date ) : ''; #config time2str format?
257   };
258 };
259
260 my %fields = (
261   #any other formatters?
262   map { $_ => &{ $date_sub_factory }($_) } qw( startdate answerdate enddate )
263 );
264
265 my %links = (
266   'svcnum' =>
267     sub { $_[0]->svcnum ? [ $p.'view/svc_phone.cgi?', 'svcnum' ] : ''; },
268 );
269
270 @fields = map { exists($fields{$_}) ? $fields{$_} : $_ } @fields;
271
272               #checkbox column
273 my @links = ( '', map { exists($links{$_}) ? $links{$_} : '' } @fields );
274
275 </%init>