<?php

namespace App\Http\Controllers;

// Controladores
use App\Http\Controllers\Controller;
use App\Http\Controllers\AnalitoEP15Controller;
use App\Http\Controllers\AnalitoController;
use App\Http\Controllers\NivelEP15Controller;

// Modelos
use App\ResultadoEP15;
use Illuminate\Support\Facades\DB;

// Requests
use Illuminate\Http\Request;
use Auth;

// Historicos
use App\Http\Historico\ResultadoEP15Historico;
use App\SuperUsuario;
use phpDocumentor\Reflection\Types\Integer;

//excel library
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;


class ResultadoEP15Controller extends Controller
{

    protected $tableResultadoEP15;
    protected $historico;

    public function __construct()
    {
        $this->tableResultadoEP15 = new ResultadoEP15();
    }

    public function informe(Request $req)
    {
        $analitoEP15 = AnalitoEP15Controller::listItem($req->id_reference);
        $obj_json = $this->valoresEstadisticosEP15($req->id_reference);
        $niveles = NivelEP15Controller::listByAnalitoEP15($req->id_reference);
        $cliente = SuperUsuario::query()->where('id', Auth::user()->super_admin_id)->first();

        return view("resultadoEP15.resultadoEP15InformeFinal")
            ->with("obj_json", $obj_json)
            ->with("niveles", $niveles)
            ->with("cliente", $cliente)
            ->with("analitoEP15", $analitoEP15);
    }

    public function informeResumen(Request $req)
    {
        $analitoEP15 = AnalitoEP15Controller::listItem($req->id_reference);
        $obj_json = $this->valoresEstadisticosEP15($req->id_reference);
        $niveles = NivelEP15Controller::listByAnalitoEP15($req->id_reference);
        $cliente = SuperUsuario::query()->where('id', Auth::user()->super_admin_id)->first();

        return view("resultadoEP15.resultadoEP15InformeResumen")
            ->with("obj_json", $obj_json)
            ->with("niveles", $niveles)
            ->with("cliente", $cliente)
            ->with("analitoEP15", $analitoEP15);
    }

    public function informePrueba(Request $req)
    {
        $analitoEP15 = AnalitoEP15Controller::listItem($req->id_reference);
        $obj_json = $this->valoresEstadisticosEP15($req->id_reference);
        $niveles = NivelEP15Controller::listByAnalitoEP15($req->id_reference);
        $cliente = SuperUsuario::query()->where('id', Auth::user()->super_admin_id)->first();

        return view("resultadoEP15.resultadoEP15InformeFinalPrueba")
            ->with("obj_json", $obj_json)
            ->with("niveles", $niveles)
            ->with("cliente", $cliente)
            ->with("analitoEP15", $analitoEP15);
    }

    public function informeResumenPdf(Request $req)
    {
        $analitoEP15 = AnalitoEP15Controller::listItem($req->id_reference);
        $obj_json = $this->valoresEstadisticosEP15($req->id_reference);
        $niveles = NivelEP15Controller::listByAnalitoEP15($req->id_reference);
        $cliente = SuperUsuario::query()->where('id', Auth::user()->super_admin_id)->first();

        return view("resultadoEP15.resultadoEP15InformeResumenPdf")
            ->with("obj_json", $obj_json)
            ->with("niveles", $niveles)
            ->with("cliente", $cliente)
            ->with("analitoEP15", $analitoEP15);
    }


    public function getResultadosJSON(Request $req)
    {
        $resultados = array();
        $num_niveles = AnalitoEP15Controller::listItem($req->id_reference);
        if ($num_niveles == null) {
            return response()->json($resultados);
        }
        $num_niveles = $num_niveles->num_niveles;
        for ($nivel = 1; $nivel <= $num_niveles; $nivel++) {
            $nivel_actual = AnalitoEP15Controller::analitoLevel($req->id_reference, $nivel);
            $resultados[$nivel] = self::listByAnalitoEP15($req->id_reference, $nivel);
            $resultados[$nivel][5] = $nivel_actual->valor_diana;
        }
        return response()->json($resultados);
    }

    public function is_not_zero($number)
    {
        if ($number - 0.000002 < 0) {
            return false;
        } else {
            return true;
        }
    }

    public function countResultadosLimpios($array)
    {
        $counter = 0;
        for ($i = 0; $i < count($array); $i++) {
            if ($this->is_not_zero($array[$i])) {
                $counter++;
            }
        }
        return $counter;
    }

    public function getGrubbs($numero_resultados)
    {
        $grubbs = [
            "3" => 1.155, "4" => 1.496, "5" => 1.764, "6" => 1.973, "7" => 2.139, "8" => 2.274, "9" => 2.387, "10" => 2.482, "11" => 2.564,
            "12" => 2.636, "13" => 2.699, "14" => 2.755, "15" => 2.806, "16" => 2.852, "17" => 2.894, "18" => 2.932, "19" => 2.968, "20" => 3.001,
            "21" => 3.031, "22" => 3.06, "23" => 3.087, "24" => 3.112, "25" => 3.135, "26" => 3.158, "27" => 3.179, "28" => 3.199, "29" => 3.218,
            "30" => 3.236, "31" => 3.253, "32" => 3.27, "33" => 3.286, "34" => 3.301, "35" => 3.316, "36" => 3.33, "37" => 3.343, "38" => 3.356,
            "39" => 3.369, "40" => 3.381, "41" => 3.392, "42" => 3.404, "43" => 3.415, "44" => 3.425, "45" => 3.435, "46" => 3.445, "47" => 3.455,
            "48" => 3.464, "49" => 3.474, "50" => 3.482, "51" => 3.491, "52" => 3.5, "53" => 3.508, "54" => 3.516, "55" => 3.524, "56" => 3.531,
            "57" => 3.539, "58" => 3.546, "59" => 3.553, "60" => 3.56, "61" => 3.567, "62" => 3.573, "63" => 3.58, "64" => 3.586, "65" => 3.592,
            "66" => 3.598, "67" => 3.604, "68" => 3.61, "69" => 3.616, "70" => 3.622, "71" => 3.627, "72" => 3.633, "73" => 3.638, "74" => 3.643,
            "75" => 3.648, "76" => 3.653, "77" => 3.658, "78" => 3.663, "79" => 3.668, "80" => 3.673, "81" => 3.678, "82" => 3.682, "83" => 3.687,
            "84" => 3.691, "85" => 3.695, "86" => 3.7, "87" => 3.704, "88" => 3.708, "89" => 3.712, "90" => 3.716, "91" => 3.72, "92" => 3.724,
            "93" => 3.728, "94" => 3.732, "95" => 3.736, "96" => 3.74, "97" => 3.743, "98" => 3.747, "99" => 3.75, "100" => 3.754
        ];
        if ($numero_resultados > 100) {
            return 3.754;
        }
        if ($numero_resultados < 3) {
            return 1.155;
        }
        return $grubbs[strval($numero_resultados)];

    }


