<?php

namespace App\Libraries\fpdf;

use App\SuperUsuario;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;

include "fpdf.php";
// use App\Libraries\fpdf\FPDF;
// require('../fpdf/fpdf.php');

class EstructuraPDFEP15 extends FPDF
{

    var $B = 0;
    var $I = 0;
    var $U = 0;
    var $HREF = '';
    var $ALIGN = '';
    var $widths;
    var $minimoResaltado = 8;
    var $aligns;
    var $con_inserto_incluido;
    public $gris_claro = [253, 253, 254];
    public $gris_oscuro = [240, 240, 241];
    public $borde_table = [174, 182, 191];
    public $fondo_table = [214, 219, 223];


    function SetWidths($w)
    {
        //Set the array of column widths
        $this->widths = $w;
    }


    function SetAligns($a)
    {
        //Set the array of column alignments
        $this->aligns = $a;
    }


    function Row($data)
    {
        //Calculate the height of the row
        $factor = 4.2;
        $nb = 0;
        for ($i = 0; $i < count($data); $i++)
            $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
        $h = $factor * $nb;
        //Issue a page break first if needed
        $this->CheckPageBreak($h);
        //Draw the cells of the row
        for ($i = 0; $i < count($data); $i++) {

            if ($i == 0) {
                $this->SetFont('Arial', 'B', 5);
            } else if ($i == 1) {
                $this->SetFont('Arial', '', 5);
            }

            $w = $this->widths[$i];
            $a = isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
            $x = $this->GetX();
            $y = $this->GetY();
            $this->SetTextColor(40, 40, 40);
            $this->SetLineWidth(0.2);
            $this->SetDrawColor(146, 169, 185);
            $this->Cell($w, $h, "", 0, 0, 0, 1);
            $this->SetXY($x, $y);

            if ($i == 0) {
                $this->MultiCell($w, $factor, $data[$i], "L", $a, false);
            } else if (($i + 1) == count($data)) { // Si es el ultimo
                $this->MultiCell($w, $factor, $data[$i], "R", $a, false);
            } else {
                $this->MultiCell($w, $factor, $data[$i], 0, $a, false);
            }

            $this->SetXY($x + $w, $y);
        }
        //Go to the next line
        $this->Ln($h);
    }

    function RowEncabezado($data)
    {
        //Calculate the height of the row
        $nb = 0;
        for ($i = 0; $i < count($data); $i++)
            $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
        $h = 3.5 * $nb;
        //Issue a page break first if needed
        $this->CheckPageBreak($h);
        //Draw the cells of the row
        for ($i = 0; $i < count($data); $i++) {

            $w = $this->widths[$i];
            $a = isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
            //Save the current position
            $x = $this->GetX();
            $y = $this->GetY();
            //Draw the border
            // $this->SetFillColor(255, 255, 255);

            $this->SetTextColor(40, 40, 40);

            $this->SetLineWidth(0.2);
            $this->SetDrawColor(146, 169, 185);

            $this->Cell($w, $h, "", 0, 0, 0, 1);
            $this->SetXY($x, $y);

            if ($data[$i] == "\n" || $data[$i] == "N/A\n") {
                $this->SetTextColor(147, 183, 196);
                $data[$i] = "N/A";
            } else {
                $this->SetTextColor(40, 40, 40);
            }

            $this->MultiCell($w, 3.5, $data[$i], 0, $a, false);
            $this->Rect($x, $y, $w, $h);


            if ($this->con_inserto_incluido) { // Si tiene el inserto incluido
                if ($i == 9 || $i == 16 || $i == 23) {
                    $this->SetLineWidth(0.3);
                    $this->SetDrawColor(27, 100, 155);
                    $this->Line($x, $y, $x, $y + $h);
                } else if ($i == 28) {
                    $this->SetLineWidth(0.3);
                    $this->SetDrawColor(27, 100, 155);
                    $this->Line($x + $w, $y, $x + $w, $y + $h);
                }
            } else { // Si no va a estar el inserto
                if ($i == 9 || $i == 16) {
                    $this->SetLineWidth(0.3);
                    $this->SetDrawColor(27, 100, 155);
                    $this->Line($x, $y, $x, $y + $h);
                } else if ($i == 22) {
                    $this->SetLineWidth(0.3);
                    $this->SetDrawColor(27, 100, 155);
                    $this->Line($x + $w, $y, $x + $w, $y + $h);
                }
            }


            $this->SetXY($x + $w, $y);
        }
        //Go to the next line
        $this->Ln($h);
    }


    function CheckPageBreak($h)
    {
        //If the height h would cause an overflow, add a new page immediately
        if ($this->GetY() + $h > $this->PageBreakTrigger)
            $this->AddPage($this->CurOrientation);
    }


    function NbLines($w, $txt)
    {
        //Computes the number of lines a MultiCell of width w will take
        $cw = &$this->CurrentFont['cw'];
        if ($w == 0)
            $w = $this->w - $this->rMargin - $this->x;
        $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
        $s = str_replace("\r", '', $txt);
        $nb = strlen($s);
        if ($nb > 0 and $s[$nb - 1] == "\n")
            $nb--;
        $sep = -1;
        $i = 0;
        $j = 0;
        $l = 0;
        $nl = 1;
        while ($i < $nb) {
            $c = $s[$i];
            if ($c == "\n") {
                $i++;
                $sep = -1;
                $j = $i;
                $l = 0;
                $nl++;
                continue;
            }
            if ($c == ' ')
                $sep = $i;
            $l += $cw[$c];
            if ($l > $wmax) {
                if ($sep == -1) {
                    if ($i == $j)
                        $i++;
                } else
                    $i = $sep + 1;
                $sep = -1;
                $j = $i;
                $l = 0;
                $nl++;
            } else
                $i++;
        }
        return $nl;
    }


    function WriteHTML($html)
    {
        //HTML parser
        $html = str_replace("\n", ' ', $html);
        $a = preg_split('/<(.*)>/U', $html, -1, PREG_SPLIT_DELIM_CAPTURE);
        foreach ($a as $i => $e) {
            if ($i % 2 == 0) {
                //Text
                if ($this->HREF)
                    $this->PutLink($this->HREF, $e);
                elseif ($this->ALIGN == 'center')
                    $this->Cell(0, 3, $e, 0, 1, 'C');
                else
                    $this->Write(3, $e);
            } else {
                //Tag
                if ($e[0] == '/')
                    $this->CloseTag(strtoupper(substr($e, 1)));
                else {
                    //Extract properties
                    $a2 = explode(' ', $e);
                    $tag = strtoupper(array_shift($a2));
                    $prop = array();
                    foreach ($a2 as $v) {
                        if (preg_match('/([^=]*)=["\']?([^"\']*)/', $v, $a3))
                            $prop[strtoupper($a3[1])] = $a3[2];
                    }
                    $this->OpenTag($tag, $prop);
                }
            }
        }
    }


    function OpenTag($tag, $prop)
    {
        //Opening tag
        if ($tag == 'B' || $tag == 'I' || $tag == 'U')
            $this->SetStyle($tag, true);
        if ($tag == 'A')
            $this->HREF = $prop['HREF'];
        if ($tag == 'BR')
            $this->Ln(5);
        if ($tag == 'P')
            $this->ALIGN = $prop['ALIGN'];
        if ($tag == 'HR') {
            if (!empty($prop['WIDTH']))
                $Width = $prop['WIDTH'];
            else
                $Width = $this->w - $this->lMargin - $this->rMargin;
            $this->Ln(2);
            $x = $this->GetX();
            $y = $this->GetY();
            $this->SetLineWidth(0.4);
            $this->Line($x, $y, $x + $Width, $y);
            $this->SetLineWidth(0.2);
            $this->Ln(2);
        }
    }


