<?php
class NfsFocus{

  private $url = GLOBAL_FOCUSNFE_URL;
  private $tk = GLOBAL_FOCUSNFE_TK;

  private $pokerweb_cnpj = '25131762000158';
  private $pokerweb_insc_munic = '64473';
  private $pokerweb_cod_munic = '4108304';

  private $nf = false;
  private $cliente = false;
  private $servico = false;
  private $aliquota = false;

  public $response = false;

  public function __construct($nf, $cliente, $servico, $aliquota){
    $this->nf = $nf;
    $this->cliente = $cliente;
    $this->servico = $servico;
    $this->aliquota = $aliquota;
    $this->ddsNF();
    $this->send();
  }

  private function ddsNF(){
    $dataHoraAtual = new DateTime();
    $dd['data_emissao'] = $dataHoraAtual->format('Y-m-d\TH:i').':00';

    $dd['prestador']['cnpj'] = $this->pokerweb_cnpj;
    $dd['prestador']['inscricao_municipal'] = $this->pokerweb_insc_munic;
    $dd['prestador']['codigo_municipio'] = $this->pokerweb_cod_munic;

    $dd['natureza_operacao'] = "1";
    $dd['optante_simples_nacional'] = false;

    $dd['servico']['discriminacao'] = $this->servico['descricao'];
    $dd['servico']['valor_servicos'] = $this->servico['valor_total'];
    $dd['servico']['aliquota'] = $this->aliquota['aliquota'];
    $dd['servico']['iss_retido'] = "false";
    $dd['servico']['codigo_cnae'] = $this->servico['cod_cnae'];
    $dd['servico']['item_lista_servico'] = $this->servico['cod_servico'];

    // Se estrangeiro
    if($this->cliente->estrangeiro){
      $dd['tomador']['cpf'] = $this->cliente->doc_numero;
      $dd['tomador']['razao_social'] = $this->cliente->doc_nome;
      $dd['tomador']['endereco']['uf'] = $this->cliente->end_uf;
    }else{
      // Pessoa jurídica ou física
      if($this->cliente->pessoa_juridica){
        $dd['tomador']['cnpj'] = $this->cliente->doc_numero;
      }else{
        $dd['tomador']['cpf'] = $this->cliente->doc_numero;
      }
      $dd['tomador']['razao_social'] = $this->cliente->doc_nome;
      $dd['tomador']['email'] = $this->cliente->email;
      $dd['tomador']['endereco']['logradouro'] = $this->cliente->end;
      $dd['tomador']['endereco']['numero'] = $this->cliente->end_numero;
      $dd['tomador']['endereco']['bairro'] = $this->cliente->end_bairro;
      $dd['tomador']['endereco']['uf'] = $this->cliente->end_uf;
      $dd['tomador']['endereco']['cep'] = $this->cliente->end_cep;
      if($this->cliente->end_cod_municipio){
        $dd['tomador']['endereco']['codigo_municipio'] = $this->cliente->end_cod_municipio;
      }
    }

    $dd = mb_convert_encoding($dd, 'UTF-8');
    $this->nf_dd = $dd;
  }

  private function send(){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->url."/v2/nfse?ref=".$this->nf['ref']);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_SSLVERSION_TLSv1_2);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->nf_dd));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
      "Authorization: Basic " . base64_encode($this->tk . ":"),
      "Content-type: application/json"
    ]);
    $response = curl_exec($ch);
    $err = curl_error($ch);
    if(!$err){
      $this->response = json_decode($response, true);
    }
  }

  /*
  public function configs($id){
    $sql = $this->bd->getConexao()->prepare('SELECT * FROM fingeral_registros WHERE id = :id');
    $sql->bindValue(':id', $id, PDO::PARAM_STR);
    if($sql->execute()){$this->fingeral_registro = $sql->fetch(PDO::FETCH_ASSOC);}

    if($this->fingeral_registro['id_nfs']>0){
      $this->id_nf = $this->fingeral_registro['id_nfs'];
    }
  }
  */

}