    public function valoresEstadisticosEP15($id_analito_ep15)
    {
        $obj_json = array();
        $analitoEP15 = AnalitoEP15Controller::listItem($id_analito_ep15);
        $niveles = NivelEP15Controller::listByAnalitoEP15($id_analito_ep15);
        for ($i = 1; $i <= $analitoEP15->num_niveles; $i++) {
            foreach ($niveles as $nivel_bd) {
                if ($nivel_bd->nivel == $i) {
                    $nivel_act = $nivel_bd;
                }
            }

            $resultados_nivel = $this->listByAnalitoEP15($id_analito_ep15, $i);
            $resultados_limpios_nvl = $this->resultadosLimipiosNivel($resultados_nivel);
            $corridas = array();
            $n_datos = $this->countResultadosLimpios($resultados_limpios_nvl);
            $corridas["general"] = [
                "n_datos" => $n_datos,
                "gran_media" => $this->stats_average($resultados_limpios_nvl),
                "gran_de" => $this->stats_standard_deviation($resultados_limpios_nvl),
                "df1" => round($n_datos / 5) - 1,
                "df2" => $n_datos - round($n_datos / 5)
            ];
            $FACTOR_GRUBBS = $this->getGrubbs($n_datos);
            $corridas["general"]["factor_grubbs"] = $FACTOR_GRUBBS;
            $corridas["general"]["limite_inferior_grubbs"] = +$corridas["general"]["gran_media"] - ($FACTOR_GRUBBS * $corridas["general"]["gran_de"]);
            $corridas["general"]["limite_superior_grubbs"] = +$corridas["general"]["gran_media"] + ($FACTOR_GRUBBS * $corridas["general"]["gran_de"]);;

            foreach ($resultados_nivel as $resultado_nivel) {

                $resultados_limpios = $this->resultadosLimipiosCorrida($resultado_nivel, $corridas["general"]["limite_inferior_grubbs"], $corridas["general"]["limite_superior_grubbs"]);
                $promedioCorrida = $this->stats_average($resultados_limpios);
                $dia_menos_gran_media = $this->is_not_zero($promedioCorrida) ? $promedioCorrida - $corridas["general"]["gran_media"] : 0;
                $corridas[$resultado_nivel->num_corrida] = [
                    "fecha" => $resultado_nivel->fecha,
                    "concentracion_1" => $resultado_nivel->concentracion_1,
                    "concentracion_2" => $resultado_nivel->concentracion_2,
                    "concentracion_3" => $resultado_nivel->concentracion_3,
                    "concentracion_4" => $resultado_nivel->concentracion_4,
                    "concentracion_5" => $resultado_nivel->concentracion_5,
                    "promedio" => $promedioCorrida,
                    "de" => $this->stats_standard_deviation($resultados_limpios),
                    "dia_menos_gran_media" => $dia_menos_gran_media,
                    "dia_menos_gran_media_al_cuadrado" => pow($dia_menos_gran_media, 2),
                    "concentracion_1_menos_dia_al_2" => ($resultado_nivel->concentracion_1 == null) ? null : pow(($resultado_nivel->concentracion_1 - $promedioCorrida), 2),
                    "concentracion_2_menos_dia_al_2" => ($resultado_nivel->concentracion_2 == null) ? null : pow(($resultado_nivel->concentracion_2 - $promedioCorrida), 2),
                    "concentracion_3_menos_dia_al_2" => ($resultado_nivel->concentracion_3 == null) ? null : pow(($resultado_nivel->concentracion_3 - $promedioCorrida), 2),
                    "concentracion_4_menos_dia_al_2" => ($resultado_nivel->concentracion_4 == null) ? null : pow(($resultado_nivel->concentracion_4 - $promedioCorrida), 2),
                    "concentracion_5_menos_dia_al_2" => ($resultado_nivel->concentracion_5 == null) ? null : pow(($resultado_nivel->concentracion_5 - $promedioCorrida), 2),
                    "promedio_diferencias_por_5" => $this->promedioDiferenciasX5Corrida($resultados_limpios, $promedioCorrida),
                    "suma_diferencias" => $this->sumaDiferenciasCorrida($resultados_limpios, $promedioCorrida)
                ];
            }
            if(isset($corridas["1"])){
                $corridas["general"]["ss1"] = round($n_datos / 5) * array_sum([
                    $corridas["1"]["dia_menos_gran_media_al_cuadrado"],
                    $corridas["2"]["dia_menos_gran_media_al_cuadrado"],
                    $corridas["3"]["dia_menos_gran_media_al_cuadrado"],
                    $corridas["4"]["dia_menos_gran_media_al_cuadrado"],
                    $corridas["5"]["dia_menos_gran_media_al_cuadrado"]
                ]);
                $corridas["general"]["ss2"] = array_sum([
                    $corridas["1"]["suma_diferencias"],
                    $corridas["2"]["suma_diferencias"],
                    $corridas["3"]["suma_diferencias"],
                    $corridas["4"]["suma_diferencias"],
                    $corridas["5"]["suma_diferencias"]
                ]);
                $corridas["general"]["ss_total"] = $corridas["general"]["ss1"] + $corridas["general"]["ss2"];
                try{
                    $corridas["general"]["ms1"] = $corridas["general"]["ss1"] / $corridas["general"]["df1"];
                }catch(\Exception $e){
                    $corridas["general"]["ms1"] = 0;
                }
                $corridas["general"]["ms2"] = $corridas["general"]["ss2"] / $corridas["general"]["df2"];
                $corridas["general"]["vb"] = ($corridas["general"]["ms1"] - $corridas["general"]["ms2"]) / $this->getfactorNsubCero($corridas["general"]["n_datos"]);
                $corridas["general"]["n0"] = $this->getfactorNsubCero($corridas["general"]["n_datos"]);
                $corridas["general"]["sr"] = sqrt($corridas["general"]["ms2"]);
                $corridas["general"]["swl"] = sqrt($corridas["general"]["ms2"] + $corridas["general"]["vb"]);
    
                // Errores estandar
                $corridas["general"]["se_x"] = sqrt((1 / 5) * (pow($corridas["general"]["swl"], 2) - (4 / 5) * pow($corridas["general"]["sr"], 2)));
                $corridas["general"]["se_rm_inserto"] = (($nivel_act->k == 0) ? null : ($nivel_act->u_incertidumbre / $nivel_act->k));
    
                if($nivel_act->n_lab_gpar != 0){
                    $corridas["general"]["se_rm_gpar"] = $nivel_act->de_gpar / sqrt($nivel_act->n_lab_gpar);
                } else {
                    $corridas["general"]["se_rm_gpar"] = 0;
                }
    
                try{
                    $corridas["general"]["calculo_tau"] = $corridas["general"]["se_rm_inserto"] / $corridas["general"]["se_x"];
                }
                catch(\Exception $e){
                    $corridas["general"]["calculo_tau"] = 0;
                }
                $corridas["general"]["se_c_inserto"] = sqrt(pow($corridas["general"]["se_x"], 2) + pow($corridas["general"]["se_rm_inserto"], 2));
                $corridas["general"]["se_c_gpar"] = sqrt(pow($corridas["general"]["se_x"], 2) + pow($corridas["general"]["se_rm_gpar"], 2));
    
                $corridas["general"]["t"] = 1 - (0.025 / $analitoEP15->num_niveles);
    
                $corridas["general"]["df_x"] = (round($n_datos / 5) - 1);
                try{
                    $corridas["general"]["dfc_inserto"] = round($corridas["general"]["df_x"], 2) * (pow(($corridas["general"]["se_c_inserto"] / $corridas["general"]["se_x"]), 4));
                } catch (\Exception $e){
                    $corridas["general"]["dfc_inserto"] = round($corridas["general"]["df_x"], 2) * 0;
                }
                $corridas["general"]["dfc_gpar"] = $this->get_df_c_gpar($corridas["general"]["calculo_tau"], $nivel_act->n_lab_gpar);
    
                $corridas["general"]["m_inserto"] = $this->getM(count($niveles), $corridas["general"]["t"], $corridas["general"]["dfc_inserto"]);
                $corridas["general"]["m_gpar"] = $this->getM(count($niveles), $corridas["general"]["t"], $corridas["general"]["dfc_gpar"]);
    
                // Calculos relacionados con la seccion UVL
                $corridas["general"]["p"] = (($nivel_act->sigma_r == 0) ? null : $nivel_act->sigma_wl / $nivel_act->sigma_r);
                $corridas["general"]["df_r"] = $corridas["general"]["n_datos"] - round($n_datos / 5);
                $corridas["general"]["df_wl"] = $this->get_df_wl($corridas["general"]["p"]);
                $corridas["general"]["factor_uvl_r"] = $this->getFactorUVL($corridas["general"]["df_r"], $analitoEP15->num_niveles);
                $corridas["general"]["factor_uvl_wl"] = $this->getFactorUVL($corridas["general"]["df_wl"], $analitoEP15->num_niveles);
                $corridas["general"]["uvl_repetibilidad"] = floatval($corridas["general"]["factor_uvl_r"]) * floatval($nivel_act->sigma_r);
                $corridas["general"]["uvl_intra_laboratorio"] = floatval($corridas["general"]["factor_uvl_wl"]) * floatval($nivel_act->sigma_wl);
    
                // Calculos relacionados con la seccion de intervalos
                $corridas["general"]["li_iv_inserto"] = $nivel_act->media_inserto - ($corridas["general"]["m_inserto"] * $corridas["general"]["se_c_inserto"]);
                $corridas["general"]["ls_iv_inserto"] = $nivel_act->media_inserto + ($corridas["general"]["m_inserto"] * $corridas["general"]["se_c_inserto"]);
                $corridas["general"]["li_iv_gpar"] = $nivel_act->media_gpar - ($corridas["general"]["m_gpar"] * $corridas["general"]["se_c_gpar"]);
                $corridas["general"]["ls_iv_gpar"] = $nivel_act->media_gpar + ($corridas["general"]["m_gpar"] * $corridas["general"]["se_c_gpar"]);
    
                // Calculos relacionados con la seccion SESGO
                $corridas["general"]["sesgo_porcentaje_inserto"] = (($nivel_act->media_inserto == 0) ? null : (($corridas["general"]["gran_media"] - $nivel_act->media_inserto) / $nivel_act->media_inserto) * 100);
                $corridas["general"]["sesgo_unidad_inserto"] = $corridas["general"]["gran_media"] - $nivel_act->media_inserto;
                $corridas["general"]["sesgo_porcentaje_gpar"] = (($nivel_act->media_gpar == 0) ? null : (($corridas["general"]["gran_media"] - $nivel_act->media_gpar) / $nivel_act->media_gpar) * 100);
                $corridas["general"]["sesgo_unidad_gpar"] = $corridas["general"]["gran_media"] - $nivel_act->media_gpar;

                $nivel = [
                    "id_nivel" => $nivel_act->id_nivel,
                    "nivel" => $nivel_act->nivel,
                    "sigma_r" => $nivel_act->sigma_r,
                    "sigma_wl" => $nivel_act->sigma_wl,
                    "smp_porcentaje" => $nivel_act->smp_porcentaje,
                    "media_inserto" => $nivel_act->media_inserto,
                    "de_inserto" => $nivel_act->de_inserto,
                    "smp_unidad" => $nivel_act->smp_unidad,
                    "media_gpar" => $nivel_act->media_gpar,
                    "de_gpar" => $nivel_act->de_gpar,
                    "n_lab_gpar" => $nivel_act->n_lab_gpar,
                    "u_incertidumbre" => $nivel_act->u_incertidumbre,
                    "k" => $nivel_act->k,
                    "corridas" => $corridas,
                    // "cvr" => $nivel_act->cvr,
                    // "cvi" => $nivel_act->cvi,
                    // "limite_s" => $nivel_act->limite_s,
                    // "limite_i" => $nivel_act->limite_i
                ];

                $obj_json[$i] = $nivel;
            }
        }

        return $obj_json;
    }

