<?php
  ob_start('ob_gzhandler');
  require_once('config.php');
  $sql = "select CHAVEACESSO, NRODOCUMENTO, TIPODOC from VENDAS V ".
         "where SITUACAO = 'I' and NROVENDA = ".$_GET["nrovenda"];
  $query = ibase_query($conexao, $sql) or die(ibase_errmsg());
  $row = ibase_fetch_row($query);
  $obj = new stdClass();
  $obj->TIPODOC = $row[2];
  if ($row != null) {
    $obj->CHAVEACESSO = $row[0];
    $obj->NRODOCUMENTO = $row[1];
  } else {
    $obj->CHAVEACESSO = "";
    $obj->NRODOCUMENTO = 0;
  }

  $sql = "select X.NRONF ".
         "from PROXNRONF X ".
         "join VENDAS V on V.NROVENDA = ".$_GET["nrovenda"].
         "where X.NROSERIE = ".
         ($obj->TIPODOC == "F" ?  " (select VE.NROSERIE_NFCE from VENDEDORES VE where VE.CODVENDEDOR = V.CODVENDEDOR) " :
         "  case when (select count(VE.CODVENDEDOR) from VENDEDORES VE where VE.NROSERIE is not null) > 0 then ".
          "(select VE.NROSERIE from VENDEDORES VE where VE.CODVENDEDOR = V.CODVENDEDOR) else V.CODVENDEDOR + 10 end");
  $query = ibase_query($conexao, $sql) or die(ibase_errmsg());
  $row = ibase_fetch_row($query);
  if ($row != null)
    $obj->PROXNRONF = $row[0];
  else
    $obj->PROXNRONF = 0;

  $array = array();
  $array[] = $obj;
  echo json_encode($array);


?>