    function CloseTag($tag)
    {
        //Closing tag
        if ($tag == 'B' || $tag == 'I' || $tag == 'U')
            $this->SetStyle($tag, false);
        if ($tag == 'A')
            $this->HREF = '';
        if ($tag == 'P')
            $this->ALIGN = '';
    }


    function SetStyle($tag, $enable)
    {
        //Modify style and select corresponding font
        $this->$tag += ($enable ? 1 : -1);
        $style = '';
        foreach (array('B', 'I', 'U') as $s)
            if ($this->$s > 0)
                $style .= $s;
        $this->SetFont('', $style);
    }


    function PutLink($URL, $txt)
    {
        //Put a hyperlink
        $this->SetTextColor(0, 0, 255);
        $this->SetStyle('U', true);
        $this->Write(5, $txt, $URL);
        $this->SetStyle('U', false);
        $this->SetTextColor(0);
    }

    function RoundedRect($x, $y, $w, $h, $r, $corners = '1234', $style = '')
    {
        $k = $this->k;
        $hp = $this->h;
        if ($style == 'F')
            $op = 'f';
        elseif ($style == 'FD' || $style == 'DF')
            $op = 'B';
        else
            $op = 'S';
        $MyArc = 4 / 3 * (sqrt(2) - 1);
        $this->_out(sprintf('%.2F %.2F m', ($x + $r) * $k, ($hp - $y) * $k));

        $xc = $x + $w - $r;
        $yc = $y + $r;
        $this->_out(sprintf('%.2F %.2F l', $xc * $k, ($hp - $y) * $k));
        if (strpos($corners, '2') === false)
            $this->_out(sprintf('%.2F %.2F l', ($x + $w) * $k, ($hp - $y) * $k));
        else
            $this->_Arc($xc + $r * $MyArc, $yc - $r, $xc + $r, $yc - $r * $MyArc, $xc + $r, $yc);

        $xc = $x + $w - $r;
        $yc = $y + $h - $r;
        $this->_out(sprintf('%.2F %.2F l', ($x + $w) * $k, ($hp - $yc) * $k));
        if (strpos($corners, '3') === false)
            $this->_out(sprintf('%.2F %.2F l', ($x + $w) * $k, ($hp - ($y + $h)) * $k));
        else
            $this->_Arc($xc + $r, $yc + $r * $MyArc, $xc + $r * $MyArc, $yc + $r, $xc, $yc + $r);

        $xc = $x + $r;
        $yc = $y + $h - $r;
        $this->_out(sprintf('%.2F %.2F l', $xc * $k, ($hp - ($y + $h)) * $k));
        if (strpos($corners, '4') === false)
            $this->_out(sprintf('%.2F %.2F l', ($x) * $k, ($hp - ($y + $h)) * $k));
        else
            $this->_Arc($xc - $r * $MyArc, $yc + $r, $xc - $r, $yc + $r * $MyArc, $xc - $r, $yc);

        $xc = $x + $r;
        $yc = $y + $r;
        $this->_out(sprintf('%.2F %.2F l', ($x) * $k, ($hp - $yc) * $k));
        if (strpos($corners, '1') === false) {
            $this->_out(sprintf('%.2F %.2F l', ($x) * $k, ($hp - $y) * $k));
            $this->_out(sprintf('%.2F %.2F l', ($x + $r) * $k, ($hp - $y) * $k));
        } else
            $this->_Arc($xc - $r, $yc - $r * $MyArc, $xc - $r * $MyArc, $yc - $r, $xc, $yc - $r);
        $this->_out($op);
    }

    function _Arc($x1, $y1, $x2, $y2, $x3, $y3)
    {
        $h = $this->h;
        $this->_out(sprintf(
            '%.2F %.2F %.2F %.2F %.2F %.2F c ',
            $x1 * $this->k,
            ($h - $y1) * $this->k,
            $x2 * $this->k,
            ($h - $y2) * $this->k,
            $x3 * $this->k,
            ($h - $y3) * $this->k
        ));
    }


    function Header()
    {

        $this->SetLineWidth(0.3);

        // Recuadro ancho completo
        $this->SetXY(5, 5);
        $this->SetDrawColor(28, 80, 164);
        $this->SetTextColor(24, 74, 156);
        $this->Cell(206, 23, "", 0, 0, 'L', 0);


        // Recuadro imagen de logo de Quik
        $this->SetXY(5, 5);
        $this->Cell(55, 23, "", 0, 0, 'L', 0);

        if(Auth::user()->rol_id_rol == 5){
      //       $this->Image(asset('/img/logo-quik.png'), 10, 11, 21);
        }else{
            $clientReference = SuperUsuario::query()->where('id', Auth::user()->super_admin_id)->first();
            if(!isset($clientReference)){
         //       $this->Image(asset('/img/logo-quik.png'), 10, 11, 21);
            } else {
        //       $this->Image(asset($clientReference->imagen_cliente), 10, 11, 21);
            }
        }
        // Logo Quik

        // Linea divisora vertical
        $this->SetLineWidth(0.1);
//        $this->line(34, 12, 34, 22);

        // Logo de V2M

        // Recuadro del titulo
        $this->SetXY(-80, 5);
        $this->SetFont('Arial', 'B', 9);
        $this->MultiCell(96, 4, "Quik SAS. www.quik.com.co", 0, 'C', 0);

        // Titulo Principal
        $this->SetFont('Arial', 'B', 9);
        $this->SetXY(60, 13);
        $this->MultiCell(96, 4, "V2M. V.1.0.", 0, 'C', 0);
        $this->SetXY(60, 17);
        $this->MultiCell(96, 4, "Verificación de la precisión y estimación del sesgo", 0, 'C', 0);

        // Recuadro de convenciones del formato
        $this->SetXY(-60, 5);
        $this->Cell(55, 23, "", 0, 0, 'L', 0);


        // Seccion de los valores fijos del formato
//        $this->SetTextColor(28, 80, 164);
//        $this->SetFont('Arial', 'B', 8);
//        $this->SetXY(-60, 5);
//        $this->Cell(27.5, 5.75, "Código:", 0, 0, 'C', 0);
//
//        $this->SetXY(-60, 10.75);
//        $this->Cell(27.5, 5.75, "Vigente desde:", 0, 0, 'C', 0);
//
//        $this->SetXY(-60, 16.5);
//        $this->Cell(27.5, 5.75, "Versión:", 0, 0, 'C', 0);
//
//        $this->SetXY(-60, 22.25);
//        $this->Cell(27.5, 5.75, "Página:", 0, 0, 'C', 0);


        // Valores de los codigos fijos del formato
//        $this->SetFont('Arial', 'B', 7);
//        $this->SetXY(-32.5, 5);
//        $this->Cell(27.5, 5.75, "V2M-EP-15 A3", 0, 0, 'C', 0);
//
//        $this->SetXY(-32.5, 10.75);
//        $this->Cell(27.5, 5.75, "15/03/2021", 0, 0, 'C', 0);
//
//        $this->SetXY(-32.5, 16.5);
//        $this->Cell(27.5, 5.75, "1", 0, 0, 'C', 0);

        $this->SetXY(-32.5, 22.25);
//        $this->Cell(27.5, 5.75, "  " . $this->PageNo() . " de {nb}", 0, 0, 'C', 0);

        $this->Ln(12);
        $this->SetX(10);
    }

