<?php

require_once "BASEPATH.php";

class ReporteListaFecha 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 listCampaignStats()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('reportelistafecha', "spanish");
                break;
            case "en":
                $this->lang->load('reportelistafecha', "english");
                break;
            default:
                $this->lang->load('reportelistafecha', "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();
            $sal = array();
            $campana = limpia($_GET["campana"]);
            if (!isset($_GET["campana"]) || $campana == -1) {
                $campana = "campaign_id";
            } else {
                $campana = "'$campana'";
            }
            $and = "";
            if ($_SESSION["Cliente"] != -1) {
                $and = "AND campaign_id IN (SELECT campaign_id FROM vicidial_campaigns WHERE user_group = '" . $_SESSION["Cliente"] . "')";
            }
            $group_SQLand = "and campaign_id = $campana $and";
            $query = "SELECT list_id from vicidial_lists where active IN('Y','N') $group_SQLand";
            $result = sv_mysqli_query($con, $query);
            $lists_id_str = "";
            while ($lrow = sv_mysqli_fetch_array($result)) {
                $lists_id_str .= "'$lrow[0]',";
            }
            $lists_id_str = substr($lists_id_str, 0, -1);
            $whC = "";
            if ($_SESSION["Cliente"] != -1) {
                $whC = "WHERE vsc_id REGEXP '^" . $_SESSION["Cliente"] . "+[0-9]+$'";
            }
            $query = "select vsc_id,vsc_name from vicidial_status_categories $whC";
            $result = sv_mysqli_query($con, $query);
            $statcats_to_print = sv_mysqli_num_rows($result);
            $i = 0;
            $vsc_count = array();
            while ($i < $statcats_to_print) {
                $row = sv_mysqli_fetch_array($result);
                $vsc_id[$i] = $row[0];
                $vsc_name[$i] = $row[1];
                $category_statuses = "";
                $status_stmt = "select distinct status from vicidial_statuses where category='$row[0]' UNION select distinct status from vicidial_campaign_statuses where category='$row[0]' $group_SQLand";
                $status_rslt = sv_mysqli_query($con, $status_stmt);
                while ($status_row = sv_mysqli_fetch_array($status_rslt)) {
                    $category_statuses .= "'" . $status_row[0] . "',";
                }
                $category_statuses = substr($category_statuses, 0, -1);
                $substre = "status in ($category_statuses)";
                $subtre2 = "list_id IN($lists_id_str)";
                if (strlen($category_statuses) == 0) {
                    $substre = "";
                }
                if (strlen($lists_id_str) == 0) {
                    $subtre2 = "";
                }
                $ad = "";
                $whe = "";
                if ($substre != "" and $subtre2 != "") {
                    $ad = "and";
                }
                if ($substre != "" || $subtre2 != "") {
                    $whe = "where";
                }

                $category_stmt = "select count(*)  from vicidial_list $whe $substre $ad $subtre2";
                $category_rslt = sv_mysqli_query($con, $category_stmt);
                $category_row = sv_mysqli_fetch_array($category_rslt);
                $vsc_count[$i] = $category_row[0];
                $i++;
            }
            $human_answered_statuses = '';
            $sale_statuses = '';
            $dnc_statuses = '';
            $customer_contact_statuses = '';
            $not_interested_statuses = '';
            $unworkable_statuses = '';
            $query = "select status,human_answered,sale,dnc,customer_contact,not_interested,unworkable,scheduled_callback,completed,status_name from vicidial_statuses";
            $result = sv_mysqli_query($con, $query);
            $statha_to_print = sv_mysqli_num_rows($result);
            $i = 0;
            $scheduled_callback_statuses = "";
            $completed_statuses = "";
            $statname_list = array();
            while ($i < $statha_to_print) {
                $row = sv_mysqli_fetch_array($result);
                $temp_status = $row[0];
                $statname_list["$temp_status"] = "$row[9]";
                if ($row[1] == 'Y') {
                    $human_answered_statuses .= "'$temp_status',";
                }
                if ($row[2] == 'Y') {
                    $sale_statuses .= "'$temp_status',";
                }
                if ($row[3] == 'Y') {
                    $dnc_statuses .= "'$temp_status',";
                }
                if ($row[4] == 'Y') {
                    $customer_contact_statuses .= "'$temp_status',";
                }
                if ($row[5] == 'Y') {
                    $not_interested_statuses .= "'$temp_status',";
                }
                if ($row[6] == 'Y') {
                    $unworkable_statuses .= "'$temp_status',";
                }
                if ($row[7] == 'Y') {
                    $scheduled_callback_statuses .= "'$temp_status',";
                }
                if ($row[8] == 'Y') {
                    $completed_statuses .= "'$temp_status',";
                }
                $i++;
            }
            $query = "select status,human_answered,sale,dnc,customer_contact,not_interested,unworkable,scheduled_callback,completed,status_name from vicidial_campaign_statuses where selectable IN('Y','N') $group_SQLand";
            $rslt = sv_mysqli_query($con, $query);
            $statha_to_print = sv_mysqli_num_rows($rslt);
            $i = 0;
            while ($i < $statha_to_print) {
                $row = sv_mysqli_fetch_array($rslt);
                $temp_status = $row[0];
                $statname_list["$temp_status"] = "$row[9]";
                if (($row[1] == 'Y') and (!preg_match("/'$temp_status'/", $human_answered_statuses))) {
                    $human_answered_statuses .= "'$temp_status',";
                }
                if ($row[2] == 'Y') {
                    $sale_statuses .= "'$temp_status',";
                }
                if ($row[3] == 'Y') {
                    $dnc_statuses .= "'$temp_status',";
                }
                if ($row[4] == 'Y') {
                    $customer_contact_statuses .= "'$temp_status',";
                }
                if ($row[5] == 'Y') {
                    $not_interested_statuses .= "'$temp_status',";
                }
                if ($row[6] == 'Y') {
                    $unworkable_statuses .= "'$temp_status',";
                }
                if ($row[7] == 'Y') {
                    $scheduled_callback_statuses .= "'$temp_status',";
                }
                if ($row[8] == 'Y') {
                    $completed_statuses .= "'$temp_status',";
                }
                $i++;
            }
            if (strlen($human_answered_statuses) > 2) {
                $human_answered_statuses = substr("$human_answered_statuses", 0, -1);
            } else {
                $human_answered_statuses = "''";
            }
            if (strlen($sale_statuses) > 2) {
                $sale_statuses = substr("$sale_statuses", 0, -1);
            } else {
                $sale_statuses = "''";
            }
            if (strlen($dnc_statuses) > 2) {
                $dnc_statuses = substr("$dnc_statuses", 0, -1);
            } else {
                $dnc_statuses = "''";
            }
            if (strlen($customer_contact_statuses) > 2) {
                $customer_contact_statuses = substr("$customer_contact_statuses", 0, -1);
            } else {
                $customer_contact_statuses = "''";
            }
            if (strlen($not_interested_statuses) > 2) {
                $not_interested_statuses = substr("$not_interested_statuses", 0, -1);
            } else {
                $not_interested_statuses = "''";
            }
            if (strlen($unworkable_statuses) > 2) {
                $unworkable_statuses = substr("$unworkable_statuses", 0, -1);
            } else {
                $unworkable_statuses = "''";
            }
            if (strlen($scheduled_callback_statuses) > 2) {
                $scheduled_callback_statuses = substr("$scheduled_callback_statuses", 0, -1);
            } else {
                $scheduled_callback_statuses = "''";
            }
            if (strlen($completed_statuses) > 2) {
                $completed_statuses = substr("$completed_statuses", 0, -1);
            } else {
                $completed_statuses = "''";
            }
            $lists_id_str = "";
            $list_stmt = "SELECT list_id from vicidial_lists where active IN('Y','N') $group_SQLand";
            $list_rslt = sv_mysqli_query($con, $list_stmt);
            while ($lrow = sv_mysqli_fetch_array($list_rslt)) {
                $lists_id_str .= "'$lrow[0]',";
            }
            $lists_id_str = substr($lists_id_str, 0, -1);
            $condition = "where list_id IN($lists_id_str)";
            if (strlen($lists_id_str) == 0) {
                $condition = "where list_id !=list_id";
            }
            $stmt = "select count(*),list_id from vicidial_list $condition group by list_id;";
            $rslt = sv_mysqli_query($con, $stmt);
            $listids_to_print = sv_mysqli_num_rows($rslt);
            $i = 0;
            $LISTIDlists = array();
            $LISTIDlist_names = array();
            $list_id_SQL = "";
            $max_calls = 0;
            while ($i < $listids_to_print) {
                $row = sv_mysqli_fetch_array($rslt);
                $LISTIDcalls[$i] = $row[0];
                $LISTIDlists[$i] = $row[1];
                $list_id_SQL .= "'$row[1]',";
                if ($row[0] > $max_calls) {
                    $max_calls = $row[0];
                }
                $i++;
            }
            if (strlen($list_id_SQL) > 2) {
                $list_id_SQL = substr("$list_id_SQL", 0, -1);
            } else {
                $list_id_SQL = "''";
            }
            $i = 0;
            $LISTIDlist_active = array();
            $TOTALleads = 0;
            $sal["resumenIdLista"] = array();
            $sal["resumenIdLista"]["Registros"] = array();
            while ($i < $listids_to_print) {
                $stmt = "select list_name,active from vicidial_lists where list_id='$LISTIDlists[$i]'";
                $rslt = sv_mysqli_query($con, $stmt);
                $list_name_to_print = sv_mysqli_num_rows($rslt);
                $LISTIDlist_active[$i] = "";
                if ($list_name_to_print > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $LISTIDlist_names[$i] = $row[0];
                    if ($row[1] == 'Y') {
                        $LISTIDlist_active[$i] = 'ACTIVE  ';
                    } else {
                        $LISTIDlist_active[$i] = 'INACTIVE';
                    }

                }
                $TOTALleads = ($TOTALleads + $LISTIDcalls[$i]);
                $LISTIDcount = sprintf("%10s", $LISTIDcalls[$i]);
                /*while (strlen($LISTIDcount) > 10) {
                $LISTIDcount = substr("$LISTIDcount", 0, -1);
                }*/
                if (array_key_exists($i, $LISTIDlist_names)) {
                    $LISTIDname = sprintf("%-40s", "$LISTIDlists[$i] - $LISTIDlist_names[$i]");
                    /*while (strlen($LISTIDname) > 40) {
                $LISTIDname = substr("$LISTIDname", 0, -1);
                }*/
                } else {
                    $LISTIDname = sprintf("%-40s", "$LISTIDlists[$i]");
                    /*while (strlen($LISTIDname) > 40) {
                $LISTIDname = substr("$LISTIDname", 0, -1);
                }*/
                }
                $sal["resumenIdLista"]["Registros"][$i]["lista"] = $LISTIDname;
                $sal["resumenIdLista"]["Registros"][$i]["registro"] = $LISTIDcount;
                $sal["resumenIdLista"]["Registros"][$i]["activo"] = $LISTIDlist_active[$i];
                $i++;
            }
            $TOTALleads = sprintf("%10s", $TOTALleads);
            $sal["resumenIdLista"]["total"] = $TOTALleads;
            $HA_count = 0;
            $HA_percent = 0;
            $SALE_count = 0;
            $SALE_percent = 0;
            $DNC_count = 0;
            $DNC_percent = 0;
            $CC_count = 0;
            $CC_percent = 0;
            $NI_count = 0;
            $NI_percent = 0;
            $UW_count = 0;
            $UW_percent = 0;
            $SC_count = 0;
            $SC_percent = 0;
            $COMP_count = 0;
            $COMP_percent = 0;
            $max_calls = 1;
            $flag_count = 0;
            $stmt = "select count(*) from vicidial_list where status IN($human_answered_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $HA_results = sv_mysqli_num_rows($rslt);
            if ($HA_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $HA_count = $row[0];
                $flag_count += $row[0];
                if ($HA_count > 0) {
                    if ($HA_count > $max_calls) {
                        $max_calls = $HA_count;
                    }
                    $HA_percent = (($HA_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($sale_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $SALE_results = sv_mysqli_num_rows($rslt);
            if ($SALE_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $SALE_count = $row[0];
                $flag_count += $row[0];
                if ($SALE_count > 0) {
                    if ($SALE_count > $max_calls) {
                        $max_calls = $SALE_count;
                    }
                    $SALE_percent = (($SALE_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($dnc_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $DNC_results = sv_mysqli_num_rows($rslt);
            if ($DNC_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $DNC_count = $row[0];
                $flag_count += $row[0];
                if ($DNC_count > 0) {
                    if ($DNC_count > $max_calls) {
                        $max_calls = $DNC_count;
                    }

                    $DNC_percent = (($DNC_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($customer_contact_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $CC_results = sv_mysqli_num_rows($rslt);
            if ($CC_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $CC_count = $row[0];
                $flag_count += $row[0];
                if ($CC_count > 0) {
                    if ($CC_count > $max_calls) {
                        $max_calls = $CC_count;
                    }

                    $CC_percent = (($CC_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($not_interested_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $NI_results = sv_mysqli_num_rows($rslt);
            if ($NI_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $NI_count = $row[0];
                $flag_count += $row[0];
                if ($NI_count > 0) {
                    if ($NI_count > $max_calls) {
                        $max_calls = $NI_count;
                    }

                    $NI_percent = (($NI_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($unworkable_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $UW_results = sv_mysqli_num_rows($rslt);
            if ($UW_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $UW_count = $row[0];
                $flag_count += $row[0];
                if ($UW_count > 0) {
                    if ($UW_count > $max_calls) {
                        $max_calls = $UW_count;
                    }
                    $UW_percent = (($UW_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($scheduled_callback_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $SC_results = sv_mysqli_num_rows($rslt);
            if ($SC_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $SC_count = $row[0];
                $flag_count += $row[0];
                if ($SC_count > 0) {
                    if ($SC_count > $max_calls) {
                        $max_calls = $SC_count;
                    }

                    $SC_percent = (($SC_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($completed_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $COMP_results = sv_mysqli_num_rows($rslt);
            if ($COMP_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $COMP_count = $row[0];
                $flag_count += $row[0];
                if ($COMP_count > 0) {
                    if ($COMP_count > $max_calls) {
                        $max_calls = $COMP_count;
                    }

                    $COMP_percent = (($COMP_count / $TOTALleads) * 100);
                }
            }
            $HA_percent = sprintf("%6.2f", "$HA_percent");
            /*while (strlen($HA_percent) > 6) {
            $HA_percent = substr("$HA_percent", 0, -1);
            }*/
            $SALE_percent = sprintf("%6.2f", "$SALE_percent");
            /*while (strlen($SALE_percent) > 6) {
            $SALE_percent = substr("$SALE_percent", 0, -1);
            }*/
            $DNC_percent = sprintf("%6.2f", "$DNC_percent");
            /*while (strlen($DNC_percent) > 6) {
            $DNC_percent = substr("$DNC_percent", 0, -1);
            }*/
            $CC_percent = sprintf("%6.2f", "$CC_percent");
            /*while (strlen($CC_percent) > 6) {
            $CC_percent = substr("$CC_percent", 0, -1);
            }*/
            $NI_percent = sprintf("%6.2f", "$NI_percent");
            /*while (strlen($NI_percent) > 6) {
            $NI_percent = substr("$NI_percent", 0, -1);
            }*/
            $UW_percent = sprintf("%6.2f", "$UW_percent");
            /*while (strlen($UW_percent) > 6) {
            $UW_percent = substr("$UW_percent", 0, -1);
            }*/
            $SC_percent = sprintf("%6.2f", "$SC_percent");
            /*while (strlen($SC_percent) > 6) {
            $SC_percent = substr("$SC_percent", 0, -1);
            }*/
            $COMP_percent = sprintf("%6.2f", "$COMP_percent");
            /*while (strlen($COMP_percent) > 6) {
            $COMP_percent = substr("$COMP_percent", 0, -1);
            }*/
            $HA_count = sprintf("%10s", "$HA_count");
            /*while (strlen($HA_count) > 10) {
            $HA_count = substr("$HA_count", 0, -1);
            }*/
            $SALE_count = sprintf("%10s", "$SALE_count");
            /*while (strlen($SALE_count) > 10) {
            $SALE_count = substr("$SALE_count", 0, -1);
            }*/
            $DNC_count = sprintf("%10s", "$DNC_count");
            /*while (strlen($DNC_count) > 10) {
            $DNC_count = substr("$DNC_count", 0, -1);
            }*/
            $CC_count = sprintf("%10s", "$CC_count");
            /*while (strlen($CC_count) > 10) {
            $CC_count = substr("$CC_count", 0, -1);
            }*/
            $NI_count = sprintf("%10s", "$NI_count");
            /*while (strlen($NI_count) > 10) {
            $NI_count = substr("$NI_count", 0, -1);
            }*/
            $UW_count = sprintf("%10s", "$UW_count");
            /*while (strlen($UW_count) > 10) {
            $UW_count = substr("$UW_count", 0, -1);
            }*/
            $SC_count = sprintf("%10s", "$SC_count");
            /*while (strlen($SC_count) > 10) {
            $SC_count = substr("$SC_count", 0, -1);
            }*/
            $COMP_count = sprintf("%10s", "$COMP_count");
            /*while (strlen($COMP_count) > 10) {
            $COMP_count = substr("$COMP_count", 0, -1);
            }*/
            $sal["resumenBanderaSistema"]["respuestaHumana"]["registros"] = $HA_count;
            $sal["resumenBanderaSistema"]["respuestaHumana"]["porcentaje"] = $HA_percent;

            $sal["resumenBanderaSistema"]["venta"]["registros"] = $SALE_count;
            $sal["resumenBanderaSistema"]["venta"]["porcentaje"] = $SALE_percent;

            $sal["resumenBanderaSistema"]["DNC"]["registros"] = $DNC_count;
            $sal["resumenBanderaSistema"]["DNC"]["porcentaje"] = $DNC_percent;

            $sal["resumenBanderaSistema"]["contactoCliente"]["registros"] = $CC_count;
            $sal["resumenBanderaSistema"]["contactoCliente"]["porcentaje"] = $CC_percent;

            $sal["resumenBanderaSistema"]["noInteresado"]["registros"] = $NI_count;
            $sal["resumenBanderaSistema"]["noInteresado"]["porcentaje"] = $NI_percent;

            $sal["resumenBanderaSistema"]["noTrabajable"]["registros"] = $UW_count;
            $sal["resumenBanderaSistema"]["noTrabajable"]["porcentaje"] = $UW_percent;

            $sal["resumenBanderaSistema"]["llamarDeVuelta"]["registros"] = $SC_count;
            $sal["resumenBanderaSistema"]["llamarDeVuelta"]["porcentaje"] = $SC_percent;

            $sal["resumenBanderaSistema"]["completado"]["registros"] = $COMP_count;
            $sal["resumenBanderaSistema"]["completado"]["porcentaje"] = $COMP_percent;
            $TOTCATcalls = 0;
            $r = 0;
            $i = 0;
            $sal["estadosPersonalizados"]["Registros"] = array();
            while ($r < $statcats_to_print) {
                if ($vsc_id[$r] != 'UNDEFINED') {
                    $TOTCATcalls = ($TOTCATcalls + $vsc_count[$r]);
                    $category = sprintf("%-20s", $vsc_id[$r]);
                    /*while (strlen($category) > 20) {
                    $category = substr("$category", 0, -1);
                    }*/
                    $CATcount = sprintf("%10s", $vsc_count[$r]);
                    /*while (strlen($CATcount) > 10) {
                    $CATcount = substr("$CATcount", 0, -1);
                    }*/
                    $CATname = sprintf("%-30s", $vsc_name[$r]);
                    /*while (strlen($CATname) > 30) {
                    $CATname = substr("$CATname", 0, -1);
                    }*/
                    if ($vsc_count[$r] > $max_calls) {
                        $max_calls = $vsc_count[$r];
                    }
                    $sal["estadosPersonalizados"]["Registros"][$i]["categoria"] = $category;
                    $sal["estadosPersonalizados"]["Registros"][$i]["llamadas"] = $CATcount;
                    $sal["estadosPersonalizados"]["Registros"][$i]["descripcion"] = $CATname;
                    $i++;
                    //$OUToutput .= "| $category | $CATcount | $CATname |\n";
                }
                $r++;
            }
            $TOTCATcalls = sprintf("%10s", $TOTCATcalls);
            /*while (strlen($TOTCATcalls) > 10) {
            $TOTCATcalls = substr("$TOTCATcalls", 0, -1);
            }*/
            $sal["estadosPersonalizados"]["total"] = $TOTCATcalls;
            $i = 0;
            while ($i < $listids_to_print) {
                $TOTALleads = 0;
                if (array_key_exists($i, $LISTIDlist_names)) {
                    $header_list_id = "$LISTIDlists[$i] - $LISTIDlist_names[$i]";
                } else {
                    $header_list_id = "$LISTIDlists[$i]";
                }
                $header_list_id = sprintf("%-51s", $header_list_id);
                /*while (strlen($header_list_id) > 51) {
                $header_list_id = substr("$header_list_id", 0, -1);
                }*/
                $header_list_count = sprintf("%10s", $LISTIDcalls[$i]);
                /*while (strlen($header_list_count) > 10) {
                $header_list_count = substr("$header_list_count", 0, -1);
                }*/
                $sal["datalle"][$i]["nombre"] = $header_list_id;
                $sal["datalle"][$i]["cantidad"] = $header_list_count;
                $max_flags = 1;
                $max_status = 1;
                $HA_count = 0;
                $HA_percent = 0;
                $SALE_count = 0;
                $SALE_percent = 0;
                $DNC_count = 0;
                $DNC_percent = 0;
                $CC_count = 0;
                $CC_percent = 0;
                $NI_count = 0;
                $NI_percent = 0;
                $UW_count = 0;
                $UW_percent = 0;
                $SC_count = 0;
                $SC_percent = 0;
                $COMP_count = 0;
                $COMP_percent = 0;
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($human_answered_statuses)";
                $rslt = sv_mysqli_query($con, $stmt);
                $HA_results = sv_mysqli_num_rows($rslt);
                if ($HA_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $HA_count = $row[0];
                    if ($HA_count > 0) {
                        $HA_percent = (($HA_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($HA_count > $max_flags) {
                    $max_flags = $HA_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($sale_statuses)";
                $rslt = sv_mysqli_query($con, $stmt);
                $SALE_results = sv_mysqli_num_rows($rslt);
                if ($SALE_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $SALE_count = $row[0];
                    if ($SALE_count > 0) {
                        $SALE_percent = (($SALE_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($SALE_count > $max_flags) {
                    $max_flags = $SALE_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($dnc_statuses);";
                $rslt = sv_mysqli_query($con, $stmt);
                $DNC_results = sv_mysqli_num_rows($rslt);
                if ($DNC_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $DNC_count = $row[0];
                    if ($DNC_count > 0) {
                        $DNC_percent = (($DNC_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($DNC_count > $max_flags) {
                    $max_flags = $DNC_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($customer_contact_statuses);";
                $rslt = sv_mysqli_query($con, $stmt);
                $CC_results = sv_mysqli_num_rows($rslt);
                if ($CC_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $CC_count = $row[0];
                    if ($CC_count > 0) {
                        $CC_percent = (($CC_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($CC_count > $max_flags) {
                    $max_flags = $CC_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($not_interested_statuses);";
                $rslt = sv_mysqli_query($con, $stmt);
                $NI_results = sv_mysqli_num_rows($rslt);
                if ($NI_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $NI_count = $row[0];
                    if ($NI_count > 0) {
                        $NI_percent = (($NI_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($NI_count > $max_flags) {
                    $max_flags = $NI_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($unworkable_statuses);";
                $rslt = sv_mysqli_query($con, $stmt);
                $UW_results = sv_mysqli_num_rows($rslt);
                if ($UW_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $UW_count = $row[0];
                    if ($UW_count > 0) {
                        $UW_percent = (($UW_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($UW_count > $max_flags) {
                    $max_flags = $UW_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($scheduled_callback_statuses);";
                $rslt = sv_mysqli_query($con, $stmt);
                $SC_results = sv_mysqli_num_rows($rslt);
                if ($SC_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $SC_count = $row[0];
                    if ($SC_count > 0) {
                        $SC_percent = (($SC_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($SC_count > $max_flags) {
                    $max_flags = $SC_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($completed_statuses);";
                $rslt = sv_mysqli_query($con, $stmt);
                $COMP_results = sv_mysqli_num_rows($rslt);
                if ($COMP_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $COMP_count = $row[0];
                    if ($COMP_count > 0) {
                        $COMP_percent = (($COMP_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($COMP_count > $max_flags) {
                    $max_flags = $COMP_count;
                }
                $HA_percent = sprintf("%6.2f", "$HA_percent");
                /*while (strlen($HA_percent) > 6) {
                $HA_percent = substr("$HA_percent", 0, -1);
                }*/
                $SALE_percent = sprintf("%6.2f", "$SALE_percent");
                /*while (strlen($SALE_percent) > 6) {
                $SALE_percent = substr("$SALE_percent", 0, -1);
                }*/
                $DNC_percent = sprintf("%6.2f", "$DNC_percent");
                /*while (strlen($DNC_percent) > 6) {
                $DNC_percent = substr("$DNC_percent", 0, -1);
                }*/
                $CC_percent = sprintf("%6.2f", "$CC_percent");
                /*while (strlen($CC_percent) > 6) {
                $CC_percent = substr("$CC_percent", 0, -1);
                }*/
                $NI_percent = sprintf("%6.2f", "$NI_percent");
                /*while (strlen($NI_percent) > 6) {
                $NI_percent = substr("$NI_percent", 0, -1);
                }*/
                $UW_percent = sprintf("%6.2f", "$UW_percent");
                /*while (strlen($UW_percent) > 6) {
                $UW_percent = substr("$UW_percent", 0, -1);
                }*/
                $SC_percent = sprintf("%6.2f", "$SC_percent");
                /*while (strlen($SC_percent) > 6) {
                $SC_percent = substr("$SC_percent", 0, -1);
                }*/
                $COMP_percent = sprintf("%6.2f", "$COMP_percent");
                /*while (strlen($COMP_percent) > 6) {
                $COMP_percent = substr("$COMP_percent", 0, -1);
                }*/

                $HA_count = sprintf("%9s", "$HA_count");
                /*while (strlen($HA_count) > 9) {
                $HA_count = substr("$HA_count", 0, -1);
                }*/
                $SALE_count = sprintf("%9s", "$SALE_count");
                /*while (strlen($SALE_count) > 9) {
                $SALE_count = substr("$SALE_count", 0, -1);
                }*/
                $DNC_count = sprintf("%9s", "$DNC_count");
                /*while (strlen($DNC_count) > 9) {
                $DNC_count = substr("$DNC_count", 0, -1);
                }*/
                $CC_count = sprintf("%9s", "$CC_count");
                /*while (strlen($CC_count) > 9) {
                $CC_count = substr("$CC_count", 0, -1);
                }*/
                $NI_count = sprintf("%9s", "$NI_count");
                /*while (strlen($NI_count) > 9) {
                $NI_count = substr("$NI_count", 0, -1);
                }*/
                $UW_count = sprintf("%9s", "$UW_count");
                /*while (strlen($UW_count) > 9) {
                $UW_count = substr("$UW_count", 0, -1);
                }*/
                $SC_count = sprintf("%9s", "$SC_count");
                /*while (strlen($SC_count) > 9) {
                $SC_count = substr("$SC_count", 0, -1);
                }*/
                $COMP_count = sprintf("%9s", "$COMP_count");
                /*while (strlen($COMP_count) > 9) {
                $COMP_count = substr("$COMP_count", 0, -1);
                }*/

                $sal["detalle"][$i]["respuestaHumana"]["registros"] = $HA_count;
                $sal["detalle"][$i]["respuestaHumana"]["porcentaje"] = $HA_percent;

                $sal["detalle"][$i]["venta"]["registros"] = $SALE_count;
                $sal["detalle"][$i]["venta"]["porcentaje"] = $SALE_percent;

                $sal["detalle"][$i]["DNC"]["registros"] = $DNC_count;
                $sal["detalle"][$i]["DNC"]["porcentaje"] = $DNC_percent;

                $sal["detalle"][$i]["contactoCliente"]["registros"] = $CC_count;
                $sal["detalle"][$i]["contactoCliente"]["porcentaje"] = $CC_percent;

                $sal["detalle"][$i]["noInteresado"]["registros"] = $NI_count;
                $sal["detalle"][$i]["noInteresado"]["porcentaje"] = $NI_percent;

                $sal["detalle"][$i]["noTrabajable"]["registros"] = $UW_count;
                $sal["detalle"][$i]["noTrabajable"]["porcentaje"] = $UW_percent;

                $sal["detalle"][$i]["llamarDeVuelta"]["registros"] = $SC_count;
                $sal["detalle"][$i]["llamarDeVuelta"]["porcentaje"] = $SC_percent;

                $sal["detalle"][$i]["completado"]["registros"] = $COMP_count;
                $sal["detalle"][$i]["completado"]["porcentaje"] = $COMP_percent;

                $stmt = "select status,count(*) from vicidial_list where list_id='$LISTIDlists[$i]' group by status order by status;";
                $rslt = sv_mysqli_query($con, $stmt);
                $liststatussum_to_print = sv_mysqli_num_rows($rslt);
                $r = 0;
                while ($r < $liststatussum_to_print) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $LISTIDstatus[$r] = $row[0];
                    $LISTIDcounts[$r] = $row[1];
                    if ($row[1] > $max_status) {
                        $max_status = $row[1];
                    }
                    $r++;
                }
                $r = 0;
                $sal["detalle"][$i]["breakdown"]["Registros"] = array();
                while ($r < $liststatussum_to_print) {
                    $LIDstatus = $LISTIDstatus[$r];
                    $LIDstatus_format = sprintf("%6s", $LIDstatus);
                    $TOTALleads = ($TOTALleads + $LISTIDcounts[$r]);
                    $LISTID_status_count = sprintf("%10s", $LISTIDcounts[$r]);
                    /*while (strlen($LISTID_status_count) > 10) {
                    $LISTID_status_count = substr("$LISTID_status_count", 0, -1);
                    }*/
                    if (array_key_exists($LIDstatus, $statname_list)) {
                        $LISTIDname = sprintf("%-42s", "$LIDstatus_format | $statname_list[$LIDstatus]");
                    } else {
                        $LISTIDname = sprintf("%-42s", "$LIDstatus_format");
                    }
                    /*while (strlen($LISTIDname) > 42) {
                    $LISTIDname = substr("$LISTIDname", 0, -1);
                    }*/
                    //$OUToutput .= "     | $LISTIDname | $LISTID_status_count |\n";
                    $sal["detalle"][$i]["breakdown"]["Registros"][$r]["status"] = $LISTIDname;
                    $sal["detalle"][$i]["breakdown"]["Registros"][$r]["cantidad"] = $LISTID_status_count;
                    $r++;
                }
                $TOTALleads = sprintf("%10s", $TOTALleads);
                $sal["detalle"][$i]["breakdown"]["total"] = $TOTALleads;
                $i++;
            }
            sv_mysqli_close($con);
            if (!isset($_GET["csv"])) {
                $sal["Estado"] = "ok";
                $this->output->set_output(json_encode($sal));
            } else {
                $seccion = $_GET["seccion"];
                $arregloCSV = array();
                if ($seccion == "1") {
                    $arregloCSV = $sal["resumenIdLista"];
                } else if ($seccion == 2) {
                    $arregloCSV = $sal["resumenBanderaSistema"];
                } else if ($seccion == 3) {
                    $arregloCSV = $sal["estadosPersonalizados"];
                } else if ($seccion == 4) {
                    $arregloCSV = $sal["respuestaHumana"];
                }
                header("Content-type: text/csv");
                header("Content-Disposition: attachment; filename=ReporteListaFecha.csv");
                header("Pragma: no-cache");
                header("Expires: 0");
                $out = fopen('php://output', 'w');
                $test = array();
                $this->trata($out, $arregloCSV, $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 listCampaignStatsCSV()
    {
        $idioma = isset($_COOKIE["idioma"]) ? $_COOKIE["idioma"] : "es";
        switch ($idioma) {
            case "es":
                $this->lang->load('reportelistafecha', "spanish");
                break;
            case "en":
                $this->lang->load('reportelistafecha', "english");
                break;
            default:
                $this->lang->load('reportelistafecha', "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();
            $sal = array();
            $campana = limpia($_GET["campana"]);
            if (!isset($_GET["campana"]) || $campana == -1) {
                $campana = "campaign_id";
            } else {
                $campana = "'$campana'";
            }
            $and = "";
            if ($_SESSION["Cliente"] != -1) {
                $and = "AND campaign_id IN (SELECT campaign_id FROM vicidial_campaigns WHERE user_group = '" . $_SESSION["Cliente"] . "')";
            }
            $group_SQLand = "and campaign_id = $campana $and";
            $query = "SELECT list_id from vicidial_lists where active IN('Y','N') $group_SQLand";
            $result = sv_mysqli_query($con, $query);
            $lists_id_str = "";
            while ($lrow = sv_mysqli_fetch_array($result)) {
                $lists_id_str .= "'$lrow[0]',";
            }
            $lists_id_str = substr($lists_id_str, 0, -1);
            $whC = "";
            if ($_SESSION["Cliente"] != -1) {
                $whC = "WHERE vsc_id REGEXP '^" . $_SESSION["Cliente"] . "+[0-9]+$'";
            }
            $query = "select vsc_id,vsc_name from vicidial_status_categories $whC";
            $result = sv_mysqli_query($con, $query);
            $statcats_to_print = sv_mysqli_num_rows($result);
            $i = 0;
            $vsc_count = array();
            while ($i < $statcats_to_print) {
                $row = sv_mysqli_fetch_array($result);
                $vsc_id[$i] = $row[0];
                $vsc_name[$i] = $row[1];
                $category_statuses = "";
                $status_stmt = "select distinct status from vicidial_statuses where category='$row[0]' UNION select distinct status from vicidial_campaign_statuses where category='$row[0]' $group_SQLand";
                $status_rslt = sv_mysqli_query($con, $status_stmt);
                while ($status_row = sv_mysqli_fetch_array($status_rslt)) {
                    $category_statuses .= "'" . $status_row[0] . "',";
                }
                $category_statuses = substr($category_statuses, 0, -1);
                $substre = "status in ($category_statuses)";
                $subtre2 = "list_id IN($lists_id_str)";
                if (strlen($category_statuses) == 0) {
                    $substre = "";
                }
                if (strlen($lists_id_str) == 0) {
                    $subtre2 = "";
                }
                $ad = "";
                $whe = "";
                if ($substre != "" and $subtre2 != "") {
                    $ad = "and";
                }
                if ($substre != "" || $subtre2 != "") {
                    $whe = "where";
                }

                $category_stmt = "select count(*)  from vicidial_list $whe $substre $ad $subtre2";
                $category_rslt = sv_mysqli_query($con, $category_stmt);
                $category_row = sv_mysqli_fetch_array($category_rslt);
                $vsc_count[$i] = $category_row[0];
                $i++;
            }
            $human_answered_statuses = '';
            $sale_statuses = '';
            $dnc_statuses = '';
            $customer_contact_statuses = '';
            $not_interested_statuses = '';
            $unworkable_statuses = '';
            $query = "select status,human_answered,sale,dnc,customer_contact,not_interested,unworkable,scheduled_callback,completed,status_name from vicidial_statuses";
            $result = sv_mysqli_query($con, $query);
            $statha_to_print = sv_mysqli_num_rows($result);
            $i = 0;
            $scheduled_callback_statuses = "";
            $completed_statuses = "";
            $statname_list = array();
            while ($i < $statha_to_print) {
                $row = sv_mysqli_fetch_array($result);
                $temp_status = $row[0];
                $statname_list["$temp_status"] = "$row[9]";
                if ($row[1] == 'Y') {
                    $human_answered_statuses .= "'$temp_status',";
                }
                if ($row[2] == 'Y') {
                    $sale_statuses .= "'$temp_status',";
                }
                if ($row[3] == 'Y') {
                    $dnc_statuses .= "'$temp_status',";
                }
                if ($row[4] == 'Y') {
                    $customer_contact_statuses .= "'$temp_status',";
                }
                if ($row[5] == 'Y') {
                    $not_interested_statuses .= "'$temp_status',";
                }
                if ($row[6] == 'Y') {
                    $unworkable_statuses .= "'$temp_status',";
                }
                if ($row[7] == 'Y') {
                    $scheduled_callback_statuses .= "'$temp_status',";
                }
                if ($row[8] == 'Y') {
                    $completed_statuses .= "'$temp_status',";
                }
                $i++;
            }
            $query = "select status,human_answered,sale,dnc,customer_contact,not_interested,unworkable,scheduled_callback,completed,status_name from vicidial_campaign_statuses where selectable IN('Y','N') $group_SQLand";
            $rslt = sv_mysqli_query($con, $query);
            $statha_to_print = sv_mysqli_num_rows($rslt);
            $i = 0;
            while ($i < $statha_to_print) {
                $row = sv_mysqli_fetch_array($rslt);
                $temp_status = $row[0];
                $statname_list["$temp_status"] = "$row[9]";
                if (($row[1] == 'Y') and (!preg_match("/'$temp_status'/", $human_answered_statuses))) {
                    $human_answered_statuses .= "'$temp_status',";
                }
                if ($row[2] == 'Y') {
                    $sale_statuses .= "'$temp_status',";
                }
                if ($row[3] == 'Y') {
                    $dnc_statuses .= "'$temp_status',";
                }
                if ($row[4] == 'Y') {
                    $customer_contact_statuses .= "'$temp_status',";
                }
                if ($row[5] == 'Y') {
                    $not_interested_statuses .= "'$temp_status',";
                }
                if ($row[6] == 'Y') {
                    $unworkable_statuses .= "'$temp_status',";
                }
                if ($row[7] == 'Y') {
                    $scheduled_callback_statuses .= "'$temp_status',";
                }
                if ($row[8] == 'Y') {
                    $completed_statuses .= "'$temp_status',";
                }
                $i++;
            }
            if (strlen($human_answered_statuses) > 2) {
                $human_answered_statuses = substr("$human_answered_statuses", 0, -1);
            } else {
                $human_answered_statuses = "''";
            }
            if (strlen($sale_statuses) > 2) {
                $sale_statuses = substr("$sale_statuses", 0, -1);
            } else {
                $sale_statuses = "''";
            }
            if (strlen($dnc_statuses) > 2) {
                $dnc_statuses = substr("$dnc_statuses", 0, -1);
            } else {
                $dnc_statuses = "''";
            }
            if (strlen($customer_contact_statuses) > 2) {
                $customer_contact_statuses = substr("$customer_contact_statuses", 0, -1);
            } else {
                $customer_contact_statuses = "''";
            }
            if (strlen($not_interested_statuses) > 2) {
                $not_interested_statuses = substr("$not_interested_statuses", 0, -1);
            } else {
                $not_interested_statuses = "''";
            }
            if (strlen($unworkable_statuses) > 2) {
                $unworkable_statuses = substr("$unworkable_statuses", 0, -1);
            } else {
                $unworkable_statuses = "''";
            }
            if (strlen($scheduled_callback_statuses) > 2) {
                $scheduled_callback_statuses = substr("$scheduled_callback_statuses", 0, -1);
            } else {
                $scheduled_callback_statuses = "''";
            }
            if (strlen($completed_statuses) > 2) {
                $completed_statuses = substr("$completed_statuses", 0, -1);
            } else {
                $completed_statuses = "''";
            }
            $lists_id_str = "";
            $list_stmt = "SELECT list_id from vicidial_lists where active IN('Y','N') $group_SQLand";
            $list_rslt = sv_mysqli_query($con, $list_stmt);
            while ($lrow = sv_mysqli_fetch_array($list_rslt)) {
                $lists_id_str .= "'$lrow[0]',";
            }
            $lists_id_str = substr($lists_id_str, 0, -1);
            $condition = "where list_id IN($lists_id_str)";
            if (strlen($lists_id_str) == 0) {
                $condition = "where list_id !=list_id";
            }
            $stmt = "select count(*),list_id from vicidial_list $condition group by list_id;";
            $rslt = sv_mysqli_query($con, $stmt);
            $listids_to_print = sv_mysqli_num_rows($rslt);
            $i = 0;
            $LISTIDlists = array();
            $LISTIDlist_names = array();
            $list_id_SQL = "";
            $max_calls = 0;
            while ($i < $listids_to_print) {
                $row = sv_mysqli_fetch_array($rslt);
                $LISTIDcalls[$i] = $row[0];
                $LISTIDlists[$i] = $row[1];
                $list_id_SQL .= "'$row[1]',";
                if ($row[0] > $max_calls) {
                    $max_calls = $row[0];
                }
                $i++;
            }
            if (strlen($list_id_SQL) > 2) {
                $list_id_SQL = substr("$list_id_SQL", 0, -1);
            } else {
                $list_id_SQL = "''";
            }
            $i = 0;
            $LISTIDlist_active = array();
            $TOTALleads = 0;
            $sal[$this->lang->line("csv_resumenIdLista")] = array();
            $sal[$this->lang->line("csv_resumenIdLista")][$this->lang->line("csv_resumenIdLista_registros")] = array();
            while ($i < $listids_to_print) {
                $stmt = "select list_name,active from vicidial_lists where list_id='$LISTIDlists[$i]'";
                $rslt = sv_mysqli_query($con, $stmt);
                $list_name_to_print = sv_mysqli_num_rows($rslt);
                $LISTIDlist_active[$i] = "";
                if ($list_name_to_print > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $LISTIDlist_names[$i] = $row[0];
                    if ($row[1] == 'Y') {
                        $LISTIDlist_active[$i] = 'ACTIVE  ';
                    } else {
                        $LISTIDlist_active[$i] = 'INACTIVE';
                    }

                }
                $TOTALleads = ($TOTALleads + $LISTIDcalls[$i]);
                $LISTIDcount = sprintf("%10s", $LISTIDcalls[$i]);
                /*while (strlen($LISTIDcount) > 10) {
                $LISTIDcount = substr("$LISTIDcount", 0, -1);
                }*/
                if (array_key_exists($i, $LISTIDlist_names)) {
                    $LISTIDname = sprintf("%-40s", "$LISTIDlists[$i] - $LISTIDlist_names[$i]");
                    /*while (strlen($LISTIDname) > 40) {
                $LISTIDname = substr("$LISTIDname", 0, -1);
                }*/
                } else {
                    $LISTIDname = sprintf("%-40s", "$LISTIDlists[$i]");
                    /*while (strlen($LISTIDname) > 40) {
                $LISTIDname = substr("$LISTIDname", 0, -1);
                }*/
                }
                $sal[$this->lang->line("csv_resumenIdLista")][$this->lang->line("csv_resumenIdLista_registros")][$i][$this->lang->line("csv_resumenIdLista_registros_n_lista")] = $LISTIDname;
                $sal[$this->lang->line("csv_resumenIdLista")][$this->lang->line("csv_resumenIdLista_registros")][$i][$this->lang->line("csv_resumenIdLista_registros_n_ctdRegistros")] = $LISTIDcount;
                $sal[$this->lang->line("csv_resumenIdLista")][$this->lang->line("csv_resumenIdLista_registros")][$i][$this->lang->line("csv_resumenIdLista_registros_n_activa")] = $LISTIDlist_active[$i];
                $i++;
            }
            $TOTALleads = sprintf("%10s", $TOTALleads);
            $sal[$this->lang->line("csv_resumenIdLista")][$this->lang->line("csv_resumenIdLista_total")] = $TOTALleads;
            $HA_count = 0;
            $HA_percent = 0;
            $SALE_count = 0;
            $SALE_percent = 0;
            $DNC_count = 0;
            $DNC_percent = 0;
            $CC_count = 0;
            $CC_percent = 0;
            $NI_count = 0;
            $NI_percent = 0;
            $UW_count = 0;
            $UW_percent = 0;
            $SC_count = 0;
            $SC_percent = 0;
            $COMP_count = 0;
            $COMP_percent = 0;
            $max_calls = 1;
            $flag_count = 0;
            $stmt = "select count(*) from vicidial_list where status IN($human_answered_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $HA_results = sv_mysqli_num_rows($rslt);
            if ($HA_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $HA_count = $row[0];
                $flag_count += $row[0];
                if ($HA_count > 0) {
                    if ($HA_count > $max_calls) {
                        $max_calls = $HA_count;
                    }
                    $HA_percent = (($HA_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($sale_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $SALE_results = sv_mysqli_num_rows($rslt);
            if ($SALE_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $SALE_count = $row[0];
                $flag_count += $row[0];
                if ($SALE_count > 0) {
                    if ($SALE_count > $max_calls) {
                        $max_calls = $SALE_count;
                    }
                    $SALE_percent = (($SALE_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($dnc_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $DNC_results = sv_mysqli_num_rows($rslt);
            if ($DNC_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $DNC_count = $row[0];
                $flag_count += $row[0];
                if ($DNC_count > 0) {
                    if ($DNC_count > $max_calls) {
                        $max_calls = $DNC_count;
                    }

                    $DNC_percent = (($DNC_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($customer_contact_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $CC_results = sv_mysqli_num_rows($rslt);
            if ($CC_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $CC_count = $row[0];
                $flag_count += $row[0];
                if ($CC_count > 0) {
                    if ($CC_count > $max_calls) {
                        $max_calls = $CC_count;
                    }

                    $CC_percent = (($CC_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($not_interested_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $NI_results = sv_mysqli_num_rows($rslt);
            if ($NI_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $NI_count = $row[0];
                $flag_count += $row[0];
                if ($NI_count > 0) {
                    if ($NI_count > $max_calls) {
                        $max_calls = $NI_count;
                    }

                    $NI_percent = (($NI_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($unworkable_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $UW_results = sv_mysqli_num_rows($rslt);
            if ($UW_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $UW_count = $row[0];
                $flag_count += $row[0];
                if ($UW_count > 0) {
                    if ($UW_count > $max_calls) {
                        $max_calls = $UW_count;
                    }
                    $UW_percent = (($UW_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($scheduled_callback_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $SC_results = sv_mysqli_num_rows($rslt);
            if ($SC_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $SC_count = $row[0];
                $flag_count += $row[0];
                if ($SC_count > 0) {
                    if ($SC_count > $max_calls) {
                        $max_calls = $SC_count;
                    }

                    $SC_percent = (($SC_count / $TOTALleads) * 100);
                }
            }
            $stmt = "select count(*) from vicidial_list where status IN($completed_statuses) and list_id IN($list_id_SQL)";
            $rslt = sv_mysqli_query($con, $stmt);
            $COMP_results = sv_mysqli_num_rows($rslt);
            if ($COMP_results > 0) {
                $row = sv_mysqli_fetch_array($rslt);
                $COMP_count = $row[0];
                $flag_count += $row[0];
                if ($COMP_count > 0) {
                    if ($COMP_count > $max_calls) {
                        $max_calls = $COMP_count;
                    }

                    $COMP_percent = (($COMP_count / $TOTALleads) * 100);
                }
            }
            $HA_percent = sprintf("%6.2f", "$HA_percent");
            /*while (strlen($HA_percent) > 6) {
            $HA_percent = substr("$HA_percent", 0, -1);
            }*/
            $SALE_percent = sprintf("%6.2f", "$SALE_percent");
            /*while (strlen($SALE_percent) > 6) {
            $SALE_percent = substr("$SALE_percent", 0, -1);
            }*/
            $DNC_percent = sprintf("%6.2f", "$DNC_percent");
            /*while (strlen($DNC_percent) > 6) {
            $DNC_percent = substr("$DNC_percent", 0, -1);
            }*/
            $CC_percent = sprintf("%6.2f", "$CC_percent");
            /*while (strlen($CC_percent) > 6) {
            $CC_percent = substr("$CC_percent", 0, -1);
            }*/
            $NI_percent = sprintf("%6.2f", "$NI_percent");
            /*while (strlen($NI_percent) > 6) {
            $NI_percent = substr("$NI_percent", 0, -1);
            }*/
            $UW_percent = sprintf("%6.2f", "$UW_percent");
            /*while (strlen($UW_percent) > 6) {
            $UW_percent = substr("$UW_percent", 0, -1);
            }*/
            $SC_percent = sprintf("%6.2f", "$SC_percent");
            /*while (strlen($SC_percent) > 6) {
            $SC_percent = substr("$SC_percent", 0, -1);
            }*/
            $COMP_percent = sprintf("%6.2f", "$COMP_percent");
            /*while (strlen($COMP_percent) > 6) {
            $COMP_percent = substr("$COMP_percent", 0, -1);
            }*/
            $HA_count = sprintf("%10s", "$HA_count");
            /*while (strlen($HA_count) > 10) {
            $HA_count = substr("$HA_count", 0, -1);
            }*/
            $SALE_count = sprintf("%10s", "$SALE_count");
            /*while (strlen($SALE_count) > 10) {
            $SALE_count = substr("$SALE_count", 0, -1);
            }*/
            $DNC_count = sprintf("%10s", "$DNC_count");
            /*while (strlen($DNC_count) > 10) {
            $DNC_count = substr("$DNC_count", 0, -1);
            }*/
            $CC_count = sprintf("%10s", "$CC_count");
            /*while (strlen($CC_count) > 10) {
            $CC_count = substr("$CC_count", 0, -1);
            }*/
            $NI_count = sprintf("%10s", "$NI_count");
            /*while (strlen($NI_count) > 10) {
            $NI_count = substr("$NI_count", 0, -1);
            }*/
            $UW_count = sprintf("%10s", "$UW_count");
            /*while (strlen($UW_count) > 10) {
            $UW_count = substr("$UW_count", 0, -1);
            }*/
            $SC_count = sprintf("%10s", "$SC_count");
            /*while (strlen($SC_count) > 10) {
            $SC_count = substr("$SC_count", 0, -1);
            }*/
            $COMP_count = sprintf("%10s", "$COMP_count");
            /*while (strlen($COMP_count) > 10) {
            $COMP_count = substr("$COMP_count", 0, -1);
            }*/
            $sal[$this->lang->line("csv_resumenBanderaSistema")] = array();

            $sal[$this->lang->line("csv_resumenBanderaSistema")][0][$this->lang->line("csv_resumenBanderaSistema_n_tipo")] = $this->lang->line("csv_resumenBanderaSistema_n_tipo_respuestaHumana");
            $sal[$this->lang->line("csv_resumenBanderaSistema")][0][$this->lang->line("csv_resumenBanderaSistema_n_ctdRegistros")] = $HA_count;
            $sal[$this->lang->line("csv_resumenBanderaSistema")][0][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $HA_percent;

            $sal[$this->lang->line("csv_resumenBanderaSistema")][1][$this->lang->line("csv_resumenBanderaSistema_n_tipo")] = $this->lang->line("csv_resumenBanderaSistema_n_tipo_venta");
            $sal[$this->lang->line("csv_resumenBanderaSistema")][1][$this->lang->line("csv_resumenBanderaSistema_n_ctdRegistros")] = $SALE_count;
            $sal[$this->lang->line("csv_resumenBanderaSistema")][1][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $SALE_percent;

            $sal[$this->lang->line("csv_resumenBanderaSistema")][2][$this->lang->line("csv_resumenBanderaSistema_n_tipo")] = $this->lang->line("csv_resumenBanderaSistema_n_tipo_dnc");
            $sal[$this->lang->line("csv_resumenBanderaSistema")][2][$this->lang->line("csv_resumenBanderaSistema_n_ctdRegistros")] = $DNC_count;
            $sal[$this->lang->line("csv_resumenBanderaSistema")][2][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $DNC_percent;

            $sal[$this->lang->line("csv_resumenBanderaSistema")][3][$this->lang->line("csv_resumenBanderaSistema_n_tipo")] = $this->lang->line("csv_resumenBanderaSistema_n_tipo_contactoCliente");
            $sal[$this->lang->line("csv_resumenBanderaSistema")][3][$this->lang->line("csv_resumenBanderaSistema_n_ctdRegistros")] = $CC_count;
            $sal[$this->lang->line("csv_resumenBanderaSistema")][3][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $CC_percent;

            $sal[$this->lang->line("csv_resumenBanderaSistema")][4][$this->lang->line("csv_resumenBanderaSistema_n_tipo")] = $this->lang->line("csv_resumenBanderaSistema_n_tipo_noInteresado");
            $sal[$this->lang->line("csv_resumenBanderaSistema")][4][$this->lang->line("csv_resumenBanderaSistema_n_ctdRegistros")] = $NI_count;
            $sal[$this->lang->line("csv_resumenBanderaSistema")][4][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $NI_percent;

            $sal[$this->lang->line("csv_resumenBanderaSistema")][5][$this->lang->line("csv_resumenBanderaSistema_n_tipo")] = $this->lang->line("csv_resumenBanderaSistema_n_tipo_noTrabajable");
            $sal[$this->lang->line("csv_resumenBanderaSistema")][5][$this->lang->line("csv_resumenBanderaSistema_n_ctdRegistros")] = $UW_count;
            $sal[$this->lang->line("csv_resumenBanderaSistema")][5][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $UW_percent;

            $sal[$this->lang->line("csv_resumenBanderaSistema")][6][$this->lang->line("csv_resumenBanderaSistema_n_tipo")] = $this->lang->line("csv_resumenBanderaSistema_n_tipo_llamarDeVuelta");
            $sal[$this->lang->line("csv_resumenBanderaSistema")][6][$this->lang->line("csv_resumenBanderaSistema_n_ctdRegistros")] = $SC_count;
            $sal[$this->lang->line("csv_resumenBanderaSistema")][6][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $SC_percent;

            $sal[$this->lang->line("csv_resumenBanderaSistema")][7][$this->lang->line("csv_resumenBanderaSistema_n_tipo")] = $this->lang->line("csv_resumenBanderaSistema_n_tipo_completado");
            $sal[$this->lang->line("csv_resumenBanderaSistema")][7][$this->lang->line("csv_resumenBanderaSistema_n_ctdRegistros")] = $COMP_count;
            $sal[$this->lang->line("csv_resumenBanderaSistema")][7][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $COMP_percent;

            $TOTCATcalls = 0;
            $r = 0;
            $i = 0;
            $sal[$this->lang->line("csv_categoriasPersonalizadas")][$this->lang->line("csv_categoriasPersonalizadas_registros")] = array();
            while ($r < $statcats_to_print) {
                if ($vsc_id[$r] != 'UNDEFINED') {
                    $TOTCATcalls = ($TOTCATcalls + $vsc_count[$r]);
                    $category = sprintf("%-20s", $vsc_id[$r]);
                    /*while (strlen($category) > 20) {
                    $category = substr("$category", 0, -1);
                    }*/
                    $CATcount = sprintf("%10s", $vsc_count[$r]);
                    /*while (strlen($CATcount) > 10) {
                    $CATcount = substr("$CATcount", 0, -1);
                    }*/
                    $CATname = sprintf("%-30s", $vsc_name[$r]);
                    /*while (strlen($CATname) > 30) {
                    $CATname = substr("$CATname", 0, -1);
                    }*/
                    if ($vsc_count[$r] > $max_calls) {
                        $max_calls = $vsc_count[$r];
                    }
                    $sal[$this->lang->line("csv_categoriasPersonalizadas")][$this->lang->line("csv_categoriasPersonalizadas_registros")][$i][$this->lang->line("csv_categoriasPersonalizadas_registros_idCategoria")] = $category;
                    $sal[$this->lang->line("csv_categoriasPersonalizadas")][$this->lang->line("csv_categoriasPersonalizadas_registros")][$i][$this->lang->line("csv_categoriasPersonalizadas_registros_ctdLlamadas")] = $CATcount;
                    $sal[$this->lang->line("csv_categoriasPersonalizadas")][$this->lang->line("csv_categoriasPersonalizadas_registros")][$i][$this->lang->line("csv_categoriasPersonalizadas_registros_nombreCategoria")] = $CATname;
                    $i++;
                    //$OUToutput .= "| $category | $CATcount | $CATname |\n";
                }
                $r++;
            }
            $TOTCATcalls = sprintf("%10s", $TOTCATcalls);
            /*while (strlen($TOTCATcalls) > 10) {
            $TOTCATcalls = substr("$TOTCATcalls", 0, -1);
            }*/
            $sal[$this->lang->line("csv_categoriasPersonalizadas")][$this->lang->line("csv_categoriasPersonalizadas_total")] = $TOTCATcalls;
            $i = 0;
            while ($i < $listids_to_print) {
                $TOTALleads = 0;
                if (array_key_exists($i, $LISTIDlist_names)) {
                    $header_list_id = "$LISTIDlists[$i] - $LISTIDlist_names[$i]";
                } else {
                    $header_list_id = "$LISTIDlists[$i]";
                }
                $header_list_id = sprintf("%-51s", $header_list_id);
                /*while (strlen($header_list_id) > 51) {
                $header_list_id = substr("$header_list_id", 0, -1);
                }*/
                $header_list_count = sprintf("%10s", $LISTIDcalls[$i]);
                /*while (strlen($header_list_count) > 10) {
                $header_list_count = substr("$header_list_count", 0, -1);
                }*/
                $sal["datalle"][$i]["nombre"] = $header_list_id;
                $sal["datalle"][$i]["cantidad"] = $header_list_count;
                $max_flags = 1;
                $max_status = 1;
                $HA_count = 0;
                $HA_percent = 0;
                $SALE_count = 0;
                $SALE_percent = 0;
                $DNC_count = 0;
                $DNC_percent = 0;
                $CC_count = 0;
                $CC_percent = 0;
                $NI_count = 0;
                $NI_percent = 0;
                $UW_count = 0;
                $UW_percent = 0;
                $SC_count = 0;
                $SC_percent = 0;
                $COMP_count = 0;
                $COMP_percent = 0;
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($human_answered_statuses)";
                $rslt = sv_mysqli_query($con, $stmt);
                $HA_results = sv_mysqli_num_rows($rslt);
                if ($HA_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $HA_count = $row[0];
                    if ($HA_count > 0) {
                        $HA_percent = (($HA_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($HA_count > $max_flags) {
                    $max_flags = $HA_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($sale_statuses)";
                $rslt = sv_mysqli_query($con, $stmt);
                $SALE_results = sv_mysqli_num_rows($rslt);
                if ($SALE_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $SALE_count = $row[0];
                    if ($SALE_count > 0) {
                        $SALE_percent = (($SALE_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($SALE_count > $max_flags) {
                    $max_flags = $SALE_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($dnc_statuses);";
                $rslt = sv_mysqli_query($con, $stmt);
                $DNC_results = sv_mysqli_num_rows($rslt);
                if ($DNC_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $DNC_count = $row[0];
                    if ($DNC_count > 0) {
                        $DNC_percent = (($DNC_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($DNC_count > $max_flags) {
                    $max_flags = $DNC_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($customer_contact_statuses);";
                $rslt = sv_mysqli_query($con, $stmt);
                $CC_results = sv_mysqli_num_rows($rslt);
                if ($CC_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $CC_count = $row[0];
                    if ($CC_count > 0) {
                        $CC_percent = (($CC_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($CC_count > $max_flags) {
                    $max_flags = $CC_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($not_interested_statuses);";
                $rslt = sv_mysqli_query($con, $stmt);
                $NI_results = sv_mysqli_num_rows($rslt);
                if ($NI_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $NI_count = $row[0];
                    if ($NI_count > 0) {
                        $NI_percent = (($NI_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($NI_count > $max_flags) {
                    $max_flags = $NI_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($unworkable_statuses);";
                $rslt = sv_mysqli_query($con, $stmt);
                $UW_results = sv_mysqli_num_rows($rslt);
                if ($UW_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $UW_count = $row[0];
                    if ($UW_count > 0) {
                        $UW_percent = (($UW_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($UW_count > $max_flags) {
                    $max_flags = $UW_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($scheduled_callback_statuses);";
                $rslt = sv_mysqli_query($con, $stmt);
                $SC_results = sv_mysqli_num_rows($rslt);
                if ($SC_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $SC_count = $row[0];
                    if ($SC_count > 0) {
                        $SC_percent = (($SC_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($SC_count > $max_flags) {
                    $max_flags = $SC_count;
                }
                $stmt = "select count(*) from vicidial_list where list_id='$LISTIDlists[$i]' and status IN($completed_statuses);";
                $rslt = sv_mysqli_query($con, $stmt);
                $COMP_results = sv_mysqli_num_rows($rslt);
                if ($COMP_results > 0) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $COMP_count = $row[0];
                    if ($COMP_count > 0) {
                        $COMP_percent = (($COMP_count / $LISTIDcalls[$i]) * 100);
                    }
                }
                if ($COMP_count > $max_flags) {
                    $max_flags = $COMP_count;
                }
                $HA_percent = sprintf("%6.2f", "$HA_percent");
                /*while (strlen($HA_percent) > 6) {
                $HA_percent = substr("$HA_percent", 0, -1);
                }*/
                $SALE_percent = sprintf("%6.2f", "$SALE_percent");
                /*while (strlen($SALE_percent) > 6) {
                $SALE_percent = substr("$SALE_percent", 0, -1);
                }*/
                $DNC_percent = sprintf("%6.2f", "$DNC_percent");
                /*while (strlen($DNC_percent) > 6) {
                $DNC_percent = substr("$DNC_percent", 0, -1);
                }*/
                $CC_percent = sprintf("%6.2f", "$CC_percent");
                /*while (strlen($CC_percent) > 6) {
                $CC_percent = substr("$CC_percent", 0, -1);
                }*/
                $NI_percent = sprintf("%6.2f", "$NI_percent");
                /*while (strlen($NI_percent) > 6) {
                $NI_percent = substr("$NI_percent", 0, -1);
                }*/
                $UW_percent = sprintf("%6.2f", "$UW_percent");
                /*while (strlen($UW_percent) > 6) {
                $UW_percent = substr("$UW_percent", 0, -1);
                }*/
                $SC_percent = sprintf("%6.2f", "$SC_percent");
                /*while (strlen($SC_percent) > 6) {
                $SC_percent = substr("$SC_percent", 0, -1);
                }*/
                $COMP_percent = sprintf("%6.2f", "$COMP_percent");
                /*while (strlen($COMP_percent) > 6) {
                $COMP_percent = substr("$COMP_percent", 0, -1);
                }*/

                $HA_count = sprintf("%9s", "$HA_count");
                /*while (strlen($HA_count) > 9) {
                $HA_count = substr("$HA_count", 0, -1);
                }*/
                $SALE_count = sprintf("%9s", "$SALE_count");
                /*while (strlen($SALE_count) > 9) {
                $SALE_count = substr("$SALE_count", 0, -1);
                }*/
                $DNC_count = sprintf("%9s", "$DNC_count");
                /*while (strlen($DNC_count) > 9) {
                $DNC_count = substr("$DNC_count", 0, -1);
                }*/
                $CC_count = sprintf("%9s", "$CC_count");
                /*while (strlen($CC_count) > 9) {
                $CC_count = substr("$CC_count", 0, -1);
                }*/
                $NI_count = sprintf("%9s", "$NI_count");
                /*while (strlen($NI_count) > 9) {
                $NI_count = substr("$NI_count", 0, -1);
                }*/
                $UW_count = sprintf("%9s", "$UW_count");
                /*while (strlen($UW_count) > 9) {
                $UW_count = substr("$UW_count", 0, -1);
                }*/
                $SC_count = sprintf("%9s", "$SC_count");
                /*while (strlen($SC_count) > 9) {
                $SC_count = substr("$SC_count", 0, -1);
                }*/
                $COMP_count = sprintf("%9s", "$COMP_count");
                /*while (strlen($COMP_count) > 9) {
                $COMP_count = substr("$COMP_count", 0, -1);
                }*/

                $sal["detalle"][$i]["respuestaHumana"]["registros"] = $HA_count;
                $sal["detalle"][$i]["respuestaHumana"][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $HA_percent;

                $sal["detalle"][$i]["venta"]["registros"] = $SALE_count;
                $sal["detalle"][$i]["venta"][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $SALE_percent;

                $sal["detalle"][$i]["DNC"]["registros"] = $DNC_count;
                $sal["detalle"][$i]["DNC"][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $DNC_percent;

                $sal["detalle"][$i]["contactoCliente"]["registros"] = $CC_count;
                $sal["detalle"][$i]["contactoCliente"][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $CC_percent;

                $sal["detalle"][$i]["noInteresado"]["registros"] = $NI_count;
                $sal["detalle"][$i]["noInteresado"][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $NI_percent;

                $sal["detalle"][$i]["noTrabajable"]["registros"] = $UW_count;
                $sal["detalle"][$i]["noTrabajable"][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $UW_percent;

                $sal["detalle"][$i]["llamarDeVuelta"]["registros"] = $SC_count;
                $sal["detalle"][$i]["llamarDeVuelta"][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $SC_percent;

                $sal["detalle"][$i]["completado"]["registros"] = $COMP_count;
                $sal["detalle"][$i]["completado"][$this->lang->line("csv_resumenBanderaSistema_n_porcentaje")] = $COMP_percent;

                $stmt = "select status,count(*) from vicidial_list where list_id='$LISTIDlists[$i]' group by status order by status;";
                $rslt = sv_mysqli_query($con, $stmt);
                $liststatussum_to_print = sv_mysqli_num_rows($rslt);
                $r = 0;
                while ($r < $liststatussum_to_print) {
                    $row = sv_mysqli_fetch_array($rslt);
                    $LISTIDstatus[$r] = $row[0];
                    $LISTIDcounts[$r] = $row[1];
                    if ($row[1] > $max_status) {
                        $max_status = $row[1];
                    }
                    $r++;
                }
                $r = 0;
                $sal["detalle"][$i]["breakdown"]["Registros"] = array();
                while ($r < $liststatussum_to_print) {
                    $LIDstatus = $LISTIDstatus[$r];
                    $LIDstatus_format = sprintf("%6s", $LIDstatus);
                    $TOTALleads = ($TOTALleads + $LISTIDcounts[$r]);
                    $LISTID_status_count = sprintf("%10s", $LISTIDcounts[$r]);
                    /*while (strlen($LISTID_status_count) > 10) {
                    $LISTID_status_count = substr("$LISTID_status_count", 0, -1);
                    }*/
                    if (array_key_exists($LIDstatus, $statname_list)) {
                        $LISTIDname = sprintf("%-42s", "$LIDstatus_format | $statname_list[$LIDstatus]");
                    } else {
                        $LISTIDname = sprintf("%-42s", "$LIDstatus_format");
                    }
                    /*while (strlen($LISTIDname) > 42) {
                    $LISTIDname = substr("$LISTIDname", 0, -1);
                    }*/
                    //$OUToutput .= "     | $LISTIDname | $LISTID_status_count |\n";
                    $sal["detalle"][$i]["breakdown"]["Registros"][$r]["status"] = $LISTIDname;
                    $sal["detalle"][$i]["breakdown"]["Registros"][$r]["cantidad"] = $LISTID_status_count;
                    $r++;
                }
                $TOTALleads = sprintf("%10s", $TOTALleads);
                $sal["detalle"][$i]["breakdown"]["total"] = $TOTALleads;
                $i++;
            }
            sv_mysqli_close($con);
            if (!isset($_GET["csv"])) {
                $sal["Estado"] = "ok";
                $this->output->set_output(json_encode($sal));
            } else {
                $seccion = $_GET["seccion"];
                $arregloCSV = array();
                $titulo = "";
                if ($seccion == "1") {
                    $arregloCSV = $sal[$this->lang->line("csv_resumenIdLista")];
                    $titulo = $this->lang->line("csv_resumenIdLista");
                } else if ($seccion == 2) {
                    $arregloCSV = $sal[$this->lang->line("csv_resumenBanderaSistema")];
                    $titulo = $this->lang->line("csv_resumenBanderaSistema");
                } else if ($seccion == 3) {
                    $arregloCSV = $sal[$this->lang->line("csv_categoriasPersonalizadas")];
                    $titulo = $this->lang->line("csv_categoriasPersonalizadas");
                } else if ($seccion == 4) {
                    $arregloCSV = $sal["respuestaHumana"];
                }
                header("Content-type: text/csv");
                header("Content-Disposition: attachment; filename=\"ReporteListaFecha - " . $titulo . ".csv\"");
                header("Pragma: no-cache");
                header("Expires: 0");
                $out = fopen('php://output', 'w');
                $test = array();
                $this->trata($out, $arregloCSV, $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('reporteListaFecha/head');
        $data["lista"] = null;
        if (isset($_GET["lista"])) {
            $data["lista"] = $_GET["lista"];
        }
        $this->load->view('reporteListaFecha/vw_reporte_lista_fecha', $data);
        $this->load->view('reporteListaFecha/foot');
    }
}
