summaryrefslogtreecommitdiff
path: root/httemplate/search/cdr.html
blob: d1f68c5c67f8244fb48d07df02952c332ea2c31b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<% include( 'elements/search.html',
               'title' => $title,
               'name'  => 'call detail records',

               'query' => { 'table'     => 'cdr',
                            'hashref'   => $hashref,
			    'extra_sql' => $qsearch,
			    'order_by'  => 'ORDER BY calldate',
                          },
               'count_query' => $count_query,
               'header' => [
                             '', # checkbox column
                             @header,
                           ],
               'fields' => [
                             sub {
                               return '' unless $edit_data;
                               $areboxes = 1;
                               my $cdr = shift;
                               my $acctid = $cdr->acctid;
                               qq!<INPUT NAME="acctid$acctid" TYPE="checkbox" VALUE="1">!;
                             },
                             @fields,       #XXX fill in some pretty-print
                                            #processing, etc.
                           ],
               'links' => \@links,

               'html_form'   => qq!<FORM NAME="cdrForm" ACTION="$p/misc/cdr.cgi" METHOD="POST">!,
               #false laziness w/queue.html
               'html_foot' => sub {
                                if ( $areboxes ) {
                                  '<BR><INPUT TYPE="button" VALUE="select all" onClick="setAll(true)">'.
                                  '<INPUT TYPE="button" VALUE="unselect all" onClick="setAll(false)">'.
                                  qq!<BR><INPUT TYPE="submit" NAME="action" VALUE="reprocess selected" onClick="return confirm('Are you sure you want to reprocess the selected CDRs?')">!.
                                  qq!<INPUT TYPE="submit" NAME="action" VALUE="delete selected" onClick="return confirm('Are you sure you want to delete the selected CDRs?')"><BR>!.
                                  '<SCRIPT TYPE="text/javascript">'.
                                  '  function setAll(setTo) { '.
                                  '    theForm = document.cdrForm;'.
                                  '    for (i=0,n=theForm.elements.length;i<n;i++)'.
                                  '      if (theForm.elements[i].name.indexOf("acctid") != -1)'.
                                  '        theForm.elements[i].checked = setTo;'.
                                  '  }'.
                                  '</SCRIPT>';
                                } else {
                                  '';
                                }
                              },

             )
%>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('List rating data');

my $edit_data = $FS::CurrentUser::CurrentUser->access_right('Edit rating data');

my $areboxes = 0;

my $title = 'Call Detail Records';
my $hashref = {};

#process params for CDR search, populate $hashref...
# and fixup $count_query

my @search = ();

###
# dates
###

my $str2time_sql = str2time_sql;

my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
push @search, "$str2time_sql calldate) >= $beginning ",
              "$str2time_sql calldate) <= $ending";

###
# duration / billsec
###

push @search, FS::UI::Web::parse_lt_gt($cgi, 'duration');
push @search, FS::UI::Web::parse_lt_gt($cgi, 'billsec');

#above here things just push @search
#below here things also have to define $hashref->{} or push @qsearch
my @qsearch = @search;

###
# freesidestatus
###

if ( $cgi->param('freesidestatus') eq 'NULL' ) {

  $title = "Unprocessed $title";
  $hashref->{'freesidestatus'} = ''; # Record.pm will take care of it
  push @search, "( freesidestatus IS NULL OR freesidestatus = '' )";

} elsif ( $cgi->param('freesidestatus') =~ /^([\w ]+)$/ ) {

  $title = "Processed $title";
  $hashref->{'freesidestatus'} = $1;
  push @search, "freesidestatus = '$1'";

}

###
# termpartNstatus
###

foreach my $param ( grep /^termpart\d+status$/, $cgi->param ) {

  my $status = $cgi->param($param);

  $param =~ /^termpart(\d+)status$/ or die 'guru meditation 54something';
  my $termpart = $1;

  my $search = '';
  if ( $status eq 'NULL' ) {

    #false lazienss w/cdr_termination.pm (i should be a part_termination method)
    my $where_term =
      "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
    #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
    $search =
      "NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";

  } elsif ( $cgi->param('freesidestatus') =~ /^([\w ]+)$/ ) {

    #false lazienss w/cdr_termination.pm (i should be a part_termination method)
    my $where_term =
      "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart AND status = '$1' ) ";
    #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
    $search =
      "EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";

  }

  if ( $search ) {
    push @search,  $search;
    push @qsearch, $search;
  }

}

