public function printviewAction()
 {
     require_once 'fpdf/pdf.php';
     $cdrequest = $_GET["cdrequest"];
     $Request = new RequestModel();
     $Attribute = new AttributeModel();
     $AttributeExaminationMaterial = new AppraisalcontrolModel();
     $Factor = new FactorModel();
     $array = array();
     $req = $Request->fetchAll($Request->getAllRequestData($cdrequest));
     if (count($req) <= 0) {
         die;
     }
     foreach ($req as $row) {
         $dtbirth = new Zend_Date($row['dtbirth'], 'dd-MM-YYYY');
         $dtrequest = new Zend_Date($row['dtrequest'], 'dd-MM-YYYY HH:mm:ss');
         $year = $dtbirth->toString('YYYY');
         $month = $dtbirth->toString('MM');
         $day = $dtbirth->toString('dd');
         $today = new Zend_Date();
         $tdYear = $today->toString('YYYY');
         $tdMonth = $today->toString('MM');
         $tdDay = $today->toString('dd');
         $nrage = 0;
         $nrage = GslabUtils::calcAgeDifference($row['dtbirth']);
         $idade = GslabUtils::calcula_idade($row['dtbirth']);
         if ($tdMonth < $month || $tdMonth == $month && $tdDay < $day) {
             $nrage--;
         }
         if ($row['fgsex'] == 1) {
             $fgsex = "Masculino";
         } else {
             $fgsex = "Feminino";
         }
         $array['nmclient'] = $row['nmclient'];
         $array['nmforward'] = $row['nmforward'];
         $array['nmcompany'] = $row['nmcompany'];
         $array['nmbarcodesample'] = $row['nmbarcodesample'];
         $array['nmcovenant'] = $row['nmcovenant'];
         $array['dtrequest'] = $dtrequest;
         $array['idade'] = $nrage;
         $array['sexo'] = $fgsex;
     }
     $GLOBALS = $array;
     $ano = explode(" ", $nrage);
     $pdf = new PDF();
     $pdf->AddPage();
     $pdf->ln(30);
     $pdf->AliasNbPages('{total}');
     $req2 = $Request->fetchAll($Request->getAllRequestData($cdrequest));
     $appraisalCommon = AppraisalCommon::getInstance($this);
     foreach ($req2 as $rows) {
         $fgstatus = true;
         /* NOME DO EXAME */
         $pdf->SetFont('Courier', 'BI', 11);
         $pdf->Cell(0, 10, strtoupper(utf8_decode($rows['nmexamination'])), 0, 2, 'L');
         $pdf->SetFont('Courier', '', 9);
         $pdf->Cell(0, 0, utf8_decode("Material: " . strtoupper($rows['nmmaterial'])), 0, 0, 'L');
         $pdf->Cell(-30, 0, utf8_decode("Método: Automação CELL-DYN 3000/3500"), 0, 2, 'R');
         $pdf->ln(2);
         // $pdf->Cell(250, 5, utf8_decode("Valores referenciais"), 0, 1, 'C');
         $pdf->ln(2);
         $sons = $this->getExamSons($rows['cdexamination']);
         if (count($sons) > 0) {
             for ($aux_i = 0; $aux_i < count($sons); $aux_i++) {
                 $pdf->SetFont('Courier', '', 10);
                 $pdf->Cell(-360, 5, strtoupper(utf8_decode($sons[$aux_i]['nmexamination'])), 0, 0, 'L');
                 /* NOME DO ATRIBUTO E VALOR */
                 $attributes = $appraisalCommon->getExamAttribs($sons[$aux_i]['cdexaminationmaterial']);
                 //print_r($attributes);
                 $fgstatus = true;
                 foreach ($attributes as $attr) {
                     $vlattr = '';
                     $cdattr = '';
                     $obj = new stdClass();
                     $obj->cdexaminationmaterial = $sons[$aux_i]['cdexaminationmaterial'];
                     $obj->cdrequest = $cdrequest;
                     $obj->cdexamination = $rows['cdexamination'];
                     $obj->cdattribute = $attr['cdattribute'];
                     $vlattribute = $appraisalCommon->getAppraisalAttributeValue($obj);
                     foreach ($vlattribute as $kAttr => $vAttr) {
                         if ($kAttr == 'vlattribute') {
                             $vlattr = $vAttr['vlattribute'];
                         }
                         if ($kAttr == 'cdattribute') {
                             $cdattr = $vAttr['cdattribute'];
                         }
                     }
                     $AttributeData = $Attribute->fetchAll($Attribute->getAtributeNotation($cdattr));
                     foreach ($AttributeData as $key => $value) {
                         if ($key == 'idnotation') {
                             $idnotation = $value['idnotation'];
                         }
                     }
                     $pdf->SetFont('Courier', '', 10);
                     $pdf->ln();
                     $pdf->Cell(40, 5, "    " . str_pad(utf8_decode($attr['nmattribute']), 15, "."), 0, 0, 'L');
                     $pdf->SetFont('Courier', 'B', 10);
                     $pdf->Cell(65, 5, str_pad($vlattr . " " . $idnotation, 15, ".", STR_PAD_LEFT), 0, 0, 'L');
                     $ref = $appraisalCommon->getExamFactors($sons[$aux_i]['cdexaminationmaterial'], $cdattr);
                     foreach ($ref as $vlref) {
                         if ($vlref['fgfactor'] == 1) {
                             $fator = "Idade            Valores referenciais";
                         } else {
                             if ($vlref['fgfactor'] == 2) {
                                 $fator = "Masculino                    Feminino";
                             } else {
                                 if ($vlref['fgfactor'] == 3) {
                                     $fator = "Idade       Masculino        Feminino";
                                 }
                             }
                         }
                     }
                     if ($fgstatus == true) {
                         $pdf->Cell(-1);
                         $pdf->Cell(1, -5, $fator, 0, 0, 'L');
                         $fgstatus = false;
                     }
                     /* VALORES DE REFERÊNCIA */
                     foreach ($ref as $vlref) {
                         /*
                          * AQUI SÃO DEFINIDAS AS REGRAS DE EXIBIÇÃO DO PDF, COLUNAS POR IDADE, SEXO OU AMBOS:
                          * 
                          * fgfactor = 1
                          * ATRIBUTO........VALOR        IDADE    REFERÊNCIA
                          * 
                          * fgfactor = 2
                          * ATRIBUTO........VALOR        MASCULINO    FEMININO
                          * 
                          * fgfactor = 3
                          * ATRIBUTO........VALOR        IDADE    MASCULINO    FEMININO
                          * 
                          */
                         $pdf->SetFont('Courier', '', 9);
                         /* DEFINE QUAL CAMPO DA TABELA ESTÁ PREENCHIDO (VALOR OU NUMÉRICO) E ALOCA À UMA VARIÁVEL */
                         if ($vlref['nrminvalue'] == '' && $vlref['nrmaxvalue'] == '' && $vlref['vlminvalue'] != '' && $vlref['vlmaxvalue'] != '') {
                             $minvalue = $vlref['vlminvalue'];
                             $maxvalue = $vlref['vlmaxvalue'];
                         } else {
                             if ($vlref['nrminvalue'] != '' && $vlref['nrmaxvalue'] != '' && $vlref['vlminvalue'] == '' && $vlref['vlmaxvalue'] == '') {
                                 $minvalue = $vlref['nrminvalue'];
                                 $maxvalue = $vlref['nrmaxvalue'];
                             }
                         }
                         if ($vlref['fgfactor'] == 1) {
                             //SEPARADOS PELA IDADE, DEFINE UMA COLUNA DE RESULTADOS
                             if ($vlref['nrminage'] <= $ano[0] && $vlref['nrmaxage'] >= $ano[0]) {
                                 $pdf->Cell(12, 5, utf8_decode($vlref['nrminage'] . " à " . $vlref['nrmaxage'] . " " . $vlref['nmtimetype']), 0, 0, 'R');
                                 $pdf->Cell(67, 5, utf8_decode($minvalue . " à " . $maxvalue), 0, 0, 'R');
                             }
                         } else {
                             if ($vlref['fgfactor'] == 2) {
                                 //SEPARADOS PELO SEXO, DEFINE DUAS COLUNAS DE RESULTADOS
                                 if ($vlref['fggender'] == 1) {
                                     // MASCULINO
                                     $pdf->Cell(20, 5, utf8_decode($vlref['vlminvalue'] . " à " . $vlref['vlmaxvalue']), 0, 0, 'R');
                                 } else {
                                     if ($vlref['fggender'] == 2) {
                                         // FEMININO
                                         $pdf->Cell(60, 5, utf8_decode($vlref['vlminvalue'] . " à " . $vlref['vlmaxvalue']), 0, 1, 'R');
                                     }
                                 }
                             } else {
                                 if ($vlref['fgfactor'] == 3) {
                                     //SEPARADOS PELO SEXO E IDADE, DEFINE TRÊS COLUNAS DE RESULTADOS
                                     if ($vlref['fggender'] == 1) {
                                         // MASCULINO
                                         $pdf->Cell(12, 5, utf8_decode($vlref['nrminage'] . " à " . $vlref['nrmaxage'] . " " . $vlref['nmtimetype']), 0, 0, 'R');
                                         $pdf->Cell(33, 5, utf8_decode($vlref['vlminvalue'] . " à " . $vlref['vlmaxvalue']), 0, 0, 'R');
                                     } else {
                                         if ($vlref['fggender'] == 2) {
                                             // FEMININO
                                             $pdf->Cell(35, 5, utf8_decode($vlref['vlminvalue'] . " à " . $vlref['vlmaxvalue']), 0, 1, 'R');
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     unset($obj);
                 }
                 $pdf->ln();
                 $pdf->ln();
             }
         }
     }
     $pdf->Output();
     //IMPRIME O PDF NA TELA
     $pdf->Output("teste.pdf");
     //IMPRIME O PDF NO ARQUIVO
 }
    public function generateprotocolAction($cdrequest = 1)
    {
        require_once 'fpdf/pdf_protocol.php';
        $gslabSession = Zend_Session::namespaceGet('Gslab_Session');
        $cdcompany = $gslabSession['user']->cdcompany;
        $companyModel = new CompanyModel();
        $requestModel = new RequestModel();
        $sampleModel = new RequestexaminationsampleModel();
        $clientModel = new ClientModel();
        $covenantModel = new CovenantModel();
        $forwardModel = new ForwardModel();
        $examinationModel = new RequestexaminationModel();
        $companyData = $companyModel->fetchRow('cdcompany = ' . $cdcompany);
        $requestData = $requestModel->fetchRow('cdrequest = ' . $cdrequest);
        $sampleData = $sampleModel->fetchRow('cdrequest = ' . $cdrequest);
        $clientData = $clientModel->fetchRow('cdclient = ' . $requestData->cdclient);
        $covenantData = $covenantModel->fetchRow('cdcovenant = ' . $requestData->cdcovenant);
        $forwardData = $forwardModel->fetchRow('cdforward = ' . $requestData->cdforward);
        $dtbirth = new Zend_Date($clientData->dtbirth, 'dd-MM-YYYY');
        $dtrequest = new Zend_Date($requestData->dtrequest, 'dd-MM-YYYY HH:mm');
        $dtdeadline = new Zend_Date($requestData->dtdeadline, 'dd-MM-YYYY HH:mm');
        $year = $dtbirth->toString('YYYY');
        $month = $dtbirth->toString('MM');
        $day = $dtbirth->toString('dd');
        $today = new Zend_Date();
        $tdYear = $today->toString('YYYY');
        $tdMonth = $today->toString('MM');
        $tdDay = $today->toString('dd');
        $nrage = 0;
        $nrage = GslabUtils::calcAgeDifferenceShort($clientData->dtbirth);
        $idade = GslabUtils::calcula_idade($clientData->dtbirth);
        if ($clientData->fgsex == 1) {
            $sexo = 'M';
        } else {
            $sexo = 'F';
        }
        if ($tdMonth < $month || $tdMonth == $month && $tdDay < $day) {
            $nrage--;
        }
        $pdf = new PDF_PROTOCOL();
        $pdf->AddPage();
        $pdf->SetFont('Courier', 'B', 11);
        $pdf->ln(55);
        $pdf->Cell(0, 0, utf8_decode(strtoupper($companyData->nmcompany)), 0, 1, 'C');
        $pdf->Cell(0, 8, utf8_decode('LABORATÓRIO DE ANÁLISES CLÍNICAS'), 0, 2, 'C');
        $pdf->Cell(0, -5, str_repeat('_', 81), 0, 1, 'L');
        $pdf->ln(8);
        $pdf->SetFont('Helvetica', '', 9);
        $pdf->Cell(0, 0, utf8_decode(strtoupper($companyData->nmstreet) . ', ' . $companyData->nrnumber), 0, 1, 'L');
        $pdf->ln(4);
        $pdf->Cell(0, 0, 'COMPROVANTE DE COLETA', 0, 1, 'L');
        $pdf->Cell(0, 0, utf8_decode(strtoupper($companyData->nmneighborhood)) . ' - Fone: (47) 3427-3668', 0, 1, 'R');
        //ADICIONAR TELEFONE NA BASE
        $pdf->ln(4);
        $pdf->Cell(0, 0, utf8_decode('Ficha: ') . $requestData->cdcompany . "-" . $requestData->cdrequest, 0, 1, 'L');
        $pdf->Cell(0, 0, 'Senha: 2301', 0, 1, 'R');
        $pdf->ln(8);
        $pdf->Cell(0, 0, utf8_decode('PRONTUÁRIO'), 0, 1, 'L');
        $pdf->ln(4);
        $pdf->SetFont('Helvetica', '', 10);
        $pdf->Cell(15, 5, utf8_decode('Amostra: '), 0, 0, 'L');
        $pdf->Cell(0, 5, $pdf->SetFont('Helvetica', 'BU', 10) . $sampleData->nmbarcodesample, 0, 1, 'L');
        $pdf->ln(4);
        $pdf->SetFont('Helvetica', 'BU', 10);
        $pdf->Write(0, utf8_decode(strtoupper($clientData->nmclient)));
        $pdf->SetFont('Helvetica', '', 10);
        $pdf->ln(8);
        $pdf->Write(0, utf8_decode('Idade: ' . $nrage));
        $pdf->Write(0, utf8_decode(' Sexo: ' . $sexo));
        $pdf->ln(6);
        $pdf->Write(0, utf8_decode('Convênio: ' . $covenantData->nmcovenant));
        $pdf->ln(6);
        $pdf->Write(0, utf8_decode('Médico: ' . $forwardData->idmaincrm . ' - ' . $forwardData->nmforward));
        $pdf->ln(6);
        $pdf->Write(0, utf8_decode('Coleta: ' . strtoupper($companyData->cdcompany . ' - ' . $companyData->nmfantasyname)));
        $pdf->ln(6);
        $pdf->Write(0, utf8_decode('Data e Hora: ') . $dtrequest);
        $pdf->ln(4);
        $pdf->SetFont('Courier', 'B', 11);
        $pdf->Cell(0, 0, str_repeat('_', 81), 0, 1, 'L');
        /* CAIXA LATERAL */
        $pdf->SetFont('Helvetica', '', 10);
        $pdf->SetY(89);
        $pdf->SetX(90);
        $pdf->drawTextBox(utf8_decode('Provável local e data da entrega:
Local: ' . strtoupper($companyData->nmfantasyname) . '
Data: ' . $dtdeadline . '
Salvo quando ocorrer:
    1-	Imprevistos técnicos
    2-	Bloqueio de resultado pelo controle de qualidade
    3-	Atraso no fornecimento de reagentes
Autorizo a coleta do(s) exame(s) acima citado(s) e declaro estar ciente dos riscos quanto ao surgimento de possíveis hematomas no local da punção.
Assinatura do cliente e/ou responsável:__________________________________________'), 109, 50, 'L', 'M');
        /* doze exames */
        $Exames = $examinationModel->fetchAll($examinationModel->getExaminationProtocol($cdrequest));
        $pdf->ln(13);
        // Exames
        $pdf->SetFont('Helvetica', 'B', 9);
        $pdf->SetWidths(array(50, 23, 18));
        $pdf->SetX(10);
        $pdf->Row(array(utf8_decode('Exames'), utf8_decode('Material'), utf8_decode('Status')));
        $pdf->SetFont('Helvetica', '', 9);
        $i = 0;
        foreach ($Exames as $examination) {
            if ($examination['nmmaterial'] == 2) {
                $status = 'Faltante';
            } else {
                $status = 'OK';
            }
            if ($i == 11) {
                $pdf->SetY(148);
            }
            $pdf->SetX(10);
            $pdf->Row(array(utf8_decode($examination['nmexamination']), utf8_decode($examination['nmmaterial']), $status));
            $i++;
        }
        $pdf->ln(5);
        /* -------------------------------------------------------------- */
        /* PESQUISA DE SATISFAÇÃO */
        $pdf->SetFont('Helvetica', '', 7);
        $pdf->SetY(205);
        $pdf->Cell(0, 0, str_repeat(' -', 125), 0, 1, 'L');
        $pdf->SetX(10);
        $pdf->Write(5, utf8_decode('Prezado cliente: com o objetivo de melhorarmos cada vez mais a'));
        $pdf->ln(3);
        $pdf->SetX(10);
        $pdf->Write(5, utf8_decode('qualidade dos nossos serviços e o atendimento à você, solicitamos'));
        $pdf->ln(3);
        $pdf->SetX(10);
        $pdf->Write(5, utf8_decode('a sua gentileza preenchendo o formulário abaixo.'));
        $pdf->ln(5);
        $pdf->SetFont('Helvetica', 'B', 7);
        $pdf->SetWidths(array(30, 13, 13, 13, 13));
        $pdf->SetX(10);
        $pdf->Row(array(utf8_decode('Assinale sua opinião'), utf8_decode('Ótimo'), 'Bom', 'Regular', 'Ruim'));
        $pdf->SetFont('Helvetica', '', 7);
        $pdf->SetX(10);
        $pdf->Row(array(utf8_decode('1. Como foi o atendimento ao telefone?'), '', '', '', ''));
        $pdf->SetX(10);
        $pdf->Row(array(utf8_decode('2. Como foi o atendimento da recepção?'), '', '', '', ''));
        $pdf->SetX(10);
        $pdf->Row(array(utf8_decode('3. Quanto ao conforto e bem estar na sala de espera?'), '', '', '', ''));
        $pdf->SetX(10);
        $pdf->Row(array(utf8_decode('4. Quanto à coleta do material para o exame?'), '', '', '', ''));
        $pdf->SetX(10);
        $pdf->Row(array(utf8_decode('5. Quanto ao prazo e entrega dos resultados?'), '', '', '', ''));
        $pdf->SetY(215);
        $pdf->SetX(117);
        $pdf->Write(5, utf8_decode('6. Como conheceu o Laboratório? _______________________________'));
        $pdf->ln(7);
        $pdf->SetX(117);
        $pdf->Write(5, utf8_decode('7. Você indicaria o laboratório para outras pessoas?  (  ) Sim   (  ) Não'));
        $pdf->ln(7);
        $pdf->SetX(117);
        $pdf->Write(5, utf8_decode('Se NÃO, por quê?____________________________________________'));
        $pdf->ln(7);
        $pdf->SetX(117);
        $pdf->Write(5, utf8_decode('8. Conhece nosso '));
        $pdf->SetFont('Helvetica', 'BU', 7);
        $pdf->Write(5, utf8_decode('Programa de Coleta Domiciliar'));
        $pdf->SetFont('Helvetica', '', 7);
        $pdf->Write(5, utf8_decode(' (tel: 3433-2127)?'));
        $pdf->ln(7);
        $pdf->SetX(117);
        $pdf->Write(5, utf8_decode('(  ) Sim   (  ) Não'));
        $pdf->ln(7);
        $pdf->SetX(117);
        $pdf->Write(5, utf8_decode('9. Sugestões e críticas:_______________________________________'));
        $pdf->ln(7);
        $pdf->SetX(117);
        $pdf->Write(5, utf8_decode('__________________________________________________________'));
        $pdf->ln(7);
        $pdf->SetX(117);
        $pdf->Write(5, utf8_decode('__________________________________________________________'));
        $pdf->Output();
        //IMPRIME O PDF NA TELA
        $pdf->Output('teste_pro.pdf', "D");
        //IMPRIME O PDF NO ARQUIVO
    }
 public function printviewAction()
 {
     require_once 'fpdf/pdf.php';
     $cdrequest = $_GET["cdrequest"];
     $Request = new RequestModel();
     $Factor = new FactorModel();
     $Method = new MethodModel();
     $Comment = new AppraisalcommentModel();
     $Patterntext = new PatterntextModel();
     $array = array();
     $req = $Request->fetchAll($Request->getAllRequestData($cdrequest));
     if (count($req) <= 0) {
         die;
     }
     foreach ($req as $row) {
         $dtbirth = new Zend_Date($row['dtbirth'], 'dd-MM-YYYY');
         $dtrequest = new Zend_Date($row['dtrequest'], 'dd-MM-YYYY HH:mm:ss');
         $year = $dtbirth->toString('YYYY');
         $month = $dtbirth->toString('MM');
         $day = $dtbirth->toString('dd');
         $today = new Zend_Date();
         $tdYear = $today->toString('YYYY');
         $tdMonth = $today->toString('MM');
         $tdDay = $today->toString('dd');
         $nrage = 0;
         $nrage = GslabUtils::calcAgeDifference($row['dtbirth']);
         $idade = GslabUtils::calcula_idade($row['dtbirth']);
         if ($tdMonth < $month || $tdMonth == $month && $tdDay < $day) {
             $nrage--;
         }
         if ($row['fgsex'] == 1) {
             $fgsex = "Masculino";
         } else {
             $fgsex = "Feminino";
         }
         $array['nmclient'] = $row['nmclient'];
         $array['nmforward'] = $row['nmforward'];
         $array['nmcompany'] = $row['nmcompany'];
         $array['nmbarcodesample'] = $row['nmbarcodesample'];
         $array['nmcovenant'] = $row['nmcovenant'];
         $array['dtrequest'] = $dtrequest;
         $array['idade'] = $nrage;
         $array['sexo'] = $fgsex;
     }
     $GLOBALS = $array;
     $ano = explode(" ", $nrage);
     $pdf = new PDF();
     $pdf->AddPage();
     $pdf->ln(30);
     $pdf->AliasNbPages('{total}');
     $req2 = $Request->fetchAll($Request->getAllRequestData($cdrequest));
     foreach ($req2 as $rows) {
         $comentario_text = '';
         /* COMENTÁRIO DOS EXAMES */
         $comments = $Comment->fetchRow($Comment->getAppraisalComments($rows['cdrequestexamination']));
         if (count($comments) > 0) {
             $dscomment = explode(',', $comments->dscomment);
             foreach ($dscomment as $comm) {
                 $comentario = $Patterntext->fetchRow($Patterntext->getPatterntextById($comm));
                 $comentario_text .= utf8_decode($comentario->dspatterntext) . "\n";
             }
         } else {
             $comentario_text = '';
         }
         /* MÉTODO DOS EXAMES */
         $method = $Method->fetchRow($Method->getMethodByCd($rows['cdmethod']));
         $metodo = utf8_decode("Método: " . $method->nmmethod);
         $fgstatus = true;
         /* NOME DO EXAME */
         $pdf->SetFont('Courier', 'BI', 11);
         $pdf->Cell(0, 10, strtoupper(utf8_decode($rows['nmexamination'])), 0, 2, 'L');
         $pdf->SetFont('Courier', '', 9);
         $pdf->Cell(0, 0, utf8_decode("Material: " . strtoupper($rows['nmmaterial'])), 0, 0, 'L');
         $pdf->Cell(-30, 0, $metodo, 0, 1, 'R');
         $pdf->ln(2);
         $pdf->ln(2);
         $sons = $this->getExamSons($rows['cdexamination'], $cdrequest);
         if (count($sons) > 0) {
             for ($aux_i = 0; $aux_i < count($sons); $aux_i++) {
                 $pdf->SetFont('Courier', '', 9);
                 $pdf->Cell(-15, 5, strtoupper(utf8_decode($sons[$aux_i]['nmexamination'])), 0, 0, 'L');
                 /* NOME DO ATRIBUTO E VALOR */
                 $attributes = $this->getAttrValues($sons[$aux_i]['cdrequestexamination'], $sons[$aux_i]['cdexaminationmaterial']);
                 if ($sons[$aux_i]['fgpercentage'] == 1) {
                     $soma = 0;
                     foreach ($attributes as $sum) {
                         $soma = $soma + $sum['vlattribute'];
                     }
                     $valor_calc = $soma / 100;
                     $pdf->SetFont('Courier', 'B', 9);
                     $pdf->Cell(100, 5, "%", 0, 0, 'R');
                 }
                 $fgstatus = true;
                 foreach ($attributes as $attr) {
                     if ($sons[$aux_i]['fgpercentage'] == 1) {
                         $valor = round($attr['vlattribute'] / $valor_calc, 1);
                         $n = explode(".", $valor);
                         if ($n[1] < 5 && $n[1] != 5) {
                             $vl_at = floor($valor);
                         } else {
                             if ($n[1] > 5 && $n[1] != 5) {
                                 $vl_at = ceil($valor);
                             } else {
                                 if ($n[1] == 5) {
                                     $vl_at = $valor;
                                 }
                             }
                         }
                     } else {
                         $vl_at = '';
                         $valor = '';
                     }
                     $pdf->SetFont('Courier', '', 9);
                     $pdf->ln();
                     $pdf->Cell(40, 5, "    " . str_pad(utf8_decode($attr['nmattribute']), 18, "."), 0, 0, 'L');
                     $pdf->SetFont('Courier', 'B', 9);
                     $pdf->Cell(65, 5, str_pad(utf8_decode($attr['vlattribute']) . " " . utf8_decode($attr['idnotation']), 15, ".", STR_PAD_LEFT) . "     " . $vl_at, 0, 0, 'L');
                     $ref = $Factor->fetchAll($Factor->getAllFactors($attr['cdattribute']));
                     //                        var_dump($ref);die;
                     foreach ($ref as $vlref) {
                         if ($vlref['fgfactor'] == 1) {
                             $fator = "Idade            Valores referenciais";
                         } else {
                             if ($vlref['fgfactor'] == 2) {
                                 $fator = "Masculino                    Feminino";
                             } else {
                                 if ($vlref['fgfactor'] == 3) {
                                     $fator = "Idade       Masculino        Feminino";
                                 }
                             }
                         }
                     }
                     if ($fgstatus == true) {
                         $pdf->Cell(-1);
                         $pdf->Cell(1, -5, $fator, 0, 0, 'L');
                         $fgstatus = false;
                     }
                     /* VALORES DE REFERÊNCIA */
                     foreach ($ref as $vlref) {
                         /*
                          * AQUI SÃO DEFINIDAS AS REGRAS DE EXIBIÇÃO DO PDF, COLUNAS POR IDADE, SEXO OU AMBOS:
                          * 
                          * fgfactor = 1
                          * ATRIBUTO........VALOR        IDADE    REFERÊNCIA
                          * 
                          * fgfactor = 2
                          * ATRIBUTO........VALOR        MASCULINO    FEMININO
                          * 
                          * fgfactor = 3
                          * ATRIBUTO........VALOR        IDADE    MASCULINO    FEMININO
                          * 
                          */
                         $pdf->SetFont('Courier', '', 8);
                         /* DEFINE QUAL CAMPO DA TABELA ESTÁ PREENCHIDO (VALOR OU NUMÉRICO) E ALOCA À UMA VARIÁVEL */
                         if ($vlref['nrminvalue'] == '' && $vlref['nrmaxvalue'] == '' && $vlref['vlminvalue'] != '' && $vlref['vlmaxvalue'] != '') {
                             $minvalue = $vlref['vlminvalue'];
                             $maxvalue = $vlref['vlmaxvalue'];
                         } else {
                             if ($vlref['nrminvalue'] != '' && $vlref['nrmaxvalue'] != '' && $vlref['vlminvalue'] == '' && $vlref['vlmaxvalue'] == '') {
                                 $minvalue = $vlref['nrminvalue'];
                                 $maxvalue = $vlref['nrmaxvalue'];
                             }
                         }
                         if ($vlref['fgfactor'] == 1) {
                             //SEPARADOS PELA IDADE, DEFINE UMA COLUNA DE RESULTADOS
                             if ($vlref['nrminage'] <= $ano[0] && $vlref['nrmaxage'] >= $ano[0]) {
                                 $pdf->Cell(12, 5, utf8_decode($vlref['nrminage'] . " à " . $vlref['nrmaxage'] . " " . $vlref['nmtimetype']), 0, 0, 'R');
                                 $pdf->Cell(67, 5, utf8_decode($minvalue . " à " . $maxvalue), 0, 0, 'R');
                             }
                         } else {
                             if ($vlref['fgfactor'] == 2) {
                                 //SEPARADOS PELO SEXO, DEFINE DUAS COLUNAS DE RESULTADOS
                                 if ($vlref['fggender'] == 1) {
                                     // MASCULINO
                                     $pdf->Cell(20, 5, utf8_decode($vlref['vlminvalue'] . " à " . $vlref['vlmaxvalue']), 0, 0, 'R');
                                 } else {
                                     if ($vlref['fggender'] == 2) {
                                         // FEMININO
                                         $pdf->Cell(60, 5, utf8_decode($vlref['vlminvalue'] . " à " . $vlref['vlmaxvalue']), 0, 0, 'R');
                                     }
                                 }
                             } else {
                                 if ($vlref['fgfactor'] == 3) {
                                     //SEPARADOS PELO SEXO E IDADE, DEFINE TRÊS COLUNAS DE RESULTADOS
                                     if ($vlref['fggender'] == 1) {
                                         // MASCULINO
                                         $pdf->Cell(12, 5, utf8_decode($vlref['nrminage'] . " à " . $vlref['nrmaxage'] . " " . $vlref['nmtimetype']), 0, 0, 'R');
                                         $pdf->Cell(33, 5, utf8_decode($vlref['vlminvalue'] . " à " . $vlref['vlmaxvalue']), 0, 0, 'R');
                                     } else {
                                         if ($vlref['fggender'] == 2) {
                                             // FEMININO
                                             $pdf->Cell(35, 5, utf8_decode($vlref['vlminvalue'] . " à " . $vlref['vlmaxvalue']), 0, 0, 'R');
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $pdf->ln();
                 $pdf->ln();
             }
         } else {
             $examination = $this->getExam($rows['cdexamination'], $cdrequest);
             for ($aux_i = 0; $aux_i < count($examination); $aux_i++) {
                 /* NOME DO ATRIBUTO E VALOR */
                 $attributes = $this->getAttrValues($examination[$aux_i]['cdrequestexamination'], $examination[$aux_i]['cdexaminationmaterial']);
                 if ($examination[$aux_i]['fgpercentage'] == 1) {
                     $soma = 0;
                     foreach ($attributes as $sum) {
                         $soma = $soma + $sum['vlattribute'];
                     }
                     $valor_calc = $soma / 100;
                     $pdf->SetFont('Courier', 'B', 9);
                     $pdf->Cell(83, 5, "%", 0, 0, 'R');
                 }
                 $rangeruleCount = count($attributes);
                 $fgstatus = true;
                 foreach ($attributes as $attr) {
                     if ($examination[$aux_i]['fgpercentage'] == 1) {
                         $valor = round($attr['vlattribute'] / $valor_calc, 1);
                         $n = explode(".", $valor);
                         if ($n[1] < 5 && $n[1] != 5) {
                             $vl_at = floor($valor);
                         } else {
                             if ($n[1] > 5 && $n[1] != 5) {
                                 $vl_at = ceil($valor);
                             } else {
                                 if ($n[1] == 5) {
                                     $vl_at = $valor;
                                 }
                             }
                         }
                     } else {
                         $vl_at = '';
                         $valor = '';
                     }
                     $pdf->SetFont('Courier', '', 9);
                     $pdf->ln();
                     $pdf->Cell(40, 15, "    " . str_pad(utf8_decode($attr['nmattribute']), 18, "."), 0, 0, 'L');
                     $pdf->SetFont('Courier', 'B', 9);
                     $pdf->Cell(65, 15, str_pad($attr['vlattribute'] . " " . $attr['idnotation'], 15, ".", STR_PAD_LEFT) . "     " . $vl_at, 0, 0, 'L');
                     $ref = $Factor->fetchAll($Factor->getAllFactors($attr['cdattribute']));
                     foreach ($ref as $vlref) {
                         if ($vlref['fgfactor'] == 1) {
                             $fator = "Idade            Valores referenciais";
                         } else {
                             if ($vlref['fgfactor'] == 2) {
                                 $fator = "Masculino                    Feminino";
                             } else {
                                 if ($vlref['fgfactor'] == 3) {
                                     $fator = "Idade       Masculino        Feminino";
                                 }
                             }
                         }
                     }
                     if ($fgstatus == true) {
                         $pdf->Cell(-1);
                         $pdf->Cell(1, 5, $fator, 0, 0, 'L');
                         $fgstatus = false;
                     }
                     /* VALORES DE REFERÊNCIA */
                     foreach ($ref as $vlref) {
                         /*
                          * AQUI SÃO DEFINIDAS AS REGRAS DE EXIBIÇÃO DO PDF, COLUNAS POR IDADE, SEXO OU AMBOS:
                          * 
                          * fgfactor = 1
                          * ATRIBUTO........VALOR        IDADE    REFERÊNCIA
                          * 
                          * fgfactor = 2
                          * ATRIBUTO........VALOR        MASCULINO    FEMININO
                          * 
                          * fgfactor = 3
                          * ATRIBUTO........VALOR        IDADE    MASCULINO    FEMININO
                          * 
                          */
                         $pdf->SetFont('Courier', '', 8);
                         $imgRR = "./images/result/rangerule_img-" . $attr['cdrequestexamination'] . "_" . $rows['cdexamination'] . "_" . $attr['cdattribute'] . "_" . $examination[$aux_i]['cdexaminationmaterial'] . ".png";
                         /* DEFINE QUAL CAMPO DA TABELA ESTÁ PREENCHIDO (VALOR OU NUMÉRICO) E ALOCA À UMA VARIÁVEL */
                         if ($vlref['nrminvalue'] == '' && $vlref['nrmaxvalue'] == '' && $vlref['vlminvalue'] != '' && $vlref['vlmaxvalue'] != '') {
                             $minvalue = $vlref['vlminvalue'];
                             $maxvalue = $vlref['vlmaxvalue'];
                         } else {
                             if ($vlref['nrminvalue'] != '' && $vlref['nrmaxvalue'] != '' && $vlref['vlminvalue'] == '' && $vlref['vlmaxvalue'] == '') {
                                 $minvalue = $vlref['nrminvalue'];
                                 $maxvalue = $vlref['nrmaxvalue'];
                             }
                         }
                         if ($vlref['fgfactor'] == 1) {
                             //SEPARADOS PELA IDADE, DEFINE UMA COLUNA DE RESULTADOS
                             if ($vlref['nrminage'] <= $ano[0] && $vlref['nrmaxage'] >= $ano[0]) {
                                 $pdf->Cell(12, 15, utf8_decode($vlref['nrminage'] . " à " . $vlref['nrmaxage'] . " " . $vlref['nmtimetype']), 0, 0, 'R');
                                 if ($rangeruleCount == 1) {
                                     $pdf->Cell(40, 50, $pdf->Image($imgRR, $pdf->GetX() + 5, $pdf->GetY() + 6, 70), 0, 0, 'L', false);
                                 }
                                 $pdf->Cell(67, 8, utf8_decode($minvalue . " à " . $maxvalue), 0, 0, 'R');
                             }
                         } else {
                             if ($vlref['fgfactor'] == 2) {
                                 //SEPARADOS PELO SEXO, DEFINE DUAS COLUNAS DE RESULTADOS
                                 if ($vlref['fggender'] == 1) {
                                     // MASCULINO
                                     $pdf->Cell(20, 5, utf8_decode($vlref['vlminvalue'] . " à " . $vlref['vlmaxvalue']), 0, 0, 'R');
                                 } else {
                                     if ($vlref['fggender'] == 2) {
                                         // FEMININO
                                         $pdf->Cell(60, 5, utf8_decode($vlref['vlminvalue'] . " à " . $vlref['vlmaxvalue']), 0, 1, 'R');
                                     }
                                 }
                             } else {
                                 if ($vlref['fgfactor'] == 3) {
                                     //SEPARADOS PELO SEXO E IDADE, DEFINE TRÊS COLUNAS DE RESULTADOS
                                     if ($vlref['fggender'] == 1) {
                                         // MASCULINO
                                         $pdf->Cell(12, 5, utf8_decode($vlref['nrminage'] . " à " . $vlref['nrmaxage'] . " " . $vlref['nmtimetype']), 0, 0, 'R');
                                         $pdf->Cell(33, 5, utf8_decode($vlref['vlminvalue'] . " à " . $vlref['vlmaxvalue']), 0, 0, 'R');
                                     } else {
                                         if ($vlref['fggender'] == 2) {
                                             // FEMININO
                                             $pdf->Cell(35, 5, utf8_decode($vlref['vlminvalue'] . " à " . $vlref['vlmaxvalue']), 0, 1, 'R');
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $pdf->ln();
                 $pdf->ln();
             }
         }
         if (count($comments) > 0) {
             $pdf->SetFont('Courier', 'B', 9);
             $pdf->Cell(5, 2, utf8_decode("Observações: "), 0, 1, 'L');
             $pdf->ln();
             $pdf->SetFont('Courier', '', 7);
             $pdf->Multicell(0, 3, $comentario_text);
             $pdf->ln();
         }
     }
     $pdf->Output();
     //IMPRIME O PDF NA TELA
     $pdf->Output('teste.pdf', "D");
     //IMPRIME O PDF NO ARQUIVO
 }