Sie sind auf Seite 1von 3

procedure TfrmMovCancelarCNT.

CancelarConhecimentoEvento;
var
dhEvento: TDateTime;
begin
DMDFe.CTe.EventoCTe.Evento.Clear;

with DMDFe.CTe.EventoCTe.Evento.Add do
begin
infEvento.nSeqEvento := 1;
infEvento.chCTe := Chave;
infEvento.CNPJ := Copy(DM_CTA.EmpresaCNPJ.AsString, 2, 14);
infEvento.dhEvento := now;
infEvento.tpEvento := teCancelamento;
infEvento.detEvento.xJust := trim(edtJustificativa.Text);
infEvento.detEvento.nProt := Protocolo;
end;

DMDFe.CTe.EventoCTe.Evento.Items[0].InfEvento.detEvento.infCorrecao.Clear;

// Lote de Eventos
DM_VEN.LoteEve.Close;
DM_VEN.LoteEve.SQL.Clear;
DM_VEN.LoteEve.SQL.Add('Select * From Eve_Lote');
DM_VEN.LoteEve.SQL.Add('Where Empresa = :xEmpresa');
DM_VEN.LoteEve.SQL.Add('Order By Lote');
DM_VEN.LoteEve.Params[0].AsString:=DM_CTA.xCodEmpresa;
DM_VEN.LoteEve.Active:=True;
DM_VEN.LoteEve.Open;
DM_VEN.LoteEve.Last;
if DM_VEN.LoteEve.RecordCount>0
then begin
if (DM_VEN.LoteEveEveInicial.AsInteger=0) and
(DM_VEN.LoteEveEveFinal.AsInteger=0)
then idLote:=DM_VEN.LoteEveLote.AsInteger
else begin
idLote:=(DM_VEN.LoteEveLote.AsInteger+1);

DM_VEN.IncAltDel.SQL.Clear;
DM_VEN.IncAltDel.SQL.Add('Insert into Eve_Lote');
DM_VEN.IncAltDel.SQL.Add('(Empresa, Lote, EveInicial, EveFinal, Data)');
DM_VEN.IncAltDel.SQL.Add('Values (:xEmpresa, :xLote, :xEveInicial, :xEveFinal,
:xData)');
DM_VEN.IncAltDel.Params[0].AsString:=DM_CTA.xCodEmpresa;
DM_VEN.IncAltDel.Params[1].AsInteger:=idLote;
DM_VEN.IncAltDel.Params[2].AsInteger:=0;
DM_VEN.IncAltDel.Params[3].AsInteger:=0;
DM_VEN.IncAltDel.Params[4].AsDateTime:=Date;
DM_VEN.IncAltDel.ExecSQL;
end;
end
else begin
idLote:=1;

DM_VEN.IncAltDel.SQL.Clear;
DM_VEN.IncAltDel.SQL.Add('Insert into Eve_Lote');
DM_VEN.IncAltDel.SQL.Add('(Empresa, Lote, EveInicial, EveFinal, Data)');
DM_VEN.IncAltDel.SQL.Add('Values (:xEmpresa, :xLote, :xEveInicial,
:xEveFinal, :xData)');
DM_VEN.IncAltDel.Params[0].AsString:=DM_CTA.xCodEmpresa;
DM_VEN.IncAltDel.Params[1].AsInteger:=idLote;
DM_VEN.IncAltDel.Params[2].AsInteger:=0;
DM_VEN.IncAltDel.Params[3].AsInteger:=0;
DM_VEN.IncAltDel.Params[4].AsDateTime:=Date;
DM_VEN.IncAltDel.ExecSQL;
end;

DMDFe.CTe.EnviarEventoCTe(idLote);

EveInicial := DMDFe.CTe.Conhecimentos.Items[0].CTe.Ide.nCT;
EveFinal := EveInicial;

DM_VEN.IncAltDel.SQL.Clear;
DM_VEN.IncAltDel.SQL.Add('Update Eve_Lote Set');
DM_VEN.IncAltDel.SQL.Add('EveInicial = :xEveInicial,');
DM_VEN.IncAltDel.SQL.Add('EveFinal = :xEveFinal,');
DM_VEN.IncAltDel.SQL.Add('Data = :xData');
DM_VEN.IncAltDel.SQL.Add('Where Empresa = :xEmpresa');
DM_VEN.IncAltDel.SQL.Add('and Lote = :xLote');
DM_VEN.IncAltDel.Params[0].AsInteger:=EveInicial;
DM_VEN.IncAltDel.Params[1].AsInteger:=EveFinal;
DM_VEN.IncAltDel.Params[2].AsDateTime:=Date;
DM_VEN.IncAltDel.Params[3].AsString:=DM_CTA.xCodEmpresa;
DM_VEN.IncAltDel.Params[4].AsInteger:=idLote;
DM_VEN.IncAltDel.ExecSQL;