###
# src/dest/charged_party
###

if ( $cgi->param('src') =~ /^\s*([\d\-\+\ ]+)\s*$/ ) {
  ( my $src = $1 ) =~ s/\D//g;
  $hashref->{'src'} = $src;
  push @search, "src = '$src'";
}

if ( $cgi->param('dst') =~ /^\s*([\d\-\+ ]+)\s*$/ ) {
  ( my $dst = $1 ) =~ s/\D//g;
  $hashref->{'dst'} = $dst;
  push @search, "dst = '$dst'";
}

if ( $cgi->param('dcontext') =~ /^\s*(.+)\s*$/ ) {
  my $dcontext = $1;
  $hashref->{'dcontext'} = $dcontext;
  push @search, "dcontext = '$dcontext'";
}

if ( $cgi->param('charged_party') =~ /^\s*([\d\-\+\ ]+)\s*$/ ) {
  ( my $charged_party = $1 ) =~ s/\D//g;
  #$hashref->{'charged_party'} = $charged_party;
  #push @search, "charged_party = '$charged_party'";
  #XXX countrycode

  my $search = " (    charged_party = '$charged_party'
                   OR charged_party = '1$charged_party' ) ";

  push @search,  $search;
  push @qsearch, $search;
}

###
# cdrbatch
###

if ( $cgi->param('cdrbatch') ne '__ALL__' ) {
  if ( $cgi->param('cdrbatch') eq '' ) {
    my $search = "( cdrbatch IS NULL OR cdrbatch = '' )";
    push @qsearch, $search;
    push @search,  $search;
  } else {
    $hashref->{cdrbatch} = $cgi->param('cdrbatch');
    push @search, 'cdrbatch = '. dbh->quote($cgi->param('cdrbatch'));
  }
}

###
# acctid
###

if ( $cgi->param('acctid') =~ /\d/ ) {
  my $acctid = $cgi->param('acctid');
  $acctid =~ s/\r\n/\n/g; #browsers?
  my @acctid = map  { /^\s*(\d+)\s*$/ or die "guru meditation #4"; $1; }
               grep { /^\s*(\d+)\s*$/ }
               split(/\n/, $acctid);
  if ( @acctid ) {
    my $search = 'acctid IN ( '. join(',', @acctid). ' )';
    push @qsearch, $search;
    push @search,  $search;
  }
}

###
# finish it up
###

my $search = join(' AND ', @search);
$search = "WHERE $search" if $search;

my $count_query = "SELECT COUNT(*) FROM cdr $search";

my $qsearch = join(' AND ', @qsearch);
$qsearch = ( scalar(keys %$hashref) ? ' AND ' : ' WHERE ' ) . $qsearch
  if $qsearch;

###
# display fields
###

my %header = %{ FS::cdr->table_info->{'fields'} };

my @first = qw( acctid calldate clid charged_party src dst dcontext );
my %first = map { $_=>1 } @first;

my @fields = ( @first, grep !$first{$_}, fields('cdr') );

if ( $cgi->param('show') ) {
  @fields = grep $cgi->param("show_$_"), @fields;
}

my @header = map {
                   if ( exists($header{$_}) ) {
                     $header{$_};
                   } else {
                     my $header = $_;
                     $header =~ s/\_/ /g; #//wtf
                     ucfirst($header);
                   }
                 } @fields;

my $date_sub_factory = sub {
  my $column = shift;
  sub {
    #my $cdr = shift;
    my $date = shift->$column();
    $date ? time2str( '%Y-%m-%d %T', $date ) : ''; #config time2str format?
  };
};

my %fields = (
  #any other formatters?
  map { $_ => &{ $date_sub_factory }($_) } qw( startdate answerdate enddate )
);

my %links = (
  'svcnum' =>
    sub { $_[0]->svcnum ? [ $p.'view/svc_phone.cgi?', 'svcnum' ] : ''; },
);

@fields = map { exists($fields{$_}) ? $fields{$_} : $_ } @fields;

              #checkbox column
my @links = ( '', map { exists($links{$_}) ? $links{$_} : '' } @fields );

</%init>