Odyssey
ivr_report.prg
1 <?php
2 $monLibrary = dirname(__FILE__) . "/../library";
3 $sharedLibrary = dirname(__FILE__) . "/../../shared/library";
4 require_once("$monLibrary/cu_top.i");
5 require_once("$monLibrary/ck_hticket.i");
6 require_once("$monLibrary/cu_pass.i");
7 require_once("$sharedLibrary/cu_flagconst.i");
8 
9 //error_reporting(E_ALL & ~E_NOTICE);
10 /*
11  * NOTE: Need to get a valid domain for the IVR server so can utilize the Monitor cookie and get $Hu, among other things.
12  * Uncomment ck_hticket.i when that happens.
13  if (!CheckPerm($link, $Hu, basename($_SERVER['SCRIPT_NAME']), $_SERVER['REMOTE_ADDR'])) {
14  // ** Permissions failed
15  // ** redirect to new page
16  header("Location: /hcuadm/hcu_noperm.prg");
17  exit;
18  }
19 */
20 
21 $dms_ok=array("Flang"=>"string", "action"=>"string",
22  "cu"=>"string", "reportstartdate"=>"string", "reportenddate"=>"string",
23  "dnid"=>"string", "livebatch"=>"string", "liveserver"=>"string",
24  "ahdropdays"=>"string", "lhdropdays"=>"string", "byDNID" => "string",
25  "retrylimit"=>"digits", "histdays"=>"digits",
26  "rv"=>"string");
27 
28 dms_import_v2($gNameSpace, "POST", $dms_ok);
29 
30  $action = $gNameSpace["POST"]["action"];
31  $cu = $gNameSpace["POST"]["cu"];
32  $byDNID = $gNameSpace["POST"]["byDNID"];
33  $byDNID = isset($byDNID) && $byDNID == "Y";
34  $self= $_SERVER['PHP_SELF'];
35  $cuName = strtoupper( $cu );
36  $cuLower = strtolower( $cu );
37 
38  $reportStartDate = $gNameSpace["POST"]["reportstartdate"];
39  if ( empty( $reportStartDate ) )
40  $reportStartDate = date( "m/01/Y" );
41  $reportEndDate = $gNameSpace["POST"]["reportenddate"];
42  if ( empty( $reportEndDate ) )
43  $reportEndDate = date( "m/t/Y" );
44 
45  // Expose column to query if option is used.
46  $aDNIDColumn = $byDNID ? ", ivr_call.call_dnid" : "";
47 
48  $callTime = "(coalesce(Extract(epoch from ev.evend)::integer, 0) - Extract(epoch from ev.evstart)::integer)";
49 
50  $where = "(b.event_date >= date '" . dms_escape_string($reportStartDate) . "'
51  AND b.event_date < date '" . dms_escape_string($reportEndDate) . "' + interval '1 day')";
52 
53  $sql = "SELECT
54  ivr_call.call_cu $aDNIDColumn,
55  count(ivr_call.call_id) as calls_count,
56  coalesce(avg(CASE WHEN ev.evstart = ev.evend THEN NULL WHEN $callTime <= 0 THEN NULL ELSE $callTime END), 0) as calls_average
57  FROM ivr_call
58  LEFT JOIN (
59  SELECT a.call_id, MIN(b.event_date), MAX(a.event_date)
60  FROM ivr_event a
61  INNER JOIN ivr_event b ON a.call_id = b.call_id AND b.event_code = 'A'
62  WHERE $where
63  GROUP BY a.call_id
64  ) as ev(call_id, evstart, evend) ON ivr_call.call_id = ev.call_id
65  WHERE coalesce(Extract(epoch from ev.evend)::integer, 0) > 0 AND ivr_call.call_cu IS NOT NULL";
66 
67  if ( strlen( $cu ) > 0 ) {
68  $sql .= " AND ivr_call.call_cu = '$cu'";
69  }
70 
71  $sql .= " GROUP BY call_cu $aDNIDColumn
72  ORDER BY call_cu $aDNIDColumn";
73 
74  $sthCalls = db_query($sql, $dbh);
75 
76  $displayData = array();
77 
78  if ( $sthCalls ) {
79  $rowCnt = 0;
80  $totalCalls = 0;
81  $totalSeconds = 0;
82  $sumTotalMinutes = 0;
83  while ( $row = db_fetch_array($sthCalls, $rowCnt++ ) ) {
84  $callCU = $row["call_cu"];
85  $callCount = $row['calls_count'];
86  $callAvg = number_format( $row['calls_average'], 1 );
87  $callTotal = $callCount * $row['calls_average'];
88 
89  $callDNID = $byDNID ? intval($row["call_dnid"]) : 0;
90 
91  // ** ONLY Show the value in Total Minutes
92  $callTotalHours = floor( $callTotal / 60 );
93  $sumTotalMinutes += $callTotalHours;
94 
95  $callTotalStr = "$callTotalHours";
96 
97  $callAvgHours = floor( ( $callAvg / 3600 ) );
98  $callAvgMinutes = floor( ( ( $callAvg - $callAvgHours * 3600 ) / 60 ) );
99  $callAvgSeconds = round( $callAvg - $callAvgMinutes * 60 - $callAvgHours * 3660 );
100 
101  $callAvgStr = "$callAvgHours:$callAvgMinutes:$callAvgSeconds";
102 
103  $displayData[] = array( "callCU" => $callCU,
104  "callDNID" => $callDNID,
105  "callCount" => $callCount,
106  "callTotal" => $callTotalStr,
107  "callAvg" => $callAvgStr );
108 
109  // add to the totals
110  $totalCalls += $callCount;
111  $totalSeconds += $callTotal;
112  }
113 
114  $callTotalStr = "$sumTotalMinutes";
115 
116  if ( $totalCalls > 0 ) {
117  $totalAvg = number_format( $totalSeconds / $totalCalls, 1 );
118  } else {
119  $totalAvg = 0;
120  }
121 
122  $callAvgHours = floor( ( $totalAvg / 3600 ) );
123  $callAvgMinutes = floor( ( ( $totalAvg - $callAvgHours * 3600 ) / 60 ) );
124  $callAvgSeconds = round( $totalAvg - $callAvgMinutes * 60 - $callAvgHours * 3660 );
125 
126  $totalAvgStr = "$callAvgHours:$callAvgMinutes:$callAvgSeconds";
127 
128  if ( $gNameSpace["POST"]["rv"] == "RAW" ) {
129  // return the data in a CSV style
130  $outString = "";
131 
132  // totals
133  $totalInfo = Array(0, 0, 0, 0);
134 
135  // start a header
136  $outString .= "CU," . ($byDNID ? "DNID," : "") . "Number Calls,Total Call Time (minutes),Average Call Time\r\n";
137 
138  for ( $i = 0; $i < count( $displayData ); $i++ ) {
139  $outString .= "{$displayData[$i]['callCU']}," . ($byDNID ? $displayData[$i]["callDNID"] . "," : "") . "{$displayData[$i]['callCount']}," .
140  "{$displayData[$i]['callTotal']},{$displayData[$i]['callAvg']}\r\n";
141  }
142 
143  $outString .= "T," . ($byDNID ? "," : "") . "$totalCalls,$callTotalStr,$totalAvgStr\r\n";
144 
145  header( "Content-length: " . strlen($outString) );
146  header( "Content-type: application/octetstream" );
147  header( "Content-disposition: inline; filename=\"download.csv\"" );
148 
149  print $outString;
150 
151  exit;
152  }
153 
154  }
155 
156  // show the report visually
157  cu_header("Credit Union IVR Report");
158 
159  ?>
160  <link href="https://<?php echo $cloudfrontDomainName; ?>/homecu/css/KendoUI/<?php echo $homecuKendoVersion; ?>/kendo.common.min.css" rel="stylesheet">
161  <link href="https://<?php echo $cloudfrontDomainName; ?>/homecu/css/KendoUI/<?php echo $homecuKendoVersion; ?>/kendo.default.min.css" rel="stylesheet">
162  <link href="https://<?php echo $cloudfrontDomainName; ?>/homecu/css/kendo.homecucustom.4492.min.css" type="text/css" rel="stylesheet">
163  <script type="text/javascript" src="https://<?php echo $cloudfrontDomainName; ?>/jquery/js/jquery-1.9.1.min.js"></script>
164  <script src="https://<?php echo $cloudfrontDomainName; ?>/homecu/js/KendoUI/<?php echo $homecuKendoVersion; ?>/kendo.core.min.js"></script>
165  <script src="https://<?php echo $cloudfrontDomainName ?>/homecu/js/KendoUI/<?php echo $homecuKendoVersion ?>/kendo.custom.min.js.gz"></script>
166  <script>
167  $(document).ready(function() {
168  // create DatePicker from input HTML element
169  $("#reportStartDate").kendoDatePicker();
170 
171  $("#reportEndDate").kendoDatePicker();
172  });
173  </script>
174  <form name="inputs" action="<?php echo $self?>" method="post">
175  <table class="dmsbg" style="border-radius:5px;">
176  <tr>
177  <td colspan="4" class="bartitle" style="text-align:center;">IVR Call Report</td>
178  </tr>
179  <tr>
180  <td colspan="4" style="background-color:white;">
181  <a href="<?php echo "$produrl/monitor/mindex.html" ?>">Return to Monitor</a>
182  </td>
183  </tr>
184 <?php
185 
186  // allow user to enter a date range and pick a CU (otional)
187  $setDNID = ($byDNID ? "CHECKED" : "");
188  print <<<EOF
189  <tr><td class='hdr' align=right>Data Range: </td>
190  <td class='dtl'><input type="text" id="reportStartDate" name="reportstartdate" value="$reportStartDate" style='color:black;'></td>
191  <td class='dtl'><input type="text" id="reportEndDate" name="reportenddate" value="$reportEndDate" style='color:black;'></td>
192  <td class='dtl'>&nbsp;</td>
193  </tr>
194  <tr>
195  <tr><td class='hdr' align=right>&nbsp;</td>
196  <td class='dtl'><input type="checkbox" name="byDNID" value="Y" $setDNID>Show Long Code?</td>
197  <td class='dtl' colspan='2'>&nbsp;</td>
198  </tr>
199 EOF;
200 
201  // get a list of active CUs
202  $sql = "SELECT DISTINCT call_cu
203  FROM ivr_call
204  WHERE call_cu IS NOT NULL
205  ORDER BY call_cu";
206  $sth = db_query($sql, $dbh);
207 
208  if ( $sth ) {
209 
210  print <<<EOF
211  <tr><td class='hdr' align=right>Credit Union Filter: </td>
212  <td class='dtl'><select name="cu" size="1">
213  <option value="">Show All</option>
214 EOF;
215  $rowCnt = 0;
216  while ( $row = db_fetch_array($sth, $rowCnt++) ) {
217  $thisCU = trim( $row["call_cu"] );
218  printf("\n<option value=\"%s\" %s>%s</option>", $thisCU, (strlen( $cu ) && $thisCU == $cu) ? "SELECTED" : "", $thisCU);
219  }
220 
221  print <<<EOF
222  </select></td>
223  <td class='dtl'>&nbsp;</td>
224  <td class='dtl'>&nbsp;</td>
225  </tr>
226 EOF;
227  }
228 
229  print <<<EOF
230  <tr><td colspan="4"><input type="submit" value="Submit"></td></tr>
231 EOF;
232 
233  $aDNIDDisplayTop = $byDNID ? "<th class='hdrc'>Long Code</th>" : "";
234  $aDNIDDisplayColspan = $byDNID ? "colspan='2'" : "";
235  if ( $sthCalls ) {
236  print <<<EOF
237  <tr>
238  <th class='hdrc'>CU</th>
239  $aDNIDDisplayTop
240  <th class='hdrc'>Number Calls</th>
241  <th class='hdrc'>Total Call Time (minutes)</th>
242  <th class='hdrc'>Average Call Time</th>\n
243 EOF;
244 
245  $RGB = "odd";
246  for ( $i = 0; $i < count( $displayData ); $i++ ) {
247  print "<tr>
248  <td class='dtl $RGB'>&nbsp;{$displayData[$i]['callCU']}&nbsp;</td>"
249  . ($byDNID ? "<td class='dtl $RGB'>&nbsp;{$displayData[$i]['callDNID']}&nbsp;</td>" : "") . "
250  <td class='dtl $RGB'>&nbsp;{$displayData[$i]['callCount']}&nbsp;</td>
251  <td class='dtl $RGB'>&nbsp;{$displayData[$i]['callTotal']}&nbsp;</td>
252  <td class='dtl $RGB'>&nbsp;{$displayData[$i]['callAvg']}&nbsp;</td>
253  </tr>";
254 
255  $RGB = ($RGB == "odd" ? "even" : "odd");
256  }
257 
258  print "<tr style='color:white; font-weight:bold;'>
259  <td $aDNIDDisplayColspan>&nbsp;Totals:&nbsp;</td>
260  <td>&nbsp;$totalCalls&nbsp;</td>
261  <td>&nbsp;$callTotalStr&nbsp;</td>
262  <td>&nbsp;$totalAvgStr&nbsp;</td>
263  </tr>";
264  } else {
265  print "Error reading call log tables";
266  }
267 
268  print "</table>";
269  print "</form>";
270 
271  cu_footer();
272 
273  function dms_escape_string($value, $len=0, $ignore_mq=0) {
274  /*
275  PARAMETERS:
276  * value - This is the value that this function works with.
277  * len - If desired, the returned value can be truncated to
278  a certain number of characters. To truncate, pass a
279  positive integer in this field. To not truncate pass a 0
280  * ignore_mq - This paramter will check to see if we check the magic quotes
281  setting and if set, the stripslashes function will be used.
282  This is useful when preparing values for the database that were
283  posted.
284  */
285 
286  // ** ignore any stripping of slashes if ignore_mq is set
287  if ($ignore_mq == 0) {
288  // First if magic quotes is on -- then strip slashes
289  if (get_magic_quotes_gpc())
290  $value = stripslashes($value);
291  }
292  $value = trim($value); // Trim the resulting value
293 
294  // ** Before escaping the string, be certain it is not longer than the length that was
295  // specified
296  if ($len > 0) {
297  $value = substr($value, 0, $len);
298  }
299 
300  // ** Escape the string to be saved
301  $ret_value = pg_escape_string($value);
302 
303  return $ret_value;
304  }
305 ?>