Skip to content

Commit

Permalink
Add CancelarNfse operation
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrocasado committed Feb 20, 2020
1 parent b2c4351 commit 2a9cb74
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 1 deletion.
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Lib to communicate with SOAP web services and generate NFS-e (Nota Fiscal de Ser
- GerarNfse
- ConsultarNfsePorRps
- ConsultarNfse
- CancelarNfse

# Install

Expand Down Expand Up @@ -190,9 +191,58 @@ if ($soapHandler->isSuccess($response)) {

```

### CancelarNfse

```php
<?php

use NFSePHP\NotaCarioca\CancelarNfseFactory;
use NFSePHP\NotaCarioca\SoapHandler;

$nfse = [
'IdentificacaoNfse' => [
'Numero' => '9',
'Cnpj' => '1111111111111',
'InscricaoMunicipal' => '11111111',
'CodigoMunicipio' => '111111',
],
'CodigoCancelamento' => '1',
// 1 Erro na emissão
// 2 Serviço não prestado
// 3 Duplicidade da nota
// 9 Outros
];

$env = 'dev'; // dev or prod
$notaCariocaCancel = new CancelarNfseFactory($nfse, $env);

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

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

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

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

var_dump($errors);
}

/* Response
array (size=2)
'DataHoraCancelamento' => string '2020-02-20T12:13:22' (length=19)
'Pedido' => int 54804

*/

```

# TODO's

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

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

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

use NFSePHP\NotaCarioca\CancelarNfseFactory;
use NFSePHP\NotaCarioca\SoapHandler;

$nfse = [
'IdentificacaoNfse' => [
'Numero' => '9',
'Cnpj' => '1111111111111',
'InscricaoMunicipal' => '11111111',
'CodigoMunicipio' => '111111',
],
'CodigoCancelamento' => '1',
// 1 Erro na emissão
// 2 Serviço não prestado
// 3 Duplicidade da nota
// 9 Outros
];

$env = 'dev'; // dev or prod
$notaCariocaCancel = new CancelarNfseFactory($nfse, $env);

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

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

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

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

var_dump($errors);
}

/* Response
array (size=2)
'DataHoraCancelamento' => string '2020-02-20T12:13:22' (length=19)
'Pedido' => int 54804
*/
110 changes: 110 additions & 0 deletions src/NotaCarioca/CancelarNfseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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 CancelarNfseFactory extends NotaCariocaFactoryBase implements XmlFactoryInterface
{
public function __construct(array $rps, string $env = 'dev')
{
parent::__construct($rps, $env);
}

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

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

if (isset($resp['Cancelamento']) and isset($resp['Cancelamento']['Confirmacao'])) {
return [
'DataHoraCancelamento' => $resp['Cancelamento']['Confirmacao']['DataHoraCancelamento'],
'Pedido' => $resp['Cancelamento']['Confirmacao']['@Id'],
];
}

return [];
}

/**
* {@inheritdoc}
*/
public function getSchemaStructure(): array
{
return [
'CancelarNfseEnvio' => [
'Pedido' => [
'InfPedidoCancelamento' => [
'IdentificacaoNfse' => [
'Numero',
'Cnpj',
'InscricaoMunicipal',
'CodigoMunicipio',
],
'CodigoCancelamento',
],
],
],
];
}

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

$rps = [
'CancelarNfseEnvio' => [
'@xmlns' => 'http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd',
'Pedido' => [
'@xmlns' => 'http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd',
'InfPedidoCancelamento' => [
'IdentificacaoNfse' => $this->rps['IdentificacaoNfse'],
'CodigoCancelamento' => $this->rps['CodigoCancelamento'],
],
],
],
];

// 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 2a9cb74

Please sign in to comment.