$resultref = db_query($sqlref);
    $linhasref = pg_num_rows($resultref);
    if ($linhasref > 0) {
        db_fieldsmemory($resultref, 0);
        $matricula1 = $j40_matric;
        $matricula = $j40_matric;
    }
}
if (isset($inscricao)) {
    $q02_inscr = $inscricao;
}
//busca dados para armazemar em cookies
if (@$_COOKIE["cookie_codigo_cgm"] == "") {
    // issbase
    if (@$inscricaow != "") {
        @($result = $clissbase->sql_record($clissbase->sql_query("", "cgm.z01_numcgm,cgm.z01_nome", "", "issbase.q02_inscr = {$inscricaow} and trim(cgm.z01_cgccpf) = '{$cgccpf}'")));
        @($linhas1 = $clissbase->numrows);
    } else {
        if (!empty($codigo_cgm) || !empty($cgc) || !empty($cpf)) {
            //CGM
            if (!empty($codigo_cgm)) {
                $condicao = "cgm.z01_numcgm = {$codigo_cgm}";
            } else {
                if (!empty($cgccpf)) {
                    $condicao = "trim(cgm.z01_cgccpf) = '{$cgccpf}'";
                }
            }
            @($result = $clcgm->sql_record($clcgm->sql_query("", "cgm.z01_numcgm,cgm.z01_nome", "", $condicao)));
            @($linhas2 = $clcgm->numrows);
        } else {
            if (@$matricula1 != "") {
 $pdf->Cell(34, 4, "TIPO LANÇAMENTO ", 1, 1, "C", 1);
 $pdf->SetFillColor(255);
 for ($x = 0; $x < $numrows; $x++) {
     $total += $i;
     db_fieldsmemory($result, $x);
     $sP12_cnpj = convert_CPFCNPJ($p12_cnpj);
     if (strlen($sP12_cnpj) >= 14) {
         $cgccpf = db_formatar($sP12_cnpj, 'cnpj');
     } else {
         $cgccpf = db_formatar($sP12_cnpj, 'cpf');
     }
     $pdf->SetFont($Letra, 'I', 6);
     $pdf->Cell(25, 4, "" . $p12_inscr, 1, 0, "C", 1);
     $pdf->Cell(35, 4, "" . $cgccpf, 1, 0, "C", 1);
     $pdf->Cell(26, 4, "" . $p12_fone, 1, 0, "C", 1);
     $result1 = $clissbase->sql_record($clissbase->sql_query("", "z01_nome", "", " issbase.q02_inscr = {$p12_inscr}"));
     db_fieldsmemory($result1, 0);
     $pdf->Cell(70, 4, "" . $z01_nome, 1, 0, "L", 1);
     if (isset($p12_tipolanc) && $p12_tipolanc == 1) {
         $pdf->Cell(34, 4, "" . "CLIENTE NOVO", 1, 1, "L", 1);
     } else {
         $pdf->Cell(34, 4, "" . "EX CLIENTE", 1, 1, "L", 1);
     }
     if ($pdf->GetY() > 270) {
         $pdf->AddPage();
         $pdf->SetFillColor(235);
         $pdf->Cell(25, 4, "INSCRIÇÃO ", 1, 0, "C", 1);
         $pdf->Cell(35, 4, "CNPJ ", 1, 0, "C", 1);
         $pdf->Cell(26, 4, "TELEFONE ", 1, 0, "C", 1);
         $pdf->Cell(70, 4, "NOME ", 1, 1, "C", 1);
         $pdf->Cell(34, 4, "TIPO LANÇAMENTO ", 1, 1, "C", 1);
 if (isset($sTipo) && $sTipo == 1) {
     $campos = "cgm.z01_nome,cgm.z01_cgccpf                                             ";
     $sql = " select {$campos}                                                           ";
     $sql .= "  from  issbase                                                           ";
     $sql .= "        inner join cgm       on cgm.z01_numcgm    = issbase.q02_numcgm    ";
     $sql .= "        left  join escrito e on e.q10_inscr = q02_inscr                   ";
     $sql .= "  where issbase.q02_inscr = {$pesquisa_chave}                             ";
     $sql .= " \t and q02_dtbaix is null                                                ";
     $sql .= "\t and not exists( select p12_inscr                                      ";
     $sql .= "\t \t\t\t\t   from listainscr                                     ";
     $sql .= "\t \t\t\t\t        inner join listainscrcab on p11_codigo = p12_codigo  ";
     $sql .= "\t \t\t\t\t  where p11_processado is false           ";
     $sql .= "\t \t\t\t\t    and p12_inscr = q02_inscr  limit 1)   ";
     $sql .= "    and ( not exists ( select q10_inscr from escrito where escrito.q10_inscr = q02_inscr limit 1 )  ";
     $sql .= "     or e.q10_dtfim is not null)  ";
     $result = $clissbase->sql_record($sql);
     if ($clissbase->numrows != 0) {
         db_fieldsmemory($result, 0);
         echo "<script>" . $funcao_js . "(\"{$z01_nome}\",\"\",false);</script>";
     } else {
         echo "<script>" . $funcao_js . "('Inscrição " . $pesquisa_chave . " não encontrado ou já está sendo utilizado','',true);</script>";
     }
 } else {
     if (isset($sTipo) && $sTipo == 0) {
         $campos = "cgm.z01_nome,cgm.z01_cgccpf,q02_dtbaix";
         $sql = " select {$campos} ";
         $sql .= "  from  issbase ";
         $sql .= "        inner join cgm     on cgm.z01_numcgm    = issbase.q02_numcgm";
         $sql .= "        left  join escrito on escrito.q10_inscr = q02_inscr";
         $sql .= "  where issbase.q02_inscr = {$pesquisa_chave} ";
         $result = $clissbase->sql_record($sql);
    	echo"<script>location.href='index.php?".base64_encode('erroscripts=3')."'</script>";
}   */
mens_help();
db_mensagem("dae_cab", "dae_rod");
$dblink = "index.php";
$db_verificaip = db_verifica_ip();
if ($db_verificaip == "0") {
    $onsubmit = "onsubmit=\"return js_verificaCGCCPF((this.cgc.value==''?'':this.cgc),'');\"";
} else {
    $onsubmit = "";
}
//busca dados para armazemar em cookies
if (@$_COOKIE["cookie_codigo_cgm"] == "") {
    // issbase
    if (@$inscricaow != "") {
        $result = $clissbase->sql_record($clissbase->sql_query("", "cgm.z01_numcgm,cgm.z01_nome", "", "issbase.q02_inscr = {$inscricaow}"));
        $linhas1 = $clissbase->numrows;
    }
    // cgm
    if (@$codigo_cgm != "") {
        $result = $clcgm->sql_record($clcgm->sql_query("", "cgm.z01_numcgm,cgm.z01_nome", "", "cgm.z01_numcgm = {$codigo_cgm}"));
        $linhas2 = $clcgm->numrows;
    }
    // iptu
    if (@$matricula1 != "") {
        $result = $cliptubase->sql_record($cliptubase->sql_query("", "cgm.z01_numcgm,cgm.z01_nome", "", "iptubase.j01_matric = {$matricula1}"));
        $linhas3 = $cliptubase->numrows;
    }
    db_fieldsmemory($result, 0);
    @setcookie("cookie_codigo_cgm", $z01_numcgm);
    @setcookie("cookie_nome_cgm", $z01_nome);
}
*/
mens_help();
db_mensagem("aidof_cab", "aidof_rod");
$db_verificaip = db_verifica_ip();
if ($db_verificaip == "0") {
    $onsubmit = "onsubmit=\"return js_verificaCGCCPF((this.cgc.value==''?'':this.cgc),'');\"";
} else {
    $onsubmit = "";
}
//verifica se está logado
if (@$id_usuario != "" || @$_COOKIE["cookie_codigo_cgm"] != "") {
    if (@$id_usuario == "") {
        $id_usuario = $_COOKIE["cookie_codigo_cgm"];
    }
    $result = $clissbase->sql_record($clissbase->sql_query("", "issbase.q02_inscr,z01_nome,z01_cgccpf", "", "q02_numcgm = {$id_usuario}"));
    $linhas = $clissbase->numrows;
    if ($linhas != 0) {
        db_fieldsmemory($result, 0);
        //11 14
        if (strlen($z01_cgccpf) > 11) {
            //armazena em var
            $var_cnpj = $z01_cgccpf;
        }
    }
}
?>
<html>
<head>
<title><?php 
echo $w01_titulo;
$sqlconf = "select db21_regracgmiptu, db21_regracgmiss from db_config where codigo = {$instit}";
$resconf = pg_query($sqlconf);
$iconf = pg_num_rows($resconf);
if ($iconf > 0) {
    db_fieldsmemory($resconf, 0);
}
$sQueryCndPorNome = "select * from configdbpref where w13_instit = {$instit}";
$resQueryCndPorNome = pg_query($sQueryCndPorNome);
$iQueryCndPorNome = pg_num_rows($resQueryCndPorNome);
if ($iQueryCndPorNome > 0) {
    db_fieldsmemory($resQueryCndPorNome, 0);
}
if (isset($id_usuario) and $id_usuario != "") {
    $numcgm = $id_usuario;
    // issbase
    $result1 = $clissbase->sql_record($clissbase->sql_query("", "issbase.q02_inscr", "", "cgm.z01_numcgm = {$numcgm}"));
    $linhas1 = $clissbase->numrows;
    // iptu
    $sqlpromitente = $cliptubase->sqlmatriculas_nome_numero($numcgm, $db21_regracgmiptu);
    $result2 = pg_query($sqlpromitente);
    $linhas2 = pg_num_rows($result2);
    if (isset($linhas1) && $linhas1 > 0) {
        db_fieldsmemory($result1, 0);
    }
}
if (isset($oPost->pesquisa) && $oPost->pesquisa == "Pesquisa") {
    if (isset($oPost->cgc) && $oPost->cgc != "") {
        $cgccpf = $oPost->cgc;
    }
    if (isset($oPost->cpf) && $oPost->cpf != "") {
        $cgccpf = $oPost->cpf;
        $matricula1 = $j40_matric;
        $matricula = $j40_matric;
    }
}
if (isset($inscricao)) {
    $q02_inscr = $inscricao;
}
//busca dados para armazemar em cookies
if (@$_COOKIE["cookie_codigo_cgm"] == "") {
    // issbase
    if (@$inscricaow != "") {
        $sWhereCPFCNPJ = "";
        if ($sConfig["w13_exigecpfcnpjinscricao"] == "t") {
            $sWhereCPFCNPJ = "and trim(cgm.z01_cgccpf) = '{$cgccpf}'";
        }
        @($result = $clissbase->sql_record($clissbase->sql_query("", "cgm.z01_numcgm,cgm.z01_nome", "", "issbase.q02_inscr = {$inscricaow} {$sWhereCPFCNPJ}")));
        @($linhas1 = $clissbase->numrows);
    } else {
        if (!empty($codigo_cgm) || !empty($cgc) || !empty($cpf)) {
            //CGM
            if (!empty($codigo_cgm)) {
                $condicao = "cgm.z01_numcgm = {$codigo_cgm}";
            } else {
                if (!empty($cgccpf)) {
                    $condicao = "trim(cgm.z01_cgccpf) = '{$cgccpf}'";
                }
            }
            @($result = $clcgm->sql_record($clcgm->sql_query("", "cgm.z01_numcgm,cgm.z01_nome", "", $condicao)));
            @($linhas2 = $clcgm->numrows);
        } else {
            if (@$matricula1 != "") {
include "classes/db_issbase_classe.php";
include "classes/db_aidof_classe.php";
$clarreinscr = new cl_arreinscr();
$clissbase = new cl_issbase();
$claidof = new cl_aidof();
$sqlmenu = "SELECT distinct m_publico,m_arquivo,m_descricao\n                   FROM db_menupref\n                   WHERE trim(m_arquivo) = 'digitaaidof.php'\n                   ORDER BY m_descricao\n                   ";
$result = pg_query($sqlmenu);
db_fieldsmemory($result, 0);
//if($m_publico != 't'){
// if(!session_is_registered("DB_acesso"))
//    echo"<script>location.href='index.php?".base64_encode('erroscripts=3')."'</script>";
//}
if (@$_COOKIE["cookie_codigo_cgm"] == "") {
    // issbase
    if ($inscricaow != "") {
        $result = $clissbase->sql_record($clissbase->sql_query("", "cgm.z01_numcgm,cgm.z01_nome", "", "issbase.q02_inscr = {$inscricaow}"));
        $linhas1 = $clissbase->numrows;
        db_fieldsmemory($result, 0);
        setcookie("cookie_codigo_cgm", $z01_numcgm);
        setcookie("cookie_nome_cgm", $z01_nome);
    }
}
$db_verifica_ip = db_verifica_ip();
//mens_help();
$dblink = "digitaaidof.php";
db_logs("", "", 0, "Digita Aidof.");
postmemory($HTTP_POST_VARS);
$clquery = new cl_query();
$inscricaow != "" ? "" : ($inscricaow = 0);
if (!empty($cgc)) {
    $cgccpf = $cgc;
?>
</style>
<link href="config/estilos.css" rel="stylesheet" type="text/css">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" bgcolor="<?php 
echo $w01_corbody;
?>
" onLoad="" <?php 
mens_OnHelp();
?>
>
<center>
<?php 
//verifica se está logado
if (@$id_usuario != "") {
    @($result = $clissbase->sql_record($clissbase->sql_query("", "issbase.q02_inscr,z01_nome,z01_cgccpf", "", "q02_numcgm = {$id_usuario}")));
    @($linhas = $clissbase->numrows);
    if ($linhas != 0) {
        db_fieldsmemory($result, 0);
        //11 14
        if (strlen($z01_cgccpf) > 11) {
            //armazena em var
            $var_cnpj = $z01_cgccpf;
        }
    } else {
        $sql_z01_cgccpf = pg_query("SELECT cgm.z01_cgccpf FROM cgm WHERE z01_numcgm = {$id_usuario}");
        $rRetorno = pg_fetch_assoc($sql_z01_cgccpf);
        $var_cnpj = $rRetorno['z01_cgccpf'];
    }
    ?>
	<form name="form1" method="post" action="" enctype="multipart/form-data">
if (pg_numrows($resultpref) != 0) {
    db_fieldsmemory($resultpref, 0);
}
$result_aidof = $claidof->sql_record($claidof->sql_query_file($codaidof));
if ($claidof->numrows != 0) {
    db_fieldsmemory($result_aidof, 0);
}
$result_notasiss = $clnotasiss->sql_record($clnotasiss->sql_query_file(null, "q09_descr as especie", null, "q09_codigo={$y08_nota}"));
if ($clnotasiss->numrows != 0) {
    db_fieldsmemory($result_notasiss, 0);
}
$result_graf = $clcgm->sql_record($clcgm->sql_query_file($y08_numcgm, "z01_nome as nome_graf,z01_ender as ender_graf,z01_numero as num_graf,z01_cgccpf as cgccpf_graf,z01_bairro as bairro_graf,z01_munic as munic_graf"));
if ($clcgm->numrows != 0) {
    db_fieldsmemory($result_graf, 0);
}
$result_graf_inscr = $clissbase->sql_record($clissbase->sql_query_file(null, "q02_inscr as inscr_graf", null, "q02_numcgm={$y08_numcgm}"));
if ($clissbase->numrows != 0) {
    db_fieldsmemory($result_graf_inscr, 0);
}
$sql_empr = "select q02_inscr as inscr_usu,z01_nome as nome_usu,z01_ender as ender_usu,z01_numero as num_usu,z01_cgccpf as cgccpf_usu,z01_bairro as bairro_usu,z01_munic as munic_usu from empresa where q02_inscr={$y08_inscr}";
$result_empresa = pg_exec($sql_empr);
if (pg_numrows($result_empresa) != 0) {
    db_fieldsmemory($result_empresa, 0);
}
//***************************
$mes = date("m");
$ano = date("Y");
$dia = date("d");
$codigo = $y08_codigo;
$ins = $inscr_usu;
$cnpj = $cgccpf_usu;