if (isset($numcgm) && !empty($numcgm) && (isset($matric) && empty($matric)) && (isset($inscr) && empty($inscr))) {
     if ($result = debitos_numcgm($numcgm, 0, $tipo, $DB_DATACALC, db_getsession("DB_anousu"))) {
         echo "<script> numcgm = '{$numcgm}'; </script>\n";
     } else {
         db_redireciona("cai3_gerfinanc007.php?erro1=1");
     }
 } else {
     if (isset($matric) && !empty($matric)) {
         if ($result = debitos_matricula($matric, 0, $tipo, $DB_DATACALC, db_getsession("DB_anousu"))) {
             echo "<script> matric = '{$matric}'; </script>\n";
         } else {
             db_redireciona("cai3_gerfinanc007.php?erro1=1");
         }
     } else {
         if (isset($inscr) && !empty($inscr)) {
             if ($result = debitos_inscricao($inscr, 0, $tipo, $DB_DATACALC, db_getsession("DB_anousu"))) {
                 echo "<script> inscr = '{$inscr}'; </script>\n";
             } else {
                 db_redireciona("cai3_gerfinanc007.php?erro1=1");
             }
         } else {
             if (isset($numpre) && !empty($numpre)) {
                 if ($result = debitos_numpre($numpre, 0, $tipo, $DB_DATACALC, db_getsession("DB_anousu"))) {
                     echo "<script> numpre = '{$numpre}'; </script>\n";
                 } else {
                     db_redireciona("cai3_gerfinanc007.php?erro1=1");
                 }
             }
         }
     }
 }
} else {
    if (!empty($matric)) {
        $result_teste = debitos_matricula($matric, 0, 0, $DB_DATACALC, $DB_anousu, '', 'k00_tipo,k00_numpre,k00_numpar,k00_receit');
        $result = debitos_matricula($matric, 0, 0, $DB_DATACALC, $DB_anousu, '', 'k00_tipo,k00_numpre,k00_numpar,k00_receit', $where);
        $outros = "Matrícula: " . $matric . ($cliptubase = new cl_iptubase());
        $result_inf = $cliptubase->proprietario_record($cliptubase->proprietario_query($matric, "j34_setor#j34_quadra#j34_lote#tipopri#j39_numero#j39_compl#nomepri#z01_nome#z01_numcgm#z01_cgmpri"));
        if ($cliptubase->numrows != 0) {
            db_fieldsmemory($result_inf, 0);
            $z01_numcgm = $z01_cgmpri;
            $outros = "Matrícula: " . $matric . " - SQL: " . $j34_setor . "/" . $j34_quadra . "/" . $j34_lote . " - Logradouro: " . $tipopri . " " . $nomepri . ", " . $j39_numero . " " . $j39_compl;
        } else {
        }
    } else {
        if (!empty($inscr)) {
            $result_teste = debitos_inscricao($inscr, 0, 0, $DB_DATACALC, $DB_anousu, '', 'k00_tipo,k00_numpre,k00_numpar,k00_receit');
            $result = debitos_inscricao($inscr, 0, 0, $DB_DATACALC, $DB_anousu, '', 'k00_tipo,k00_numpre,k00_numpar,k00_receit', $where);
            $outros = "Inscrição: " . $inscr;
            $clissbase = new cl_issbase();
            $result_inf = pg_exec("select * from empresa where q02_inscr = {$inscr}");
            //      if($clissbase->numrows!=0){
            if (pg_numrows($result_inf) != 0) {
                db_fieldsmemory($result_inf, 0);
                $z01_numcgm = $q02_numcgm;
            } else {
            }
        } else {
            if (!empty($numpre)) {
                $result_teste = debitos_numpre($numpre, 0, 0, $DB_DATACALC, $DB_anousu, 0, '', 'k00_tipo,k00_numpre,k00_numpar,k00_receit');
                $outros = "Código Arrecadação: " . $numpre;
                $result = debitos_numpre($numpre, 0, 0, $DB_DATACALC, $DB_anousu, 0, '', 'k00_tipo,k00_numpre,k00_numpar,k00_receit');
                $z01_numcgm = pg_result($result, 0, "k00_numcgm");
         }
     } else {
         if (isset($numcgm) && $numcgm != "") {
             $acesso = "1";
             $campo = $numcgm;
             $result = debitos_numcgm($numcgm, 0, $tipo, time(), date("Y"));
         } else {
             if (isset($matricula) && $matricula != "") {
                 $acesso = "3";
                 $campo = $matricula;
                 $result = debitos_matricula($matricula, 0, $tipo, time(), date("Y"));
             } else {
                 if (isset($inscricao) && $inscricao != "") {
                     $acesso = "2";
                     $campo = $inscricao;
                     $result = debitos_inscricao($inscricao, 0, $tipo, time(), date("Y"));
                 } else {
                     if (isset($numpre)) {
                         $result = debitos_numpre($numpre, 0, $tipo, time(), date("Y"));
                     }
                 }
             }
         }
     }
 }
 if (!isset($DB_LOGADO) && !isset($numpre) && $m_publico != 't') {
     $sql = "select fc_permissaodbpref(" . db_getsession("DB_login") . ",{$acesso},{$campo})";
     $result1 = db_query($sql);
     if (pg_numrows($result1) == 0) {
         db_redireciona("centro_pref.php?" . base64_encode("erroscripts=3"));
         exit;
 if ($result != false && pg_numrows($result) > 0) {
     $cor = "#EFE029";
     $ttvlrhis = 0;
     $ttvlrcor = 0;
     $ttvlrjuros = 0;
     $ttvlrmulta = 0;
     $ttvlrdesconto = 0;
     $tttotal = 0;
     for ($x = 0; $x < pg_numrows($result); $x++) {
         db_fieldsmemory($result, $x, true);
         if (in_array($k00_tipo, $tipos) == true) {
             if (isset($matric)) {
                 $debitos = debitos_matricula($matric, 0, $k00_tipo, $DB_DATACALC, db_getsession("DB_anousu"), "", "", $where);
             } else {
                 if (isset($inscr)) {
                     $debitos = debitos_inscricao($inscr, 0, $k00_tipo, $DB_DATACALC, db_getsession("DB_anousu"), "", "", $where);
                 } else {
                     if (isset($numcgm)) {
                         $debitos = debitos_numcgm($numcgm, 0, $k00_tipo, $DB_DATACALC, db_getsession("DB_anousu"), "", "", $where);
                     } else {
                         if (isset($numpre)) {
                             $debitos = debitos_numpre($numpre, 0, $k00_tipo, $DB_DATACALC, db_getsession("DB_anousu"), "", "", $where);
                         } else {
                             break;
                         }
                     }
                 }
             }
             if ($debitos == false || $debitos == 1) {
                 continue;
             }