    function CreateDocument($analitoEP15, $obj_json, $PDFObj)
    {
        $this->AliasNbPages();
        $this->SetAutoPageBreak(true, 5);

        for ($nivel = 1; $nivel <= $analitoEP15->num_niveles; $nivel++) {

            $this->AddPage();

            // Información de NIVEL
            $this->Ln(2);
            $this->SetTextColor(40, 40, 40);
            $this->SetFont('Arial', 'B', 15);
            $this->Cell(196, 4, "Nivel " . $nivel, 0, 0, 'C', 0);
            $this->Ln(5);

            // Información de Analito
            $this->SetTextColor(100, 100, 100);
            $this->SetFont('Arial', '', 11);
            $this->Cell(196, 4, $analitoEP15->nom_analito, 0, 0, 'C', 0);
            $this->Ln(8);

            // I informacion general
            $this->SetTextColor(40, 40, 40);
            $this->SetFillColor(172, 195, 212);
            $this->SetDrawColor(113, 181, 228);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 7.5);
            $this->Cell(196, 5, "I. Información general", 1, 0, 'C', 1);

            // Caja contenedora de informacion del nivel
            $this->SetXY(10, 54.3);
            $this->SetDrawColor(150, 150, 150);
            $this->Cell(196, 21, "", "LRB", 0, 'C', 0);


            /* Primer nivel */
            // Sistema de medicion
            $this->SetXY(12, 56);
            $this->SetDrawColor(240, 240, 240);
            $this->SetTextColor(60, 60, 60);
            $this->SetFont('Arial', 'B', 7);
            $this->Cell(48, 4.5, "Sistema de medición", 0, 0, 'C', 0);
            $this->Cell(48, 4.5, "Lote de reactivos", 0, 0, 'C', 0);
            $this->Cell(48, 4.5, "Lote de calibrador", 0, 0, 'C', 0);
            $this->Cell(48, 4.5, "Material utilizado", 0, 0, 'C', 0);


            $this->SetXY(12, 60.5);
            $this->SetDrawColor(240, 240, 240);
            $this->SetTextColor(60, 60, 60);
            $this->SetFont('Arial', '', 6.5);
            $this->Cell(48, 4.5, $analitoEP15->nom_analizador, 0, 0, 'C', 0);
            $this->Cell(48, 4.5, $analitoEP15->lote_reactivo, 0, 0, 'C', 0);
            $this->Cell(48, 4.5, $analitoEP15->lote_calibrador, 0, 0, 'C', 0);
            $this->Cell(48, 4.5, $analitoEP15->material_utilizado, 0, 0, 'C', 0);


            /* Segundo nivel */
            $this->SetXY(12, 65);
            $this->SetDrawColor(240, 240, 240);
            $this->SetTextColor(60, 60, 60);
            $this->SetFont('Arial', 'B', 7);
            $this->Cell(48, 4.5, "Limite APS%", 0, 0, 'C', 0);
            $this->Cell(48, 4.5, "Numero de datos N", 0, 0, 'C', 0);

            $this->Cell(48, 4.5, "Sigma", 0, 0, 'C', 0);
            $this->SetX(135);
            $this->subWrite(4, 'R', '', 6, -3);

            $this->SetX(155);

            $this->Cell(48, 4.5, "Sigma", 0, 0, 'C', 0);
            $this->SetX(182);
            $this->subWrite(4, 'WL', '', 6, -3);

            $this->SetXY(12, 69.5);
            $this->SetDrawColor(240, 240, 240);
            $this->SetTextColor(60, 60, 60);
            $this->SetFont('Arial', '', 6.5);
            $this->Cell(48, 4.5, round($analitoEP15->valor_aps, 2), 0, 0, 'C', 0);
            $this->Cell(48, 4.5, $obj_json[$nivel]["corridas"]["general"]["n_datos"], 0, 0, 'C', 0);
            $this->Cell(48, 4.5, round($obj_json[$nivel]["sigma_r"], 2), 0, 0, 'C', 0);
            $this->Cell(48, 4.5, round($obj_json[$nivel]["sigma_wl"], 2), 0, 0, 'C', 0);

            // limite de grubbs
            /* $this->SetXY(10, 300); */
            $this->SetXY(10, 78);
            $this->SetFont('Arial', 'B', 7.5);
            $this->SetDrawColor(113, 181, 228);
            $this->SetFillColor(172, 195, 212);
            $this->Cell(196, 5, "VII. Limite de grubbs        ", 1, 0, 'C', 1);

            $this->SetXY(10, 83);
            $this->SetFillColor(255, 255, 255);
            $this->SetDrawColor(198, 203, 207);
            $this->Cell(196, 30, "", 1, 0, 'C', 1);

            $text_result_inferior = ' + '. round($obj_json[$nivel]["corridas"]["general"]["gran_media"],2);
            $text_result_inferior = $text_result_inferior . ' - (' . $obj_json[$nivel]["corridas"]["general"]["factor_grubbs"];
            $text_result_inferior = $text_result_inferior . ' + ' . round($obj_json[$nivel]["corridas"]["general"]["gran_de"],2);

            $text_result_superior = ' + '. round($obj_json[$nivel]["corridas"]["general"]["gran_media"],2);
            $text_result_superior = $text_result_superior . ' + (' . $obj_json[$nivel]["corridas"]["general"]["factor_grubbs"];
            $text_result_superior = $text_result_superior . ' + ' . round($obj_json[$nivel]["corridas"]["general"]["gran_de"],2);

            $this->SetXY(10.5, 90);
            $this->SetFillColor(255, 255, 255);
            $this->SetFont('Arial');
            $this->Cell(98, 5, "Límite inferior de grubbs: ". $text_result_inferior, 0, 0, 'C', 1);
            $this->Cell(98, 5, "Límite superior de grubbs: ". $text_result_superior, 0, 0, 'C', 1);

            $this->SetXY(10.5, 95);
            $this->SetFont('Arial');
            $this->Cell(98, 5, "Límite inferior de grubbs: ". round($obj_json[$nivel]["corridas"]["general"]["limite_inferior_grubbs"], 2), 0, 0, 'C', 0);
            $this->Cell(98, 5, "Límite superior de grubbs: ". round($obj_json[$nivel]["corridas"]["general"]["limite_superior_grubbs"], 2), 0, 0, 'C', 0);


            // II Diagrama de flujo SR y SWL
            $this->SetXY(10, 115);
            $this->SetTextColor(40, 40, 40);
            $this->SetFillColor(172, 195, 212);
            $this->SetDrawColor(113, 181, 228);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 7.5);
            // $this->Cell(196,5,"II. Diagrama de flujo SR y SWL",1,0,'C',1);

            $this->Cell(196, 5, "II. Diagrama de flujo S        ", 1, 0, 'C', 1);
            $this->SetX(117.8);
            $this->subWrite(4, 'R', '', 6, -3);
            $this->SetX(120.4);
            $this->Write(5, 'y S');
            $this->SetX(124.4);
            $this->subWrite(4, 'WL', '', 6, -3);
            // $this->Cell(196,5,"S",1,0,'C',1);
            // $this->subWrite(4,'WL','',6,-3);
            // $this->Cell(48,4.5,"Sigma",0,0,'C',0);
            // $this->SetX(135);
            // $this->subWrite(4,'R','',6,-3);

            // $this->SetX(155);

            // $this->Cell(48,4.5,"Sigma",0,0,'C',0);
            // $this->SetX(182);
            // $this->subWrite(4,'WL','',6,-3);


            // Caja contenedora de diagrama de flujo
            $this->SetXY(10, 118);
            $this->SetDrawColor(150, 150, 150);
            // $this->Cell(196,94,"","LRB",0,'C',0);
            // $this->Cell(196,100,"","LRB",0,'C',0);
            $this->Cell(196, 105, "", "LRB", 0, 'C', 0);

  //           // Imagen de diagrama de flujo
      //       $this->Image(asset('img/diagramas_de_flujo4.png'), 32, 86, 152);