    public function getFactorUVL($df, $num_niveles)
    {
        if ($num_niveles == 1) {
            $correlacion_uvl = array(
                array("df" => "5", "factor_uvl" => "1.49"),
                array("df" => "6", "factor_uvl" => "1.45"),
                array("df" => "7", "factor_uvl" => "1.42"),
                array("df" => "8", "factor_uvl" => "1.39"),
                array("df" => "9", "factor_uvl" => "1.37"),
                array("df" => "10", "factor_uvl" => "1.35"),
                array("df" => "11", "factor_uvl" => "1.34"),
                array("df" => "12", "factor_uvl" => "1.32"),
                array("df" => "13", "factor_uvl" => "1.31"),
                array("df" => "14", "factor_uvl" => "1.30"),
                array("df" => "15", "factor_uvl" => "1.29"),
                array("df" => "16", "factor_uvl" => "1.28"),
                array("df" => "17", "factor_uvl" => "1.27"),
                array("df" => "18", "factor_uvl" => "1.27"),
                array("df" => "19", "factor_uvl" => "1.26"),
                array("df" => "20", "factor_uvl" => "1.25"),
                array("df" => "21", "factor_uvl" => "1.25"),
                array("df" => "22", "factor_uvl" => "1.24"),
                array("df" => "23", "factor_uvl" => "1.24"),
                array("df" => "24", "factor_uvl" => "1.23"),
                array("df" => "25", "factor_uvl" => "1.23"),
                array("df" => "26", "factor_uvl" => "1.22"),
                array("df" => "27", "factor_uvl" => "1.22"),
                array("df" => "28", "factor_uvl" => "1.22"),
                array("df" => "29", "factor_uvl" => "1.21"),
                array("df" => "30", "factor_uvl" => "1.21"),
                array("df" => "31", "factor_uvl" => "1.20"),
                array("df" => "32", "factor_uvl" => "1.20"),
                array("df" => "33", "factor_uvl" => "1.20"),
                array("df" => "34", "factor_uvl" => "1.20")
            );
        } else if ($num_niveles == 2) {
            $correlacion_uvl = array(
                array("df" => "5", "factor_uvl" => "1.60"),
                array("df" => "6", "factor_uvl" => "1.55"),
                array("df" => "7", "factor_uvl" => "1.51"),
                array("df" => "8", "factor_uvl" => "1.48"),
                array("df" => "9", "factor_uvl" => "1.45"),
                array("df" => "10", "factor_uvl" => "1.43"),
                array("df" => "11", "factor_uvl" => "1.41"),
                array("df" => "12", "factor_uvl" => "1.39"),
                array("df" => "13", "factor_uvl" => "1.38"),
                array("df" => "14", "factor_uvl" => "1.37"),
                array("df" => "15", "factor_uvl" => "1.35"),
                array("df" => "16", "factor_uvl" => "1.34"),
                array("df" => "17", "factor_uvl" => "1.33"),
                array("df" => "18", "factor_uvl" => "1.32"),
                array("df" => "19", "factor_uvl" => "1.31"),
                array("df" => "20", "factor_uvl" => "1.31"),
                array("df" => "21", "factor_uvl" => "1.30"),
                array("df" => "22", "factor_uvl" => "1.29"),
                array("df" => "23", "factor_uvl" => "1.29"),
                array("df" => "24", "factor_uvl" => "1.28"),
                array("df" => "25", "factor_uvl" => "1.28"),
                array("df" => "26", "factor_uvl" => "1.27"),
                array("df" => "27", "factor_uvl" => "1.26"),
                array("df" => "28", "factor_uvl" => "1.26"),
                array("df" => "29", "factor_uvl" => "1.26"),
                array("df" => "30", "factor_uvl" => "1.25"),
                array("df" => "31", "factor_uvl" => "1.25"),
                array("df" => "32", "factor_uvl" => "1.24"),
                array("df" => "33", "factor_uvl" => "1.24"),
                array("df" => "34", "factor_uvl" => "1.24")
            );
        } else if ($num_niveles == 3) {
            $correlacion_uvl = array(
                array("df" => "5", "factor_uvl" => "1.66"),
                array("df" => "6", "factor_uvl" => "1.61"),
                array("df" => "7", "factor_uvl" => "1.56"),
                array("df" => "8", "factor_uvl" => "1.53"),
                array("df" => "9", "factor_uvl" => "1.5O"),
                array("df" => "10", "factor_uvl" => "1.47"),
                array("df" => "11", "factor_uvl" => "1.45"),
                array("df" => "12", "factor_uvl" => "1.43"),
                array("df" => "13", "factor_uvl" => "1.42"),
                array("df" => "14", "factor_uvl" => "1.40"),
                array("df" => "15", "factor_uvl" => "1.39"),
                array("df" => "16", "factor_uvl" => "1.38"),
                array("df" => "17", "factor_uvl" => "1.36"),
                array("df" => "18", "factor_uvl" => "1.35"),
                array("df" => "19", "factor_uvl" => "1.34"),
                array("df" => "20", "factor_uvl" => "1.34"),
                array("df" => "21", "factor_uvl" => "1.33"),
                array("df" => "22", "factor_uvl" => "1.32"),
                array("df" => "23", "factor_uvl" => "1.31"),
                array("df" => "24", "factor_uvl" => "1.31"),
                array("df" => "25", "factor_uvl" => "1.30"),
                array("df" => "26", "factor_uvl" => "1.30"),
                array("df" => "27", "factor_uvl" => "1.29"),
                array("df" => "28", "factor_uvl" => "1.28"),
                array("df" => "29", "factor_uvl" => "1.28"),
                array("df" => "30", "factor_uvl" => "1.27"),
                array("df" => "31", "factor_uvl" => "1.27"),
                array("df" => "32", "factor_uvl" => "1.27"),
                array("df" => "33", "factor_uvl" => "1.26"),
                array("df" => "34", "factor_uvl" => "1.26")
            );
        } else if ($num_niveles == 4) {
            $correlacion_uvl = array(
                array("df" => "5", "factor_uvl" => "1.71"),
                array("df" => "6", "factor_uvl" => "1.65"),
                array("df" => "7", "factor_uvl" => "1.60"),
                array("df" => "8", "factor_uvl" => "1.56"),
                array("df" => "9", "factor_uvl" => "1.53"),
                array("df" => "10", "factor_uvl" => "1.50"),
                array("df" => "11", "factor_uvl" => "1.48"),
                array("df" => "12", "factor_uvl" => "1.46"),
                array("df" => "13", "factor_uvl" => "1.44"),
                array("df" => "14", "factor_uvl" => "1.42"),
                array("df" => "15", "factor_uvl" => "1.41"),
                array("df" => "16", "factor_uvl" => "1.40"),
                array("df" => "17", "factor_uvl" => "1.39"),
                array("df" => "18", "factor_uvl" => "1.37"),
                array("df" => "19", "factor_uvl" => "1.36"),
                array("df" => "20", "factor_uvl" => "1.36"),
                array("df" => "21", "factor_uvl" => "1.35"),
                array("df" => "22", "factor_uvl" => "1.34"),
                array("df" => "23", "factor_uvl" => "1.33"),
                array("df" => "24", "factor_uvl" => "1.32"),
                array("df" => "25", "factor_uvl" => "1.32"),
                array("df" => "26", "factor_uvl" => "1.31"),
                array("df" => "27", "factor_uvl" => "1.31"),
                array("df" => "28", "factor_uvl" => "1.30"),
                array("df" => "29", "factor_uvl" => "1.30"),
                array("df" => "30", "factor_uvl" => "1.29"),
                array("df" => "31", "factor_uvl" => "1.29"),
                array("df" => "32", "factor_uvl" => "1.28"),
                array("df" => "33", "factor_uvl" => "1.28"),
                array("df" => "34", "factor_uvl" => "1.27")
            );
        } else if ($num_niveles == 5) {
            $correlacion_uvl = array(
                array("df" => "5", "factor_uvl" => "1.74"),
                array("df" => "6", "factor_uvl" => "1.67"),
                array("df" => "7", "factor_uvl" => "1.62"),
                array("df" => "8", "factor_uvl" => "1.58"),
                array("df" => "9", "factor_uvl" => "1.55"),
                array("df" => "10", "factor_uvl" => "1.52"),
                array("df" => "11", "factor_uvl" => "1.50"),
                array("df" => "12", "factor_uvl" => "1.48"),
                array("df" => "13", "factor_uvl" => "1.46"),
                array("df" => "14", "factor_uvl" => "1.44"),
                array("df" => "15", "factor_uvl" => "1.43"),
                array("df" => "16", "factor_uvl" => "1.41"),
                array("df" => "17", "factor_uvl" => "1.40"),
                array("df" => "18", "factor_uvl" => "1.39"),
                array("df" => "19", "factor_uvl" => "1.38"),
                array("df" => "20", "factor_uvl" => "1.37"),
                array("df" => "21", "factor_uvl" => "1.36"),
                array("df" => "22", "factor_uvl" => "1.35"),
                array("df" => "23", "factor_uvl" => "1.35"),
                array("df" => "24", "factor_uvl" => "1.34"),
                array("df" => "25", "factor_uvl" => "1.33"),
                array("df" => "26", "factor_uvl" => "1.32"),
                array("df" => "27", "factor_uvl" => "1.32"),
                array("df" => "28", "factor_uvl" => "1.31"),
                array("df" => "29", "factor_uvl" => "1.31"),
                array("df" => "30", "factor_uvl" => "1.30"),
                array("df" => "31", "factor_uvl" => "1.30"),
                array("df" => "32", "factor_uvl" => "1.29"),
                array("df" => "33", "factor_uvl" => "1.29"),
                array("df" => "34", "factor_uvl" => "1.28")
            );
        } else if ($num_niveles == 6) {
            $correlacion_uvl = array(
                array("df" => "5", "factor_uvl" => "1.76"),
                array("df" => "6", "factor_uvl" => "1.70"),
                array("df" => "7", "factor_uvl" => "1.65"),
                array("df" => "8", "factor_uvl" => "1.60"),
                array("df" => "9", "factor_uvl" => "1.57"),
                array("df" => "10", "factor_uvl" => "1.54"),
                array("df" => "11", "factor_uvl" => "1.52"),
                array("df" => "12", "factor_uvl" => "1.49"),
                array("df" => "13", "factor_uvl" => "1.47"),
                array("df" => "14", "factor_uvl" => "1.46"),
                array("df" => "15", "factor_uvl" => "1.44"),
                array("df" => "16", "factor_uvl" => "1.43"),
                array("df" => "17", "factor_uvl" => "1.41"),
                array("df" => "18", "factor_uvl" => "1.40"),
                array("df" => "19", "factor_uvl" => "1.39"),
                array("df" => "20", "factor_uvl" => "1.38"),
                array("df" => "21", "factor_uvl" => "1.37"),
                array("df" => "22", "factor_uvl" => "1.36"),
                array("df" => "23", "factor_uvl" => "1.36"),
                array("df" => "24", "factor_uvl" => "1.35"),
                array("df" => "25", "factor_uvl" => "1.34"),
                array("df" => "26", "factor_uvl" => "1.34"),
                array("df" => "27", "factor_uvl" => "1.33"),
                array("df" => "28", "factor_uvl" => "1.32"),
                array("df" => "29", "factor_uvl" => "1.32"),
                array("df" => "30", "factor_uvl" => "1.31"),
                array("df" => "31", "factor_uvl" => "1.31"),
                array("df" => "32", "factor_uvl" => "1.30"),
                array("df" => "33", "factor_uvl" => "1.30"),
                array("df" => "34", "factor_uvl" => "1.29")
            );
        }

        foreach ($correlacion_uvl as $cu) {
            if (floatval($df) <= floatval($cu["df"])) {
                return $cu["factor_uvl"];
            }
        }
        return null;
    }


