<?php

require_once "BASEPATH.php";

class ReporteEntranteDiario extends MY_Controller
{
    public function trata(&$out, $arreglo, &$lastLinePrinted)
    {
        $llaves = array_keys($arreglo);
        //fputcsv($out,array_values($llaves));
        $llavesNoArreglos = array();
        for ($i = 0; $i < count($llaves); $i++) {
            if (!is_array($arreglo[$llaves[$i]])) {
                //$this->trata($out,$arreglo[$llaves[$i]]);
                $llavesNoArreglos[] = $llaves[$i];
            }
        }
        $temp = array();
        for ($i = 0; $i < count($llaves); $i++) {
            if (!is_array($arreglo[$llaves[$i]])) {
                $temp[] = $arreglo[$llaves[$i]];
            }
        }
        if (count($temp) > 0) {
            if ($llavesNoArreglos != $lastLinePrinted) {
                fputcsv($out, array_values($llavesNoArreglos));
                $lastLinePrinted = $llavesNoArreglos;
            }
            fputcsv($out, array_values($temp));
        }
        for ($i = 0; $i < count($llaves); $i++) {
            if (is_array($arreglo[$llaves[$i]])) {
                $temp2 = array();
                $temp2[0] = $llaves[$i];
                if (!is_numeric($temp2[0])) {
                    fputcsv($out, array_values($temp2));
                }

                $this->trata($out, $arreglo[$llaves[$i]], $lastLinePrinted);
            }
        }
    }
    public function dailyInbound()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('reporteentrantediario', "spanish");
                break;
            case "en":
                $this->lang->load('reporteentrantediario', "english");
                break;
            default:
                $this->lang->load('reporteentrantediario', "spanish");
                break;
        }
        include_once RUTA_APP . 'Conexion.php';
        if (!empty($_SESSION["Usuario"]) && ($_SESSION["Grupo"] == GRUPO_ADMINS || $_SESSION["Grupo"] == GRUPO_SUPERVISORES)) {
            include_once RUTA_APP . 'Sesiones.php';
            $con = getConnection();
            $and = "";
            if ($_SESSION["Cliente"] != -1) {
                $cliente = $_SESSION["Cliente"];
                $and = "AND campaign_id IN (SELECT campaign_id FROM vicidial_campaigns WHERE user_group  = '$cliente')";
            }
            $sal = array();
            $campana = limpia($_GET["campana"]);
            $campana1 = $campana;
            if ($campana == -1) {
                $campana = "campaign_id";
            } else {
                $campana = "'" . $campana . "'";
            }
            $archived = false;
            if (isset($_GET["archived"]) && limpia($_GET["archived"]) == 1) {
                $archived = true;
            }

            $fechaInicial = limpia($_GET["fechaInicial"]);
            $fechaFinal = limpia($_GET["fechaFinal"]);
            $d = 0;
            $q = 0;
            $hr = 0;
            $shift_hrs = 0;
            $prev_week = $daySTART[0];
            $prev_month = $daySTART[0];
            $prev_qtr = $daySTART[0];
            $i = 0;
            $h = 4;
            $j = 0;
            $Zhour = 1;
            $active_time = 0;
            $hour = ($SQtime_ARY[0] - 1);
            $startSEC = ($SQsec - 900);
            $endSEC = ($SQsec - 1);
            if ($SQtime_ARY[1] > 14) {
                $h = 1;
                $hour++;
                if ($hour < 10) {$hour = "0$hour";}
            }
            if ($SQtime_ARY[1] > 29) {$h = 2;}
            if ($SQtime_ARY[1] > 44) {$h = 3;}
            while ($i < 96) {
                $startSEC = ($startSEC + 900);
                $endSEC = ($endSEC + 900);
                $time = '      ';
                if ($h >= 4) {
                    $hour++;
                    if ($Zhour == '00') {
                        $startSEC = 0;
                        $endSEC = 899;
                    }
                    $h = 0;
                    if ($hour < 10) {$hour = "0$hour";}
                    $Stime = "$hour:00";
                    $Etime = "$hour:15";
                    $time = "+$Stime-$Etime+";
                }
                if ($h == 1) {
                    $Stime = "$hour:15";
                    $Etime = "$hour:30";
                    $time = " $Stime-$Etime ";
                }
                if ($h == 2) {
                    $Stime = "$hour:30";
                    $Etime = "$hour:45";
                    $time = " $Stime-$Etime ";
                }
                if ($h == 3) {
                    $Zhour = $hour;
                    $Zhour++;
                    if ($Zhour < 10) {$Zhour = "0$Zhour";}
                    if ($Zhour == 24) {$Zhour = "00";}
                    $Stime = "$hour:45";
                    $Etime = "$Zhour:00";
                    $time = " $Stime-$Etime ";
                    if ($Zhour == '00') {$hour = ($Zhour - 1);}
                }

                if ((($startSEC >= $SQsec) and ($endSEC <= $EQsec) and ($EQsec > $SQsec)) or
                    (($startSEC >= $SQsec) and ($EQsec < $SQsec)) or
                    (($endSEC <= $EQsec) and ($EQsec < $SQsec))) {
                    $HMdisplay[$j] = $time;
                    $HMstart[$j] = $Stime;
                    $HMend[$j] = $Etime;
                    $HMSepoch[$j] = $startSEC;
                    $HMEepoch[$j] = $endSEC;

                    $j++;
                }

                $h++;
                $i++;
            }
            $TOTintervals = $q;
            $query = "select queue_seconds,UNIX_TIMESTAMP(call_date),length_in_sec,status,term_reason,call_date,user,UNIX_TIMESTAMP(NOW()) from vicidial_closer_log where DATE(call_date) >= DATE('$fechaInicial') and DATE(call_date) <= DATE('$fechaFinal') and  campaign_id = $campana $and ";
            if ($archived) {
                $query .= " UNION select queue_seconds,UNIX_TIMESTAMP(call_date),length_in_sec,status,term_reason,call_date,user,UNIX_TIMESTAMP(NOW()) from vicidial_closer_log_archived where DATE(call_date) >= DATE('$fechaInicial') and DATE(call_date) <= DATE('$fechaFinal') and  campaign_id = $campana $and ";
            }
            $result = sv_mysqli_query($con, $query);
            $records_to_grab = sv_mysqli_num_rows($result);
            $i = 0;
            $qs = $dt = $ut = $ls = $st = $tr = $at = array();
            $epoch_interval = 86400;
            $ag = array();
            while ($i < $records_to_grab) {
                $row = sv_mysqli_fetch_array($result);
                $qs[$i] = $row[0];
                $dt[$i] = 0;
                $ut[$i] = ($row[7] - $row[1]);
                while ($ut[$i] >= $epoch_interval) {
                    $ut[$i] = ($ut[$i] - $epoch_interval);
                    $dt[$i]++;
                }
                $dt[$i] = ($dt[$i] - 1);
                $ls[$i] = $row[2];
                $st[$i] = $row[3];
                $tr[$i] = $row[4];
                $at[$i] = $row[5]; # Actual time
                if ($row[6] != "VDCL" && $row[6] != "") {
                    $ag[$i] = $row[6];
                } # User
                $i++;
            }
            $MT[0] = '0';
            $totCALLS = 0;
            $totDROPS = 0;
            $totQUEUE = 0;
            $totCALLSsec = 0;
            $totDROPSsec = 0;
            $totQUEUEsec = 0;
            $totCALLSmax = 0;
            $totDROPSmax = 0;
            $totQUEUEmax = 0;
            $totCALLSdate = $MT;
            $totDROPSdate = $MT;
            $totQUEUEdate = $MT;
            $qrtCALLS = $MT;
            $qrtDROPS = $MT;
            $qrtQUEUE = $MT;
            $qrtCALLSsec = $MT;
            $qrtDROPSsec = $MT;
            $qrtQUEUEsec = $MT;
            $qrtCALLSavg = $MT;
            $qrtDROPSavg = $MT;
            $qrtQUEUEavg = $MT;
            $qrtCALLSmax = $MT;
            $qrtDROPSmax = $MT;
            $qrtQUEUEmax = $MT;
            $totABANDONSdate = $MT;
            $totANSWERSdate = $MT;
            $totANSWERS = 0;
            $totAGENTS = 0;
            $totABANDONS = 0;
            $totANSWERSsec = 0;
            $totABANDONSsec = 0;
            $totANSWERSspeed = 0;
            $totSTATUSES = array();

            $FtotANSWERS = 0;
            $FtotAGENTS = count(array_count_values($ag));
            $FtotABANDONS = 0;
            $FtotANSWERSsec = 0;
            $FtotABANDONSsec = 0;
            $FtotANSWERSspeed = 0;
            $j = $i = 0;
            $j = 0;
            while ($j < $TOTintervals) {
                $totABANDONSdate[$j] = 0;
                $totABANDONSsecdate[$j] = 0;
                $totANSWERSdate[$j] = 0;
                $totANSWERSsecdate[$j] = 0;
                $totANSWERSspeeddate[$j] = 0;
                $i = 0;
                $agents_array = array();
                while ($i < $records_to_grab) {
                    if (($at[$i] >= $daySTART[$j]) and ($at[$i] <= $dayEND[$j])) {
                        $totCALLS++;
                        $totCALLSsec = ($totCALLSsec + $ls[$i]);
                        $totCALLSsecDATE[$j] = ($totCALLSsecDATE[$j] + $ls[$i]);
                        $totCALLSdate[$j]++;
                        if ($ag[$i] != "VDCL" && $ag[$i] != "") {
                            $totAGENTSdate[$j][$ag[$i]]++;
                        }

                        $totSTATUSES[$st[$i]]++;
                        $totSTATUSESdate[$j][$st[$i]]++;
                        if ($totCALLSmax < $ls[$i]) {
                            $totCALLSmax = $ls[$i];
                        }

                        if ($qrtCALLSmax[$j] < $ls[$i]) {
                            $qrtCALLSmax[$j] = $ls[$i];
                        }

                        if (preg_match('/ABANDON|NOAGENT|QUEUETIMEOUT|AFTERHOURS|MAXCALLS/', $tr[$i])) {
                            $totABANDONSdate[$j]++;
                            $totABANDONSsecdate[$j] += $ls[$i];
                            $FtotABANDONS++;
                            $FtotABANDONSsec += $ls[$i];
                        } else {
                            $totANSWERSdate[$j]++;
                            if (($ls[$i] - $qs[$i] - 15) > 0) {
                                $totANSWERSsecdate[$j] += ($ls[$i] - $qs[$i] - 15);
                                $FtotANSWERSsec += ($ls[$i] - $qs[$i] - 15);
                            }
                            $totANSWERSspeeddate[$j] += $qs[$i];
                            $FtotANSWERS++;
                            $FtotANSWERSspeeddate += $qs[$i];
                        }
                        if (preg_match('/DROP/', $st[$i])) {
                            $totDROPS++;
                            $totDROPSsec = ($totDROPSsec + $ls[$i]);
                            $totDROPSsecDATE[$j] = ($totDROPSsecDATE[$j] + $ls[$i]);
                            $totDROPSdate[$j]++;
                        }
                        if ($qs[$i] > 0) {
                            $totQUEUE++;
                            $totQUEUEsec = ($totQUEUEsec + $qs[$i]);
                            $totQUEUEsecDATE[$j] = ($totQUEUEsecDATE[$j] + $qs[$i]);
                            $totQUEUEdate[$j]++;
                        }
                    }
                    $i++;
                }
                $j++;
            }
            $graph_stats = array();
            $mtd_graph_stats = array();
            $wtd_graph_stats = array();
            $qtd_graph_stats = array();
            $da = 0;
            $wa = 0;
            $ma = 0;
            $qa = 0;
            $max_offered = 1;
            $max_answered = 1;
            $max_agents = 1;
            $max_abandoned = 1;
            $max_abandonpct = 1;
            $max_avgabandontime = 1;
            $max_avganswerspeed = 1;
            $max_avgtalktime = 1;
            $max_totaltalktime = 1;
            $max_totalwraptime = 1;
            $max_totalcalltime = 1;
            $max_wtd_offered = 1;
            $max_wtd_answered = 1;
            $max_wtd_agents = 1;
            $max_wtd_abandoned = 1;
            $max_wtd_abandonpct = 1;
            $max_wtd_avgabandontime = 1;
            $max_wtd_avganswerspeed = 1;
            $max_wtd_avgtalktime = 1;
            $max_wtd_totaltalktime = 1;
            $max_wtd_totalwraptime = 1;
            $max_wtd_totalcalltime = 1;
            $max_mtd_offered = 1;
            $max_mtd_answered = 1;
            $max_mtd_abandoned = 1;
            $max_mtd_agents = 1;
            $max_mtd_abandonpct = 1;
            $max_mtd_avgabandontime = 1;
            $max_mtd_avganswerspeed = 1;
            $max_mtd_avgtalktime = 1;
            $max_mtd_totaltalktime = 1;
            $max_mtd_totalwraptime = 1;
            $max_mtd_totalcalltime = 1;
            $max_qtd_offered = 1;
            $max_qtd_answered = 1;
            $max_qtd_abandoned = 1;
            $max_qtd_agents = 1;
            $max_qtd_abandonpct = 1;
            $max_qtd_avgabandontime = 1;
            $max_qtd_avganswerspeed = 1;
            $max_qtd_avgtalktime = 1;
            $max_qtd_totaltalktime = 1;
            $max_qtd_totalwraptime = 1;
            $max_qtd_totalcalltime = 1;
            $totCALLSwtd = 0;
            $totANSWERSwtd = 0;
            $totAGENTSwtd = 0;
            $AGENTS_wtd_array = array();
            $totANSWERSsecwtd = 0;
            $totANSWERSspeedwtd = 0;
            $totABANDONSwtd = 0;
            $totABANDONSsecwtd = 0;
            $totSTATUSESwtd = array();

            $totCALLSmtd = 0;
            $totANSWERSmtd = 0;
            $totAGENTSmtd = 0;
            $AGENTS_mtd_array = array();
            $totANSWERSsecmtd = 0;
            $totANSWERSspeedmtd = 0;
            $totABANDONSmtd = 0;
            $totABANDONSsecmtd = 0;
            $totSTATUSESmtd = array();

            $totCALLSqtd = 0;
            $totANSWERSqtd = 0;
            $totAGENTSqtd = 0;
            $AGENTS_qtd_array = array();
            $totANSWERSsecqtd = 0;
            $totANSWERSspeedqtd = 0;
            $totABANDONSqtd = 0;
            $totABANDONSsecqtd = 0;
            $d = 0;
            $totSTATUSESqtd = array();
            while ($d < $TOTintervals) {
                if ($totDROPSdate[$d] < 1) {$totDROPSdate[$d] = 0;}
                if ($totQUEUEdate[$d] < 1) {$totQUEUEdate[$d] = 0;}
                if ($totCALLSdate[$d] < 1) {$totCALLSdate[$d] = 0;}
                if ($totDROPSdate[$d] > 0) {$totDROPSpctDATE[$d] = (($totDROPSdate[$d] / $totCALLSdate[$d]) * 100);} else { $totDROPSpctDATE[$d] = 0;}
                $totDROPSpctDATE[$d] = round($totDROPSpctDATE[$d], 2);
                if ($totQUEUEdate[$d] > 0) {$totQUEUEpctDATE[$d] = (($totQUEUEdate[$d] / $totCALLSdate[$d]) * 100);} else { $totQUEUEpctDATE[$d] = 0;}
                $totQUEUEpctDATE[$d] = round($totQUEUEpctDATE[$d], 2);
                if ($totDROPSsecDATE[$d] > 0) {$totDROPSavgDATE[$d] = ($totDROPSsecDATE[$d] / $totDROPSdate[$d]);} else { $totDROPSavgDATE[$d] = 0;}
                if ($totQUEUEsecDATE[$d] > 0) {$totQUEUEavgDATE[$d] = ($totQUEUEsecDATE[$d] / $totQUEUEdate[$d]);} else { $totQUEUEavgDATE[$d] = 0;}
                if ($totQUEUEsecDATE[$d] > 0) {$totQUEUEtotDATE[$d] = ($totQUEUEsecDATE[$d] / $totCALLSdate[$d]);} else { $totQUEUEtotDATE[$d] = 0;}
                if ($totCALLSsecDATE[$d] > 0) {
                    $totCALLSavgDATE[$d] = ($totCALLSsecDATE[$d] / $totCALLSdate[$d]);
                    $totTIME_M = ($totCALLSsecDATE[$d] / 60);
                    $totTIME_M_int = round($totTIME_M, 2);
                    $totTIME_M_int = intval("$totTIME_M");
                    $totTIME_S = ($totTIME_M - $totTIME_M_int);
                    $totTIME_S = ($totTIME_S * 60);
                    $totTIME_S = round($totTIME_S, 0);
                    if ($totTIME_S < 10) {$totTIME_S = "0$totTIME_S";}
                    $totTIME_MS = "$totTIME_M_int:$totTIME_S";
                    $totTIME_MS = sprintf("%8s", $totTIME_MS);
                } else {
                    $totCALLSavgDATE[$d] = 0;
                    $totTIME_MS = '        ';
                }
                $totCALLSdate[$d] = sprintf("%7s", $totCALLSdate[$d]);
                if ($totCALLSdate[$d] > 0) {
                    $totABANDONSpctDATE[$d] = sprintf("%7.2f", (100 * $totABANDONSdate[$d] / $totCALLSdate[$d]));
                } else {
                    $totCALLSdate[$d] = "      0";
                    $totABANDONSpctDATE[$d] = "    0.0";
                }
                if ($totABANDONSdate[$d] > 0) {
                    $totABANDONSavgTIME[$d] = sprintf("%7s", date("i:s", mktime(0, 0, round($totABANDONSsecdate[$d] / $totABANDONSdate[$d]))));
                    if (round($totABANDONSsecdate[$d] / $totABANDONSdate[$d]) > $max_avgabandontime) {$max_avgabandontime = round($totABANDONSsecdate[$d] / $totABANDONSdate[$d]);}
                    $graph_stats[$d][11] = round($totABANDONSsecdate[$d] / $totABANDONSdate[$d]);
                } else {
                    $totABANDONSdate[$d] = "0";
                    $totABANDONSavgTIME[$d] = "  00:00";
                    $graph_stats[$d][11] = 0;
                }
                if ($totANSWERSdate[$d] > 0) {
                    $totANSWERSavgspeedTIME[$d] = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSspeeddate[$d] / $totANSWERSdate[$d]))));
                    $totANSWERSavgTIME[$d] = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSsecdate[$d] / $totANSWERSdate[$d]))));
                    if (round($totANSWERSspeeddate[$d] / $totANSWERSdate[$d]) > $max_avganswerspeed) {$max_avganswerspeed = round($totANSWERSspeeddate[$d] / $totANSWERSdate[$d]);}
                    $graph_stats[$d][12] = round($totANSWERSspeeddate[$d] / $totANSWERSdate[$d]);
                    $graph_stats[$d][16] = round($totANSWERSsecdate[$d] / $totANSWERSdate[$d]);
                } else {
                    $totANSWERSdate[$d] = "0";
                    $totANSWERSavgspeedTIME[$d] = " 00:00";
                    $totANSWERSavgTIME[$d] = " 00:00";
                    $graph_stats[$d][12] = 0;
                    $graph_stats[$d][16] = 0;
                }
                $totANSWERStalkTIME[$d] = sprintf("%10s", floor($totANSWERSsecdate[$d] / 3600) . date(":i:s", mktime(0, 0, $totANSWERSsecdate[$d])));
                $totANSWERSwrapTIME[$d] = sprintf("%10s", floor(($totANSWERSdate[$d] * 15) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSdate[$d] * 15))));
                if (($totANSWERSdate[$d] * 15) > $max_totalwraptime) {$max_totalwraptime = ($totANSWERSdate[$d] * 15);}
                $graph_stats[$d][13] = ($totANSWERSdate[$d] * 15);
                $graph_stats[$d][14] = ($totANSWERSsecdate[$d] + ($totANSWERSdate[$d] * 15));
                $graph_stats[$d][15] = $totANSWERSsecdate[$d];
                $totANSWERStotTIME[$d] = sprintf("%10s", floor(($totANSWERSsecdate[$d] + ($totANSWERSdate[$d] * 15)) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSsecdate[$d] + ($totANSWERSdate[$d] * 15)))));
                $totANSWERSdate[$d] = sprintf("%8s", $totANSWERSdate[$d]);
                $totABANDONSdate[$d] = sprintf("%9s", $totABANDONSdate[$d]);
                if (date("w", strtotime($daySTART[$d])) == 0 && date("w", strtotime($daySTART[$d - 1])) != 0 && $d > 0) {
                    $totAGENTSwtd = count(array_count_values($AGENTS_wtd_array));
                    if ($totCALLSwtd > 0) {
                        $totABANDONSpctwtd = sprintf("%7.2f", (100 * $totABANDONSwtd / $totCALLSwtd));
                    } else {
                        $totABANDONSpctwtd = "    0.0";
                    }
                    if ($totABANDONSwtd > 0) {
                        $totABANDONSavgTIMEwtd = sprintf("%7s", date("i:s", mktime(0, 0, round($totABANDONSsecwtd / $totABANDONSwtd))));
                        if (round($totABANDONSsecwtd / $totABANDONSwtd) > $max_wtd_avgabandontime) {$max_wtd_avgabandontime = round($totABANDONSsecwtd / $totABANDONSwtd);}
                        $wtd_graph_stats[$wa][11] = round($totABANDONSsecwtd / $totABANDONSwtd);
                    } else {
                        $totABANDONSavgTIMEwtd = "  00:00";
                        $wtd_graph_stats[$wa][11] = 0;
                    }
                    if ($totANSWERSwtd > 0) {
                        $totANSWERSavgspeedTIMEwtd = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSspeedwtd / $totANSWERSwtd))));
                        $totANSWERSavgTIMEwtd = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSsecwtd / $totANSWERSwtd))));
                        if (round($totANSWERSspeedwtd / $totANSWERSwtd) > $max_wtd_avganswerspeed) {$max_wtd_avganswerspeed = round($totANSWERSspeedwtd / $totANSWERSwtd);}
                        $wtd_graph_stats[$wa][12] = round($totANSWERSspeedwtd / $totANSWERSwtd);
                        $wtd_graph_stats[$wa][16] = round($totANSWERSsecwtd / $totANSWERSwtd);
                    } else {
                        $totANSWERSavgspeedTIMEwtd = " 00:00";
                        $totANSWERSavgTIMEwtd = " 00:00";
                        $wtd_graph_stats[$wa][12] = 0;
                        $wtd_graph_stats[$wa][16] = 0;
                    }
                    $totANSWERStalkTIMEwtd = sprintf("%10s", floor($totANSWERSsecwtd / 3600) . date(":i:s", mktime(0, 0, $totANSWERSsecwtd)));
                    $totANSWERSwrapTIMEwtd = sprintf("%10s", floor(($totANSWERSwtd * 15) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSwtd * 15))));
                    if (($totANSWERSwtd * 15) > $max_wtd_totalwraptime) {$max_wtd_totalwraptime = ($totANSWERSwtd * 15);}
                    $wtd_graph_stats[$wa][13] = ($totANSWERSwtd * 15);
                    $wtd_graph_stats[$wa][14] = ($totANSWERSsecwtd + ($totANSWERSwtd * 15));
                    $wtd_graph_stats[$wa][15] = $totANSWERSsecwtd;
                    $totANSWERStotTIMEwtd = sprintf("%10s", floor(($totANSWERSsecwtd + ($totANSWERSwtd * 15)) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSsecwtd + ($totANSWERSwtd * 15)))));
                    $totAGENTSwtd = count(array_count_values($AGENTS_wtd_array));
                    $totAGENTSwtd = sprintf("%8s", $totAGENTSwtd);
                    $totANSWERSwtd = sprintf("%8s", $totANSWERSwtd);
                    $totABANDONSwtd = sprintf("%9s", $totABANDONSwtd);
                    $totCALLSwtd = sprintf("%7s", $totCALLSwtd);
                    if (trim($totCALLSwtd) > $max_wtd_offered) {$max_wtd_offered = trim($totCALLSwtd);}
                    if (trim($totANSWERSwtd) > $max_wtd_answered) {$max_wtd_answered = trim($totANSWERSwtd);}
                    if (trim($totAGENTSwtd) > $max_wtd_agents) {$max_wtd_agents = trim($totAGENTSwtd);}
                    if (trim($totABANDONSwtd) > $max_wtd_abandoned) {$max_wtd_abandoned = trim($totABANDONSwtd);}
                    if (trim($totABANDONSpctwtd) > $max_wtd_abandonpct) {$max_wtd_abandonpct = trim($totABANDONSpctwtd);}
                    if (round($totANSWERSsecwtd / $totANSWERSwtd) > $max_wtd_avgtalktime) {$max_wtd_avgtalktime = round($totANSWERSsecwtd / $totANSWERSwtd);}
                    if (trim($totANSWERSsecwtd) > $max_wtd_totaltalktime) {$max_wtd_totaltalktime = trim($totANSWERSsecwtd);}
                    if (trim($totANSWERSsecwtd + ($totANSWERSwtd * 15)) > $max_wtd_totalcalltime) {$max_wtd_totalcalltime = trim($totANSWERSsecwtd + ($totANSWERSwtd * 15));}
                    $week = date("W", strtotime($dayEND[$d - 1]));
                    $year = substr($dayEND[$d - 1], 0, 4);
                    $wtd_graph_stats[$wa][0] = "Week $week, $year";
                    $wtd_graph_stats[$wa][1] = trim($totCALLSwtd);
                    $wtd_graph_stats[$wa][2] = trim($totANSWERSwtd);
                    $wtd_graph_stats[$wa][3] = trim($totABANDONSwtd);
                    $wtd_graph_stats[$wa][4] = trim($totABANDONSpctwtd);
                    $wtd_graph_stats[$wa][5] = trim($totABANDONSavgTIMEwtd);
                    $wtd_graph_stats[$wa][6] = trim($totANSWERSavgspeedTIMEwtd);
                    $wtd_graph_stats[$wa][7] = trim($totANSWERSavgTIMEwtd);
                    $wtd_graph_stats[$wa][8] = trim($totANSWERStalkTIMEwtd);
                    $wtd_graph_stats[$wa][9] = trim($totANSWERSwrapTIMEwtd);
                    $wtd_graph_stats[$wa][10] = trim($totANSWERStotTIMEwtd);
                    $wtd_graph_stats[$wa][17] = trim($totAGENTSwtd);
                    $obj = array();
                    $obj["TotalCallsOffered"] = $totCALLSwtd;
                    $obj["TotalCallsAnswered"] = $totANSWERSwtd;
                    $obj["TotalAgentsAnswered"] = $totAGENTSwtd;
                    $obj["TotalCallsAbandoned"] = $totABANDONSwtd;
                    $obj["TotalAbandonsPercent"] = $totABANDONSpctwtd;
                    $obj["AvgAbandonTime"] = $totABANDONSavgTIMEwtd;
                    $obj["AvgAnswerSpeed"] = $totANSWERSavgspeedTIMEwtd;
                    $obj["AvgTalkTime"] = $totANSWERSavgTIMEwtd;
                    $obj["TotalTalkTime"] = $totANSWERStalkTIMEwtd;
                    $obj["TotalWrapTime"] = $totANSWERSwrapTIMEwtd;
                    $obj["TotalCallTime"] = $totANSWERStotTIMEwtd;
                    $sal["Tabla"]["Registros"][] = $obj;
                    $wa++;
                    $totCALLSwtd = 0;
                    $totANSWERSwtd = 0;
                    $AGENTS_wtd_array = array();
                    $totANSWERSsecwtd = 0;
                    $totANSWERSspeedwtd = 0;
                    $totABANDONSwtd = 0;
                    $totABANDONSsecwtd = 0;
                    $totSTATUSESwtd = array();
                }
                if (date("d", strtotime($daySTART[$d])) == 1 && $d > 0 && date("d", strtotime($daySTART[$d - 1])) != 1) {
                    $totAGENTSmtd = count(array_count_values($AGENTS_mtd_array));
                    if ($totCALLSmtd > 0) {
                        $totABANDONSpctmtd = sprintf("%7.2f", (100 * $totABANDONSmtd / $totCALLSmtd));
                    } else {
                        $totABANDONSpctmtd = "    0.0";
                    }
                    if ($totABANDONSmtd > 0) {
                        $totABANDONSavgTIMEmtd = sprintf("%7s", date("i:s", mktime(0, 0, round($totABANDONSsecmtd / $totABANDONSmtd))));
                        if (round($totABANDONSsecmtd / $totABANDONSmtd) > $max_mtd_avgabandontime) {$max_mtd_avgabandontime = round($totABANDONSsecmtd / $totABANDONSmtd);}
                        $mtd_graph_stats[$ma][11] = round($totABANDONSsecmtd / $totABANDONSmtd);
                    } else {
                        $totABANDONSavgTIMEmtd = "  00:00";
                        $mtd_graph_stats[$ma][11] = 0;
                    }
                    if ($totANSWERSmtd > 0) {
                        $totANSWERSavgspeedTIMEmtd = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSspeedmtd / $totANSWERSmtd))));
                        $totANSWERSavgTIMEmtd = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSsecmtd / $totANSWERSmtd))));
                        if (round($totANSWERSspeedmtd / $totANSWERSmtd) > $max_mtd_avganswerspeed) {$max_mtd_avganswerspeed = round($totANSWERSspeedmtd / $totANSWERSmtd);}
                        $mtd_graph_stats[$ma][12] = round($totANSWERSspeedmtd / $totANSWERSmtd);
                        $mtd_graph_stats[$ma][16] = round($totANSWERSsecmtd / $totANSWERSmtd);
                    } else {
                        $totANSWERSavgspeedTIMEmtd = " 00:00";
                        $totANSWERSavgTIMEmtd = " 00:00";
                        $mtd_graph_stats[$ma][12] = 0;
                        $mtd_graph_stats[$ma][16] = 0;
                    }
                    $totANSWERStalkTIMEmtd = sprintf("%10s", floor($totANSWERSsecmtd / 3600) . date(":i:s", mktime(0, 0, $totANSWERSsecmtd)));
                    $totANSWERSwrapTIMEmtd = sprintf("%10s", floor(($totANSWERSmtd * 15) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSmtd * 15))));
                    if (($totANSWERSmtd * 15) > $max_mtd_totalwraptime) {$max_mtd_totalwraptime = ($totANSWERSmtd * 15);}
                    $mtd_graph_stats[$ma][13] = ($totANSWERSmtd * 15);
                    $mtd_graph_stats[$ma][14] = ($totANSWERSsecmtd + ($totANSWERSmtd * 15));
                    $mtd_graph_stats[$ma][15] = $totANSWERSsecmtd;
                    $totANSWERStotTIMEmtd = sprintf("%10s", floor(($totANSWERSsecmtd + ($totANSWERSmtd * 15)) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSsecmtd + ($totANSWERSmtd * 15)))));
                    $totAGENTSmtd = count(array_count_values($AGENTS_mtd_array));
                    $totAGENTSmtd = sprintf("%8s", $totAGENTSmtd);
                    $totANSWERSmtd = sprintf("%8s", $totANSWERSmtd);
                    $totABANDONSmtd = sprintf("%9s", $totABANDONSmtd);
                    $totCALLSmtd = sprintf("%7s", $totCALLSmtd);
                    if (trim($totCALLSmtd) > $max_mtd_offered) {$max_mtd_offered = trim($totCALLSmtd);}
                    if (trim($totANSWERSmtd) > $max_mtd_answered) {$max_mtd_answered = trim($totANSWERSmtd);}
                    if (trim($totABANDONSmtd) > $max_mtd_abandoned) {$max_mtd_abandoned = trim($totABANDONSmtd);}
                    if (trim($totAGENTSmtd) > $max_mtd_agents) {$max_mtd_agents = trim($totAGENTSmtd);}
                    if (trim($totABANDONSpctmtd) > $max_mtd_abandonpct) {$max_mtd_abandonpct = trim($totABANDONSpctmtd);}
                    if (round($totANSWERSsecmtd / $totANSWERSmtd) > $max_mtd_avgtalktime) {$max_mtd_avgtalktime = round($totANSWERSsecmtd / $totANSWERSmtd);}
                    if (trim($totANSWERSsecmtd) > $max_mtd_totaltalktime) {$max_mtd_totaltalktime = trim($totANSWERSsecmtd);}
                    if (trim($totANSWERSsecmtd + ($totANSWERSmtd * 15)) > $max_mtd_totalcalltime) {$max_mtd_totalcalltime = trim($totANSWERSsecmtd + ($totANSWERSmtd * 15));}
                    $month = date("F", strtotime($dayEND[$d - 1]));
                    $year = substr($dayEND[$d - 1], 0, 4);
                    $mtd_graph_stats[$ma][0] = "$month $year";
                    $mtd_graph_stats[$ma][1] = trim($totCALLSmtd);
                    $mtd_graph_stats[$ma][2] = trim($totANSWERSmtd);
                    $mtd_graph_stats[$ma][3] = trim($totABANDONSmtd);
                    $mtd_graph_stats[$ma][4] = trim($totABANDONSpctmtd);
                    $mtd_graph_stats[$ma][5] = trim($totABANDONSavgTIMEmtd);
                    $mtd_graph_stats[$ma][6] = trim($totANSWERSavgspeedTIMEmtd);
                    $mtd_graph_stats[$ma][7] = trim($totANSWERSavgTIMEmtd);
                    $mtd_graph_stats[$ma][8] = trim($totANSWERStalkTIMEmtd);
                    $mtd_graph_stats[$ma][9] = trim($totANSWERSwrapTIMEmtd);
                    $mtd_graph_stats[$ma][10] = trim($totANSWERStotTIMEmtd);
                    $mtd_graph_stats[$ma][17] = trim($totAGENTSmtd);
                    $ma++;
                    $obj = array();
                    $obj["TotalCallsOffered"] = $totCALLSmtd;
                    $obj["TotalCallsAnswered"] = $totANSWERSmtd;
                    $obj["TotalAgentsAnswered"] = $totAGENTSmtd;
                    $obj["TotalCallsAbandoned"] = $totABANDONSmtd;
                    $obj["TotalAbandonsPercent"] = $totABANDONSpctmtd;
                    $obj["AvgAbandonTime"] = $totABANDONSavgTIMEmtd;
                    $obj["AvgAnswerSpeed"] = $totANSWERSavgspeedTIMEmtd;
                    $obj["AvgTalkTime"] = $totANSWERSavgTIMEmtd;
                    $obj["TotalTalkTime"] = $totANSWERStalkTIMEmtd;
                    $obj["TotalWrapTime"] = $totANSWERSwrapTIMEmtd;
                    $obj["TotalCallTime"] = $totANSWERStotTIMEmtd;
                    $sal["Tabla"]["Registros"][] = $obj;
                    $totCALLSmtd = 0;
                    $totANSWERSmtd = 0;
                    $AGENTS_mtd_array = array();
                    $totANSWERSsecmtd = 0;
                    $totANSWERSspeedmtd = 0;
                    $totABANDONSmtd = 0;
                    $totABANDONSsecmtd = 0;
                    $totSTATUSESmtd = array();
                    if (date("m", strtotime($daySTART[$d])) == 1 || date("m", strtotime($daySTART[$d])) == 4 || date("m", strtotime($daySTART[$d])) == 7 || date("m", strtotime($daySTART[$d])) == 10) {
                        $totAGENTSqtd = count(array_count_values($AGENTS_qtd_array));
                        if ($totCALLSqtd > 0) {
                            $totABANDONSpctqtd = sprintf("%7.2f", (100 * $totABANDONSqtd / $totCALLSqtd));
                        } else {
                            $totABANDONSpctqtd = "    0.0";
                        }
                        if ($totABANDONSqtd > 0) {
                            $totABANDONSavgTIMEqtd = sprintf("%7s", date("i:s", mktime(0, 0, round($totABANDONSsecqtd / $totABANDONSqtd))));
                            if (round($totABANDONSsecqtd / $totABANDONSqtd) > $max_qtd_avgabandontime) {$max_qtd_avgabandontime = round($totABANDONSsecqtd / $totABANDONSqtd);}
                            $qtd_graph_stats[$qa][11] = round($totABANDONSsecqtd / $totABANDONSqtd);
                        } else {
                            $totABANDONSavgTIMEqtd = "  00:00";
                            $qtd_graph_stats[$qa][11] = 0;
                        }
                        if ($totANSWERSqtd > 0) {
                            $totANSWERSavgspeedTIMEqtd = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSspeedqtd / $totANSWERSqtd))));
                            $totANSWERSavgTIMEqtd = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSsecqtd / $totANSWERSqtd))));
                            if (round($totANSWERSspeedqtd / $totANSWERSqtd) > $max_qtd_avganswerspeed) {$max_qtd_avganswerspeed = round($totANSWERSspeedqtd / $totANSWERSqtd);}
                            $qtd_graph_stats[$qa][12] = round($totANSWERSspeedqtd / $totANSWERSqtd);
                            $qtd_graph_stats[$qa][16] = round($totANSWERSsecqtd / $totANSWERSqtd);
                        } else {
                            $totANSWERSavgspeedTIMEqtd = " 00:00";
                            $totANSWERSavgTIMEqtd = " 00:00";
                            $qtd_graph_stats[$qa][12] = 0;
                            $qtd_graph_stats[$qa][16] = 0;
                        }
                        $totANSWERStalkTIMEqtd = sprintf("%10s", floor($totANSWERSsecqtd / 3600) . date(":i:s", mktime(0, 0, $totANSWERSsecqtd)));
                        $totANSWERSwrapTIMEqtd = sprintf("%10s", floor(($totANSWERSqtd * 15) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSqtd * 15))));
                        if (($totANSWERSqtd * 15) > $max_qtd_totalwraptime) {$max_qtd_totalwraptime = ($totANSWERSqtd * 15);}
                        $qtd_graph_stats[$qa][13] = ($totANSWERSqtd * 15);
                        $qtd_graph_stats[$qa][14] = ($totANSWERSsecqtd + ($totANSWERSqtd * 15));
                        $qtd_graph_stats[$qa][15] = $totANSWERSsecqtd;
                        $totANSWERStotTIMEqtd = sprintf("%10s", floor(($totANSWERSsecqtd + ($totANSWERSqtd * 15)) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSsecqtd + ($totANSWERSqtd * 15)))));
                        $totAGENTSqtd = count(array_count_values($AGENTS_qtd_array));
                        $totAGENTSqtd = sprintf("%8s", $totAGENTSqtd);
                        $totANSWERSqtd = sprintf("%8s", $totANSWERSqtd);
                        $totABANDONSqtd = sprintf("%9s", $totABANDONSqtd);
                        $totCALLSqtd = sprintf("%7s", $totCALLSqtd);
                        if (trim($totCALLSqtd) > $max_qtd_offered) {$max_qtd_offered = trim($totCALLSqtd);}
                        if (trim($totANSWERSqtd) > $max_qtd_answered) {$max_qtd_answered = trim($totANSWERSqtd);}
                        if (trim($totABANDONSqtd) > $max_qtd_abandoned) {$max_qtd_abandoned = trim($totABANDONSqtd);}
                        if (trim($totAGENTSqtd) > $max_qtd_answers) {$max_qtd_answered = trim($totAGENTSqtd);}
                        if (trim($totABANDONSpctqtd) > $max_qtd_abandonpct) {$max_qtd_abandonpct = trim($totABANDONSpctqtd);}
                        if (round($totANSWERSsecqtd / $totANSWERSqtd) > $max_qtd_avgtalktime) {$max_qtd_avgtalktime = round($totANSWERSsecqtd / $totANSWERSqtd);}
                        if (trim($totANSWERSsecqtd) > $max_qtd_totaltalktime) {$max_qtd_totaltalktime = trim($totANSWERSsecqtd);}
                        if (trim($totANSWERSsecqtd + ($totANSWERSqtd * 15)) > $max_qtd_totalcalltime) {$max_qtd_totalcalltime = trim($totANSWERSsecqtd + ($totANSWERSqtd * 15));}
                        $month = date("m", strtotime($dayEND[$d]));
                        $year = substr($dayEND[$d], 0, 4);
                        $qtr4 = array(01, 02, 03);
                        $qtr1 = array(04, 05, 06);
                        $qtr2 = array(7, 8, 9);
                        $qtr3 = array(10, 11, 12);
                        if (in_array($month, $qtr1)) {
                            $qtr = "1st";
                        } else if (in_array($month, $qtr2)) {
                            $qtr = "2nd";
                        } else if (in_array($month, $qtr3)) {
                            $qtr = "3rd";
                        } else if (in_array($month, $qtr4)) {
                            $qtr = "4th";
                        }
                        $qtd_graph_stats[$qa][0] = "$qtr quarter, $year";
                        $qtd_graph_stats[$qa][1] = trim($totCALLSqtd);
                        $qtd_graph_stats[$qa][2] = trim($totANSWERSqtd);
                        $qtd_graph_stats[$qa][3] = trim($totABANDONSqtd);
                        $qtd_graph_stats[$qa][4] = trim($totABANDONSpctqtd);
                        $qtd_graph_stats[$qa][5] = trim($totABANDONSavgTIMEqtd);
                        $qtd_graph_stats[$qa][6] = trim($totANSWERSavgspeedTIMEqtd);
                        $qtd_graph_stats[$qa][7] = trim($totANSWERSavgTIMEqtd);
                        $qtd_graph_stats[$qa][8] = trim($totANSWERStalkTIMEqtd);
                        $qtd_graph_stats[$qa][9] = trim($totANSWERSwrapTIMEqtd);
                        $qtd_graph_stats[$qa][10] = trim($totANSWERStotTIMEqtd);
                        $qtd_graph_stats[$qa][17] = trim($totAGENTSqtd);
                        $obj = array();
                        $obj["TotalCallsOffered"] = $totCALLSqtd;
                        $obj["TotalCallsAnswered"] = $totANSWERSqtd;
                        $obj["TotalAgentsAnswered"] = $totAGENTSqtd;
                        $obj["TotalCallsAbandoned"] = $totABANDONSqtd;
                        $obj["TotalAbandonsPercent"] = $totABANDONSpctqtd;
                        $obj["AvgAbandonTime"] = $totABANDONSavgTIMEqtd;
                        $obj["AvgAnswerSpeed"] = $totANSWERSavgspeedTIMEqtd;
                        $obj["AvgTalkTime"] = $totANSWERSavgTIMEqtd;
                        $obj["TotalTalkTime"] = $totANSWERStalkTIMEqtd;
                        $obj["TotalWrapTime"] = $totANSWERSwrapTIMEqtd;
                        $obj["TotalCallTime"] = $totANSWERStotTIMEqtd;
                        $sal["Tabla"]["Registros"][] = $obj;
                        $qa++;
                        $totCALLSqtd = 0;
                        $totANSWERSqtd = 0;
                        $AGENTS_qtd_array = array();
                        $totANSWERSsecqtd = 0;
                        $totANSWERSspeedqtd = 0;
                        $totABANDONSqtd = 0;
                        $totABANDONSsecqtd = 0;
                        $totSTATUSESqtd = array();
                    }
                }
                $totAGENTSday = sprintf("%8s", count($totAGENTSdate[$d]));
                $temp_agent_array = array_keys($totAGENTSdate[$d]);
                for ($x = 0; $x < count($temp_agent_array); $x++) {
                    if ($temp_agent_array[$x] != "") {
                        array_push($AGENTS_wtd_array, $temp_agent_array[$x]);
                        array_push($AGENTS_mtd_array, $temp_agent_array[$x]);
                        array_push($AGENTS_qtd_array, $temp_agent_array[$x]);
                    }
                }
                $totCALLSwtd += $totCALLSdate[$d];
                $totANSWERSwtd += $totANSWERSdate[$d];
                $totANSWERSsecwtd += $totANSWERSsecdate[$d];
                $totANSWERSspeedwtd += $totANSWERSspeeddate[$d];
                $totABANDONSwtd += $totABANDONSdate[$d];
                $totABANDONSsecwtd += $totABANDONSsecdate[$d];
                $totCALLSmtd += $totCALLSdate[$d];
                $totANSWERSmtd += $totANSWERSdate[$d];
                $totANSWERSsecmtd += $totANSWERSsecdate[$d];
                $totANSWERSspeedmtd += $totANSWERSspeeddate[$d];
                $totABANDONSmtd += $totABANDONSdate[$d];
                $totABANDONSsecmtd += $totABANDONSsecdate[$d];
                $totCALLSqtd += $totCALLSdate[$d];
                $totANSWERSqtd += $totANSWERSdate[$d];
                $totANSWERSsecqtd += $totANSWERSsecdate[$d];
                $totANSWERSspeedqtd += $totANSWERSspeeddate[$d];
                $totABANDONSqtd += $totABANDONSdate[$d];
                $totABANDONSsecqtd += $totABANDONSsecdate[$d];
                if (trim($totCALLSdate[$d]) > $max_offered) {$max_offered = trim($totCALLSdate[$d]);}
                if (trim($totANSWERSdate[$d]) > $max_answered) {$max_answered = trim($totANSWERSdate[$d]);}
                if (trim($totAGENTSday) > $max_agents) {$max_agents = trim($totAGENTSday);}
                if (trim($totABANDONSdate[$d]) > $max_abandoned) {$max_abandoned = trim($totABANDONSdate[$d]);}
                if (trim($totABANDONSpctDATE[$d]) > $max_abandonpct) {$max_abandonpct = trim($totABANDONSpctDATE[$d]);}
                if (round($totANSWERSsecdate[$d] / $totANSWERSdate[$d]) > $max_avgtalktime) {$max_avgtalktime = round($totANSWERSsecdate[$d] / $totANSWERSdate[$d]);}
                if (trim($totANSWERSsecdate[$d]) > $max_totaltalktime) {$max_totaltalktime = trim($totANSWERSsecdate[$d]);}
                if (trim($totANSWERSsecdate[$d] + ($totANSWERSdate[$d] * 15)) > $max_totalcalltime) {$max_totalcalltime = trim($totANSWERSsecdate[$d] + ($totANSWERSdate[$d] * 15));}
                $obj = array();
                $obj["Titulo"] = $daySTART[$d] . "-" . $dayEND[$d];
                $obj["TotalCallsOffered"] = $totCALLSdate[$d];
                $obj["TotalCallsAnswered"] = $totANSWERSdate[$d];
                $obj["TotalAgentsAnswered"] = $$totAGENTSday;
                $obj["TotalCallsAbandoned"] = $totABANDONSdate[$d];
                $obj["TotalAbandonsPercent"] = $totABANDONSpctDATE[$d];
                $obj["AvgAbandonTime"] = $totABANDONSavgTIME[$d];
                $obj["AvgAnswerSpeed"] = $totANSWERSavgspeedTIME[$d];
                $obj["AvgTalkTime"] = $totANSWERSavgTIME[$d];
                $obj["TotalTalkTime"] = $totANSWERStalkTIME[$d];
                $obj["TotalWrapTime"] = $totANSWERSwrapTIME[$d];
                $obj["TotalCallTime"] = $totANSWERStotTIME[$d];
                $sal["Tabla"]["Registros"][] = $obj;
                $d++;
            }
            $i = 0;
            $totCALLSsecDATE = array();
            while ($i < $records_to_grab) {
                $totCALLS++;
                $totCALLSsec = ($totCALLSsec + $ls[$i]);
                if (!array_key_exists($j, $totCALLSsecDATE)) {
                    $totCALLSsecDATE[$j] = 0;
                }

                $totCALLSsecDATE[$j] = ($totCALLSsecDATE[$j] + $ls[$i]);
                if (!array_key_exists($j, $totCALLSdate)) {
                    $totCALLSdate[$j] = 0;
                }

                $totCALLSdate[$j]++;

                if ($ag[$i] != "VDCL" && $ag[$i] != "") {
                    $totAGENTSdate[$j][$ag[$i]]++;
                }
                $totSTATUSES[$st[$i]]++;
                $totSTATUSESdate[$j][$st[$i]]++;
                if ($totCALLSmax < $ls[$i]) {
                    $totCALLSmax = $ls[$i];
                }
                if ($qrtCALLSmax[$j] < $ls[$i]) {
                    $qrtCALLSmax[$j] = $ls[$i];
                }
                if (preg_match('/ABANDON|NOAGENT|QUEUETIMEOUT|AFTERHOURS|MAXCALLS/', $tr[$i])) {
                    $totABANDONSdate[$j]++;
                    $totABANDONSsecdate[$j] += $ls[$i];
                    $FtotABANDONS++;
                    $FtotABANDONSsec += $ls[$i];
                } else {
                    $totANSWERSdate[$j]++;
                    if (($ls[$i] - $qs[$i] - 15) > 0) {
                        $totANSWERSsecdate[$j] += ($ls[$i] - $qs[$i] - 15);
                        $FtotANSWERSsec += ($ls[$i] - $qs[$i] - 15);
                    }
                    $totANSWERSspeeddate[$j] += $qs[$i];
                    $FtotANSWERS++;
                    $FtotANSWERSspeeddate += $qs[$i];
                }
                if (preg_match('/DROP/', $st[$i])) {
                    $totDROPS++;
                    $totDROPSsec = ($totDROPSsec + $ls[$i]);
                    $totDROPSsecDATE[$j] = ($totDROPSsecDATE[$j] + $ls[$i]);
                    $totDROPSdate[$j]++;
                }
                if ($qs[$i] > 0) {
                    $totQUEUE++;
                    $totQUEUEsec = ($totQUEUEsec + $qs[$i]);
                    $totQUEUEsecDATE[$j] = ($totQUEUEsecDATE[$j] + $qs[$i]);
                    $totQUEUEdate[$j]++;
                }
                $i++;
            }
            if ($totDROPS > 0) {$totDROPSpct = (($totDROPS / $totCALLS) * 100);} else { $totDROPSpct = 0;}
            $totDROPSpct = round($totDROPSpct, 2);
            if ($totQUEUE > 0) {$totQUEUEpct = (($totQUEUE / $totCALLS) * 100);} else { $totQUEUEpct = 0;}
            $totQUEUEpct = round($totQUEUEpct, 2);
            if ($totDROPSsec > 0) {$totDROPSavg = ($totDROPSsec / $totDROPS);} else { $totDROPSavg = 0;}
            if ($totQUEUEsec > 0) {$totQUEUEavg = ($totQUEUEsec / $totQUEUE);} else { $totQUEUEavg = 0;}
            if ($totQUEUEsec > 0) {$totQUEUEtot = ($totQUEUEsec / $totCALLS);} else { $totQUEUEtot = 0;}
            if ($totCALLSsec > 0) {
                $totCALLSavg = ($totCALLSsec / $totCALLS);
                $totTIME_M = ($totCALLSsec / 60);
                $totTIME_M_int = round($totTIME_M, 2);
                $totTIME_M_int = intval("$totTIME_M");
                $totTIME_S = ($totTIME_M - $totTIME_M_int);
                $totTIME_S = ($totTIME_S * 60);
                $totTIME_S = round($totTIME_S, 0);
                if ($totTIME_S < 10) {$totTIME_S = "0$totTIME_S";}
                $totTIME_MS = "$totTIME_M_int:$totTIME_S";
                $totTIME_MS = sprintf("%9s", $totTIME_MS);
            } else {
                $totCALLSavg = 0;
                $totTIME_MS = '         ';
            }
            $FtotCALLSavg = sprintf("%6.0f", $totCALLSavg);
            $FtotDROPSavg = sprintf("%7.2f", $totDROPSavg);
            $FtotQUEUEavg = sprintf("%7.2f", $totQUEUEavg);
            $FtotQUEUEtot = sprintf("%7.2f", $totQUEUEtot);
            $FtotDROPSpct = sprintf("%6.2f", $totDROPSpct);
            $FtotQUEUEpct = sprintf("%6.2f", $totQUEUEpct);
            $FtotDROPS = sprintf("%6s", $totDROPS);
            $FtotQUEUE = sprintf("%6s", $totQUEUE);
            $FtotCALLS = sprintf("%7s", $totCALLS);
            if ($FtotCALLS > 0) {
                $FtotABANDONSpct = sprintf("%7.2f", (100 * $FtotABANDONS / $FtotCALLS));
            } else {
                $FtotABANDONSpct = "    0.0";
            }
            if ($FtotABANDONS > 0) {
                $FtotABANDONSavgTIME = sprintf("%7s", date("i:s", mktime(0, 0, round($FtotABANDONSsec / $FtotABANDONS))));
            } else {
                $FtotABANDONSavgTIME = sprintf("%7s", "00:00");
            }
            if ($FtotANSWERS > 0) {
                $FtotANSWERSavgspeedTIME = sprintf("%6s", date("i:s", mktime(0, 0, round($FtotANSWERSspeed / $FtotANSWERS))));
                $FtotANSWERSavgTIME = sprintf("%6s", date("i:s", mktime(0, 0, round($FtotANSWERSsec / $FtotANSWERS))));
            } else {
                $FtotANSWERSavgspeedTIME = sprintf("%6s", "00:00");
                $FtotANSWERSavgTIME = sprintf("%6s", "00:00");
            }
            $FtotANSWERStalkTIME = sprintf("%10s", floor($FtotANSWERSsec / 3600) . date(":i:s", mktime(0, 0, $FtotANSWERSsec)));
            $FtotANSWERSwrapTIME = sprintf("%10s", floor(($FtotANSWERS * 15) / 3600) . date(":i:s", mktime(0, 0, ($FtotANSWERS * 15))));
            $FtotANSWERStotTIME = sprintf("%10s", floor(($FtotANSWERSsec + ($FtotANSWERS * 15)) / 3600) . date(":i:s", mktime(0, 0, ($FtotANSWERSsec + ($FtotANSWERS * 15)))));
            $FtotANSWERS = sprintf("%8s", $FtotANSWERS);
            $FtotABANDONS = sprintf("%9s", $FtotABANDONS);
            if (date("w", strtotime($daySTART[$d])) > 0) {
                $totAGENTSwtd = count(array_count_values($AGENTS_wtd_array));
                if ($totCALLSwtd > 0) {
                    $totABANDONSpctwtd = sprintf("%7.2f", (100 * $totABANDONSwtd / $totCALLSwtd));
                } else {
                    $totABANDONSpctwtd = "    0.0";
                }
                if ($totABANDONSwtd > 0) {
                    $totABANDONSavgTIMEwtd = sprintf("%7s", date("i:s", mktime(0, 0, round($totABANDONSsecwtd / $totABANDONSwtd))));
                    if (round($totABANDONSsecwtd / $totABANDONSwtd) > $max_wtd_avgabandontime) {$max_wtd_avgabandontime = round($totABANDONSsecwtd / $totABANDONSwtd);}
                    $wtd_graph_stats[$wa][11] = round($totABANDONSsecwtd / $totABANDONSwtd);
                } else {
                    $totABANDONSavgTIMEwtd = "  00:00";
                    $wtd_graph_stats[$wa][11] = 0;
                }
                if ($totANSWERSwtd > 0) {
                    $totANSWERSavgspeedTIMEwtd = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSspeedwtd / $totANSWERSwtd))));
                    $totANSWERSavgTIMEwtd = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSsecwtd / $totANSWERSwtd))));
                    if (round($totANSWERSspeedwtd / $totANSWERSwtd) > $max_wtd_avganswerspeed) {$max_wtd_avganswerspeed = round($totANSWERSspeedwtd / $totANSWERSwtd);}
                    $wtd_graph_stats[$wa][12] = round($totANSWERSspeedwtd / $totANSWERSwtd);
                    $wtd_graph_stats[$wa][16] = round($totANSWERSsecwtd / $totANSWERSwtd);
                } else {
                    $totANSWERSavgspeedTIMEwtd = " 00:00";
                    $totANSWERSavgTIMEwtd = " 00:00";
                    $wtd_graph_stats[$wa][12] = 0;
                    $wtd_graph_stats[$wa][16] = 0;
                }
                $totANSWERStalkTIMEwtd = sprintf("%10s", floor($totANSWERSsecwtd / 3600) . date(":i:s", mktime(0, 0, $totANSWERSsecwtd)));
                $totANSWERSwrapTIMEwtd = sprintf("%10s", floor(($totANSWERSwtd * 15) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSwtd * 15))));
                if (($totANSWERSwtd * 15) > $max_wtd_totalwraptime) {$max_wtd_totalwraptime = ($totANSWERSwtd * 15);}
                $wtd_graph_stats[$wa][13] = ($totANSWERSwtd * 15);
                $wtd_graph_stats[$wa][14] = ($totANSWERSsecwtd + ($totANSWERSwtd * 15));
                $wtd_graph_stats[$wa][15] = $totANSWERSsecwtd;
                $totANSWERStotTIMEwtd = sprintf("%10s", floor(($totANSWERSsecwtd + ($totANSWERSwtd * 15)) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSsecwtd + ($totANSWERSwtd * 15)))));
                $totAGENTSwtd = count(array_count_values($AGENTS_wtd_array));
                $totAGENTSwtd = sprintf("%8s", $totAGENTSwtd);
                $totANSWERSwtd = sprintf("%8s", $totANSWERSwtd);
                $totABANDONSwtd = sprintf("%9s", $totABANDONSwtd);
                $totCALLSwtd = sprintf("%7s", $totCALLSwtd);
                if (trim($totCALLSwtd) > $max_wtd_offered) {$max_wtd_offered = trim($totCALLSwtd);}
                if (trim($totANSWERSwtd) > $max_wtd_answered) {$max_wtd_answered = trim($totANSWERSwtd);}
                if (trim($totAGENTSwtd) > $max_wtd_agents) {$max_wtd_agents = trim($totAGENTSwtd);}
                if (trim($totABANDONSwtd) > $max_wtd_abandoned) {$max_wtd_abandoned = trim($totABANDONSwtd);}
                if (trim($totABANDONSpctwtd) > $max_wtd_abandonpct) {$max_wtd_abandonpct = trim($totABANDONSpctwtd);}
                if (trim($totANSWERSavgTIMEwtd) > $max_wtd_avgtalktime) {$max_wtd_avgtalktime = trim($totANSWERSavgTIMEwtd);}
                if (trim($totANSWERSsecwtd) > $max_wtd_totaltalktime) {$max_wtd_totaltalktime = trim($totANSWERSsecwtd);}
                if (trim($totANSWERSsecwtd + ($totANSWERSwtd * 15)) > $max_wtd_totalcalltime) {$max_wtd_totalcalltime = trim($totANSWERSsecwtd + ($totANSWERSwtd * 15));}
                $obj = array();
                $obj["Titulo"] = "WTD";
                $obj["TotalCallsOffered"] = $totCALLSwtd;
                $obj["TotalCallsAnswered"] = $totANSWERSwtd;
                $obj["TotalAgentsAnswered"] = $totAGENTSwtd;
                $obj["TotalCallsAbandoned"] = $totABANDONSwtd;
                $obj["TotalAbandonsPercent"] = $totABANDONSpctwtd;
                $obj["AvgAbandonTime"] = $totABANDONSavgTIMEwtd;
                $obj["AvgAnswerSpeed"] = $totANSWERSavgspeedTIMEwtd;
                $obj["AvgTalkTime"] = $totANSWERSavgTIMEwtd;
                $obj["TotalTalkTime"] = $totANSWERStalkTIMEwtd;
                $obj["TotalWrapTime"] = $totANSWERSwrapTIMEwtd;
                $obj["TotalCallTime"] = $totANSWERStotTIMEwtd;
                $sal["Tabla"]["Registros"][] = $obj;
                $totCALLSwtd = 0;
                $totANSWERSwtd = 0;
                $AGENTS_wtd_array = array();
                $totANSWERSsecwtd = 0;
                $totANSWERSspeedwtd = 0;
                $totABANDONSwtd = 0;
                $totABANDONSsecwtd = 0;
            }
            if (date("d", strtotime($daySTART[$d])) != 1) {
                $totAGENTSmtd = count(array_count_values($AGENTS_mtd_array));
                if ($totCALLSmtd > 0) {
                    $totABANDONSpctmtd = sprintf("%7.2f", (100 * $totABANDONSmtd / $totCALLSmtd));
                } else {
                    $totABANDONSpctmtd = "    0.0";
                }
                if ($totABANDONSmtd > 0) {
                    $totABANDONSavgTIMEmtd = sprintf("%7s", date("i:s", mktime(0, 0, round($totABANDONSsecmtd / $totABANDONSmtd))));
                    if (round($totABANDONSsecmtd / $totABANDONSmtd) > $max_mtd_avgabandontime) {$max_mtd_avgabandontime = round($totABANDONSsecmtd / $totABANDONSmtd);}
                    $mtd_graph_stats[$ma][11] = round($totABANDONSsecmtd / $totABANDONSmtd);
                } else {
                    $totABANDONSavgTIMEmtd = "  00:00";
                    $mtd_graph_stats[$ma][11] = 0;
                }
                if ($totANSWERSmtd > 0) {
                    $totANSWERSavgspeedTIMEmtd = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSspeedmtd / $totANSWERSmtd))));
                    $totANSWERSavgTIMEmtd = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSsecmtd / $totANSWERSmtd))));
                    if (round($totANSWERSspeedmtd / $totANSWERSmtd) > $max_mtd_avganswerspeed) {$max_mtd_avganswerspeed = round($totANSWERSspeedmtd / $totANSWERSmtd);}
                    $mtd_graph_stats[$ma][12] = round($totANSWERSspeedmtd / $totANSWERSmtd);
                    $mtd_graph_stats[$ma][16] = round($totANSWERSsecmtd / $totANSWERSmtd);
                } else {
                    $totANSWERSavgspeedTIMEmtd = " 00:00";
                    $totANSWERSavgTIMEmtd = " 00:00";
                    $mtd_graph_stats[$ma][12] = 0;
                    $mtd_graph_stats[$ma][16] = 0;
                }
                $totANSWERStalkTIMEmtd = sprintf("%10s", floor($totANSWERSsecmtd / 3600) . date(":i:s", mktime(0, 0, $totANSWERSsecmtd)));
                $totANSWERSwrapTIMEmtd = sprintf("%10s", floor(($totANSWERSmtd * 15) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSmtd * 15))));
                if (($totANSWERSmtd * 15) > $max_mtd_totalwraptime) {$max_mtd_totalwraptime = ($totANSWERSmtd * 15);}
                $mtd_graph_stats[$ma][13] = ($totANSWERSmtd * 15);
                $mtd_graph_stats[$ma][14] = ($totANSWERSsecmtd + ($totANSWERSmtd * 15));
                $mtd_graph_stats[$ma][15] = $totANSWERSsecmtd;
                $totANSWERStotTIMEmtd = sprintf("%10s", floor(($totANSWERSsecmtd + ($totANSWERSmtd * 15)) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSsecmtd + ($totANSWERSmtd * 15)))));
                $totAGENTSmtd = count(array_count_values($AGENTS_mtd_array));
                $totAGENTSmtd = sprintf("%8s", $totAGENTSmtd);
                $totANSWERSmtd = sprintf("%8s", $totANSWERSmtd);
                $totABANDONSmtd = sprintf("%9s", $totABANDONSmtd);
                $totCALLSmtd = sprintf("%7s", $totCALLSmtd);
                if (trim($totCALLSmtd) > $max_mtd_offered) {$max_mtd_offered = trim($totCALLSmtd);}
                if (trim($totANSWERSmtd) > $max_mtd_answered) {$max_mtd_answered = trim($totANSWERSmtd);}
                if (trim($totAGENTSmtd) > $max_mtd_agents) {$max_mtd_agents = trim($totAGENTSmtd);}
                if (trim($totABANDONSmtd) > $max_mtd_abandoned) {$max_mtd_abandoned = trim($totABANDONSmtd);}
                if (trim($totABANDONSpctmtd) > $max_mtd_abandonpct) {$max_mtd_abandonpct = trim($totABANDONSpctmtd);}
                if (round($totANSWERSsecmtd / $totANSWERSmtd) > $max_mtd_avgtalktime) {$max_mtd_avgtalktime = round($totANSWERSsecmtd / $totANSWERSmtd);}
                if (trim($totANSWERSsecmtd) > $max_mtd_totaltalktime) {$max_mtd_totaltalktime = trim($totANSWERSsecmtd);}
                if (trim($totANSWERSsecmtd + ($totANSWERSmtd * 15)) > $max_mtd_totalcalltime) {$max_mtd_totalcalltime = trim($totANSWERSsecmtd + ($totANSWERSmtd * 15));}
                $obj = array();
                $obj["Titulo"] = "MTD";
                $obj["TotalCallsOffered"] = $totCALLSmtd;
                $obj["TotalCallsAnswered"] = $totANSWERSmtd;
                $obj["TotalAgentsAnswered"] = $totAGENTSmtd;
                $obj["TotalCallsAbandoned"] = $totABANDONSmtd;
                $obj["TotalAbandonsPercent"] = $totABANDONSpctmtd;
                $obj["AvgAbandonTime"] = $totABANDONSavgTIMEmtd;
                $obj["AvgAnswerSpeed"] = $totANSWERSavgspeedTIMEmtd;
                $obj["AvgTalkTime"] = $totANSWERSavgTIMEmtd;
                $obj["TotalTalkTime"] = $totANSWERStalkTIMEmtd;
                $obj["TotalWrapTime"] = $totANSWERSwrapTIMEmtd;
                $obj["TotalCallTime"] = $totANSWERStotTIMEmtd;
                $sal["Tabla"]["Registros"][] = $obj;
                $totCALLSmtd = 0;
                $totANSWERSmtd = 0;
                $AGENTS_mtd_array = array();
                $totANSWERSsecmtd = 0;
                $totANSWERSspeedmtd = 0;
                $totABANDONSmtd = 0;
                $totABANDONSsecmtd = 0;
                $totAGENTSqtd = count(array_count_values($AGENTS_qtd_array));
                if ($totCALLSqtd > 0) {
                    $totABANDONSpctqtd = sprintf("%7.2f", (100 * $totABANDONSqtd / $totCALLSqtd));
                } else {
                    $totABANDONSpctqtd = "    0.0";
                }
                if ($totABANDONSqtd > 0) {
                    $totABANDONSavgTIMEqtd = sprintf("%7s", date("i:s", mktime(0, 0, round($totABANDONSsecqtd / $totABANDONSqtd))));
                    if (round($totABANDONSsecqtd / $totABANDONSqtd) > $max_qtd_avgabandontime) {$max_qtd_avgabandontime = round($totABANDONSsecqtd / $totABANDONSqtd);}
                    $qtd_graph_stats[$qa][11] = round($totABANDONSsecqtd / $totABANDONSqtd);
                } else {
                    $totABANDONSavgTIMEqtd = "  00:00";
                    $qtd_graph_stats[$qa][11] = 0;
                }
                if ($totANSWERSqtd > 0) {
                    $totANSWERSavgspeedTIMEqtd = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSspeedqtd / $totANSWERSqtd))));
                    $totANSWERSavgTIMEqtd = sprintf("%6s", date("i:s", mktime(0, 0, round($totANSWERSsecqtd / $totANSWERSqtd))));
                    if (round($totANSWERSspeedqtd / $totANSWERSqtd) > $max_qtd_avganswerspeed) {$max_qtd_avganswerspeed = round($totANSWERSspeedqtd / $totANSWERSqtd);}
                    $qtd_graph_stats[$qa][12] = round($totANSWERSspeedqtd / $totANSWERSqtd);
                    $qtd_graph_stats[$qa][16] = round($totANSWERSsecqtd / $totANSWERSqtd);
                } else {
                    $totANSWERSavgspeedTIMEqtd = " 00:00";
                    $totANSWERSavgTIMEqtd = " 00:00";
                    $qtd_graph_stats[$qa][12] = 0;
                    $qtd_graph_stats[$qa][16] = 0;
                }
                $totANSWERStalkTIMEqtd = sprintf("%10s", floor($totANSWERSsecqtd / 3600) . date(":i:s", mktime(0, 0, $totANSWERSsecqtd)));
                $totANSWERSwrapTIMEqtd = sprintf("%10s", floor(($totANSWERSqtd * 15) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSqtd * 15))));
                if (($totANSWERSqtd * 15) > $max_qtd_totalwraptime) {$max_qtd_totalwraptime = ($totANSWERSqtd * 15);}
                $qtd_graph_stats[$qa][13] = ($totANSWERSqtd * 15);
                $qtd_graph_stats[$qa][14] = ($totANSWERSsecqtd + ($totANSWERSqtd * 15));
                $qtd_graph_stats[$qa][15] = $totANSWERSsecqtd;
                $totANSWERStotTIMEqtd = sprintf("%10s", floor(($totANSWERSsecqtd + ($totANSWERSqtd * 15)) / 3600) . date(":i:s", mktime(0, 0, ($totANSWERSsecqtd + ($totANSWERSqtd * 15)))));
                $totAGENTSqtd = count(array_count_values($AGENTS_qtd_array));
                $totAGENTSqtd = sprintf("%8s", $totAGENTSqtd);
                $totANSWERSqtd = sprintf("%8s", $totANSWERSqtd);
                $totABANDONSqtd = sprintf("%9s", $totABANDONSqtd);
                $totCALLSqtd = sprintf("%7s", $totCALLSqtd);
                if (trim($totCALLSqtd) > $max_qtd_offered) {$max_qtd_offered = trim($totCALLSqtd);}
                if (trim($totANSWERSqtd) > $max_qtd_answered) {$max_qtd_answered = trim($totANSWERSqtd);}
                if (trim($totAGENTSqtd) > $max_qtd_agents) {$max_qtd_agents = trim($totAGENTSqtd);}
                if (trim($totABANDONSqtd) > $max_qtd_abandoned) {$max_qtd_abandoned = trim($totABANDONSqtd);}
                if (trim($totABANDONSpctqtd) > $max_qtd_abandonpct) {$max_qtd_abandonpct = trim($totABANDONSpctqtd);}
                if (round($totANSWERSsecqtd / $totANSWERSqtd) > $max_qtd_avgtalktime) {$max_qtd_avgtalktime = round($totANSWERSsecqtd / $totANSWERSqtd);}
                if (trim($totANSWERSsecqtd) > $max_qtd_totaltalktime) {$max_qtd_totaltalktime = trim($totANSWERSsecqtd);}
                if (trim($totANSWERSsecqtd + ($totANSWERSqtd * 15)) > $max_qtd_totalcalltime) {$max_qtd_totalcalltime = trim($totANSWERSsecqtd + ($totANSWERSqtd * 15));}
                $obj = array();
                $obj["Titulo"] = "QTD";
                $obj["TotalCallsOffered"] = $totCALLSqtd;
                $obj["TotalCallsAnswered"] = $totANSWERSqtd;
                $obj["TotalAgentsAnswered"] = $totAGENTSqtd;
                $obj["TotalCallsAbandoned"] = $totABANDONSqtd;
                $obj["TotalAbandonsPercent"] = $totABANDONSpctqtd;
                $obj["AvgAbandonTime"] = $totABANDONSavgTIMEqtd;
                $obj["AvgAnswerSpeed"] = $totANSWERSavgspeedTIMEqtd;
                $obj["AvgTalkTime"] = $totANSWERSavgTIMEqtd;
                $obj["TotalTalkTime"] = $totANSWERStalkTIMEqtd;
                $obj["TotalWrapTime"] = $totANSWERSwrapTIMEqtd;
                $obj["TotalCallTime"] = $totANSWERStotTIMEqtd;
                $sal["Tabla"]["Registros"][] = $obj;
                $totCALLSqtd = 0;
                $totANSWERSqtd = 0;
                $AGENTS_qtd_array = array();
                $totANSWERSsecqtd = 0;
                $totANSWERSspeedqtd = 0;
                $totABANDONSqtd = 0;
                $totABANDONSsecqtd = 0;
            }
            $obj = array();
            $obj["Titulo"] = "TOTALES";
            $obj["TotalCallsOffered"] = $FtotCALLS;
            $obj["TotalCallsAnswered"] = $FtotANSWERS;
            $obj["TotalAgentsAnswered"] = $FtotAGENTS;
            $obj["TotalCallsAbandoned"] = $FtotABANDONS;
            $obj["TotalAbandonsPercent"] = $FtotABANDONSpct;
            $obj["AvgAbandonTime"] = $FtotABANDONSavgTIME;
            $obj["AvgAnswerSpeed"] = $FtotANSWERSavgspeedTIME;
            $obj["AvgTalkTime"] = $FtotANSWERSavgTIME;
            $obj["TotalTalkTime"] = $FtotANSWERStalkTIME;
            $obj["TotalWrapTime"] = $FtotANSWERSwrapTIME;
            $obj["TotalCallTime"] = $FtotANSWERStotTIME;
            $sal["Tabla"]["Registros"][] = $obj;
            sv_mysqli_close($con);
            if (!isset($_GET["csv"])) {
                $sal["Estado"] = "ok";
                $this->output->set_output(json_encode($sal));
            } else {
                header("Content-type: text/csv");
                header("Content-Disposition: attachment; filename=ReporteEntranteDiario(" . $fechaInicial . "-" . $fechaFinal . ").csv");
                header("Pragma: no-cache");
                header("Expires: 0");
                $out = fopen('php://output', 'w');
                $test = array();
                $this->trata($out, $sal, $test);
                fclose($out);
            }
        } else {
            $sal = array();
            $sal["Estado"] = "error";
            $sal["Descripcion"] = $this->lang->line("desc_SinPermisosAccion");
            $this->output->set_output(json_encode($sal));
        }
    }
    public function index()
    {
        $this->load->view('reporteEntranteDiario/head');
        $this->load->view('reporteEntranteDiario/vw_reporte_entrante_diario');
        $this->load->view('reporteEntranteDiario/foot');
    }
}
