settlement cdr processing, RT#5495
[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
27                'html_form'   => qq!<FORM NAME="cdrForm" ACTION="$p/misc/cdr.cgi" METHOD="POST">!,
28                #false laziness w/queue.html
29                'html_foot' => sub {
30                                 if ( $areboxes ) {
31                                   '<BR><INPUT TYPE="button" VALUE="select all" onClick="setAll(true)">'.
32                                   '<INPUT TYPE="button" VALUE="unselect all" onClick="setAll(false)">'.
33                                   qq!<BR><INPUT TYPE="submit" NAME="action" VALUE="reprocess selected" onClick="return confirm('Are you sure you want to reprocess the selected CDRs?')">!.
34                                   qq!<INPUT TYPE="submit" NAME="action" VALUE="delete selected" onClick="return confirm('Are you sure you want to delete the selected CDRs?')"><BR>!.
35                                   '<SCRIPT TYPE="text/javascript">'.
36                                   '  function setAll(setTo) { '.
37                                   '    theForm = document.cdrForm;'.
38                                   '    for (i=0,n=theForm.elements.length;i<n;i++)'.
39                                   '      if (theForm.elements[i].name.indexOf("acctid") != -1)'.
40                                   '        theForm.elements[i].checked = setTo;'.
41                                   '  }'.
42                                   '</SCRIPT>';
43                                 } else {
44                                   '';
45                                 }
46                               },
47
48              )
49 %>
50 <%init>
51
52 die "access denied"
53   unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
54
55 my $edit_data = $FS::CurrentUser::CurrentUser->access_right('Edit rating data');
56
57 my $areboxes = 0;
58
59 my $title = 'Call Detail Records';
60 my $hashref = {};
61
62 #process params for CDR search, populate $hashref...
63 # and fixup $count_query
64
65 my @search = ();
66
67 ###
68 # dates
69 ###
70
71 my $str2time_sql = str2time_sql;
72
73 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
74 push @search, "$str2time_sql calldate) >= $beginning ",
75               "$str2time_sql calldate) <= $ending";
76
77 ###
78 # duration / billsec
79 ###
80
81 push @search, FS::UI::Web::parse_lt_gt($cgi, 'duration');
82 push @search, FS::UI::Web::parse_lt_gt($cgi, 'billsec');
83
84 #above here things just push @search
85 #below here things also have to define $hashref->{} or push @qsearch
86 my @qsearch = @search;
87
88 ###
89 # freesidestatus
90 ###
91
92 if ( $cgi->param('freesidestatus') eq 'NULL' ) {
93
94   $title = "Unprocessed $title";
95   $hashref->{'freesidestatus'} = ''; # Record.pm will take care of it
96   push @search, "( freesidestatus IS NULL OR freesidestatus = '' )";
97
98 } elsif ( $cgi->param('freesidestatus') =~ /^([\w ]+)$/ ) {
99
100   $title = "Processed $title";
101   $hashref->{'freesidestatus'} = $1;
102   push @search, "freesidestatus = '$1'";
103
104 }
105
106 ###
107 # termpartNstatus
108 ###
109
110 foreach my $param ( grep /^termpart\d+status$/, $cgi->param ) {
111
112   my $status = $cgi->param($param);
113
114   $param =~ /^termpart(\d+)status$/ or die 'guru meditation 54something';
115   my $termpart = $1;
116
117   my $search = '';
118   if ( $status eq 'NULL' ) {
119
120     #false lazienss w/cdr_termination.pm (i should be a part_termination method)
121     my $where_term =
122       "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
123     #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
124     $search =
125       "NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
126
127   } elsif ( $cgi->param('freesidestatus') =~ /^([\w ]+)$/ ) {
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 AND status = '$1' ) ";
132     #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
133     $search =
134       "EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
135
136   }
137
138   if ( $search ) {
139     push @search,  $search;
140     push @qsearch, $search;
141   }
142
143 }
144
145 ###
146 # src/dest/charged_party
147 ###
148
149 if ( $cgi->param('src') =~ /^\s*([\d\-\+\ ]+)\s*$/ ) {
150   ( my $src = $1 ) =~ s/\D//g;
151   $hashref->{'src'} = $src;
152   push @search, "src = '$src'";
153 }
154
155 if ( $cgi->param('dst') =~ /^\s*([\d\-\+ ]+)\s*$/ ) {
156   ( my $dst = $1 ) =~ s/\D//g;
157   $hashref->{'dst'} = $dst;
158   push @search, "dst = '$dst'";
159 }
160
161 if ( $cgi->param('charged_party') =~ /^\s*([\d\-\+\ ]+)\s*$/ ) {
162   ( my $charged_party = $1 ) =~ s/\D//g;
163   #$hashref->{'charged_party'} = $charged_party;
164   #push @search, "charged_party = '$charged_party'";
165   #XXX countrycode
166
167   my $search = " (    charged_party = '$charged_party'
168                    OR charged_party = '1$charged_party' ) ";
169
170   push @search,  $search;
171   push @qsearch, $search;
172 }
173
174 ###
175 # cdrbatch
176 ###
177
178 if ( $cgi->param('cdrbatch') ne '__ALL__' ) {
179   if ( $cgi->param('cdrbatch') eq '' ) {
180     my $search = "( cdrbatch IS NULL OR cdrbatch = '' )";
181     push @qsearch, $search;
182     push @search,  $search;
183   } else {
184     $hashref->{cdrbatch} = $cgi->param('cdrbatch');
185     push @search, 'cdrbatch = '. dbh->quote($cgi->param('cdrbatch'));
186   }
187 }
188
189 ###
190 # finish it up
191 ###
192
193 my $search = join(' AND ', @search);
194 $search = "WHERE $search" if $search;
195
196 my $count_query = "SELECT COUNT(*) FROM cdr $search";
197
198 my $qsearch = join(' AND ', @qsearch);
199 $qsearch = ( scalar(keys %$hashref) ? ' AND ' : ' WHERE ' ) . $qsearch
200   if $qsearch;
201
202 ###
203 # display fields
204 ###
205
206 #XXX fill in some (more) nice names
207 my %header = (
208   'calldate'       => 'Call Date',
209   'clid'           => 'Caller ID',
210   'charged_party'  => 'Charged party',
211   'src'            => 'Source',
212   'dst'            => 'Destination',
213   'dcontext'       => 'Destination Context',
214   'channel'        => 'Channel',
215   'dstchannel'     => 'Destination Channel',
216   'freesidestatus' => 'Freeside status',
217 );
218
219 my @first = qw( acctid calldate clid charged_party src dst dcontext );
220 my %first = map { $_=>1 } @first;
221
222 my @fields = ( @first, grep !$first{$_}, fields('cdr') );
223
224 my @header = map {
225                    if ( exists($header{$_}) ) {
226                      $header{$_};
227                    } else {
228                      my $header = $_;
229                      $header =~ s/\_/ /g;
230                      ucfirst($header);
231                    }
232                  } @fields;
233
234 </%init>