    public function getM($niveles, $t, $dfc)
    {
        if($niveles <= 2){
            return 1.96;
        }else if($niveles >= 3){
            return 2.78;
        }
        //cambiar
        // if ($t >= 0.995) {
        //     $correlacion_m = array(
        //         array("dfc" => "1", "m" => "63.6567"),
        //         array("dfc" => "2", "m" => "99.248"),
        //         array("dfc" => "3", "m" => "58.409"),
        //         array("dfc" => "4", "m" => "46.041"),
        //         array("dfc" => "5", "m" => "40.321"),
        //         array("dfc" => "6", "m" => "37.074"),
        //         array("dfc" => "7", "m" => "34.995"),
        //         array("dfc" => "8", "m" => "33.554"),
        //         array("dfc" => "9", "m" => "32.498"),
        //         array("dfc" => "10", "m" => "31.693"),
        //         array("dfc" => "11", "m" => "31.058"),
        //         array("dfc" => "12", "m" => "30.545"),
        //         array("dfc" => "13", "m" => "30.123"),
        //         array("dfc" => "14", "m" => "29.768"),
        //         array("dfc" => "15", "m" => "29.467"),
        //         array("dfc" => "16", "m" => "29.208"),
        //         array("dfc" => "17", "m" => "28.982"),
        //         array("dfc" => "18", "m" => "28.784"),
        //         array("dfc" => "19", "m" => "28.609"),
        //         array("dfc" => "20", "m" => "28.453"),
        //         array("dfc" => "21", "m" => "28.314"),
        //         array("dfc" => "22", "m" => "28.188"),
        //         array("dfc" => "23", "m" => "28.073"),
        //         array("dfc" => "24", "m" => "27.969"),
        //         array("dfc" => "25", "m" => "27.874"),
        //         array("dfc" => "26", "m" => "27.787"),
        //         array("dfc" => "27", "m" => "27.707"),
        //         array("dfc" => "28", "m" => "27.633"),
        //         array("dfc" => "29", "m" => "27.564"),
        //         array("dfc" => "30", "m" => "27.5"),
        //         array("dfc" => "40", "m" => "27.045"),
        //         array("dfc" => "50", "m" => "26.778"),
        //         array("dfc" => "60", "m" => "26.603"),
        //         array("dfc" => "80", "m" => "26.387"),
        //         array("dfc" => "100", "m" => "26.259"),
        //         array("dfc" => "120", "m" => "26.174"),
        //         array("dfc" => "9999999999999999999999999999999999999999999999999999", "m" => "2.576")
        //     );
        // } else if ($t >= 0.99) {
        //     $correlacion_m = array(
        //         array("dfc" => "1", "m" => "31.8205"),
        //         array("dfc" => "2", "m" => "69.646"),
        //         array("dfc" => "3", "m" => "45.407"),
        //         array("dfc" => "4", "m" => "37.469"),
        //         array("dfc" => "5", "m" => "33.649"),
        //         array("dfc" => "6", "m" => "31.427"),
        //         array("dfc" => "7", "m" => "29.98"),
        //         array("dfc" => "8", "m" => "28.965"),
        //         array("dfc" => "9", "m" => "28.214"),
        //         array("dfc" => "10", "m" => "27.638"),
        //         array("dfc" => "11", "m" => "27.181"),
        //         array("dfc" => "12", "m" => "26.81"),
        //         array("dfc" => "13", "m" => "26.503"),
        //         array("dfc" => "14", "m" => "26.245"),
        //         array("dfc" => "15", "m" => "26.025"),
        //         array("dfc" => "16", "m" => "25.835"),
        //         array("dfc" => "17", "m" => "25.669"),
        //         array("dfc" => "18", "m" => "25.524"),
        //         array("dfc" => "19", "m" => "25.395"),
        //         array("dfc" => "20", "m" => "25.28"),
        //         array("dfc" => "21", "m" => "25.176"),
        //         array("dfc" => "22", "m" => "25.083"),
        //         array("dfc" => "23", "m" => "24.999"),
        //         array("dfc" => "24", "m" => "24.922"),
        //         array("dfc" => "25", "m" => "24.851"),
        //         array("dfc" => "26", "m" => "24.786"),
        //         array("dfc" => "27", "m" => "24.727"),
        //         array("dfc" => "28", "m" => "24.671"),
        //         array("dfc" => "29", "m" => "24.62"),
        //         array("dfc" => "30", "m" => "24.573"),
        //         array("dfc" => "40", "m" => "24.233"),
        //         array("dfc" => "50", "m" => "2.4033"),
        //         array("dfc" => "60", "m" => "23.901"),
        //         array("dfc" => "80", "m" => "23.739"),
        //         array("dfc" => "100", "m" => "23.642"),
        //         array("dfc" => "120", "m" => "23.578"),
        //         array("dfc" => "9999999999999999999999999999999999999999999999999999", "m" => "2.327")
        //     );
        // } else if ($t >= 0.975) {
        //     $correlacion_m = array(
        //         array("dfc" => "1", "m" => "12.7062"),
        //         array("dfc" => "2", "m" => "43.027"),
        //         array("dfc" => "3", "m" => "31.824"),
        //         array("dfc" => "4", "m" => "27.764"),
        //         array("dfc" => "5", "m" => "25.706"),
        //         array("dfc" => "6", "m" => "24.469"),
        //         array("dfc" => "7", "m" => "23.646"),
        //         array("dfc" => "8", "m" => "23.06"),
        //         array("dfc" => "9", "m" => "22.622"),
        //         array("dfc" => "10", "m" => "22.281"),
        //         array("dfc" => "11", "m" => "22.01"),
        //         array("dfc" => "12", "m" => "21.788"),
        //         array("dfc" => "13", "m" => "21.604"),
        //         array("dfc" => "14", "m" => "21.448"),
        //         array("dfc" => "15", "m" => "21.314"),
        //         array("dfc" => "16", "m" => "21.199"),
        //         array("dfc" => "17", "m" => "21.098"),
        //         array("dfc" => "18", "m" => "21.009"),
        //         array("dfc" => "19", "m" => "20.93"),
        //         array("dfc" => "20", "m" => "20.86"),
        //         array("dfc" => "21", "m" => "20.796"),
        //         array("dfc" => "22", "m" => "20.739"),
        //         array("dfc" => "23", "m" => "20.687"),
        //         array("dfc" => "24", "m" => "20.639"),
        //         array("dfc" => "25", "m" => "20.595"),
        //         array("dfc" => "26", "m" => "20.555"),
        //         array("dfc" => "27", "m" => "20.518"),
        //         array("dfc" => "28", "m" => "20.484"),
        //         array("dfc" => "29", "m" => "20.452"),
        //         array("dfc" => "30", "m" => "20.423"),
        //         array("dfc" => "40", "m" => "20.211"),
        //         array("dfc" => "50", "m" => "20.086"),
        //         array("dfc" => "60", "m" => "20.003"),
        //         array("dfc" => "80", "m" => "19.901"),
        //         array("dfc" => "100", "m" => "19.84"),
        //         array("dfc" => "120", "m" => "19.799"),
        //         array("dfc" => "9999999999999999999999999999999999999999999999999999", "m" => "1.96")
        //     );
        // } else if ($t >= 0.95) {
        //     $correlacion_m = array(
        //         array("dfc" => "1", "m" => "6.3138"),
        //         array("dfc" => "2", "m" => "29.2"),
        //         array("dfc" => "3", "m" => "23.534"),
        //         array("dfc" => "4", "m" => "21.318"),
        //         array("dfc" => "5", "m" => "20.15"),
        //         array("dfc" => "6", "m" => "19.432"),
        //         array("dfc" => "7", "m" => "18.946"),
        //         array("dfc" => "8", "m" => "18.595"),
        //         array("dfc" => "9", "m" => "18.331"),
        //         array("dfc" => "10", "m" => "18.125"),
        //         array("dfc" => "11", "m" => "17.959"),
        //         array("dfc" => "12", "m" => "17.823"),
        //         array("dfc" => "13", "m" => "17.709"),
        //         array("dfc" => "14", "m" => "17.613"),
        //         array("dfc" => "15", "m" => "17.531"),
        //         array("dfc" => "16", "m" => "17.459"),
        //         array("dfc" => "17", "m" => "17.396"),
        //         array("dfc" => "18", "m" => "17.341"),
        //         array("dfc" => "19", "m" => "17.291"),
        //         array("dfc" => "20", "m" => "17.247"),
        //         array("dfc" => "21", "m" => "17.207"),
        //         array("dfc" => "22", "m" => "17.171"),
        //         array("dfc" => "23", "m" => "17.139"),
        //         array("dfc" => "24", "m" => "17.109"),
        //         array("dfc" => "25", "m" => "17.081"),
        //         array("dfc" => "26", "m" => "17.056"),
        //         array("dfc" => "27", "m" => "17.033"),
        //         array("dfc" => "28", "m" => "17.011"),
        //         array("dfc" => "29", "m" => "16.991"),
        //         array("dfc" => "30", "m" => "16.973"),
        //         array("dfc" => "40", "m" => "16.839"),
        //         array("dfc" => "50", "m" => "16.759"),
        //         array("dfc" => "60", "m" => "16.706"),
        //         array("dfc" => "80", "m" => "16.641"),
        //         array("dfc" => "100", "m" => "16.602"),
        //         array("dfc" => "120", "m" => "16.577"),
        //         array("dfc" => "9999999999999999999999999999999999999999999999999999", "m" => "1.645")
        //     );
        // } else if ($t >= 0.9) {
        //     $correlacion_m = array(
        //         array("dfc" => "1", "m" => "3.0777"),
        //         array("dfc" => "2", "m" => "18.856"),
        //         array("dfc" => "3", "m" => "16.377"),
        //         array("dfc" => "4", "m" => "15.332"),
        //         array("dfc" => "5", "m" => "14.759"),
        //         array("dfc" => "6", "m" => "14.398"),
        //         array("dfc" => "7", "m" => "14.149"),
        //         array("dfc" => "8", "m" => "13.968"),
        //         array("dfc" => "9", "m" => "13.83"),
        //         array("dfc" => "10", "m" => "13.722"),
        //         array("dfc" => "11", "m" => "13.634"),
        //         array("dfc" => "12", "m" => "13.562"),
        //         array("dfc" => "13", "m" => "13.502"),
        //         array("dfc" => "14", "m" => "13.45"),
        //         array("dfc" => "15", "m" => "13.406"),
        //         array("dfc" => "16", "m" => "13.368"),
        //         array("dfc" => "17", "m" => "13.334"),
        //         array("dfc" => "18", "m" => "13.304"),
        //         array("dfc" => "19", "m" => "13.277"),
        //         array("dfc" => "20", "m" => "13.253"),
        //         array("dfc" => "21", "m" => "13.232"),
        //         array("dfc" => "22", "m" => "13.212"),
        //         array("dfc" => "23", "m" => "13.195"),
        //         array("dfc" => "24", "m" => "13.178"),
        //         array("dfc" => "25", "m" => "13.163"),
        //         array("dfc" => "26", "m" => "13.15"),
        //         array("dfc" => "27", "m" => "13.137"),
        //         array("dfc" => "28", "m" => "13.125"),
        //         array("dfc" => "29", "m" => "13.114"),
        //         array("dfc" => "30", "m" => "13.104"),
        //         array("dfc" => "40", "m" => "13.031"),
        //         array("dfc" => "50", "m" => "12.987"),
        //         array("dfc" => "60", "m" => "12.958"),
        //         array("dfc" => "80", "m" => "12.922"),
        //         array("dfc" => "100", "m" => "12.901"),
        //         array("dfc" => "120", "m" => "12.886"),
        //         array("dfc" => "9999999999999999999999999999999999999999999999999999", "m" => "1.282")
        //     );
        // } else if ($t >= 0.8) {
        //     $correlacion_m = array(
        //         array("dfc" => "1", "m" => "1.3764"),
        //         array("dfc" => "2", "m" => "10.607"),
        //         array("dfc" => "3", "m" => "0.9785"),
        //         array("dfc" => "4", "m" => "0.941"),
        //         array("dfc" => "5", "m" => "0.9195"),
        //         array("dfc" => "6", "m" => "0.9057"),
        //         array("dfc" => "7", "m" => "0.896"),
        //         array("dfc" => "8", "m" => "0.8889"),
        //         array("dfc" => "9", "m" => "0.8834"),
        //         array("dfc" => "10", "m" => "0.8791"),
        //         array("dfc" => "11", "m" => "0.8755"),
        //         array("dfc" => "12", "m" => "0.8726"),
        //         array("dfc" => "13", "m" => "0.8702"),
        //         array("dfc" => "14", "m" => "0.8681"),
        //         array("dfc" => "15", "m" => "0.8662"),
        //         array("dfc" => "16", "m" => "0.8647"),
        //         array("dfc" => "17", "m" => "0.8633"),
        //         array("dfc" => "18", "m" => "0.862"),
        //         array("dfc" => "19", "m" => "0.861"),
        //         array("dfc" => "20", "m" => "0.86"),
        //         array("dfc" => "21", "m" => "0.8591"),
        //         array("dfc" => "22", "m" => "0.8583"),
        //         array("dfc" => "23", "m" => "0.8575"),
        //         array("dfc" => "24", "m" => "0.8569"),
        //         array("dfc" => "25", "m" => "0.8562"),
        //         array("dfc" => "26", "m" => "0.8557"),
        //         array("dfc" => "27", "m" => "0.8551"),
        //         array("dfc" => "28", "m" => "0.8546"),
        //         array("dfc" => "29", "m" => "0.8542"),
        //         array("dfc" => "30", "m" => "0.8538"),
        //         array("dfc" => "40", "m" => "0.8507"),
        //         array("dfc" => "50", "m" => "0.8489"),
        //         array("dfc" => "60", "m" => "0.8477"),
        //         array("dfc" => "80", "m" => "0.8461"),
        //         array("dfc" => "100", "m" => "0.8452"),
        //         array("dfc" => "120", "m" => "0.8446"),
        //         array("dfc" => "9999999999999999999999999999999999999999999999999999", "m" => "0.842")
        //     );
        // } else if ($t >= 0.7) {
        //     $correlacion_m = array(
        //         array("dfc" => "1", "m" => "0.7265"),
        //         array("dfc" => "2", "m" => "0.6172"),
        //         array("dfc" => "3", "m" => "0.5844"),
        //         array("dfc" => "4", "m" => "0.5686"),
        //         array("dfc" => "5", "m" => "0.5594"),
        //         array("dfc" => "6", "m" => "0.5534"),
        //         array("dfc" => "7", "m" => "0.5491"),
        //         array("dfc" => "8", "m" => "0.5459"),
        //         array("dfc" => "9", "m" => "0.5435"),
        //         array("dfc" => "10", "m" => "0.5415"),
        //         array("dfc" => "11", "m" => "0.5399"),
        //         array("dfc" => "12", "m" => "0.5386"),
        //         array("dfc" => "13", "m" => "0.5375"),
        //         array("dfc" => "14", "m" => "0.5366"),
        //         array("dfc" => "15", "m" => "0.5357"),
        //         array("dfc" => "16", "m" => "0.535"),
        //         array("dfc" => "17", "m" => "0.5344"),
        //         array("dfc" => "18", "m" => "0.5338"),
        //         array("dfc" => "19", "m" => "0.5333"),
        //         array("dfc" => "20", "m" => "0.5329"),
        //         array("dfc" => "21", "m" => "0.5325"),
        //         array("dfc" => "22", "m" => "0.5321"),
        //         array("dfc" => "23", "m" => "0.5317"),
        //         array("dfc" => "24", "m" => "0.5314"),
        //         array("dfc" => "25", "m" => "0.5312"),
        //         array("dfc" => "26", "m" => "0.5309"),
        //         array("dfc" => "27", "m" => "0.5306"),
        //         array("dfc" => "28", "m" => "0.5304"),
        //         array("dfc" => "29", "m" => "0.5302"),
        //         array("dfc" => "30", "m" => "0.53"),
        //         array("dfc" => "40", "m" => "0.5286"),
        //         array("dfc" => "50", "m" => "0.5278"),
        //         array("dfc" => "60", "m" => "0.5272"),
        //         array("dfc" => "80", "m" => "0.5265"),
        //         array("dfc" => "100", "m" => "0.5261"),
        //         array("dfc" => "120", "m" => "0.5258"),
        //         array("dfc" => "9999999999999999999999999999999999999999999999999999", "m" => "0.524")
        //     );
        // } else if ($t >= 0.6) {
        //     $correlacion_m = array(
        //         array("dfc" => "1", "m" => "0.3249"),
        //         array("dfc" => "2", "m" => "0.2887"),
        //         array("dfc" => "3", "m" => "0.2767"),
        //         array("dfc" => "4", "m" => "0.2707"),
        //         array("dfc" => "5", "m" => "0.2672"),
        //         array("dfc" => "6", "m" => "0.2648"),
        //         array("dfc" => "7", "m" => "0.2632"),
        //         array("dfc" => "8", "m" => "0.2619"),
        //         array("dfc" => "9", "m" => "0.261"),
        //         array("dfc" => "10", "m" => "0.2602"),
        //         array("dfc" => "11", "m" => "0.2596"),
        //         array("dfc" => "12", "m" => "0.259"),
        //         array("dfc" => "13", "m" => "0.2586"),
        //         array("dfc" => "14", "m" => "0.2582"),
        //         array("dfc" => "15", "m" => "0.2579"),
        //         array("dfc" => "16", "m" => "0.2576"),
        //         array("dfc" => "17", "m" => "0.2573"),
        //         array("dfc" => "18", "m" => "0.2571"),
        //         array("dfc" => "19", "m" => "0.2569"),
        //         array("dfc" => "20", "m" => "0.2567"),
        //         array("dfc" => "21", "m" => "0.2566"),
        //         array("dfc" => "22", "m" => "0.2564"),
        //         array("dfc" => "23", "m" => "0.2563"),
        //         array("dfc" => "24", "m" => "0.2562"),
        //         array("dfc" => "25", "m" => "0.2561"),
        //         array("dfc" => "26", "m" => "0.256"),
        //         array("dfc" => "27", "m" => "0.2559"),
        //         array("dfc" => "28", "m" => "0.2558"),
        //         array("dfc" => "29", "m" => "0.2557"),
        //         array("dfc" => "30", "m" => "0.2556"),
        //         array("dfc" => "40", "m" => "0.255"),
        //         array("dfc" => "50", "m" => "0.2547"),
        //         array("dfc" => "60", "m" => "0.2545"),
        //         array("dfc" => "80", "m" => "0.2542"),
        //         array("dfc" => "100", "m" => "0.254"),
        //         array("dfc" => "120", "m" => "0.2539"),
        //         array("dfc" => "9999999999999999999999999999999999999999999999999999", "m" => "0.253")
        //     );
        // } else { // 0.55
        //     $correlacion_m = array(
        //         array("dfc" => "1", "m" => "0.1584"),
        //         array("dfc" => "2", "m" => "0.1421"),
        //         array("dfc" => "3", "m" => "0.1366"),
        //         array("dfc" => "4", "m" => "0.1338"),
        //         array("dfc" => "5", "m" => "0.1322"),
        //         array("dfc" => "6", "m" => "0.1311"),
        //         array("dfc" => "7", "m" => "0.1303"),
        //         array("dfc" => "8", "m" => "0.1297"),
        //         array("dfc" => "9", "m" => "0.1293"),
        //         array("dfc" => "10", "m" => "0.1289"),
        //         array("dfc" => "11", "m" => "0.1286"),
        //         array("dfc" => "12", "m" => "0.1283"),
        //         array("dfc" => "13", "m" => "0.1281"),
        //         array("dfc" => "14", "m" => "0.128"),
        //         array("dfc" => "15", "m" => "0.1278"),
        //         array("dfc" => "16", "m" => "0.1277"),
        //         array("dfc" => "17", "m" => "0.1276"),
        //         array("dfc" => "18", "m" => "0.1274"),
        //         array("dfc" => "19", "m" => "0.1274"),
        //         array("dfc" => "20", "m" => "0.1273"),
        //         array("dfc" => "21", "m" => "0.1272"),
        //         array("dfc" => "22", "m" => "0.1271"),
        //         array("dfc" => "23", "m" => "0.1271"),
        //         array("dfc" => "24", "m" => "0.127"),
        //         array("dfc" => "25", "m" => "0.1269"),
        //         array("dfc" => "26", "m" => "0.1269"),
        //         array("dfc" => "27", "m" => "0.1268"),
        //         array("dfc" => "28", "m" => "0.1268"),
        //         array("dfc" => "29", "m" => "0.1268"),
        //         array("dfc" => "30", "m" => "0.1267"),
        //         array("dfc" => "40", "m" => "0.1265"),
        //         array("dfc" => "50", "m" => "0.1263"),
        //         array("dfc" => "60", "m" => "0.1262"),
        //         array("dfc" => "80", "m" => "0.1261"),
        //         array("dfc" => "100", "m" => "0.126"),
        //         array("dfc" => "120", "m" => "0.1259"),
        //         array("dfc" => "9999999999999999999999999999999999999999999999999999", "m" => "0.126")
        //     );
        // }

        // foreach ($correlacion_m as $cm) {
        //     if (floatval($dfc) <= floatval($cm["dfc"])) {
        //         return $cm["m"];
        //     }
        // }

        return null;
    }