            // Estlos generales para las formulas del diagrama de flujo
            $this->SetFont('Arial', '', 5);
            $this->SetTextColor(50, 50, 50);
            $this->SetDrawColor(150, 150, 150);
            $this->SetFillColor(255, 0, 0);

            // P1
            $this->SetXY(43, 174.6);
            $this->Cell(14.2, 2.7, round($obj_json[$nivel]["corridas"]["general"]["sr"], 2), 0, 0, 'C', 0);

            // P2
            $this->SetXY(87.5, 174.9);
            $this->Cell(14.2, 2.7, round($obj_json[$nivel]["sigma_r"], 2), 0, 0, 'C', 0);

            // P3
            $this->SetXY(120.3, 175.4);
            $this->Cell(14.2, 2.7, round($obj_json[$nivel]["corridas"]["general"]["swl"], 2), 0, 0, 'C', 0);

            // P4
            $this->SetXY(165, 175);
            $this->Cell(14.2, 2.7, round($obj_json[$nivel]["sigma_wl"], 2), 0, 0, 'C', 0);

            // P5
            $this->SetXY(63, 159.9);
            $this->Cell(14.2, 2.7, "(" . round($obj_json[$nivel]["corridas"]["general"]["ms2"], 3) . ")^(1÷2)", 0, 0, 'C', 0);

            // P6
            $this->SetFont('Arial', '', 3.9);
            $this->SetXY(146, 158.4);
            $this->Cell(14.2, 2.7, "(" . round($obj_json[$nivel]["corridas"]["general"]["ms2"], 3) . " + " . round($obj_json[$nivel]["corridas"]["general"]["vb"], 3) . ")^(1÷2)", 0, 0, 'C', 0);
            $this->SetFont('Arial', '', 5);

            // P7
            $this->SetXY(63.5, 141.5);
            $this->Cell(14.2, 2.7, round($obj_json[$nivel]["corridas"]["general"]["ms2"], 3), 0, 0, 'C', 0);

            // P8
            $this->SetXY(147, 139.5);
            $this->Cell(14.2, 2.7, round($obj_json[$nivel]["corridas"]["general"]["vb"], 3), 0, 0, 'C', 0);

            // P9
            $this->SetXY(63.4, 122);
            $this->Cell(14.2, 2.7, round($obj_json[$nivel]["corridas"]["general"]["ss2"], 3) . " ÷ " . round($obj_json[$nivel]["corridas"]["general"]["df2"], 3), 0, 0, 'C', 0);

            // P10
            $this->SetXY(147, 121.2);
            $this->Cell(14.2, 2.7, round($obj_json[$nivel]["corridas"]["general"]["ss1"], 3) . " ÷ " . round($obj_json[$nivel]["corridas"]["general"]["df1"], 3), 0, 0, 'C', 0);

            // P11
            $this->SetXY(40.5, 104);
            $this->Cell(14.2, 2.7, round($obj_json[$nivel]["corridas"]["general"]["ss_total"], 3) . " - " . round($obj_json[$nivel]["corridas"]["general"]["ss1"], 3), 0, 0, 'C', 0);

            // P12
            $this->SetXY(83, 103.5);
            $this->Cell(14.2, 2.7, $obj_json[$nivel]["corridas"]["general"]["n_datos"] . " - " . ($obj_json[$nivel]["corridas"]["general"]["df1"] + 1), 0, 0, 'C', 0);

            // P13
            $this->SetXY(131.5, 101.8);
            $this->Cell(14.2, 2.7, ($obj_json[$nivel]["corridas"]["general"]["df1"] + 1) . " x " . round($obj_json[$nivel]["corridas"]["general"]["ss1"] / ($obj_json[$nivel]["corridas"]["general"]["df1"] + 1), 3), 0, 0, 'C', 0);


            // P14
            // $this->SetXY(163.8, 100);
            $this->SetXY(162.8, 101);
            $this->Cell(14.2, 2.7, ($obj_json[$nivel]["corridas"]["general"]["df1"] + 1) . " - 1", 0, 0, 'C', 0);