MemoStatus.Lines.Add('-------------------------------------------------------------
------------------------------');
MemoStatus.Lines.Add(' => Cancelando CT-e com chave de acesso n ' +
DM_CNT.Conhec2ChaveCTe.AsString );

MemoStatus.Lines.Add('-------------------------------------------------------------
------------------------------');
MemoStatus.Lines.Add(' Justificativa: ' + edtJustificativa.Text);

sProtocolo :=
DMDFe.CTe.WebServices.EnvEvento.EventoRetorno.retEvento.Items[0].RetInfEvento.nProt
;
sStat :=
IntToStr(DMDFe.CTe.WebServices.EnvEvento.EventoRetorno.retEvento.Items[0].RetInfEve
nto.cStat);
sMotivo :=
DMDFe.CTe.WebServices.EnvEvento.EventoRetorno.retEvento.Items[0].RetInfEvento.xMoti
vo;
dhEvento :=
DMDFe.CTe.WebServices.EnvEvento.EventoRetorno.retEvento.Items[0].RetInfEvento.dhReg
Evento;
sDataHora := DateTimeToStr(dhEvento);

DM_CNT.IncAltDel.SQL.Clear;
DM_CNT.IncAltDel.SQL.Add('Insert into Cnt_Eventos');
DM_CNT.IncAltDel.SQL.Add('(Codigo, Serie, Numero, Evento, Sequencia, DataEvento,
Protocolo)');
DM_CNT.IncAltDel.SQL.Add('Values (:xCodigo, :xSerie, :xNumero, :xEvento,
:xSequencia, :xData, :xProtocolo)');
DM_CNT.IncAltDel.Params[0].AsInteger:=edtUnidade.ValueInt;
DM_CNT.IncAltDel.Params[1].AsInteger:=DM_CNT.Conhec2Serie.AsInteger;
DM_CNT.IncAltDel.Params[2].AsInteger:=edtNumero.ValueInt;
DM_CNT.IncAltDel.Params[3].AsInteger:=110111;
DM_CNT.IncAltDel.Params[4].AsInteger:=1;
DM_CNT.IncAltDel.Params[5].AsDateTime:=dhEvento;
DM_CNT.IncAltDel.Params[6].AsString:=sProtocolo;
DM_CNT.IncAltDel.ExecSQL;

DM_CNT.IncAltDel.SQL.Clear;
DM_CNT.IncAltDel.SQL.Add('Update Cnt_Conhecimento Set');
DM_CNT.IncAltDel.SQL.Add('Cancelado = :xCancelado,');
DM_CNT.IncAltDel.SQL.Add('CanProtocolo = :xProtocolo,');
DM_CNT.IncAltDel.SQL.Add('CanMotivo = :xMotivo');
DM_CNT.IncAltDel.SQL.Add('Where Codigo = :xCodigo');
DM_CNT.IncAltDel.SQL.Add('and Numero = :xNumero');
DM_CNT.IncAltDel.Params[0].AsString:='S';
DM_CNT.IncAltDel.Params[1].AsString:=sProtocolo;
DM_CNT.IncAltDel.Params[2].AsString:=edtJustificativa.Text;
DM_CNT.IncAltDel.Params[3].AsInteger:=edtUnidade.ValueInt;
DM_CNT.IncAltDel.Params[4].AsInteger:=edtNumero.ValueInt;
DM_CNT.IncAltDel.ExecSQL;

MemoStatus.Lines.Add(' Protocolo : ' + sProtocolo);


MemoStatus.Lines.Add(' Status : ' + sStat);
MemoStatus.Lines.Add(' Motivo : ' + sMotivo);
MemoStatus.Lines.Add(' Data/Hora : ' + sDataHora);
MemoStatus.Lines.Add(' ');

MemoStatus.Lines.Add('-------------------------------------------------------------
------------------------------');
MemoStatus.Lines.Add(' Aguarde a Impresso...');

PathEvento := DMDFe.CTe.Configuracoes.Arquivos.GetPathEvento(teCancelamento,
dhEvento);
NomeEvento := PathEvento + '\' + DM_CNT.Conhec2ChaveCTe.AsString +
'110111' + '01' + '-procEventoCTe.xml';

DMDFe.CTe.EventoCTe.Evento.Clear;
DMDFe.CTe.EventoCTe.LerXML(NomeEvento);
DMDFe.CTe.ImprimirEvento;
end;

Das könnte Ihnen auch gefallen