    public function get_df_wl($p)
    {
        $correlacion_dfwl = array(
            array("p" => "2.74", "dfwl" => 5),
            array("p" => "2.06", "dfwl" => 6),
            array("p" => "1.78", "dfwl" => 7),
            array("p" => "1.62", "dfwl" => 8),
            array("p" => "1.51", "dfwl" => 9),
            array("p" => "1.43", "dfwl" => 10),
            array("p" => "1.37", "dfwl" => 11),
            array("p" => "1.32", "dfwl" => 12),
            array("p" => "1.28", "dfwl" => 13),
            array("p" => "1.24", "dfwl" => 14),
            array("p" => "1.21", "dfwl" => 15),
            array("p" => "1.19", "dfwl" => 16),
            array("p" => "1.16", "dfwl" => 17),
            array("p" => "1.14", "dfwl" => 18),
            array("p" => "1.12", "dfwl" => 19),
            array("p" => "1.1", "dfwl" => 20),
            array("p" => "1.08", "dfwl" => 21),
            array("p" => "1.05", "dfwl" => 22),
            array("p" => "1.03", "dfwl" => 23),
            array("p" => "1", "dfwl" => 24),
            array("p" => "0", "dfwl" => 24),
        );

        foreach ($correlacion_dfwl as $cd) {
            if (floatval($p) >= floatval($cd["p"])) {
                return $cd["dfwl"];
            }
        }

    }