            // III Grafica de control
            // $this->SetXY(10,180);
            // $this->SetXY(10,186);
            $this->SetXY(10, 195);
            $this->SetTextColor(40, 40, 40);
            $this->SetFillColor(172, 195, 212);
            $this->SetDrawColor(113, 181, 228);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 7.5);
            $this->Cell(196, 5, "III. Gráfica integrada de control de calidad", 1, 0, 'C', 1);


            // Caja contenedora de grafica de control
            // $this->SetXY(10,185);
            // $this->SetXY(10,191);
            $this->SetXY(10, 200);
            $this->SetDrawColor(150, 150, 150);
            $this->Cell(196, 62, "", "LRB", 0, 'C', 0);


  //           // Imagen de a grafica correspondiente del nivel
         //    // $this->Image($PDFObj->urlBases64["base64_grafica_ep15_" . $nivel], 26, 195, 170);
      //       $this->Image($PDFObj->urlBases64["base64_grafica_ep15_" . $nivel], 26, 205, 170);

            $this->AddPage();


            // IV Recolección de información
            $this->SetX(10);
            $this->SetTextColor(40, 40, 40);
            $this->SetFillColor(172, 195, 212);
            $this->SetDrawColor(113, 181, 228);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 7.5);
            $this->Cell(196, 5, "IV. Recolección de información", 1, 0, 'C', 1);


            $this->SetXY(10, 39.4);
            $this->SetDrawColor(150, 150, 150);
            $this->Cell(196, 35, "", "LRB", 0, 'C', 0);


            $this->SetX(10);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetFillColor(214, 219, 223);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 6);
            $this->Cell(10, 5, "Corrida", 1, 0, 'C', 1);
            $this->Cell(30, 5, "Fecha", 1, 0, 'C', 1);
            $this->Cell(22.29, 5, "R1", 1, 0, 'C', 1);
            $this->Cell(22.29, 5, "R2", 1, 0, 'C', 1);
            $this->Cell(22.29, 5, "R3", 1, 0, 'C', 1);
            $this->Cell(22.29, 5, "R4", 1, 0, 'C', 1);
            $this->Cell(22.29, 5, "R5", 1, 0, 'C', 1);
            $this->Cell(22.29, 5, "Promedio", 1, 0, 'C', 1);
            $this->Cell(22.29, 5, "D.E.", 1, 1, 'C', 1);


            for ($x = 1; $x < sizeof($obj_json[$nivel]["corridas"]); $x++) {
                $this->SetX(10);
                $this->SetTextColor(40, 40, 40);
                $this->SetDrawColor(198, 203, 207);
                $this->SetLineWidth(0.2);
                $this->SetFont('Arial', '', 6);
                $this->Cell(10, 5, "C" . $x, 1, 0, 'C', 0);
                $this->Cell(30, 5, $obj_json[$nivel]["corridas"][$x]["fecha"], 1, 0, 'C', 0);
                for($concentracion_index = 1; $concentracion_index <= 5; $concentracion_index++){
                    $limite_inferior_grubbs = $obj_json[$nivel]["corridas"]["general"]["limite_inferior_grubbs"];
                    $limite_superior_grubbs = $obj_json[$nivel]["corridas"]["general"]["limite_superior_grubbs"];
                    $valor_concentracion = $obj_json[$nivel]["corridas"][$x]["concentracion_".$concentracion_index];
                    if($valor_concentracion < $limite_inferior_grubbs ||
                        $valor_concentracion > $limite_superior_grubbs) {
                        $this->SetFillColor(179, 0, 0);
                        $this->Cell(22.29, 5, ($valor_concentracion > 0.000002 ?
                            round($valor_concentracion, 2) : "N/A"), 1, 0, 'C', 1);
                    } else {
                        $this->SetFillColor(214, 219, 223);
                        $this->Cell(22.29, 5, ($valor_concentracion > 0.000002 ?
                            round($valor_concentracion, 2) : "N/A"), 1, 0, 'C', 0);
                    }
                }
                /* $this->Cell(22.29, 5, ($obj_json[$nivel]["corridas"][$x]["concentracion_2"] > 0.000002 ? round($obj_json[$nivel]["corridas"][$x]["concentracion_2"], 2) : "N/A"), 1, 0, 'C', 0); */
                /* $this->Cell(22.29, 5, ($obj_json[$nivel]["corridas"][$x]["concentracion_3"] > 0.000002 ? round($obj_json[$nivel]["corridas"][$x]["concentracion_3"], 2) : "N/A"), 1, 0, 'C', 0); */
                /* $this->Cell(22.29, 5, ($obj_json[$nivel]["corridas"][$x]["concentracion_4"] > 0.000002 ? round($obj_json[$nivel]["corridas"][$x]["concentracion_4"], 2) : "N/A"), 1, 0, 'C', 0); */
                /* $this->Cell(22.29, 5, ($obj_json[$nivel]["corridas"][$x]["concentracion_5"] > 0.000002 ? round($obj_json[$nivel]["corridas"][$x]["concentracion_5"], 2) : "N/A"), 1, 0, 'C', 0); */
                $this->Cell(22.29, 5, round($obj_json[$nivel]["corridas"][$x]["promedio"], 2), 1, 0, 'C', 0);
                $this->Cell(22.29, 5, round($obj_json[$nivel]["corridas"][$x]["de"], 2), 1, 1, 'C', 0);
            }


            $this->SetX(13);
            $this->SetFont('Arial', 'B', 6);
            $this->Cell(53, 5, "Gran media: " . round($obj_json[$nivel]["corridas"]["general"]["gran_media"], 2), 0, 0, 'C', 0);
            $this->Cell(87, 5, "D.E.: " . round($obj_json[$nivel]["corridas"]["general"]["gran_de"], 2), 0, 0, 'C', 0);
            $this->Cell(30, 5, "C.V.: " . round($obj_json[$nivel]["corridas"]["general"]["gran_de"] * 100 / $obj_json[$nivel]["corridas"]["general"]["gran_media"],2), 0, 1, 'C', 0);

            // V. Verificación de la precisión
            $this->Ln(4);
            $this->SetX(10);
            $this->SetTextColor(40, 40, 40);
            $this->SetFillColor(172, 195, 212);
            $this->SetDrawColor(113, 181, 228);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 7.5);
            $this->Cell(196, 5, "V. Verificación de la precisión", 1, 0, 'C', 1);


            $this->SetXY(10, 83.4);
            $this->SetDrawColor(150, 150, 150);
            $this->Cell(196, 86, "", "LRB", 0, 'C', 0);


            $this->Ln(2.5);
            $this->SetX(13);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetFillColor(214, 219, 223);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 6);
            $this->Cell(22, 5, "Fórmula", 1, 0, 'C', 1);
            $this->Cell(23.5, 5, "C1", 1, 0, 'C', 1);
            $this->Cell(23.5, 5, "C2", 1, 0, 'C', 1);
            $this->Cell(23.5, 5, "C3", 1, 0, 'C', 1);
            $this->Cell(23.5, 5, "C4", 1, 0, 'C', 1);
            $this->Cell(23.5, 5, "C5", 1, 1, 'C', 1);

            $getYtemp = $this->GetY();

            // SS1
            $this->SetXY(163, 85.9);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetFillColor(214, 219, 223);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 6);
            $this->Cell(40, 5, "SS1", 1, 0, 'C', 1);

            // Formula SS1
            $this->SetXY(163, 90.9);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', '', 6);
            $this->Cell(22, 12, "", 1, 0, 'C', 0);
            $this->Cell(18, 12, round($obj_json[$nivel]["corridas"]["general"]["ss1"], 2), 1, 0, 'C', 0);
      //       $this->Image(asset('img/formulas_ep15/ss1.png'), 167.8, 91.6, 14);


            // Diferencias de las medias
            $this->SetXY(13, $getYtemp);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', '', 6);
            $this->Cell(22, 6, "", 1, 0, 'C', 0);
            $this->Cell(23.5, 6, round($obj_json[$nivel]["corridas"]["1"]["dia_menos_gran_media"], 2), 1, 0, 'C', 0);
            $this->Cell(23.5, 6, round($obj_json[$nivel]["corridas"]["2"]["dia_menos_gran_media"], 2), 1, 0, 'C', 0);
            $this->Cell(23.5, 6, round($obj_json[$nivel]["corridas"]["3"]["dia_menos_gran_media"], 2), 1, 0, 'C', 0);
            $this->Cell(23.5, 6, round($obj_json[$nivel]["corridas"]["4"]["dia_menos_gran_media"], 2), 1, 0, 'C', 0);
            $this->Cell(23.5, 6, round($obj_json[$nivel]["corridas"]["5"]["dia_menos_gran_media"], 2), 1, 1, 'C', 0);
      //       $this->Image(asset('img/formulas_ep15/diferencia.png'), 19.7, 92, 8.5);

            // Diferencias al cuadrado
            $this->SetX(13);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', '', 6);
            $this->Cell(22, 6, "", 1, 0, 'C', 0);
            $this->Cell(23.5, 6, round($obj_json[$nivel]["corridas"]["1"]["dia_menos_gran_media_al_cuadrado"], 2), 1, 0, 'C', 0);
            $this->Cell(23.5, 6, round($obj_json[$nivel]["corridas"]["2"]["dia_menos_gran_media_al_cuadrado"], 2), 1, 0, 'C', 0);
            $this->Cell(23.5, 6, round($obj_json[$nivel]["corridas"]["3"]["dia_menos_gran_media_al_cuadrado"], 2), 1, 0, 'C', 0);
            $this->Cell(23.5, 6, round($obj_json[$nivel]["corridas"]["4"]["dia_menos_gran_media_al_cuadrado"], 2), 1, 0, 'C', 0);
            $this->Cell(23.5, 6, round($obj_json[$nivel]["corridas"]["5"]["dia_menos_gran_media_al_cuadrado"], 2), 1, 1, 'C', 0);
      //       $this->Image(asset('img/formulas_ep15/diferencia_cuadrado.png'), 19.5, 98, 10);

            $this->Ln(4);
            $this->SetTextColor(40, 40, 40);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 6);
            $this->SetX(13);
            $this->Cell(139.5, 3, "Diferencia de las medias al cuadrado", 0, 0, 'C', 0);
            $this->Ln(4);


            $getYtemp = $this->GetY();

            // SS2
            $this->SetXY(163, 110.9);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetFillColor(214, 219, 223);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 6);
            $this->Cell(40, 5, "SS2", 1, 0, 'C', 1);

            // Formula SS2
            $this->SetXY(163, 115.9);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', '', 6);
            $this->Cell(22, 12, "", 1, 0, 'C', 0);
            $this->Cell(18, 12, round($obj_json[$nivel]["corridas"]["general"]["ss2"], 2), 1, 0, 'C', 0);
      //       $this->Image(asset('img/formulas_ep15/ss2.png'), 166, 117, 18);


            $this->SetXY(13, $getYtemp);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetFillColor(214, 219, 223);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 6);
            $this->Cell(22, 5, "Corrida", 1, 0, 'C', 1);
            $this->Cell(23.5, 5, "R1", 1, 0, 'C', 1);
            $this->Cell(23.5, 5, "R2", 1, 0, 'C', 1);
            $this->Cell(23.5, 5, "R3", 1, 0, 'C', 1);
            $this->Cell(23.5, 5, "R4", 1, 0, 'C', 1);
            $this->Cell(23.5, 5, "R5", 1, 1, 'C', 1);

            for ($x = 1; $x < sizeof($obj_json[$nivel]["corridas"]); $x++) {
                $this->SetX(13);
                $this->SetTextColor(40, 40, 40);
                $this->SetDrawColor(198, 203, 207);
                $this->SetLineWidth(0.2);
                $this->SetFont('Arial', '', 6);
                $this->Cell(22, 5, "C" . $x, 1, 0, 'C', 0);
                $this->Cell(23.5, 5, ($obj_json[$nivel]["corridas"][$x]["concentracion_1"] == null) ? "N/A" : round($obj_json[$nivel]["corridas"][$x]["concentracion_1_menos_dia_al_2"], 2), 1, 0, 'C', 0);
                $this->Cell(23.5, 5, ($obj_json[$nivel]["corridas"][$x]["concentracion_2"] == null) ? "N/A" : round($obj_json[$nivel]["corridas"][$x]["concentracion_2_menos_dia_al_2"], 2), 1, 0, 'C', 0);
                $this->Cell(23.5, 5, ($obj_json[$nivel]["corridas"][$x]["concentracion_3"] == null) ? "N/A" : round($obj_json[$nivel]["corridas"][$x]["concentracion_3_menos_dia_al_2"], 2), 1, 0, 'C', 0);
                $this->Cell(23.5, 5, ($obj_json[$nivel]["corridas"][$x]["concentracion_4"] == null) ? "N/A" : round($obj_json[$nivel]["corridas"][$x]["concentracion_4_menos_dia_al_2"], 2), 1, 0, 'C', 0);
                $this->Cell(23.5, 5, ($obj_json[$nivel]["corridas"][$x]["concentracion_5"] == null) ? "N/A" : round($obj_json[$nivel]["corridas"][$x]["concentracion_5_menos_dia_al_2"], 2), 1, 1, 'C', 0);
            }

            $this->Ln(5);

            // Formulas primer nivel
            $this->SetX(13);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetFillColor(214, 219, 223);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 6);
            $this->Cell(31.66, 10, "", 1, 0, 'C', 0);
            $this->Cell(31.66, 10, "", 1, 0, 'C', 0);
            $this->Cell(31.66, 10, "", 1, 0, 'C', 0);
            $this->Cell(31.66, 10, "", 1, 0, 'C', 0);
            $this->Cell(31.66, 10, "", 1, 0, 'C', 0);
            $this->Cell(31.66, 10, "", 1, 0, 'C', 0);

            // DF1
      //       $this->Image(asset('img/formulas_ep15/df1.png'), 22, 147, 14);
            // DF2
      //       $this->Image(asset('img/formulas_ep15/df2.png'), 54, 147, 15);
            // MS1
      //       $this->Image(asset('img/formulas_ep15/ms1.png'), 82, 147, 20);
            // MS2
      //       $this->Image(asset('img/formulas_ep15/ms2.png'), 111.6, 147, 20);
      //       $this->Image(asset('img/formulas_ep15/ms2-2.png'), 131.6, 147, 6);
            // N0
      //       $this->Image(asset('img/formulas_ep15/n0.png'), 154, 146.5, 4);
            // VB
      //       $this->Image(asset('img/formulas_ep15/vb.png'), 176, 147, 24);

            // Recuadros de valores de las formulas
            $this->SetFont('Arial', '', 6);
            $this->SetDrawColor(218, 223, 227);
            // DF1
            $this->SetXY(23, 152);
            $this->Cell(12, 3, round($obj_json[$nivel]["corridas"]["general"]["df1"], 2), 0, 0, 'C', 1);
            // DF2
            $this->SetXY(55, 152);
            $this->Cell(12, 3, round($obj_json[$nivel]["corridas"]["general"]["df2"], 2), 0, 0, 'C', 1);
            // MS1
            $this->SetXY(87, 152);
            $this->Cell(12, 3, round($obj_json[$nivel]["corridas"]["general"]["ms1"], 2), 0, 0, 'C', 1);
            // MS2
            $this->SetXY(118, 152);
            $this->Cell(12, 3, round($obj_json[$nivel]["corridas"]["general"]["ms2"], 2), 0, 0, 'C', 1);
            // n sub 0
            $this->SetXY(150, 152);
            $this->Cell(12, 3, round($obj_json[$nivel]["corridas"]["general"]["n0"], 2), 0, 0, 'C', 1);
            // V sub b
            $this->SetXY(182, 152);
            $this->Cell(12, 3, round($obj_json[$nivel]["corridas"]["general"]["vb"], 2), 0, 0, 'C', 1);

            // Seccion de Sr y Swl
            $this->Ln(8);
            $this->SetXY(57, 160);

            $this->SetFont('Arial', '', 7);
            $this->Cell(30, 7, "", 1, 0, 'C', 0);
            $this->SetFont('Arial', 'B', 7);

            if ($obj_json[$nivel]["corridas"]["general"]["sr"] <= $obj_json[$nivel]["sigma_r"]) {
                $this->SetFillColor(242, 248, 238);
            } else {
                $this->SetFillColor(253, 243, 237);
            }

            $this->Cell(20, 7, round($obj_json[$nivel]["corridas"]["general"]["sr"], 2), 1, 0, 'C', 1);
            $this->SetFont('Arial', '', 7);
            $this->Cell(30, 7, "", 1, 0, 'C', 0);
            $this->SetFont('Arial', 'B', 7);

            if ($obj_json[$nivel]["corridas"]["general"]["swl"] <= $obj_json[$nivel]["sigma_wl"]) {
                $this->SetFillColor(242, 248, 238);
            } else {
                $this->SetFillColor(253, 243, 237);
            }

            $this->Cell(20, 7, round($obj_json[$nivel]["corridas"]["general"]["swl"], 2), 1, 0, 'C', 1);

            // Formula S R
      //       $this->Image(asset('img/formulas_ep15/sr.png'), 63, 161, 16);
            // Formula S WL
      //       $this->Image(asset('img/formulas_ep15/swl.png'), 110, 161, 21);


            // VI. Límite de verificación UVL
            $this->Ln(13);
            $this->SetX(10);
            $this->SetTextColor(40, 40, 40);
            $this->SetFillColor(172, 195, 212);
            $this->SetDrawColor(113, 181, 228);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 7.5);
            $this->Cell(196, 5, "VI. Límite de verificación superior UVL (Upper Verification Limit)", 1, 0, 'C', 1);

            $this->SetXY(10, 178.2);
            $this->SetDrawColor(150, 150, 150);
            $this->Cell(196, 12, "", "LRB", 0, 'C', 0);

            $this->Ln(2);
            $this->SetX(12);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetFillColor(214, 219, 223);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', '', 7);
            $this->Cell(63.3, 3.5, "dfR Repetibilidad: " . round($obj_json[$nivel]["corridas"]["general"]["df_r"], 2), 0, 0, 'C', 0);
            $this->Cell(63.3, 3.5, "Factor UVL R: " . round($obj_json[$nivel]["corridas"]["general"]["factor_uvl_r"], 2), 0, 0, 'C', 0);
            $this->Cell(63.3, 3.5, "UVL Repetibilidad: " . round($obj_json[$nivel]["corridas"]["general"]["uvl_repetibilidad"], 2), 0, 1, 'C', 0);

            $this->SetX(13);
            $this->Cell(63.3, 3.5, "dfWL Imprecisión intra-laboratorio: " . round($obj_json[$nivel]["corridas"]["general"]["df_wl"], 2) . " (p=" . round($obj_json[$nivel]["corridas"]["general"]["p"], 2) . ")", 0, 0, 'C', 0);
            $this->Cell(63.3, 3.5, "Factor UVL WL: " . round($obj_json[$nivel]["corridas"]["general"]["factor_uvl_wl"], 2), 0, 0, 'C', 0);
            $this->Cell(63.3, 3.5, "UVL intra-laboratorio: " . round($obj_json[$nivel]["corridas"]["general"]["uvl_intra_laboratorio"], 2), 0, 1, 'C', 0);


            // Condicional de verificación (Repetibilidad)
            if (round($obj_json[$nivel]["corridas"]["general"]["sr"], 2) < round($obj_json[$nivel]["sigma_r"], 2)) { // Si SR < σR || P1 < P2
                $this->SetXY(16, 181);
                $this->SetFillColor(240, 255, 240);
                $this->SetFont('Arial', 'B', 7.5);
                $this->Cell(90, 5, "La declaración de repetibilidad ha sido verificada por el laboratorio", 0, 0, 'C', 1);
            } else {
                $this->SetXY(16, 181);
                $this->SetFillColor(255, 245, 238);
                $this->SetFont('Arial', 'B', 7.5);
                $this->Cell(90, 5, "La declaración de repetibilidad NO ha sido verificada por el laboratorio", 0, 0, 'C', 1);
            }

            // Condicional de verificacion (Imprecisión)
            if (round($obj_json[$nivel]["corridas"]["general"]["swl"], 2) < round($obj_json[$nivel]["sigma_wl"], 2)) { // Si SWL < SWL || P3 < P4
                $this->SetXY(113, 181);
                $this->SetFillColor(240, 255, 240);
                $this->SetFont('Arial', 'B', 7.5);
                $this->Cell(90, 5, "La declaración de imprecisión ha sido verificada por el laboratorio", 0, 0, 'C', 1);
            } else {
                $this->SetXY(113, 181);
                $this->SetFillColor(255, 245, 238);
                $this->SetFont('Arial', 'B', 7.5);
                $this->Cell(90, 5, "La declaración de imprecisión NO ha sido verificada por el laboratorio", 0, 0, 'C', 1);
            }

            // VII. Estimación del sesgo
            $this->Ln(6);
            $this->SetX(10);
            $this->SetTextColor(40, 40, 40);
            $this->SetFillColor(172, 195, 212);
            $this->SetDrawColor(113, 181, 228);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 7.5);
            $this->Cell(196, 5, "VII. Estimación del sesgo", 1, 0, 'C', 1);
            $this->SetXY(10, 198.4);
            $this->SetDrawColor(150, 150, 150);
            $this->Cell(196, 60, "", "LRB", 0, 'C', 0);


            // Recuadros de delimitación INSERTO y GRUPO PAR
            $this->SetDrawColor(210, 210, 210);
            $this->SetXY(13, 200.5);
            $this->Cell(93.5, 55, "", 1, 0, 'C', 0);
            $this->SetXY(109.6, 200.5);
            $this->Cell(93.5, 55, "", 1, 0, 'C', 0);

            // Titulos de seccion Inserto y Grupo PAR
            $this->SetTextColor(70, 70, 70);
            $this->SetFont('Arial', 'B', 6.5);
            $this->SetXY(10, 201);
            $this->Cell(20, 4, "INSERTO", "", 0, 'C', 0);
            $this->SetXY(109, 201);
            $this->Cell(20, 4, "GRUPO PAR", "", 0, 'C', 0);

            // Primer nivel
            $this->SetFont('Arial', '', 6);
            $this->SetXY(14, 205);
            $this->Cell(20, 4, "Media", 0, 0, 'L', 0);
            $this->Cell(20, 4, "D.E.", 0, 0, 'L', 0);
            $this->Cell(20, 4, "U", 0, 0, 'L', 0);
            $this->Cell(20, 4, "k", 0, 0, 'L', 0);
            $this->SetXY(111, 205);
            $this->Cell(25, 4, "Media", 0, 0, 'L', 0);
            $this->Cell(25, 4, "D.E.", 0, 0, 'L', 0);
            $this->Cell(25, 4, "Nlab", 0, 0, 'L', 0);
            // Recuadros de campos
            $this->SetFont('Arial', '', 5.5);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetFillColor(214, 219, 223);
            $this->SetLineWidth(0.2);
            $this->SetXY(23.4, 205.5);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["media_inserto"],2), 0, 0, 'C', 1);
            $this->SetXY(41.2, 205.5);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["de_inserto"],2), 0, 0, 'C', 1);
            $this->SetXY(59.8, 205.5);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["u_incertidumbre"],2), 0, 0, 'C', 1);
            $this->SetXY(79.8, 205.5);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["k"],2), 0, 0, 'C', 1);
            $this->SetXY(121, 205.5);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["media_gpar"],2), 0, 0, 'C', 1);
            $this->SetXY(144, 205.5);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["de_gpar"],2), 0, 0, 'C', 1);
            $this->SetXY(169, 205.5);
            $this->Cell(7.5, 3, $obj_json[$nivel]["n_lab_gpar"], 0, 0, 'C', 1);


            // Formulas relacionadas con el inserto
            $this->SetXY(13, 213);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetFillColor(214, 219, 223);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 6);
            $this->Cell(31.17, 13, "", 1, 0, 'C', 0);
            $this->Cell(31.17, 13, "", 1, 0, 'C', 0);
            $this->Cell(31.17, 13, "", 1, 0, 'C', 0);
            $this->SetXY(13, 226);
            $this->Cell(31.17, 13, "", 1, 0, 'C', 0);
            $this->Cell(31.17, 13, "", 1, 0, 'C', 0);
            $this->Cell(31.17, 13, "", 1, 0, 'C', 0);

            // Formulas relacionadas con el grupo par
            $this->SetXY(109.6, 213);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetFillColor(214, 219, 223);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', 'B', 6);
            $this->Cell(31.17, 13, "", 1, 0, 'C', 0);
            $this->Cell(31.17, 13, "", 1, 0, 'C', 0);
            $this->Cell(31.17, 13, "", 1, 0, 'C', 0);
            $this->SetXY(109.6, 226);
            $this->Cell(31.17, 13, "", 1, 0, 'C', 0);
            $this->Cell(31.17, 13, "", 1, 0, 'C', 0);
            $this->Cell(31.17, 13, "", 1, 0, 'C', 0);



            // Recuadros de campos relacionados con el inserto
            $this->SetFont('Arial', '', 5.5);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetFillColor(214, 219, 223);
            $this->SetLineWidth(0.2);
            $this->SetXY(24.2, 222);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["corridas"]["general"]["se_x"], 2), 0, 0, 'C', 1);
            $this->SetXY(56, 222);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["corridas"]["general"]["se_rm_inserto"], 2), 0, 0, 'C', 1);
            $this->SetXY(87, 222);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["corridas"]["general"]["se_c_inserto"], 2), 0, 0, 'C', 1);
            $this->SetXY(24.2, 235);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["corridas"]["general"]["df_x"], 2), 0, 0, 'C', 1);
            $this->SetXY(53, 235);
            $this->Cell(13.5, 3, round($obj_json[$nivel]["corridas"]["general"]["dfc_inserto"], 2), 0, 0, 'C', 1);
            $this->SetXY(87, 235);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["corridas"]["general"]["m_inserto"], 2), 0, 0, 'C', 1);

      //       $this->Image(asset('img/formulas_ep15/sex.png'), 14.5, 215, 28);
      //       $this->Image(asset('img/formulas_ep15/serm_2.png'), 53, 214, 13);
      //       $this->Image(asset('img/formulas_ep15/sec.png'), 81, 214, 19);

      //       $this->Image(asset('img/formulas_ep15/dfx.png'), 22, 229, 16);
      //       $this->Image(asset('img/formulas_ep15/dfc_2.png'), 50, 229, 20);
      //       $this->Image(asset('img/formulas_ep15/m.png'), 82, 229, 18);


            // Recuadros de campos relacionados con el grupo par
            $this->SetFont('Arial', '', 5.5);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetFillColor(214, 219, 223);
            $this->SetLineWidth(0.2);
            $this->SetXY(121.2, 222);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["corridas"]["general"]["se_x"], 2), 0, 0, 'C', 1);
            $this->SetXY(153, 222);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["corridas"]["general"]["se_rm_gpar"], 2), 0, 0, 'C', 1);
            $this->SetXY(184, 222);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["corridas"]["general"]["se_c_gpar"], 2), 0, 0, 'C', 1);
            $this->SetXY(121.2, 235);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["corridas"]["general"]["calculo_tau"], 2), 0, 0, 'C', 1);
            $this->SetXY(153, 235);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["corridas"]["general"]["dfc_gpar"], 2), 0, 0, 'C', 1);
            $this->SetXY(184, 235);
            $this->Cell(7.5, 3, round($obj_json[$nivel]["corridas"]["general"]["m_gpar"], 2), 0, 0, 'C', 1);

      //       $this->Image(asset('img/formulas_ep15/sex.png'), 111, 215, 28);
      //       $this->Image(asset('img/formulas_ep15/serm.png'), 150, 214, 14);
      //       $this->Image(asset('img/formulas_ep15/sec.png'), 178, 214, 19);

      //       $this->Image(asset('img/formulas_ep15/tau.png'), 119.5, 228, 11.5);
      //       $this->Image(asset('img/formulas_ep15/dfc.png'), 154, 229, 4.5);
      //       $this->Image(asset('img/formulas_ep15/m.png'), 179, 229, 18);

            // Intervalos de verificación
      //       $this->Image(asset('img/formulas_ep15/vi.png'), 33, 243, 35);
            $this->SetXY(72.5, 243);
            $this->Cell(15, 3, round($obj_json[$nivel]["corridas"]["general"]["li_iv_inserto"], 2) . " - " . round($obj_json[$nivel]["corridas"]["general"]["ls_iv_inserto"], 2), 0, 0, 'C', 1);

      //       $this->Image(asset('img/formulas_ep15/vi.png'), 130, 243, 35);
            $this->SetXY(169.5, 243);
            $this->Cell(15, 3, round($obj_json[$nivel]["corridas"]["general"]["li_iv_gpar"], 2) . " - " . round($obj_json[$nivel]["corridas"]["general"]["ls_iv_gpar"], 2), 0, 0, 'C', 1);

            // Sexto nivel
            $this->SetXY(15, 249);
            $this->SetTextColor(40, 40, 40);
            $this->SetDrawColor(198, 203, 207);
            $this->SetLineWidth(0.2);
            $this->SetFont('Arial', '', 6);
            $this->Cell(26.755, 4, "Sesgo de la prueba %", 0, 0, 'C', 0);
            $this->SetFont('Arial', '', 6);

            if ($obj_json[$nivel]["corridas"]["general"]["sesgo_porcentaje_inserto"] <= $obj_json[$nivel]["smp_porcentaje"]) {
                $this->SetFillColor(242, 248, 238);
            } else {
                $this->SetFillColor(253, 243, 237);
            }

            $this->Cell(14, 4, round($obj_json[$nivel]["corridas"]["general"]["sesgo_porcentaje_inserto"], 2) . " %", 0, 0, 'C', 1);
            $this->SetFont('Arial', '', 6);
            $this->Cell(34.755, 4, "Sesgo de la prueba en unidades", 0, 0, 'C', 0);
            $this->SetFont('Arial', '', 6);

            if ($obj_json[$nivel]["corridas"]["general"]["sesgo_unidad_inserto"] <= $obj_json[$nivel]["smp_unidad"]) {
                $this->SetFillColor(242, 248, 238);
            } else {
                $this->SetFillColor(253, 243, 237);
            }

            $this->Cell(14, 4, round($obj_json[$nivel]["corridas"]["general"]["sesgo_unidad_inserto"], 2), 0, 0, 'C', 1);

            $this->SetXY(111.6, 249);
            $this->SetFont('Arial', '', 6);
            $this->Cell(26.755, 4, "Sesgo de la prueba %", 0, 0, 'C', 0);
            $this->SetFont('Arial', '', 6);

            if ($obj_json[$nivel]["corridas"]["general"]["sesgo_porcentaje_gpar"] <= $obj_json[$nivel]["smp_porcentaje"]) {
                $this->SetFillColor(242, 248, 238);
            } else {
                $this->SetFillColor(253, 243, 237);
            }

            $this->Cell(14, 4, round($obj_json[$nivel]["corridas"]["general"]["sesgo_porcentaje_gpar"], 2) . " %", 0, 0, 'C', 1);
            $this->SetFont('Arial', '', 6);
            $this->Cell(34.755, 4, "Sesgo de la prueba en unidades", 0, 0, 'C', 0);
            $this->SetFont('Arial', '', 6);

            if ($obj_json[$nivel]["corridas"]["general"]["sesgo_unidad_gpar"] <= $obj_json[$nivel]["smp_unidad"]) {
                $this->SetFillColor(242, 248, 238);
            } else {
                $this->SetFillColor(253, 243, 237);
            }

            $this->Cell(14, 4, round($obj_json[$nivel]["corridas"]["general"]["sesgo_unidad_gpar"], 2), 0, 1, 'C', 1);

            $this->SetXY(10, 300);
            $this->SetFont('Arial', 'B', 7.5);
            $this->SetDrawColor(113, 181, 228);
            $this->SetFillColor(172, 195, 212);
            $this->Cell(196, 5, "VII. Limite de grubbs        ", 1, 0, 'C', 1);

            $this->SetXY(10, 39.5);
            $this->SetFillColor(255, 255, 255);
            $this->SetDrawColor(198, 203, 207);
            $this->Cell(196, 30, "", 1, 0, 'C', 1);



            $text_result_inferior = ' + '. round($obj_json[$nivel]["corridas"]["general"]["gran_media"],2);
            $text_result_inferior = $text_result_inferior . ' - (' . $obj_json[$nivel]["corridas"]["general"]["factor_grubbs"];
            $text_result_inferior = $text_result_inferior . ' + ' . round($obj_json[$nivel]["corridas"]["general"]["gran_de"],2);

            $text_result_superior = ' + '. round($obj_json[$nivel]["corridas"]["general"]["gran_media"],2);
            $text_result_superior = $text_result_superior . ' + (' . $obj_json[$nivel]["corridas"]["general"]["factor_grubbs"];
            $text_result_superior = $text_result_superior . ' + ' . round($obj_json[$nivel]["corridas"]["general"]["gran_de"],2);

            $this->SetXY(10.5, 50);
            $this->SetFillColor(255, 255, 255);
            $this->SetFont('Arial');
            $this->Cell(98, 5, "Límite inferior de grubbs: ". $text_result_inferior, 0, 0, 'C', 1);
            $this->Cell(98, 5, "Límite superior de grubbs: ". $text_result_superior, 0, 0, 'C', 1);

            $this->SetXY(10.5, 55);
            $this->SetFont('Arial');
            $this->Cell(98, 5, "Límite inferior de grubbs: ". round($obj_json[$nivel]["corridas"]["general"]["limite_inferior_grubbs"], 2), 0, 0, 'C', 0);
            $this->Cell(98, 5, "Límite superior de grubbs: ". round($obj_json[$nivel]["corridas"]["general"]["limite_superior_grubbs"], 2), 0, 0, 'C', 0);

            // II Diagrama de flujo SR y SWL

            // $this->Cell(196,5,"II. Diagrama de flujo SR y SWL",1,0,'C',1);

        }
    }

    function CreateDocument2()
    {
        $this->AddPage();
    }

    function Footer()
    {
        // Sin nada
    }
}
