<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);
require_once '../vendor/autoload.php';
$data_hora = new DateTime();
$nfe = new NFePHP\NFe\Make();
$std = new \stdClass();
$std->versao = '3.10';
$nfe->taginfNFe($std);
$std = new \stdClass();
$std->cUF = 29;
$std->cNF = '10205812';
$std->natOp = 'VENDA DE MERCADORIA';
$std->indPag = 0;
$std->mod = 65;
$std->serie = 1;
$std->nNF = 2586;
$std->dhEmi = $data_hora->format(DateTime::ATOM);
//$std->dhEmi = '2018-02-06T20:48:00-02:00';
$std->dhSaiEnt = $data_hora->format(DateTime::ATOM);
//$std->dhSaiEnt = '2018-02-06T20:48:00-02:00';
$std->tpNF = 1;
$std->idDest = 1;
$std->cMunFG = 2918407;
$std->tpImp = 4;
$std->tpEmis = 1;
$std->cDV = 5;
$std->tpAmb = 1; // Se deixar o tpAmb como 2 você emitirá a nota em ambiente de homologação(teste) e as notas fiscais aqui não tem valor fiscal
$std->finNFe = 1;
$std->indFinal = 1;
$std->indPres = 1;
$std->procEmi = 0;
$std->verProc = '3.1.1';
$nfe->tagide($std);
$std = new \stdClass();
$std->xNome = 'ADENILSON AURELIO DE AMORIM DE JUAZEIRO EPP';
$std->IE = '054094053';
/*$std->IM = 'ISENTO';
$std->CNAE = '4773300';*/
$std->CRT = 1;
$std->CNPJ = '04112864000156';
$nfe->tagemit($std);
$std = new \stdClass();
$std->xLgr = "TRAVESSA MARAVILHA";
$std->nro = '138';
$std->xCpl = 'LOJA';
$std->xBairro = 'CENTRO';
$std->cMun = '2918407';
$std->xMun = 'JUAZEIRO';
$std->UF = 'BA';
$std->CEP = '48900000';
$std->cPais = '1058';
$std->xPais = 'BRASIL';
$nfe->tagenderEmit($std);
$std = new \stdClass();
$std->item = 1;
$std->cProd = '9244620';
$std->xProd = "ABRACADEIRA DE ENCAIXE DO PEDAL ELEVAVEL";
$std->NCM = '87142000';
$std->CFOP = '5102';
$std->uCom = 'UND';
$std->qCom = '1.0000';
$std->vUnCom = '1.99';
$std->vProd = '1.99';
$std->uTrib = 'UND';
$std->qTrib = '1.0000';
$std->vUnTrib = '1.99';
$std->indTot = 1;
$nfe->tagprod($std);
$std = new \stdClass();
$std->item = 1;
$std->vTotTrib = 1.99;
$nfe->tagimposto($std);
$std = new \stdClass();
$std->item = 1;
$std->orig = 0;
$std->CSOSN = '102';
$nfe->tagICMSSN($std);
$std = new \stdClass();
$std->item = 1;
$std->CST = '07';
$nfe->tagPIS($std);
$std = new \stdClass();
$std->item = 1;
$std->CST = '07';
$nfe->tagCOFINS($std);
$std = new \stdClass();
$std->vBC = 0.00;
$std->vICMS = 0.00;
$std->vICMSDeson = 0.00;
$std->vBCST = 0.00;
$std->vST = 0.00;
$std->vProd = 1.99;
$std->vFrete = 0.00;
$std->vSeg = 0.00;
$std->vDesc = 0.00;
$std->vII = 0.00;
$std->vIPI = 0.00;
$std->vPIS = 0.00;
$std->vCOFINS = 0.00;
$std->vOutro = 0.00;
$std->vNF = 1.99;
$nfe->tagICMSTot($std);
$std = new stdClass();
$std->modFrete = 9;
$nfe->tagtransp($std);
$std = new stdClass();
$std->vTroco = null; //incluso no layout 4.00, obrigatório informar para NFCe (65)
$nfe->tagpag($std);
$std = new stdClass();
$std->tPag = '01'; //Dinheiro
$std->vPag = 1.99; //Obs: deve ser informado o valor pago pelo cliente
/*$std->CNPJ = '12345678901234';
$std->tBand = '01';
$std->cAut = '3333333';
$std->tpIntegra = 1; //incluso na NT 2015/002*/
$nfe->tagdetPag($std);
$std = new stdClass();
//$std->infAdFisco = 'informacoes para o fisco';
$std->infCpl = 'DOCUMENTO EMITIDO POR ME OU EPP OPTANTE PELO SIMPLES NACIONAL NAO GERA DIREITO A CREDITO FISCAL DE IPI';
$elem = $nfe->taginfAdic($std);
$xml = $nfe->getXML();
$config = [
"atualizacao" => $data_hora->format('Y-m-d H:i:s'),
"tpAmb" => 1, // Se deixar o tpAmb como 2 você emitirá a nota em ambiente de homologação(teste) e as notas fiscais aqui não tem valor fiscal
"razaosocial" => "ADENILSON AURELIO DE AMORIM DE JUAZEIRO EPP",
"siglaUF" => "BA",
"cnpj" => "04112864000156",
"schemes" => "PL_008i2",
"versao" => "3.10",
"tokenIBPT" => "AAAAAAA",
"CSC" => "9E252ACA340048A4995F7AB4A5F9DC4B",
"CSCid" => "4"
];
$configJson = json_encode($config);
$certificadoDigital = file_get_contents('59664.pfx');
$tools = new NFePHP\NFe\Tools($configJson, NFePHP\Common\Certificate::readPfx($certificadoDigital, '1234'));
$xmlAssinado = $tools->signNFe($xml);
$idLote = str_pad(2586, 15, '0', STR_PAD_LEFT); // Identificador do lote
file_put_contents('enviado16.xml',$xmlAssinado);
$resp = $tools->sefazEnviaLote([$xmlAssinado], $idLote);
file_put_contents('retorno16.xml',$resp);
$st = new NFePHP\NFe\Common\Standardize();
$std = $st->toStd($resp);
if ($std->cStat != 103) {
//erro registrar e voltar
exit("[$std->cStat] $std->xMotivo");
}
echo '<pre><hr>';
print_r('ok');
echo '<hr></pre>';
$recibo = $std->infRec->nRec; // Vamos usar a variável $recibo para consultar o status da nota
$protocolo = $tools->seFazConsultaRecibo($recibo);
$protocol = new NFePHP\NFe\Factories\Protocol();
$xmlProtocolado = $protocol->add($xmlAssinado,$protocolo);
file_put_contents('nota.xml',$xmlProtocolado);