    public function get_df_c_gpar($tau, $n_lab)
    {
        if ($n_lab >= 0 && $n_lab <= 15) { // 10 Laboratories
            $correlacion_tau = array(
                array("tau" => "0.000", "dfc" => 4),
                array("tau" => "0.349", "dfc" => 5),
                array("tau" => "0.490", "dfc" => 6),
                array("tau" => "0.600", "dfc" => 7),
                array("tau" => "0.698", "dfc" => 8),
                array("tau" => "0.791", "dfc" => 9),
                array("tau" => "0.886", "dfc" => 10),
                array("tau" => "0.991", "dfc" => 11),
                array("tau" => "1.126", "dfc" => 12),
                array("tau" => "1.500", "dfc" => 13),
                array("tau" => "2.175", "dfc" => 12),
                array("tau" => "2.832", "dfc" => 11),
                array("tau" => "4.149", "dfc" => 10),
                array("tau" => "9999999999999999999999999999999999999999999999999999", "dfc" => 9)
            );
        } else if ($n_lab >= 16 && $n_lab <= 35) { // 20 Laboratories
            $correlacion_tau = array(
                array("tau" => "0.000", "dfc" => 4),
                array("tau" => "0.346", "dfc" => 5),
                array("tau" => "0.481", "dfc" => 6),
                array("tau" => "0.582", "dfc" => 7),
                array("tau" => "0.666", "dfc" => 8),
                array("tau" => "0.739", "dfc" => 9),
                array("tau" => "0.806", "dfc" => 10),
                array("tau" => "0.869", "dfc" => 11),
                array("tau" => "0.929", "dfc" => 12),
                array("tau" => "0.987", "dfc" => 13),
                array("tau" => "1.045", "dfc" => 14),
                array("tau" => "1.104", "dfc" => 15),
                array("tau" => "1.164", "dfc" => 16),
                array("tau" => "1.227", "dfc" => 17),
                array("tau" => "1.295", "dfc" => 18),
                array("tau" => "1.369", "dfc" => 19),
                array("tau" => "1.455", "dfc" => 20),
                array("tau" => "1.561", "dfc" => 21),
                array("tau" => "1.711", "dfc" => 22),
                array("tau" => "2.179", "dfc" => 23),
                array("tau" => "3.121", "dfc" => 22),
                array("tau" => "4.070", "dfc" => 21),
                array("tau" => "5.990", "dfc" => 20),
                array("tau" => "9999999999999999999999999999999999999999999999999999", "dfc" => 19)
            );
        } else if ($n_lab >= 36 && $n_lab <= 75) { // 50 Laboratories
            $correlacion_tau = array(
                array("tau" => "0.000", "dfc" => 4),
                array("tau" => "0.344", "dfc" => 5),
                array("tau" => "0.477", "dfc" => 6),
                array("tau" => "0.573", "dfc" => 7),
                array("tau" => "0.652", "dfc" => 8),
                array("tau" => "0.718", "dfc" => 9),
                array("tau" => "0.778", "dfc" => 10),
                array("tau" => "0.831", "dfc" => 11),
                array("tau" => "0.880", "dfc" => 12),
                array("tau" => "0.925", "dfc" => 13),
                array("tau" => "0.968", "dfc" => 14),
                array("tau" => "1.008", "dfc" => 15),
                array("tau" => "1.047", "dfc" => 16),
                array("tau" => "1.084", "dfc" => 17),
                array("tau" => "1.119", "dfc" => 18),
                array("tau" => "1.154", "dfc" => 19),
                array("tau" => "1.188", "dfc" => 20),
                array("tau" => "1.221", "dfc" => 21),
                array("tau" => "1.253", "dfc" => 22),
                array("tau" => "1.285", "dfc" => 23),
                array("tau" => "1.317", "dfc" => 24),
                array("tau" => "1.348", "dfc" => 25),
                array("tau" => "1.379", "dfc" => 26),
                array("tau" => "1.410", "dfc" => 27),
                array("tau" => "1.441", "dfc" => 28),
                array("tau" => "1.472", "dfc" => 29),
                array("tau" => "1.503", "dfc" => 30),
                array("tau" => "1.850", "dfc" => 40),
                array("tau" => "3.500", "dfc" => 53),
                array("tau" => "9999999999999999999999999999999999999999999999999999", "dfc" => 49)
            );
        } else if ($n_lab >= 76 && $n_lab <= 150) { // 100 Laboratories
            $correlacion_tau = array(
                array("tau" => "0.000", "dfc" => 4),
                array("tau" => "0.344", "dfc" => 5),
                array("tau" => "0.475", "dfc" => 6),
                array("tau" => "0.571", "dfc" => 7),
                array("tau" => "0.647", "dfc" => 8),
                array("tau" => "0.713", "dfc" => 9),
                array("tau" => "0.770", "dfc" => 10),
                array("tau" => "0.821", "dfc" => 11),
                array("tau" => "0.867", "dfc" => 12),
                array("tau" => "0.910", "dfc" => 13),
                array("tau" => "0.949", "dfc" => 14),
                array("tau" => "0.987", "dfc" => 15),
                array("tau" => "1.022", "dfc" => 16),
                array("tau" => "1.055", "dfc" => 17),
                array("tau" => "1.086", "dfc" => 18),
                array("tau" => "1.117", "dfc" => 19),
                array("tau" => "1.146", "dfc" => 20),
                array("tau" => "1.174", "dfc" => 21),
                array("tau" => "1.201", "dfc" => 22),
                array("tau" => "1.227", "dfc" => 23),
                array("tau" => "1.252", "dfc" => 24),
                array("tau" => "1.277", "dfc" => 25),
                array("tau" => "1.301", "dfc" => 26),
                array("tau" => "1.325", "dfc" => 27),
                array("tau" => "1.348", "dfc" => 28),
                array("tau" => "1.370", "dfc" => 29),
                array("tau" => "1.393", "dfc" => 30),
                array("tau" => "1.598", "dfc" => 40),
                array("tau" => "1.985", "dfc" => 60),
                array("tau" => "4.975", "dfc" => 103),
                array("tau" => "9999999999999999999999999999999999999999999999999999", "dfc" => 99)
            );
        } else if ($n_lab >= 151) { // 200 or more Laboratories
            $correlacion_tau = array(
                array("tau" => "0.000", "dfc" => 4),
                array("tau" => "0.344", "dfc" => 5),
                array("tau" => "0.475", "dfc" => 6),
                array("tau" => "0.559", "dfc" => 7),
                array("tau" => "0.646", "dfc" => 8),
                array("tau" => "0.710", "dfc" => 9),
                array("tau" => "0.766", "dfc" => 10),
                array("tau" => "0.816", "dfc" => 11),
                array("tau" => "0.861", "dfc" => 12),
                array("tau" => "0.903", "dfc" => 13),
                array("tau" => "0.941", "dfc" => 14),
                array("tau" => "0.977", "dfc" => 15),
                array("tau" => "1.010", "dfc" => 16),
                array("tau" => "1.042", "dfc" => 17),
                array("tau" => "1.072", "dfc" => 18),
                array("tau" => "1.101", "dfc" => 19),
                array("tau" => "1.128", "dfc" => 20),
                array("tau" => "1.154", "dfc" => 21),
                array("tau" => "1.179", "dfc" => 22),
                array("tau" => "1.203", "dfc" => 23),
                array("tau" => "1.117", "dfc" => 24),
                array("tau" => "1.249", "dfc" => 25),
                array("tau" => "1.271", "dfc" => 26),
                array("tau" => "1.191", "dfc" => 27),
                array("tau" => "1.313", "dfc" => 28),
                array("tau" => "1.333", "dfc" => 29),
                array("tau" => "1.352", "dfc" => 30),
                array("tau" => "1.527", "dfc" => 40),
                array("tau" => "1.808", "dfc" => 60),
                array("tau" => "2.528", "dfc" => 120),
                array("tau" => "7.053", "dfc" => 203),
                array("tau" => "9999999999999999999999999999999999999999999999999999", "dfc" => 199)
            );
        }

        foreach ($correlacion_tau as $ct) {
            if (floatval($tau) <= floatval($ct["tau"])) {
                return $ct["dfc"];
            }
        }
    }

