Se faço o cancelamento ou emissão de uma CCe direto funciona corretamente. Mas se emito uma NFe com impressão do DANFE sistema gera erro de "Access vilation at address...". Esse erro o sistema esta apontando para um componente ou variável que não existe, foi destruída. Abaixo esta o código que chama o evento e as funções vinculadas a ela.
PreparaEvento(NFeMotivoCancelamento,teCancelamento);
dmTMP.NFe.EnviarEvento(1);
with dmTMP.NFe.WebServices.EnvEvento do begin
if (EventoRetorno.retEvento.Items[0].RetInfEvento.cStat <> 135) and
(EventoRetorno.retEvento.Items[0].RetInfEvento.cStat <> 155) then begin
oEvento := EventoRetorno.retEvento.Items[0].RetInfEvento.xMotivo;
ConsultarNFe(False);
if Pos('CANCEL',UpperCase(dmTMP.NFe.WebServices.Retorno.XMotivo)) > 0 then
dmTMP.NFe.ImprimirEvento;
Mensagem(oEvento);
FRetorno := '135';
exit;
end;
DM.sdsNfeEvento.Close;
dmTMP.NFe.ImprimirEvento;
Procedure TfrmMensagemNfe.PreparaEvento(NFeMotivo : string;
tpEven : TpcnTpEvento);
begin
dmTMP.NFe.EventoNFe.Evento.Clear;
dmTMP.NFe.NotasFiscais.Clear;
dmTMP.PreparaEmissorNFE;
dmTMP.NFe.EventoNFe.idLote := 1;
with dmTMP.NFe.EventoNFe.Evento.Add do begin
InfEvento.cOrgao := DM.sdsEmpresaCodigoUF.AsInteger;
InfEvento.chNFe := sdsGNFeCHAVENFE.AsString;
InfEvento.CNPJ := DM.sdsEmpresaCNPJ.AsString;
InfEvento.dhEvento := now;
InfEvento.tpEvento := tpEven;
if tpEven = teCCe then begin
infEvento.nSeqEvento := oSeqEvento;
infEvento.detEvento.xCorrecao := NFeMotivo;
end else begin
InfEvento.detEvento.xJust := NFeMotivo;
InfEvento.detEvento.nProt := sdsGNFePROTOCOLO.AsString;
end;
end;
end;
function TdmTMP.PreparaEmissorNFE: Boolean;
var
Mes: string;
Ano: string;
Lista: String;
begin
if (DM.sdsEmpresalogoNFe.AsString <> '') and
(FileExists(DM.sdsEmpresalogoNFe.AsString)) then
NFe.DANFE.Logo := DM.sdsEmpresalogoNFe.AsString;
Mes := FormatDateTime('mm', NOW);
Ano := FormatDateTime('yyyy', NOW);
dmTMP.NFe.Configuracoes.WebServices.Ambiente := taHomologacao
Lista := '';
Result := true;
ACBRDANFE.FastFile := ExtractFilePath(Application.ExeName) + 'report\DANFeRetrato.fr3';
ACBRDANFE.FastFileEvento := ExtractFilePath(Application.ExeName) + 'report\EVENTOS.fr3';
ACBRDANFE.FastFileInutilizacao := ExtractFilePath(Application.ExeName) + 'report\INUTILIZACAO.fr3';
NFe.Configuracoes.arquivos.PathNFe := ExtractFilePath(Application.ExeName) + 'NFE\XML';
NFe.Configuracoes.arquivos.PathSchemas := ExtractFilePath(Application.ExeName) + 'Schemas';
NFe.Configuracoes.arquivos.PathInu := ExtractFilePath(Application.ExeName) + 'NFE\XML';
NFe.Configuracoes.arquivos.PathEvento := ExtractFilePath(Application.ExeName) + 'NFE\XML';
NFe.Configuracoes.arquivos.PathSalvar := ExtractFilePath(Application.ExeName) + 'NFE\XML';
ACBRDANFE.PathPDF := ExtractFilePath(Application.ExeName) + 'NFE\PDF\' + Mes + Ano;
ChecaDiretorios(ACBRDANFE.PathPDF);
ChecaDiretorios(ExtractFilePath(Application.ExeName) + 'NFE');
ChecaDiretorios(ExtractFilePath(Application.ExeName) + 'NFE\XML');
ChecaDiretorios(NFe.Configuracoes.arquivos.PathNFe);
ChecaDiretorios(NFe.Configuracoes.arquivos.PathInu);
ChecaDiretorios(NFe.Configuracoes.arquivos.PathEvento);
ChecaDiretorios(NFe.Configuracoes.arquivos.PathSalvar);
ChecaDiretorios(ACBRDANFE.PathPDF);
if ConfigSYS[67] = '' then begin
Lista := Lista + 'Versão da NF-e não definida' + #13;
end;
if ConfigSYS[63] = '' then begin
Lista := Lista + 'Série da NF-e não definida' + #13;
if (DM.sdsEmpresaCNAE.AsString = '0') or (DM.sdsEmpresaCNAE.AsString = '') then
Lista := Lista + 'CNAE empresa não definido' + #13;
end;
if not DirectoryExists(ExtractFilePath(Application.ExeName) + 'Schemas') then
Lista := Lista + NFe.Configuracoes.arquivos.PathSchemas + #13;
if not ChecaArquivos(ACBRDANFE.FastFile) then
Lista := Lista + ACBRDANFE.FastFile + #13;
if not ChecaArquivos(ACBRDANFE.FastFileEvento) then
Lista := Lista + ACBRDANFE.FastFileEvento + #13;
if not ChecaArquivos(ACBRDANFE.FastFileInutilizacao) then
Lista := Lista + ACBRDANFE.FastFileInutilizacao + #13;
if not ChecaArquivos(ExtractFilePath(Application.ExeName) + 'Report\rptPreVenda.fr3') then
Lista := Lista + ExtractFilePath(Application.ExeName) + 'ReportrptPreVenda.fr3' + #13;
if Lista <> '' then begin
ShowMessage('Erro ao iniciar NFe. Entre em contato com o suporte. Os seguintes arquivos não foram encontrados.: ' + #13 + Lista);
Result := false
end;
NFe.WebServices.Retorno.Clear;
end;