$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}");
}
$mesv = $q20_mes + 1;
$anov = $q20_ano;
if ($q20_mes > 12) {
    $mesv = 1;
    $anov++;
}
$dtvenc = date('Y-m-d', mktime(0, 0, 0, $mesv, 10, $anov));
if ($dtvenc < date("Y-m-d")) {
    echo "<script>window.opener.alert('Documento Vencido. Emissão não Permitida.');window.close()</script>";
    exit;
}
if (isset($valortotal)) {
    $tot = $valortotal;
} else {
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);
    db_fieldsmemory($clquery->result, 0);
    $clquery->sql_update("issplan", "q20_numpre = {$q20_numpre}, q20_situacao= 3", "q20_planilha = {$planilha}");
    $clquery->sql_query("issplan left join issplanit on q20_planilha = q21_planilha ", "q20_ano,q20_mes,q20_numcgm,sum(q21_valor) ", "", " q21_status = 1 and q20_numpre = {$q20_numpre} group by q20_ano,q20_mes,q20_numcgm");
    $clquery->sql_record($clquery->sql);
    // insere na issvar ......................
    db_fieldsmemory($clquery->result, 0);
    $result = pg_query("insert into issvar (q05_codigo,\n            q05_numpre,q05_numpar,q05_valor,q05_ano,q05_mes,q05_histor,q05_aliq,q05_bruto,q05_vlrinf)\n              values(nextval('issvar_q05_codigo_seq'),{$q20_numpre},1,{$sum}," . $q20_ano . "," . $q20_mes . ",'issqn retenção na fonte',0,0,0)");
    $q20_mes += 1;
    if ($q20_mes > 12) {
        $q20_mes = 1;
        $q20_ano += 1;
    }
    $dtarrecad = date($q20_ano . "-" . $q20_mes . "-" . $w10_dia);
    $w10_diaoper = '01';
    $dtoperaarrecad = date($q20_ano . "-" . $q20_mes . "-" . $w10_diaoper);
    $sql = "insert into arrecad (\n            k00_numcgm,k00_dtoper,k00_receit,k00_hist,k00_valor,k00_dtvenc,k00_numpre,k00_numpar,k00_numtot,k00_numdig,k00_tipo,k00_tipojm)\n              values(" . $q20_numcgm . ",'" . $dtoperaarrecad . "'," . $w10_receit . "," . $w10_hist . ",round(" . $sum . ",2),'" . $dtarrecad . "'," . $q20_numpre . ",1,1,1," . $w10_tipo . ",0)";
    $result = pg_query($sql) or die($sql);