    public function promedioDiferenciasX5Corrida($resultados_limpios, $promedioCorrida)
    {
        if (sizeof($resultados_limpios) == 0) {
            return 0;
        } else {
            $new_array = array();
            for ($x = 0; $x < sizeof($resultados_limpios); $x++) {
                $resultado_limpio = $resultados_limpios[$x];
                array_push($new_array, (pow($resultado_limpio - $promedioCorrida, 2)));
            }
            $promedio = array_sum($new_array) / sizeof($new_array);
            return ($promedio * 5);
        }
    }

    public function sumaDiferenciasCorrida($resultados_limpios, $promedioCorrida)
    {
        if (sizeof($resultados_limpios) == 0) {
            return 0;
        } else {
            $new_array = array();
            for ($x = 0; $x < sizeof($resultados_limpios); $x++) {
                $resultado_limpio = $resultados_limpios[$x];
                array_push($new_array, (pow($resultado_limpio - $promedioCorrida, 2)));
            }
            $sumatoria = array_sum($new_array);
            return $sumatoria;
        }
    }

    public function getfactorNsubCero($n_puntos)
    {
        if (is_numeric($n_puntos) && $n_puntos % 5 == 3) {
            return 4.565;
        } else if (is_numeric($n_puntos) && $n_puntos % 5 == 4) {
            return 4.792;
        } else if (is_numeric($n_puntos) && $n_puntos % 5 == 0) {
            return 5;
        }
        return null;
    }

    public function resultadosLimipiosNivel($resultados_nivel)
    {
        $resultados_limpios = array();
        foreach ($resultados_nivel as $resultado_nivel) {
            if ($resultado_nivel->concentracion_1 != null) {
                array_push($resultados_limpios, $resultado_nivel->concentracion_1);
            }
            if ($resultado_nivel->concentracion_2 != null) {
                array_push($resultados_limpios, $resultado_nivel->concentracion_2);
            }
            if ($resultado_nivel->concentracion_3 != null) {
                array_push($resultados_limpios, $resultado_nivel->concentracion_3);
            }
            if ($resultado_nivel->concentracion_4 != null) {
                array_push($resultados_limpios, $resultado_nivel->concentracion_4);
            }
            if ($resultado_nivel->concentracion_5 != null) {
                array_push($resultados_limpios, $resultado_nivel->concentracion_5);
            }
        }

        return $resultados_limpios;
    }

    public function resultadosLimipiosCorrida($resultado_nivel, $limite_inferior_grubbs, $limite_superior_grubbs)
    {
        $resultados_limpios = array();

        if ($resultado_nivel->concentracion_1 != null && $resultado_nivel->concentracion_1 >= $limite_inferior_grubbs && $resultado_nivel->concentracion_1 <= $limite_superior_grubbs) {
            array_push($resultados_limpios, $resultado_nivel->concentracion_1);
        }
        if ($resultado_nivel->concentracion_2 != null && $resultado_nivel->concentracion_2 >= $limite_inferior_grubbs && $resultado_nivel->concentracion_2 <= $limite_superior_grubbs) {
            array_push($resultados_limpios, $resultado_nivel->concentracion_2);
        }
        if ($resultado_nivel->concentracion_3 != null && $resultado_nivel->concentracion_3 >= $limite_inferior_grubbs && $resultado_nivel->concentracion_3 <= $limite_superior_grubbs) {
            array_push($resultados_limpios, $resultado_nivel->concentracion_3);
        }
        if ($resultado_nivel->concentracion_4 != null && $resultado_nivel->concentracion_4 >= $limite_inferior_grubbs && $resultado_nivel->concentracion_4 <= $limite_superior_grubbs) {
            array_push($resultados_limpios, $resultado_nivel->concentracion_4);
        }
        if ($resultado_nivel->concentracion_5 != null && $resultado_nivel->concentracion_5 >= $limite_inferior_grubbs && $resultado_nivel->concentracion_5 <= $limite_superior_grubbs) {
            array_push($resultados_limpios, $resultado_nivel->concentracion_5);
        }
        return $resultados_limpios;
    }


    public function stats_standard_deviation(array $a)
    {

        $new_array = array();
        for ($i = 0; $i < count($a); $i++) {
            if ($this->is_not_zero($a[$i])) {
                array_push($new_array, $a[$i]);
            }
        }

        $n = count($new_array);
        if ($n === 0) {
            // trigger_error("The array has zero elements", E_USER_WARNING);
            return 0;
        }
        if ($n === 1) {
            // trigger_error("The array has only 1 element", E_USER_WARNING);
            return 0;
        }
        $mean = array_sum($new_array) / $n;
        $carry = 0.0;
        foreach ($new_array as $val) {
            $d = ((double)$val) - $mean;
            $carry += $d * $d;
        };
        return sqrt($carry / ($n - 1));
    }


