Ok, segue abaixo a rotina, só para constar,
estou testando no ambiente de homologação,
SEFAZ-PE
// Cria lote de cancelamento
NumeroLote := StrToInt(FormatDateTime('yymmddhhmm', NOW));
Dm1.Nfe.EventoNFe.Evento.Clear;
Dm1.Nfe.EventoNFe.idLote := NumeroLote;
// Seta atributos
with Dm1.Nfe.EventoNFe.Evento.Add do
begin
infEvento.chNFe := CDSGeradaChaveNFE.AsString;
infEvento.CNPJ := Dm1.sCNPJ;
infEvento.dhEvento := NOW;
infEvento.tpEvento := teCancelamento;
infEvento.detEvento.nProt := CDSGeradaProtocoloNFE.AsString;
infEvento.detEvento.xJust := EdJustifica.Text;
end;
// Envia evento de cancelamento
if Dm1.Nfe.EnviarEventoNFe(NumeroLote) then
begin
with Dm1.Nfe.WebServices.EnvEvento do
begin
if EventoRetorno.retEvento.Items[0].RetInfEvento.cStat <> 135 then
begin
raise Exception.CreateFmt(
'Ocorreu o seguinte erro ao cancelar a nota fiscal eletrônica:' + sLineBreak +
'Código:%d' + sLineBreak +
'Motivo: %s', [
EventoRetorno.retEvento.Items[0].RetInfEvento.cStat,
EventoRetorno.retEvento.Items[0].RetInfEvento.xMotivo]);
end;
// retornos
DataHoraEvento := EventoRetorno.retEvento.Items[0].RetInfEvento.dhRegEvento;
NumeroProtocolo := EventoRetorno.retEvento.Items[0].RetInfEvento.nProt;
XMLCancelamento := EventoRetorno.retEvento.Items[0].RetInfEvento.XML;
CodigoStatus := EventoRetorno.retEvento.Items[0].RetInfEvento.cStat;
MotivoStatus := EventoRetorno.retEvento.Items[0].RetInfEvento.xMotivo;
// atualiza tabela de entrada/saida fiscal
with Dm1.QyGeral do
begin
Close;
if CBoxTpMov.ItemIndex = 0 then
begin
Sql.Text := ' UPDATE TBENTRADAFISCAL SET SITUACAONFISCAL = ' + QuotedStr('C') + ', ProtocoloCancelou = ' +
QuotedStr(NumeroProtocolo) + ' ' +
' WHERE NUMERONFISCAL = ' + QuotedStr(CDSGerada.FieldByName('NumeroNFiscal').AsString) + ' ' +
' AND SERIENFISCAL = ' + QuotedStr(CDSGerada.FieldByName('SerieNFiscal').AsString) + ' ' ;
sTabela := 'TBENTRADAFISCAL';
end
else
begin
Sql.Text := ' UPDATE TBSAIDAFISCAL SET SITUACAONFISCAL = ' + QuotedStr('C') + ', ProtocoloCancelou = ' +
QuotedStr(NumeroProtocolo) + ' ' +
' WHERE NUMERONFISCAL = ' + QuotedStr(CDSGerada.FieldByName('NumeroNFiscal').AsString) + ' ' +
' AND SERIENFISCAL = ' + QuotedStr(CDSGerada.FieldByName('SerieNFiscal').AsString) + ' ' ;
sTabela := 'TBSAIDAFISCAL';
end;
ExecSQL;
Close;
end;
// Salva em auditoria
Dm1.salvaAuditoria('FATURAMENTO', sTabela, 'FATURAMENTO', 'NOTA FISCAL: ' + CDSGerada.FieldByName('NumeroNFiscal').AsString + ' SERIE ' + CDSGerada.FieldByName('SerieNFiscal').AsString, '5');
ShowMessage('A NF-e ' + CDSGerada.FieldByName('NumeroNFiscal').AsString + ' Foi cancelada...' +
'Retornou : ' + MotivoStatus + #13 + 'Protocolo : ' + NumeroProtocolo);
end;
end
else
begin
with Dm1.Nfe.WebServices.EnvEvento do
begin
raise Exception.Create(
'Ocorreram erros ao tentar efetuar o cancelamento:' + sLineBreak +
'Lote: ' + IntToStr(EventoRetorno.idLote) + sLineBreak +
'Ambiente: ' + TpAmbToStr(EventoRetorno.tpAmb) + sLineBreak +
'Orgao: ' + IntToStr(EventoRetorno.cOrgao) + sLineBreak +
sLineBreak +
'Status: ' + IntToStr(EventoRetorno.cStat) + sLineBreak +
'Motivo: ' + EventoRetorno.xMotivo);
end;
Exit;
end;