<?php
  $user = "";
  $pwd  = "";
  $host = '189.7.33.11:8087';//'localhost:13925';
  define('URL', 'http://'.$host.'/Guias/resources/guia');
  define('VER','v2.0');
  require_once "JSON.php"; // usar somente se o php for inferior a versao 5


function consulta_empresa($ncadastro) {
  header("Content-Type: text/html; charset=iso-8859-1");
  $curl = curl_init(URL.'/empresa/'.$ncadastro);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  $page = curl_exec($curl);
  curl_close($curl);

  if (strnatcmp(phpversion(),'5.2.10') >= 0) {
    return json_decode($page,true); //# equal or newer
  }else {
    $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
    return $json->decode($page); //;
  }

}

function guia_recolhimento($codfirma,$codrec,$ref) {
  header("Content-Type: text/html; charset=utf-8");
  $curl = curl_init(URL.'/recolhimento/'.$codfirma.'/'.$codrec.'/'.$ref);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  $page = curl_exec($curl);
  curl_close($curl);

  if (strnatcmp(phpversion(),'5.2.10') >= 0) {
    return json_decode($page,true); //# equal or newer
  }else {
    $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
    return $json->decode($page);
  }

}

function tiposrecolhimento() {
  header("Content-Type: text/html; charset=utf-8");
  $curl = curl_init(URL.'/tiporecolhimento');
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  $page = curl_exec($curl);
  curl_close($curl);

  if (strnatcmp(phpversion(),'5.2.10') >= 0) {
    $row = json_decode($page,true); //# equal or newer
  }else {
    $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
    $row = $json->decode($page);
  }

  $componente = "<select name='tiporecolhimento' id='tiporecolhimento'>";
  $componente = $componente."<option value='Escolha'>Escolha o Tipo</option>";
  for ($i = 0; $i < count($row); $i++)  {
      $linha = $row[$i];
      $componente = $componente."<option value='".$linha['codRecolhimento']."'>".$linha['descricao']."</option>";
  }
  $componente = $componente."</select>";
  return $componente;
}


function listarecolhimentos($cnpj_cpf) {
  header("Content-Type: text/html; charset=utf-8");
  $curl = curl_init(URL.'/lista/'.$cnpj_cpf);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  $page = curl_exec($curl);
  curl_close($curl);

  if (strnatcmp(phpversion(),'5.2.10') >= 0) {
    $row = json_decode($page,true); //# equal or newer
  }else {
    $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
    $row = $json->decode($page);
  }

  $tabela = "<table width='600' class=''> <tr class='cabecalho'><td>Referencia</td><td align='left' >Recolhimento</td><td>Vencimento</td><td>Guia</td></tr>";
  for ($i = 0; $i < count($row); $i++)  {
      if ($i%2==0) {
        $class = "linha1";
      } else {
        $class = "linha2";
      }
      $linha = $row[$i];
      $descricao_tipo = $linha['tipoRec'];
      $dados_guia = $linha['recolhimentosPK'];
      $ref = '"'.str_replace("/","-",$dados_guia['referencia']).'"';
      $tipoR = '"'.$descricao_tipo['tipoR'].'"';
      $tabela = $tabela."<tr class='".$class."'><td>".$dados_guia['referencia']."</td><td align='left'>".$descricao_tipo['descricao']."</td><td>".$dados_guia['datVen']."</td>";
      $tabela = $tabela."<td align='right'><input type='button' value='Imprimir' onClick='javascript:valor(".$tipoR.",".$dados_guia['codFirma'].",".$dados_guia['codRecolhimento'].",".$ref.");'/></td></tr>";
  }
  $tabela = $tabela."<tr class='rodape'><td colspan='4'>&nbsp;</td></tr></table>";
  return $tabela;
}

function grava_log($empresa, $evento) {
  $ip       = $_SERVER['REMOTE_ADDR'];
  $curl = curl_init(URL.'/log/'.$empresa.'/'.$ip.'/'.$evento.'');
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  $page = curl_exec($curl);
  curl_close($curl);
  return '';
}

?>