    /*
    public function stats_varianza(array $a) {
        $n = count($a);
        if ($n === 0 || $n === 1) {
            return 0;
        }
        $mean = array_sum($a) / $n;
        $carry = 0.0;
        foreach ($a as $val) {
            $d = ((double) $val) - $mean;
            $carry += $d * $d;
        };
        --$n;
        return $carry / $n;
    }
    */


    public function stats_average($array)
    {
        if (count($array) == 0) {
            return 0;
        } else {

            $new_array = array();

            for ($i = 0; $i < count($array); $i++) {
                if ($this->is_not_zero($array[$i])) {
                    array_push($new_array, $array[$i]);
                }
            }

            if (count($new_array) == 0) {
                return 0;
            }
            return array_sum($new_array) / count($new_array);
        }
    }


    public function create(Request $req)
    {
        $resultados = self::listByAnalitoEP15($req->id_reference);
        $analitoEP15 = AnalitoEP15Controller::listItem($req->id_reference);
        return view("resultadoEP15.resultadoEP15Create")
            ->with("resultados", $resultados)
            ->with("analitoEP15", $analitoEP15);
    }


    public static function listByAnalitoEP15($id_referencia, $nivel = 0)
    {
        $tableResultadoEP15 = new ResultadoEP15();
        $objReturn = $tableResultadoEP15
            ->select(
                "resultado_ep15.id_resultado_ep15",
                "resultado_ep15.fecha",
                "resultado_ep15.concentracion_1",
                "resultado_ep15.concentracion_2",
                "resultado_ep15.concentracion_3",
                "resultado_ep15.concentracion_4",
                "resultado_ep15.concentracion_5",
                "resultado_ep15.num_corrida",
                "resultado_ep15.estado",
                "resultado_ep15.nivel_id_nivel",
                "usuario.id_usuario",
                "usuario.nom_usuario",
                "analito_ep15.*",
                "analito.valor_aps"
            )
            ->join("usuario", "usuario.id_usuario", "=", "resultado_ep15.usuario_id_usuario")
            ->join("analito_ep15", "analito_ep15.id_analito_ep15", "=", "resultado_ep15.analito_ep15_id_analito_ep15")
            ->join("analito", "analito.id_analito", "=", "analito_ep15.analito_id_analito");


        if ($nivel != 0) {
            return $objReturn
                ->where([
                    ["analito_ep15.id_analito_ep15", $id_referencia],
                    ["resultado_ep15.nivel_id_nivel", $nivel]
                ])
                ->orderBy("resultado_ep15.fecha", "asc")
                ->get();
        } else {
            return $objReturn
                ->where("analito_ep15.id_analito_ep15", $id_referencia)
                ->orderBy("resultado_ep15.fecha", "asc")
                ->get();
        }


        // $MYQUERY = "SELECT resultado_ep15.id_resultado_ep15, resultado_ep15.fecha, resultado_ep15.concentracion_1, resultado_ep15.concentracion_2, resultado_ep15.concentracion_3, resultado_ep15.concentracion_4, resultado_ep15.concentracion_5, resultado_ep15.num_corrida, resultado_ep15.estado,resultado_ep15.nivel_id_nivel, usuario.id_usuario, usuario.nom_usuario, analito_ep15.*, analito.valor_aps

        //     FROM resultado_ep15

        //     INNER JOIN usuario
        //     ON usuario.id_usuario = resultado_ep15.usuario_id_usuario
        //     INNER JOIN analito_ep15
        //     ON analito_ep15.id_analito_ep15 = resultado_ep15.analito_ep15_id_analito_ep15
        //     INNER JOIN analito
        //     ON analito.id_analito = analito_ep15.analito_id_analito";

        // $ORDERBY = " ORDER BY resultado_ep15.fecha ASC";

        // if ($nivel != 0) {
        //     $MYQUERY .= " WHERE analito_ep15.id_analito_ep15 = " . $id_referencia . " AND resultado_ep15.nivel_id_nivel = " . $nivel . $ORDERBY;
        // } else {
        //     $MYQUERY .= " WHERE analito_ep15.id_analito_ep15 = " . $id_referencia . $ORDERBY;
        // }

        // return DB::select(DB::raw($MYQUERY));

    }


    public function multipleStore(Request $req)
    {
        if (isset($req->registro)) {
            foreach ($req->registro as $registro) {
                $this->store($registro);
            }
        }
        if (isset($req->modificacion)) {
            foreach ($req->modificacion as $modificacion) {
                $this->update($modificacion);
            }
        }
    }

    public function store($registro)
    {
        $id_usuario = Auth::user()->id_usuario;
        DB::table('resultado_ep15')->insert(
            [
                'nivel_id_nivel' => intval($registro["nivel_id_nivel"]),
                'analito_ep15_id_analito_ep15' => intval($registro["analito_ep15"]),
                'num_corrida' => intval($registro["num_corrida"]),
                'usuario_id_usuario' => intval($id_usuario),
                "fecha" => $registro["fecha"],
                "concentracion_1" => ((floatval($registro["concentracion_1"]) == 0) ? 0.000001 : floatval($registro["concentracion_1"])),
                "concentracion_2" => ((floatval($registro["concentracion_2"]) == 0) ? 0.000001 : floatval($registro["concentracion_2"])),
                "concentracion_3" => ((floatval($registro["concentracion_3"]) == 0) ? 0.000001 : floatval($registro["concentracion_3"])),
                "concentracion_4" => ((floatval($registro["concentracion_4"]) == 0) ? 0.000001 : floatval($registro["concentracion_4"])),
                "concentracion_5" => ((floatval($registro["concentracion_5"]) == 0) ? 0.000001 : floatval($registro["concentracion_5"]))
            ]
        );
    }

    public function update($modificacion)
    {
        $this->tableResultadoEP15
            ->where("id_resultado_ep15", "=", $modificacion["reference_id"])
            ->update([
                "fecha" => $modificacion["fecha"],
                'analito_ep15_id_analito_ep15' => intval($modificacion["analito_ep15"]),
                "concentracion_1" => ((floatval($modificacion["concentracion_1"]) == 0) ? 0.000001 : floatval($modificacion["concentracion_1"])),
                "concentracion_2" => ((floatval($modificacion["concentracion_2"]) == 0) ? 0.000001 : floatval($modificacion["concentracion_2"])),
                "concentracion_3" => ((floatval($modificacion["concentracion_3"]) == 0) ? 0.000001 : floatval($modificacion["concentracion_3"])),
                "concentracion_4" => ((floatval($modificacion["concentracion_4"]) == 0) ? 0.000001 : floatval($modificacion["concentracion_4"])),
                "concentracion_5" => ((floatval($modificacion["concentracion_5"]) == 0) ? 0.000001 : floatval($modificacion["concentracion_5"]))
            ]);
    }
    public function downloadTemplateResults(){
        $id_reference = $_GET['id_reference'];
        $resultados = self::listByAnalitoEP15($id_reference);
        $analitoEP15 = AnalitoEP15Controller::listItem($id_reference);

        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        function addNivel($sheet, $nivel){
            $sheet->setCellValueByColumnAndRow(($nivel-1)*5,1, 'Repeticiones nivel '.$nivel);
            $sheet->mergeCellsByColumnAndRow(($nivel-1)*5,1, ($nivel-1)*5+4,1);
            $sheet->getStyleByColumnAndRow(($nivel-1)*5,1)->getFont()->setBold(true);
            $sheet->getStyleByColumnAndRow(($nivel-1)*5,1)->getFont()->setSize(14);
            $sheet->getStyleByColumnAndRow(($nivel-1)*5,1)->getAlignment()->setHorizontal('center');
            for($col=0; $col<5; $col++){
                $sheet->setCellValueByColumnAndRow(($nivel-1)*5 + $col,2, $col+1);
                $sheet->getStyleByColumnAndRow(($nivel-1)*5 + $col,2)->getFont()->setBold(true);
                $sheet->getStyleByColumnAndRow(($nivel-1)*5 + $col,2)->getAlignment()->setHorizontal('center');
            }
        }

        $fila = 1;
        for($nivel=1; $nivel<=$analitoEP15->num_niveles; $nivel++){
            addNivel($sheet, $nivel);
            $fila+=2;
            for($corrida=1; $corrida<=5; $corrida++){
                $resultado_corrida = null;
                foreach($resultados as $resultado){
                    if($resultado['num_corrida']==$corrida && $resultado['nivel_id_nivel']==$nivel){
                        $resultado_corrida = $resultado;
                        break;
                    }
                }
                if($resultado_corrida){
                    $sheet->setCellValueByColumnAndRow(($nivel-1)*5, $fila, $resultado_corrida['concentracion_1']);
                    $sheet->setCellValueByColumnAndRow(($nivel-1)*5 + 1, $fila, $resultado_corrida['concentracion_2']);
                    $sheet->setCellValueByColumnAndRow(($nivel-1)*5 + 2, $fila, $resultado_corrida['concentracion_3']);
                    $sheet->setCellValueByColumnAndRow(($nivel-1)*5 + 3, $fila, $resultado_corrida['concentracion_4']);
                    $sheet->setCellValueByColumnAndRow(($nivel-1)*5 + 4, $fila, $resultado_corrida['concentracion_5']);
                }
                $fila++;
            }
            $fila = 1;
        }

        $writer = new Xlsx($spreadsheet);
        
        $filename = 'template_resultados_ep15.xlsx';

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'. $filename .'"');
        header('Cache-Control: max-age=0');
        
        $writer->save('php://output');

        exit;
    }
}
