<?php

  include_once('config.php');
  require_once("./phone_gap/dao/ConfigDAO.class.php");

  ob_start('ob_gzhandler');
  
  $query = ibase_query( 
	" select C.NROEMPRESA " .
    "   from CONFIG C "
  );
  $config = array();
  while ($row = ibase_fetch_assoc($query)) {
    $config[] = $row;
  } 
  
  $codcliente = $_GET["codcliente"];
  
  $configdao = new ConfigDAO();
  $moduloAcertoCarga = "F";
  if(($configdao->getDisponibilizar()->getAcertoDeCarga()) || ($configdao->getDisponibilizar()->getAcertoDeCarga2())){
	$moduloAcertoCarga = "T";
  }

  $trans = ibase_trans();
  try{
	  ibase_query($trans, "execute procedure ATUALIZAR_CREDITOCLIENTE(" . $config[0]["NROEMPRESA"] . ", '". $moduloAcertoCarga . "', " . $codcliente . " );");		
	
  	  ibase_commit($trans);
  }catch(Exception $e){
	  echo "Erro: ".$e->getMessage();
	  ibase_rollback($trans);
  }

  $query = ibase_query( 
	" select C.CREDITOCLIENTE, CC.CREDITOCLIENTE as CREDITODISPONIVEL, " .
	"        CC.DATAHORACRED ".
    "   from CLIENTES C " .
    "   join CLIENTES_CREDITO CC on C.CODCLIENTE = CC.CODCLIENTE " . 
	"  where C.CODCLIENTE = " . $codcliente
  );
  $array = array();
  while ($row = ibase_fetch_assoc($query)) {
    $array[] = $row;
  }

  print json_encode($array);

?>