Skip to content

Commit

Permalink
add NotaCarioca ConsultarNfse operation
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrocasado committed Feb 20, 2020
1 parent 1613cf7 commit f229a28
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 1 deletion.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Lib to communicate with SOAP web services and generate NFS-e (Nota Fiscal de Ser
- Nota Carioca
- GerarNfse
- ConsultarNfsePorRps
- ConsultarNfse

# Install

Expand All @@ -20,6 +21,8 @@ Check examples/ folder

You must have a valid certificate to use Nota Carioca staging environment.

### GerarNfse

```php
use NFSePHP\NotaCarioca\GerarNfseNotaCariocaFactory;
use NFSePHP\NotaCarioca\SoapHandler;
Expand Down Expand Up @@ -145,9 +148,51 @@ array (size=1)

```

### ConsultaNfse

```php
use NFSePHP\NotaCarioca\ConsultarNfseFactory;
use NFSePHP\NotaCarioca\SoapHandler;

$rps = [
'Prestador' => [
'Cnpj' => '10738989000199',
'InscricaoMunicipal' => '04409477',
],
'PeriodoEmissao' => [
'DataInicial' => '2020-01-30',
'DataFinal' => '2020-01-30',
],
'Tomador' => [
'CpfCnpj' => [
'Cpf' => '11911464728',
],
],
];

$env = 'dev'; // dev or prod
$notaCariocaConsulta = new ConsultarNfseFactory($rps, $env);

$soapHandler = new SoapHandler(['cert_path' => '/path/to/valid/cert.pfx', 'cert_pass' => 'certpassword']);

// Send SOAP xml
$response = $soapHandler->send($notaCariocaConsulta);

if ($soapHandler->isSuccess($response)) {
$nfs = $notaCariocaConsulta->formatSuccessResponse($response);

var_dump($nfs);
} else {
$errors = $soapHandler->getErrors($response);

var_dump($errors);
}

```

# TODO's

- Add missing operations (CancelarNfse, ConsultaNfse, ConsultarLoteRps, ConsultarSituacaoLoteRps, EnviarLoteRps)
- Add missing operations (CancelarNfse, ConsultarLoteRps, ConsultarSituacaoLoteRps, EnviarLoteRps)
- Add tests

Inspired by https://github.com/nfephp-org
119 changes: 119 additions & 0 deletions examples/NotaCarioca/ConsultarNfse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

require __DIR__.'/../../vendor/autoload.php';

use NFSePHP\NotaCarioca\ConsultarNfseFactory;
use NFSePHP\NotaCarioca\SoapHandler;

$rps = [
'Prestador' => [
'Cnpj' => '11111111111111',
'InscricaoMunicipal' => '11111111',
],
'PeriodoEmissao' => [
'DataInicial' => '2020-01-30',
'DataFinal' => '2020-01-30',
],
'Tomador' => [
'CpfCnpj' => [
'Cpf' => '11111111111',
],
],
];

$env = 'dev'; // dev or prod
$notaCariocaConsulta = new ConsultarNfseFactory($rps, $env);

$soapHandler = new SoapHandler(['cert_path' => '/path/to/valid/cert.pfx', 'cert_pass' => 'certpassword']);

// Send SOAP xml
$response = $soapHandler->send($notaCariocaConsulta);

if ($soapHandler->isSuccess($response)) {
$nfs = $notaCariocaConsulta->formatSuccessResponse($response);

var_dump($nfs);
} else {
$errors = $soapHandler->getErrors($response);

var_dump($errors);
}

/* Response
array (size=7)
0 =>
array (size=14)
'Numero' => string '6' (length=1)
'CodigoVerificacao' => string 'RE4C-ZXKX' (length=9)
'DataEmissao' => string '2020-01-30T09:15:03' (length=19)
'IdentificacaoRps' =>
array (size=3)
'Numero' => string '7' (length=1)
'Serie' => string '1' (length=1)
'Tipo' => string '1' (length=1)
'DataEmissaoRps' => string '2020-01-30' (length=10)
'NaturezaOperacao' => string '1' (length=1)
'OptanteSimplesNacional' => string '2' (length=1)
'IncentivadorCultural' => string '2' (length=1)
'Competencia' => string '2020-01-30T00:00:00' (length=19)
'Servico' =>
array (size=5)
'Valores' =>
array (size=6)
'ValorServicos' => string '100' (length=3)
'IssRetido' => string '2' (length=1)
'ValorIss' => string '5' (length=1)
'BaseCalculo' => string '100' (length=3)
'Aliquota' => string '0.05' (length=4)
'ValorLiquidoNfse' => string '100' (length=3)
'ItemListaServico' => string '1002' (length=4)
'CodigoTributacaoMunicipio' => string '100203' (length=6)
'Discriminacao' => string 'Teste de RPS' (length=12)
'CodigoMunicipio' => string '111111' (length=7)
'ValorCredito' => string '0.5' (length=3)
'PrestadorServico' =>
array (size=4)
'IdentificacaoPrestador' =>
array (size=2)
'Cnpj' => string '111111' (length=14)
'InscricaoMunicipal' => string '4409477' (length=7)
'RazaoSocial' => string '111111' (length=48)
'Endereco' =>
array (size=7)
'Endereco' => string '1111111' (length=39)
'Numero' => string '111' (length=3)
'Complemento' => string '11111' (length=15)
'Bairro' => string '11111' (length=7)
'CodigoMunicipio' => string '111111' (length=7)
'Uf' => string 'RJ' (length=2)
'Cep' => string '11111111' (length=8)
'Contato' => string '' (length=0)
'TomadorServico' =>
array (size=3)
'IdentificacaoTomador' =>
array (size=1)
'CpfCnpj' =>
array (size=1)
'Cpf' => string '11111111111' (length=11)
'RazaoSocial' => string 'Fulano de Tal' (length=13)
'Endereco' =>
array (size=7)
'Endereco' => string '1111111' (length=29)
'Numero' => string '111' (length=3)
'Complemento' => string 'Sobre Loja' (length=10)
'Bairro' => string 'Centro' (length=6)
'CodigoMunicipio' => string '3304557' (length=7)
'Uf' => string 'RJ' (length=2)
'Cep' => string '11111111' (length=8)
'OrgaoGerador' =>
array (size=2)
'CodigoMunicipio' => string '11111' (length=7)
'Uf' => string 'RJ' (length=2)
1 =>
array (size=14)
'Numero' => string '7' (length=1)
'CodigoVerificacao' => string 'QKSW-PINP' (length=9)
'DataEmissao' => string '2020-01-30T09:20:56' (length=19)
......
*/
File renamed without changes.
103 changes: 103 additions & 0 deletions src/NotaCarioca/ConsultarNfseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace NFSePHP\NotaCarioca;

use Garden\Schema\Schema;
use Garden\Schema\ValidationException;
use NFSePHP\XmlFactoryInterface;

/**
* Class to generate XML to the ConsultarNfse Web Service operation.
*/
class ConsultarNfseFactory extends NotaCariocaFactoryBase implements XmlFactoryInterface
{
public function __construct(array $rps, string $env = 'dev')
{
parent::__construct($rps, $env);
}

/**
* {@inheritdoc}
*/
public function getOperation(): string
{
return 'ConsultarNfse';
}

/**
* {@inheritdoc}
*/
public function formatSuccessResponse(string $responseXml): array
{
$resultArr = $this->getEncoder()->decode($responseXml, '');

$responseArr = [];
if (isset($resultArr['ListaNfse']) and isset($resultArr['ListaNfse']['CompNfse'])) {
foreach ($resultArr['ListaNfse']['CompNfse'] as $nfse) {
$responseArr[] = $nfse['Nfse']['InfNfse'];
}
}

return $responseArr;
}

/**
* {@inheritdoc}
*/
public function getSchemaStructure(): array
{
return [
'ConsultarNfseEnvio' => [
'Prestador' => ['Cnpj', 'InscricaoMunicipal'],
'PeriodoEmissao' => ['DataInicial', 'DataFinal'],
'Tomador' => [
'CpfCnpj' => [
'Cpf?',
'Cnpj?',
],
],
],
];
}

/**
* {@inheritdoc}
*/
public function getEnvelopeXml(): string
{
$structure = $this->getSchemaStructure();

$rps = [
'ConsultarNfseEnvio' => [
'@xmlns' => 'http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd',
'Prestador' => $this->rps['Prestador'],
'PeriodoEmissao' => $this->rps['PeriodoEmissao'],
'Tomador' => $this->rps['Tomador'],
],
];

// Validate array based on structure
try {
$schema = Schema::parse($structure);
$valid = $schema->validate($rps);
} catch (ValidationException $ex) {
throw new \Exception(__FILE__.':'.__LINE__.' - '.$ex->getMessage());
}

$xml = $this->getEncoder()->encode($rps, 'xml', ['xml_root_node_name' => 'rootnode', 'remove_empty_tags' => true]);

// clean up encode tag added by encoder
$xml = str_replace('<?xml version="1.0"?>', '', $xml);
$xml = str_replace('<rootnode>', '', $xml);
$xml = str_replace('</rootnode>', '', $xml);

// Envelope request
$this->addEnvelope($xml);

// header('Content-type: text/xml');
// print_r($xml);
// exit();

return $xml;
}
}

0 comments on commit f229a28

Please sign in to comment.