CavaloMarinho,
Lhe recomendo sempre gravar no BD o XML cancelado e o XML autorizado, veja abaixo a minha rotina, eu busco o XML salvo no HD e insiro no BD:
Assim, você organiza e tem mais segurança.
procedure TfrmNotaFiscal.CancelarNFe1Click(Sender: TObject);
var
idLote, CNPJ, Justificativa : string;
quGeral, quCrTit: TADOQuery;
begin
quCrTit := TADOQuery.Create(Self);
quCrTit.Connection := frmPrincipal.BD;
quCrTit.SQL.Add('SELECT 1 FROM NOTAVENPARCEL T0');
quCrTit.SQL.Add('WHERE T0.CODIGO = '+Codigo.Text);
quCrTit.SQL.Add('AND T0.CODEMP = '+frmPrincipal.EmpAtiva);
quCrTit.SQL.Add('AND T0.VALORPARCELA <> T0.VALORABERTO');
quCrTit.Open;
if quCrTit.IsEmpty then
begin
IniciaComponente;
CNPJ := copy(ChaveNFe.Text,7,14);
Justificativa := 'Justificativa do Cancelamento';
if not(InputQuery('WebServices Eventos: Cancelamento', 'Justificativa do Cancelamento', Justificativa)) then
exit;
ACBNF.EventoNFe.Evento.Clear;
with ACBNF.EventoNFe.Evento.Add do
begin
infEvento.chNFe := ChaveNFe.Text;
infEvento.CNPJ := copy(ChaveNFe.Text,7,14);
infEvento.dhEvento := now;
infEvento.tpEvento := teCancelamento;
infEvento.detEvento.xJust := Justificativa;
infEvento.detEvento.nProt := Protocolo.Text;
end;
try
ACBNF.EnviarEventoNFe(StrToInt(Codigo.Text));
//verificar ainda se foi baixado o titulo
CodigoNFe.Text := IntToStr(ACBNF.WebServices.EnvEvento.EventoRetorno.retEvento.Items[0].RetInfEvento.cStat);
MsgSefaz.Lines.Add('Justificativa: '+Justificativa);
MsgSefaz.Lines.Add(
DateTimeToStr(ACBNF.WebServices.EnvEvento.EventoRetorno.retEvento.Items[0].RetInfEvento.dhRegEvento) + ': ' +
ACBNF.WebServices.EnvEvento.EventoRetorno.retEvento.Items[0].RetInfEvento.xMotivo);
if CodigoNFe.Text = '135' then
begin
Status.ItemIndex := 6;
EstornaEstoque;
InserirXMLdaNFe(ChaveNFe.Text + '1101111-ProcEventoNFe.xml', 'Can');
ACBNF.Free;
ShowMessage('Nota Fiscal Cancelada com Exito.');
end
else
begin
quGeral := TADOQuery.Create(Self);
quGeral.Connection := frmPrincipal.BD;
quGeral.SQL.Add('UPDATE NOTAVENCAB');
quGeral.SQL.Add('SET MSGSEFAZ = :MSGSEFAZ');
quGeral.SQL.Add('WHERE CODIGO = '+Codigo.Text);
quGeral.SQL.Add('AND CODEMP = '+frmPrincipal.EmpAtiva);
quGeral.Parameters.ParamByName('MSGSEFAZ').Value := MsgSefaz.Text;
quGeral.ExecSQL;
quGeral.Free;
ACBNF.Free;
MessageDlg('Erro ao cancelar a NF: '+#13+ACBNF.WebServices.EnvEvento.EventoRetorno.retEvento.Items[0].RetInfEvento.xMotivo, mtError, [mbOK], 0);
end;
Except
on e:exception do
begin
CodigoNFe.Text := IntToStr(ACBNF.WebServices.EnvEvento.cStat);
MsgSefaz.Lines.Add(DateTimeToStr(now) + ': '+ACBNF.WebServices.EnvEvento.xMotivo);
MsgSefaz.Lines.Add(DateTimeToStr(now) + ': '+E.Message);
ACBNF.Free;
MessageDlg('Erro ao cancelar a NF: '+#13+E.Message, mtError, [mbOK], 0);
end;
end;
end
else
begin
MessageDlg('Erro ao cancelar a nota fiscal já possui baixas.'+sLineBreak+'Cancele a baixa e tente baixar novamente.', mtError, [mbOK], 0);
end;
end;