$cgccpf = "";
            }
        }
        $cgccpf = str_replace(".", "", $cgccpf);
        $cgccpf = str_replace("/", "", $cgccpf);
        $cgccpf = str_replace("-", "", $cgccpf);
        $result = pg_exec("select * from issbase inner join cgm on q02_numcgm = z01_numcgm where z01_cgccpf = '{$cgccpf}' and q02_inscr  = {$inscricaow}");
        if (pg_numrows($result) != 0) {
            db_fieldsmemory($result, 0);
        } else {
            redireciona("digitaissqn.php?" . base64_encode("erroscripts=Acesso a Rotina Inválido, verifique os dados digitados!"));
        }
    }
}
if (isset($primeiravez)) {
    $clquery->sql_query("issplan", "q20_mes, q20_ano", "q20_mes", " q20_ano = {$ano} and q20_mes={$mes} and q20_numcgm= {$z01_numcgm}");
    $clquery->sql_record($clquery->sql);
    $num = $clquery->numrows;
    if ($num != 0) {
        if (isset($q02_inscr)) {
            $inscricaow = $q02_inscr;
        }
        redireciona("planilha.php?" . base64_encode("nomecontri=" . @$nomecontri . "&fonecontri=" . @$fonecontri . "&inscricaow=" . $inscricaow . "&mesx=" . $mesx . "&mes=" . $mes . "&ano=" . $ano . "&numcgm=" . $z01_numcgm . "&nomes=" . $z01_nome));
    }
}
if (isset($grava)) {
    if ($modificando == true) {
        $sqlx1 = "delete from issplanit where q21_planilha={$plani}";
        $sqlx2 = "delete from issplan where q20_planilha={$plani}";
        pg_exec($sqlx1) or die($sqlx1);
        $planilha = $plani;
include "libs/db_stdlib.php";
define('FPDF_FONTPATH', 'font/');
require "fpdf151/fpdf.php";
postmemory($HTTP_POST_VARS);
$clquery = new cl_query();
$nova = false;
$head1 = "";
$head2 = "";
$head3 = "";
$head4 = "";
$head5 = "";
$head6 = "";
$head7 = "";
$head8 = "";
$head9 = "";
$clquery->sql_query("issplan inner join cgm on q20_numcgm = z01_numcgm", "issplan.*,z01_nome,z01_ender,z01_munic", "", "q20_planilha= {$planilha}");
$clquery->sql_record($clquery->sql);
db_fieldsmemory($clquery->result, 0);
$clquery->sql_query("issplanit", "round(sum(q21_valor),2) as q21_valor", "", "q21_planilha= {$planilha}");
$clquery->sql_record($clquery->sql);
db_fieldsmemory($clquery->result, 0);
if ($q20_numpre == "") {
    $clquery->sql_query("", " nextval('numpre_campo') as q20_numpre");
    $clquery->sql_record($clquery->sql);
    db_fieldsmemory($clquery->result, 0);
    $clquery->sql_query("", " nextval('numbanco_campo') as q20_numbco");
    $clquery->sql_record($clquery->sql);
    db_fieldsmemory($clquery->result, 0);
    // $q20_numbco = '8200572002';
    $clquery->sql_update("issplan", " q20_numpre = {$q20_numpre}, q20_numbco = {$q20_numbco}", " q20_planilha = {$planilha}");
}
$result = db_query("SELECT distinct m_publico,m_arquivo,m_descricao\n                   FROM db_menupref\n                   WHERE m_arquivo = 'digitadae.php'\n                   ORDER BY m_descricao\n                   ");
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>";
    }
    echo "<script>location.href='opcoesdae.php?nova=1'</script>";
}
$db_verifica_ip = db_verifica_ip();
mens_help();
$dblink = "digitadae.php";
db_logs("", "", 0, "Digita inscricao do Contribuinte.");
postmemory($HTTP_POST_VARS);
$clquery = new cl_query();
if (isset($nova)) {
    $clquery->sql_query("issbase", " q02_numcgm ", "", "q02_inscr  = {$inscricaow}");
    $clquery->sql_record($clquery->sql);
    $num = $clquery->numrows;
    if ($num != 0) {
        db_fieldsmemory($clquery->result, 0);
        $clquery->sql_query("cgm", "z01_nome, z01_numcgm", "", " z01_numcgm = {$q02_numcgm}");
        // select z01_nome, z01_numcgm from cgm where z01_numcgm = q02_numcgm
        $clquery->sql_record($clquery->sql);
        // conta as linhas no banco
        db_fieldsmemory($clquery->result, 0);
        // cria variaveis (z01_nome, z01_numcgm) apartir dos campos
    } else {
        redireciona("digitadae.php?" . base64_encode('erroscripts=Acesso a Rotina Inválido, verifique os dados digitados!'));
    }
} else {
    if (isset($first)) {
$result = pg_exec("SELECT distinct m_publico,m_arquivo,m_descricao\n                   FROM db_menupref\n                   WHERE m_arquivo = 'digitaissqn.php'\n                   ORDER BY m_descricao\n                  ");
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>";
    }
}
mens_help();
db_logs("", "", 0, "Digita Codigo da Inscricao.");
db_mensagem("alvara_cab", "alvara_rod");
postmemory($HTTP_POST_VARS);
$clquery = new cl_query();
$alterando = false;
$matri = array("1" => "janeiro", "2" => "Fevereiro", "3" => "Março", "4" => "Abril", "5" => "Maio", "6" => "Junho", "7" => "Julho", "8" => "Agosto", "9" => "Setembro", "10" => "Outubro", "11" => "Novembro", "12" => "Dezembro");
$mesx = $matri[$mes];
$clquery->sql_query("issplan", "q20_nomecontri,q20_planilha, q20_numcgm, q20_ano, q20_mes,q20_numpre", "q20_mes", "q20_ano = {$ano} and q20_mes={$mes} and q20_numcgm= {$numcgm} or q20_ano = {$ano} and q20_numpre is null and q20_numcgm= {$numcgm}");
//die("$clquery->sql");  // traz todos do ano e mes selecionado ou ano e numpre = nada pelo cgc.
$clquery->sql_record($clquery->sql);
$numrows = $clquery->numrows;
$result = $clquery->result;
for ($x = 0; $x < $numrows; $x++) {
    $alterar = "alterar_" . $x;
    if (isset(${$alterar})) {
        $alterando = true;
        $numer = $x;
        break;
    }
}
if ($alterando == true) {
    $clquery->sql_result($result, $numer, 0);
    $codigopla = $clquery->resultado;
$clquery = new cl_query();
$nova = false;
$head1 = "";
$head2 = "";
$head3 = "";
$head4 = "";
$head5 = "";
$head6 = "";
$head7 = "";
$head8 = "";
$head9 = "";
$situacao = "";
$sqluf = "select * from db_config  inner join db_uf on db12_uf=uf  where codigo = " . db_getsession('DB_instit');
$resultuf = pg_query($sqluf);
db_fieldsmemory($resultuf, 0);
$clquery->sql_query("issplan left join issplaninscr on q20_planilha=q24_planilha", "*", "", "q20_planilha= {$planilha}");
$clquery->sql_record($clquery->sql);
db_fieldsmemory($clquery->result, 0);
if ($q20_situacao == 5) {
    $situacao = " (ANULADA)";
}
$matri = array("1" => "janeiro", "2" => "Fevereiro", "3" => "Março", "4" => "Abril", "5" => "Maio", "6" => "Junho", "7" => "Julho", "8" => "Agosto", "9" => "Setembro", "10" => "Outubro", "11" => "Novembro", "12" => "Dezembro");
$mesx = $matri[$q20_mes];
$pdf = new FPDF();
$pdf->Open();
$pdf->AliasNbPages();
$pdf->AddPage("L");
// monta cabecalho do relatório
$pdf->Image('imagens/files/logo_boleto.png', 140, 3, 20);
$pdf->SetFont('Times', '', 9);
$pdf->Text(118, 31, $nomeinst);
include "classes/db_arreold_classe.php";
include "classes/db_issplannumpre_classe.php";
include "classes/db_cairetordem_classe.php";
include "classes/db_issvar_classe.php";
include "classes/db_issplannumpreissplanit_classe.php";
parse_str($HTTP_SERVER_VARS["QUERY_STRING"]);
postmemory($HTTP_POST_VARS);
$clquery = new cl_query();
$cl_issvar = new cl_issvar();
$cl_arreold = new cl_arreold();
$cl_issplannumpre = new cl_issplannumpre();
$cl_arrecad = new cl_arrecad();
$cl_cairetordem = new cl_cairetordem();
$cl_issplannumpreissplanit = new cl_issplannumpreissplanit();
$q24_inscr = 0;
$clquery->sql_query("db_confplan", " * ", "", "");
$clquery->sql_record($clquery->sql);
if (pg_numrows($clquery->result) == 0) {
    echo "<script>window.close();window.opener.alert('Não é possivel gerar recibo. Por favor, contate com a prefeitura.');window.opener.location.href='digitaissqn.php'</script>";
    exit;
}
db_fieldsmemory($clquery->result, 0);
if (!isset($dados_recibo)) {
    // ################### SE FOR EMITE RECIBO  #######################################
    // die(" emite ");
    //$mes = 0;
    //$ano = 0;
    $sqlerro = false;
    pg_query("begin");
    $clquery->sql_query("", " nextval('numpref_k03_numpre_seq') as q20_numpre");
    $clquery->sql_record($clquery->sql);