{$I ACBr.inc} unit Frm_MovEmitirCNT; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Buttons, StdCtrls, EditNumber, Grids, DBGrids, ExtCtrls, ComCtrls, jpeg, Mask, CheckLst; type TfrmMovEmitirCNT = class(TForm) PrintDialog: TPrintDialog; gbPeriodo: TGroupBox; Label4: TLabel; Label5: TLabel; edtDataInicial: TMaskEdit; edtDataFinal: TMaskEdit; rgTipoEmissao: TRadioGroup; gbContingencia: TGroupBox; Label1: TLabel; Label2: TLabel; Label6: TLabel; edtDataCont: TMaskEdit; edtHoraCont: TMaskEdit; edtJustificativa: TEdit; rgCondicao: TRadioGroup; chkVisualizar: TCheckBox; Label11: TLabel; edtNumCopias: TEditNumber; Label7: TLabel; Label8: TLabel; Label3: TLabel; Label9: TLabel; Label10: TLabel; Label14: TLabel; Label13: TLabel; chkListaConhecimentos: TCheckListBox; lstChave3: TListBox; lstChave4: TListBox; lstChave1: TListBox; lstChave2: TListBox; Label16: TLabel; Panel1: TPanel; btnImprimir: TBitBtn; btnLimpar: TBitBtn; btnEmail: TBitBtn; btnConsultaStatus: TBitBtn; btnMarcar: TBitBtn; btnDesmarcar: TBitBtn; MemoStatus: TRichEdit; gbProgresso: TGroupBox; pbProgresso: TProgressBar; btnInformacoes: TSpeedButton; lstChaveS1: TListBox; lstChaveS2: TListBox; Label12: TLabel; procedure ListaConhecimentos; procedure LimpaCampos; procedure FormShow(Sender: TObject); procedure FormKeyPress(Sender: TObject; var Key: Char); procedure rgCondicaoClick(Sender: TObject); procedure AtivaBotao; procedure chkListaConhecimentosClickCheck(Sender: TObject); procedure btnMarcarClick(Sender: TObject); procedure btnDesmarcarClick(Sender: TObject); procedure edtDataInicialExit(Sender: TObject); procedure edtDataFinalExit(Sender: TObject); procedure MontaListadosSelecionados; procedure rgTipoEmissaoClick(Sender: TObject); procedure edtJustificativaChange(Sender: TObject); procedure edtDataContExit(Sender: TObject); procedure edtHoraContExit(Sender: TObject); procedure edtNumCopiasExit(Sender: TObject); procedure GerarLoteCTe; procedure EnviarLote(Lote: Integer); procedure EnviarEmail; procedure NaoEmitidos; procedure Emitidos; procedure NaoProtocolados; procedure NaoProtocoladosContingencia; procedure btnImprimirClick(Sender: TObject); procedure btnLimparClick(Sender: TObject); procedure btnEmailClick(Sender: TObject); procedure btnConsultaStatusClick(Sender: TObject); procedure btnInformacoesClick(Sender: TObject); private { Private declarations } sDocumento, sDataHora, sStat, sMotivo, sRecibo, sProtocolo, sConhecimento, Endereco, Texto: String; Selecionado, Ok, Falha: Boolean; iHost, jHost: Integer; Mensagem, sCC: TStrings; public { Public declarations } end; var frmMovEmitirCNT: TfrmMovEmitirCNT; Const LimiteCTe = 40; implementation uses DateUtils, Math, Printers, pcnConversao, pcnAuxiliar, ACBrDFeUtil, CTA_DataModule, CNT_DataModule, BPM_DataModule, DFe_DataModule, Frm_ACBrCTe; {$R *.dfm} { TfrmMovEmitirCNT } procedure TfrmMovEmitirCNT.ListaConhecimentos; var i, j: Integer; Chave, Impresso: String; begin DM_CNT.Conhec2.Close; DM_CNT.Conhec2.SQL.Clear; DM_CNT.Conhec2.SQL.Add('Select Cnt_Conhecimento.*, Sis_PessoaFJ.*'); DM_CNT.Conhec2.SQL.Add('From Cnt_Conhecimento, Sis_PessoaFJ'); DM_CNT.Conhec2.SQL.Add('Where Cnt_Conhecimento.Destinatario = Sis_PessoaFJ.CGC'); DM_CNT.Conhec2.SQL.Add('and Cnt_Conhecimento.Codigo = :xCodigo'); DM_CNT.Conhec2.SQL.Add('and Cnt_Conhecimento.Serie = :xSerie'); DM_CNT.Conhec2.SQL.Add('and Data >= :xDataI'); DM_CNT.Conhec2.SQL.Add('and Data <= :xDataF'); DM_CNT.Conhec2.SQL.Add('and Gravado = :xGravado'); if rgCondicao.ItemIndex<2 // alterado de <> para < then DM_CNT.Conhec2.SQL.Add('and Impresso = :xImpresso') else DM_CNT.Conhec2.SQL.Add('and Protocolado = :xProtocolado'); if rgCondicao.ItemIndex<>1 then DM_CNT.Conhec2.SQL.Add('and Cancelado = :xCancelado'); if rgCondicao.ItemIndex>1 then DM_CNT.Conhec2.SQL.Add('and TipoEmissao = :xTipoEmissao'); DM_CNT.Conhec2.SQL.Add('Order By Data, Cnt_Conhecimento.Codigo, Cnt_Conhecimento.Numero'); DM_CNT.Conhec2.Params[0].AsInteger:=DM_CTA.ParamDFeUnidadeOrg.AsInteger; // Unidade Operacional DM_CNT.Conhec2.Params[1].AsInteger:=DM_CTA.ParamDFeSerie.AsInteger; // Serie DM_CNT.Conhec2.Params[2].AsDateTime:=StrToDate(edtDataInicial.Text); DM_CNT.Conhec2.Params[3].AsDateTime:=StrToDate(edtDataFinal.Text); DM_CNT.Conhec2.Params[4].AsString:='S'; if rgCondicao.ItemIndex=1 then DM_CNT.Conhec2.Params[5].AsString:='S' else DM_CNT.Conhec2.Params[5].AsString:='N'; if rgCondicao.ItemIndex<>1 then DM_CNT.Conhec2.Params[6].AsString:='N'; if rgCondicao.ItemIndex=2 then DM_CNT.Conhec2.Params[7].AsInteger:=0; if rgCondicao.ItemIndex=3 then DM_CNT.Conhec2.Params[7].AsInteger:=1; DM_CNT.Conhec2.Active:=True; DM_CNT.Conhec2.Open; DM_CNT.Conhec2.Last; j:=DM_CNT.Conhec2.RecordCount-1; lstChave1.Clear; lstChave2.Clear; lstChaveS1.Clear; chkListaConhecimentos.Clear; if j>=0 then begin DM_CNT.Conhec2.First; for i:=0 to j do begin Chave:=DM_CNT.Conhec2Codigo.AsString; lstChave1.Items.Add(Chave); Chave:=DM_CNT.Conhec2Numero.AsString; lstChave2.Items.Add(Chave); Chave:=DM_CNT.Conhec2Serie.AsString; lstChaveS1.Items.Add(Chave); Impresso:=' '; if DM_CNT.Conhec2Impresso.AsString='S' then Impresso:='I'; if DM_CNT.Conhec2Cancelado.AsString='S' then Impresso:=Impresso+'C'; Chave:=FormatFloat('000', DM_CNT.Conhec2Codigo.AsInteger)+'-'+ FormatFloat('0000000000',DM_CNT.Conhec2Numero.AsInteger)+'-'+ DM_CNT.Conhec2Data.AsString+'-'+ DM_CNT.Conhec2Destinatario.AsString+'-'+ DM_CTA.Alinhar(Copy(DM_CNT.Conhec2RSocial.AsString,1,30),30,'L')+'-'+ DM_CTA.Alinhar(FormatFloat('###,##0.00',DM_CNT.Conhec2ValorTotal.AsFloat),10,'R')+'-'+ DM_CTA.Alinhar(DM_CNT.Conhec2Protocolo.AsString, 15, 'L')+'-'+Impresso; chkListaConhecimentos.Items.Add(Chave); DM_CNT.Conhec2.Next; end; end; AtivaBotao; end; procedure TfrmMovEmitirCNT.LimpaCampos; begin Texto := 'Resumo'; chkVisualizar.Visible := True; chkVisualizar.Checked := False; rgTipoEmissao.ItemIndex := 0; gbContingencia.Visible := False; sDocumento := 'CTE'; DMDFe.ConfigurarCTe(sDocumento); edtDataInicial.Text := DateToStr(Date-30); edtDataInicial.SetFocus; edtDataFinal.Text := DateToStr(Date); rgCondicao.ItemIndex := 0; edtNumCopias.StoreValue(DM_CTA.ParamDFeDAVias.AsInteger); pbProgresso.Position := 0; ListaConhecimentos; end; procedure TfrmMovEmitirCNT.FormShow(Sender: TObject); begin MemoStatus.Clear; MemoStatus.Lines.Clear; LimpaCampos; end; procedure TfrmMovEmitirCNT.FormKeyPress(Sender: TObject; var Key: Char); begin if key=#13 then begin Perform(CM_DialogKey, VK_TAB, 0); Key:=#0; end; end; procedure TfrmMovEmitirCNT.rgCondicaoClick(Sender: TObject); begin ListaConhecimentos; end; procedure TfrmMovEmitirCNT.AtivaBotao; var Ok: Boolean; begin if rgTipoEmissao.ItemIndex >= 1 then Ok:=(Length(trim(edtJustificativa.Text))>15) else Ok:=True; btnImprimir.Enabled:=(chkListaConhecimentos.Items.Count>0) and Selecionado and Ok; btnMarcar.Enabled:=(chkListaConhecimentos.Items.Count>0); btnDesmarcar.Enabled:=(chkListaConhecimentos.Items.Count>0); btnEmail.Enabled:=(chkListaConhecimentos.Items.Count>0) and Selecionado; end; procedure TfrmMovEmitirCNT.chkListaConhecimentosClickCheck(Sender: TObject); var i, j: Integer; begin Selecionado := False; j:=chkListaConhecimentos.Items.Count-1; if j>=0 then begin for i:=0 to j do begin if chkListaConhecimentos.Checked[i] then Selecionado:=True; end; end; AtivaBotao; end; procedure TfrmMovEmitirCNT.btnMarcarClick(Sender: TObject); var i, j: Integer; begin j:=chkListaConhecimentos.Items.Count-1; if j>=0 then begin for i:=0 to j do begin chkListaConhecimentos.Checked[i]:=True; end; end; Selecionado:=True; AtivaBotao; end; procedure TfrmMovEmitirCNT.btnDesmarcarClick(Sender: TObject); var i, j: Integer; begin j:=chkListaConhecimentos.Items.Count-1; if j>=0 then begin for i:=0 to j do begin chkListaConhecimentos.Checked[i]:=False; end; end; Selecionado:=False; AtivaBotao; end; procedure TfrmMovEmitirCNT.edtDataInicialExit(Sender: TObject); begin if edtDataInicial.Text=' / / ' then edtDataInicial.Text:=DateToStr(Date); end; procedure TfrmMovEmitirCNT.edtDataFinalExit(Sender: TObject); begin if edtDataFinal.Text=' / / ' then edtDataFinal.Text:=DateToStr(Date); ListaConhecimentos; end; procedure TfrmMovEmitirCNT.MontaListadosSelecionados; var i, j: Integer; begin j:=chkListaConhecimentos.Items.Count-1; lstChave3.Clear; lstChave4.Clear; lstChaveS2.Clear; if (j>=0) and Selecionado then begin for i:=0 to j do begin if chkListaConhecimentos.Checked[i] then begin lstChave3.Items.Add(lstChave1.Items.Strings[i]); lstChave4.Items.Add(lstChave2.Items.Strings[i]); lstChaveS2.Items.Add(lstChaveS1.Items.Strings[i]); end; end; end else MessageDlg('Nenhum Conhecimento Selecionado.', mtInformation, [mbOk], 0); end; procedure TfrmMovEmitirCNT.rgTipoEmissaoClick(Sender: TObject); begin gbContingencia.Visible := (rgTipoEmissao.ItemIndex <> 0); if rgTipoEmissao.ItemIndex <> 0 then begin edtDataCont.Text:=DateToStr(Now); edtHoraCont.Text:=Copy(TimeToStr(Now), 1, 5); edtJustificativa.Text:=''; edtJustificativa.SetFocus; end; // (AC,AL,AP,AM,BA,CE,DF,ES,GO,MA,MT,MS,MG,PA,PB,PR,PE,PI,RJ,RN,RS,RO,RR,SC,SP,SE,TO); // (12,27,16,13,29,23,53,32,52,21,51,50,31,15,25,41,26,22,33,24,43,11,14,42,35,28,17); case rgTipoEmissao.ItemIndex of 0: DMDFe.CTe.Configuracoes.Geral.FormaEmissao := teNormal; 1: DMDFe.CTe.Configuracoes.Geral.FormaEmissao := teFSDA; // Contingencia FSDA 2: if DMDFe.CTe.Configuracoes.WebServices.UFCodigo in [14, 16, 26, 35, 50, 51] then DMDFe.CTe.Configuracoes.Geral.FormaEmissao := teSVCRS else DMDFe.CTe.Configuracoes.Geral.FormaEmissao := teSVCSP; end; end; procedure TfrmMovEmitirCNT.edtJustificativaChange(Sender: TObject); begin AtivaBotao; end; procedure TfrmMovEmitirCNT.edtDataContExit(Sender: TObject); begin if edtDataCont.Text=' / / ' then edtDataCont.Text:=DateToStr(Now); end; procedure TfrmMovEmitirCNT.edtHoraContExit(Sender: TObject); begin if edtHoraCont.Text=' / / ' then edtHoraCont.Text:=Copy(TimeToStr(Now), 1, 5); end; procedure TfrmMovEmitirCNT.edtNumCopiasExit(Sender: TObject); begin if (edtNumCopias.ValueInt=0) or (edtNumCopias.ValueInt>10) then edtNumCopias.StoreValue(1); end; // ============================================================================= // // Gerar Lote de CTe // // ============================================================================= procedure TfrmMovEmitirCNT.GerarLoteCTe; var i, j, k, l, CodigoMunicipio: Integer; IE: String; OkConversao: Boolean; begin with DMDFe.CTe.Conhecimentos.Add.CTe do begin // // Dados de Identificação do CT-e // Ide.cUF := DM_CTA.EmpresaCodigoEstado.AsInteger; Ide.cCT := DM_CNT.ConhecimentoCTChave.AsInteger; // Código Aleatório Ide.CFOP := DM_CNT.ConhecimentoCFOP.AsInteger; Ide.natOp := 'PRESTAÇÃO DE SERVIÇO'; if DM_CNT.ConhecimentoForPag.AsInteger=0 then Ide.forPag := fpPago else Ide.forPag := fpAPagar; Ide.modelo := '57'; Ide.serie := DM_CNT.ConhecimentoSerie.AsInteger; Ide.nCT := DM_CNT.ConhecimentoNumero.AsInteger; Ide.dhEmi := Now; Ide.tpImp := tiRetrato; // TpcnTipoEmissao = (teNormal, teContingencia, teSCAN, teDPEC, teFSDA, teSVCRS, teSVCSP); // (AC,AL,AP,AM,BA,CE,DF,ES,GO,MA,MT,MS,MG,PA,PB,PR,PE,PI,RJ,RN,RS,RO,RR,SC,SP,SE,TO); // (12,27,16,13,29,23,53,32,52,21,51,50,31,15,25,41,26,22,33,24,43,11,14,42,35,28,17); case rgTipoEmissao.ItemIndex of 0: Ide.tpEmis := teNormal; 1: Ide.tpEmis := teFSDA; // Contingencia FSDA 2: if DMDFe.CTe.Configuracoes.WebServices.UFCodigo in [14, 16, 26, 35, 50, 51] then Ide.tpEmis := teSVCRS else Ide.tpEmis := teSVCSP; end; // TpcnTipoAmbiente = (taProducao, taHomologacao); case DM_CTA.ParamDFeWSAmbiente.AsInteger of 0: Ide.tpAmb := taHomologacao; 1: Ide.tpAmb := taProducao; end; // TpcteTipoCTe = (tcNormal, tcComplemento, tcAnulacao, tcSubstituto); case DM_CNT.ConhecimentoTipoCTe.AsInteger of 0: Ide.tpCTe := tcNormal; 1: Ide.tpCTe := tcComplemento; 2: Ide.tpCTe := tcAnulacao; 3: Ide.tpCTe := tcSubstituto; end; // TpcnProcessoEmissao = (peAplicativoContribuinte, peAvulsaFisco, peAvulsaContribuinte, peContribuinteAplicativoFisco); Ide.procEmi := peAplicativoContribuinte; Ide.verProc := '4.0'; Ide.refCTE := trim(DM_CNT.ConhecimentoRefCTe.AsString); // Chave de Acesso do CT-e Referenciado CodigoMunicipio := DM_CTA.EmpresaCodigoEstado.AsInteger * 100000 + DM_CTA.EmpresaCodigoMunicipio.AsInteger; Ide.cMunEnv := CodigoMunicipio; Ide.xMunEnv := DM_CTA.EmpresaCidade.AsString; Ide.UFEnv := DM_CTA.EmpresaEstado.AsString; // TpcteModal = (mdRodoviario, mdAereo, mdAquaviario, mdFerroviario, mdDutoviario); Ide.modal := mdRodoviario; // TpcteTipoServico = (tsNormal, tsSubcontratacao, tsRedespacho, tsIntermediario); case DM_CNT.ConhecimentoTipoServico.AsInteger of 0: Ide.tpServ := tsNormal; 1: Ide.tpServ := tsSubcontratacao; 2: Ide.tpServ := tsRedespacho; 3: Ide.tpServ := tsIntermediario; end; // Origem da Prestação Ide.cMunIni := DM_CNT.ConhecimentoCodCidadeColeta.AsInteger; Ide.xMunIni := DM_CNT.ConhecimentoNomeCidadeColeta.AsString; Ide.UFIni := DM_CNT.ConhecimentoEstadoColeta.AsString; // Destino da Prestação Ide.cMunFim := DM_CNT.ConhecimentoCodCidadeEntrega.AsInteger; Ide.xMunFim := DM_CNT.ConhecimentoNomeCidadeEntrega.AsString; Ide.UFFim := DM_CNT.ConhecimentoEstadoEntrega.AsString; // TpcteRetira = (rtSim, rtNao); Ide.retira := rtSim; Ide.xdetretira := ''; case DM_CNT.ConhecimentoTomadorServico.AsInteger of 0: Ide.Toma03.Toma := tmRemetente; 1: Ide.Toma03.Toma := tmExpedidor; 2: Ide.Toma03.Toma := tmRecebedor; 3: Ide.Toma03.Toma := tmDestinatario; 4: Ide.Toma03.Toma := tmRemetente; end; // Totamdor do Serviço no CTe 4 = Outros if DM_CNT.ConhecimentoTomadorServico.AsInteger = 4 then begin DM_CTA.PessoaFJ.Close; DM_CTA.PessoaFJ.SQL.Clear; DM_CTA.PessoaFJ.SQL.Add('Select * From Sis_PessoaFJ'); DM_CTA.PessoaFJ.SQL.Add('Where CGC = :xCGC'); DM_CTA.PessoaFJ.Params[0].AsString := DM_CNT.ConhecimentoOutros.AsString; DM_CTA.PessoaFJ.Active := True; DM_CTA.PessoaFJ.Open; Ide.Toma4.Toma:=tmOutros; if copy(DM_CTA.PessoaFJCGC.AsString,10,4) <> '0000' then begin Ide.Toma4.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 2, 14); IE := DM_CTA.PessoaFJIEstadual.AsString; end else begin Ide.Toma4.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 1, 9) + Copy(DM_CTA.PessoaFJCGC.AsString, 14, 2); IE := 'ISENTO'; end; Ide.Toma4.IE := IE; Ide.Toma4.xNome := DM_CTA.PessoaFJRSocial.AsString; Ide.Toma4.xFant := DM_CTA.PessoaFJFantasia.AsString; Ide.Toma4.fone := DM_CTA.PessoaFJTelefone.AsString; Ide.Toma4.EnderToma.xLgr := DM_CTA.PessoaFJEndereco.AsString; Ide.Toma4.EnderToma.nro := DM_CTA.PessoaFJNumero.AsString; Ide.Toma4.EnderToma.xCpl := DM_CTA.PessoaFJComplemento.AsString; Ide.Toma4.EnderToma.xBairro := DM_CTA.PessoaFJBairro.AsString; CodigoMunicipio := DM_CTA.PessoaFJCodigoEstado.AsInteger * 100000 + DM_CTA.PessoaFJCodigoMunicipio.AsInteger; Ide.Toma4.EnderToma.cMun := CodigoMunicipio; Ide.Toma4.EnderToma.xMun := DM_CTA.PessoaFJCidade.AsString; Ide.Toma4.EnderToma.CEP := StrToIntDef(DM_CTA.PessoaFJCEP.AsString, 0); Ide.Toma4.EnderToma.UF := DM_CTA.PessoaFJEstado.AsString; Ide.Toma4.EnderToma.cPais := DM_CTA.PessoaFJCodigoPais.AsInteger; Ide.Toma4.EnderToma.xPais := DM_CTA.PessoaFJPais.AsString; end; if rgTipoEmissao.ItemIndex >= 1 then begin Ide.dhCont := StrToDateTime(edtDataCont.Text+' '+edtHoraCont.Text+':00'); Ide.xJust := edtJustificativa.Text; end; // // Informações Complementares do CTe // compl.xCaracAd := Trim(DM_CNT.ConhecimentoCaracAdTrans.AsString); compl.xCaracSer := Trim(DM_CNT.ConhecimentoCaracAdServ.AsString); compl.xEmi := Trim(DM_CNT.ConhecimentoFuncioEmissorCTe.AsString); compl.fluxo.xOrig := Trim(DM_CNT.ConhecimentoFluxoOrigem.AsString); if Trim(DM_CNT.ConhecimentoFluxoPassagem.AsString) <> '' then begin with compl.fluxo.pass.Add do begin xPass := Trim(DM_CNT.ConhecimentoFluxoPassagem.AsString); end; end; compl.fluxo.xDest := Trim(DM_CNT.ConhecimentoFluxoDestino.AsString); compl.fluxo.xRota := Trim(DM_CNT.ConhecimentoFluxoRota.AsString); compl.Entrega.TipoData := StrToTpDataPeriodo(okConversao, IntToStr(DM_CNT.ConhecimentoEntregaTipoData.AsInteger)); case DM_CNT.ConhecimentoEntregaTipoData.AsInteger of 0: compl.Entrega.semData.tpPer := tdSemData; 1,2,3: begin compl.Entrega.comData.tpPer := StrToTpDataPeriodo(okConversao, IntToStr(DM_CNT.ConhecimentoEntregaTipoData.AsInteger)); compl.Entrega.comData.dProg := DM_CNT.ConhecimentoEntregaDataI.AsDateTime; end; 4: begin compl.Entrega.noPeriodo.tpPer := tdNoPeriodo; compl.Entrega.noPeriodo.dIni := DM_CNT.ConhecimentoEntregaDataI.AsDateTime; compl.Entrega.noPeriodo.dFim := DM_CNT.ConhecimentoEntregaDataF.AsDateTime; end; end; compl.Entrega.TipoHora := StrToTpHorarioIntervalo(okConversao, IntToStr(DM_CNT.ConhecimentoEntregaTipoHora.AsInteger)); case DM_CNT.ConhecimentoEntregaTipoHora.AsInteger of 0: compl.Entrega.semHora.tpHor := thSemHorario; 1,2,3: begin compl.Entrega.comHora.tpHor := StrToTpHorarioIntervalo(okConversao, IntToStr(DM_CNT.ConhecimentoEntregaTipoHora.AsInteger)); compl.Entrega.comHora.hProg := StrToTime(DM_CNT.ConhecimentoEntregaHoraI.AsString); end; 4: begin compl.Entrega.noInter.tpHor := thNoIntervalo; compl.Entrega.noInter.hIni := StrToTime(DM_CNT.ConhecimentoEntregaHoraI.AsString); compl.Entrega.noInter.hFim := StrToTime(DM_CNT.ConhecimentoEntregaHoraF.AsString); end; end; compl.origCalc := DM_CNT.ConhecimentoNomeCidadeColeta.AsString; compl.destCalc := DM_CNT.ConhecimentoNomeCidadeEntrega.AsString; compl.xObs := DM_CNT.ConhecimentoMensagem.AsString; DM_CNT.Tabelas.Close; DM_CNT.Tabelas.SQL.Clear; DM_CNT.Tabelas.SQL.Add('Select * From Cnt_Tabelas'); DM_CNT.Tabelas.SQL.Add('Where Codigo = :xTabela'); DM_CNT.Tabelas.Params[0].AsString := DM_CNT.ConhecimentoTabela.AsString; DM_CNT.Tabelas.Active := True; DM_CNT.Tabelas.Open; if DM_CNT.TabelasCST.AsInteger = 41 then compl.xObs := compl.xObs + ';Documento emitido por ME ou EPP optante pelo Simples Nacional' + ';Nao gera direito a credito fiscal de ICMS'; // Obs do Contribuinte if (trim(DM_CNT.ConhecimentoObsContCampo.AsString) <> '') and (trim(DM_CNT.ConhecimentoObsContTexto.AsString) <> '') then begin with compl.ObsCont.Add do begin xCampo := DM_CNT.ConhecimentoObsContCampo.AsString; xTexto := DM_CNT.ConhecimentoObsContTexto.AsString; end; end; // Obs para o Fisco if (trim(DM_CNT.ConhecimentoObsFiscoCampo.AsString) <> '') and (trim(DM_CNT.ConhecimentoObsFiscoTexto.AsString) <> '') then begin with compl.ObsFisco.Add do begin xCampo := DM_CNT.ConhecimentoObsFiscoCampo.AsString; xTexto := DM_CNT.ConhecimentoObsFiscoTexto.AsString; end; end; // // Dados do Emitente // DM_CTA.Empresa.Close; DM_CTA.Empresa.SQL.Clear; DM_CTA.Empresa.SQL.Add('Select * From Sis_Empresa'); DM_CTA.Empresa.SQL.Add('Where Codigo = :xCodigo'); DM_CTA.Empresa.Params[0].AsString := DM_CTA.ParamDFeEmitente.AsString; DM_CTA.Empresa.Active := True; DM_CTA.Empresa.Open; if copy(DM_CTA.EmpresaCNPJ.AsString,10,4) <> '0000' then Emit.CNPJ := Copy(DM_CTA.EmpresaCNPJ.AsString, 2, 14) else Emit.CNPJ := Copy(DM_CTA.EmpresaCNPJ.AsString, 1, 9) + Copy(DM_CTA.EmpresaCNPJ.AsString, 14, 2); if (trim(DM_CTA.EmpresaInscEstadual.AsString) = '') or (trim(DM_CTA.EmpresaInscEstadual.AsString) = 'ISENTO') then Emit.IE := '1234567890' else Emit.IE := trim(DM_CTA.EmpresaInscEstadual.AsString); Emit.xNome := DM_CTA.EmpresaNome.AsString; Emit.xFant := DM_CTA.EmpresaFantasia.AsString; Emit.EnderEmit.xLgr := DM_CTA.EmpresaEndereco.AsString; Emit.EnderEmit.nro := DM_CTA.EmpresaNumero.AsString; Emit.EnderEmit.xCpl := DM_CTA.EmpresaComplemento.AsString; Emit.EnderEmit.xBairro := DM_CTA.EmpresaBairro.AsString; CodigoMunicipio := DM_CTA.EmpresaCodigoEstado.AsInteger * 100000 + DM_CTA.EmpresaCodigoMunicipio.AsInteger; Emit.EnderEmit.cMun := CodigoMunicipio; Emit.EnderEmit.xMun := DM_CTA.EmpresaCidade.AsString; Emit.EnderEmit.CEP := StrToIntDef(DM_CTA.EmpresaCEP.AsString, 0); Emit.EnderEmit.UF := DM_CTA.EmpresaEstado.AsString; Emit.EnderEmit.fone := DM_CTA.EmpresaTelefone.AsString; // // Dados do Remetente // if trim(DM_CNT.ConhecimentoRemetente.AsString) <> '' then begin DM_CTA.PessoaFJ.Close; DM_CTA.PessoaFJ.SQL.Clear; DM_CTA.PessoaFJ.SQL.Add('Select * From Sis_PessoaFJ'); DM_CTA.PessoaFJ.SQL.Add('Where CGC = :xCGC'); DM_CTA.PessoaFJ.Params[0].AsString := DM_CNT.ConhecimentoRemetente.AsString; DM_CTA.PessoaFJ.Active := True; DM_CTA.PessoaFJ.Open; DM_CTA.PessoaFJE.Close; DM_CTA.PessoaFJE.SQL.Clear; DM_CTA.PessoaFJE.SQL.Add('Select * From Sis_PessoaFJEmail'); DM_CTA.PessoaFJE.SQL.Add('Where CNPJ = :xCNPJ'); DM_CTA.PessoaFJE.SQL.Add('and Modulo = :xModulo'); DM_CTA.PessoaFJE.Params[0].AsString := DM_CTA.PessoaFJCGC.AsString; DM_CTA.PessoaFJE.Params[1].AsString := 'DFE'; DM_CTA.PessoaFJE.Active := True; DM_CTA.PessoaFJE.Open; if (DM_CTA.PessoaFJECNPJ.AsString = DM_CTA.PessoaFJCGC.AsString) and (DM_CTA.PessoaFJEModulo.AsString = 'DFE') then Rem.email := Trim(DM_CTA.PessoaFJEEmail.AsString) else Rem.email := ''; Rem.xNome := DM_CTA.PessoaFJRSocial.AsString; Rem.xFant := DM_CTA.PessoaFJFantasia.AsString; Rem.EnderReme.xLgr := DM_CTA.PessoaFJEndereco.AsString; Rem.EnderReme.nro := DM_CTA.PessoaFJNumero.AsString; Rem.EnderReme.xCpl := DM_CTA.PessoaFJComplemento.AsString; Rem.EnderReme.xBairro := DM_CTA.PessoaFJBairro.AsString; CodigoMunicipio := DM_CTA.PessoaFJCodigoEstado.AsInteger * 100000 + DM_CTA.PessoaFJCodigoMunicipio.AsInteger; Rem.EnderReme.cMun := CodigoMunicipio; Rem.EnderReme.xMun := DM_CTA.PessoaFJCidade.AsString; Rem.EnderReme.CEP := StrToIntDef(DM_CTA.PessoaFJCEP.AsString, 0); Rem.EnderReme.UF := DM_CTA.PessoaFJEstado.AsString; Rem.EnderReme.cPais := DM_CTA.PessoaFJCodigoPais.AsInteger; Rem.EnderReme.xPais := DM_CTA.PessoaFJPais.AsString; {$IFDEF PL_200} // Local da Coleta if trim(DM_CNT.NotasCNPJRet.AsString) <> '' then begin DM_CTA.PessoaFJ.Close; DM_CTA.PessoaFJ.SQL.Clear; DM_CTA.PessoaFJ.SQL.Add('Select * From Sis_PessoaFJ'); DM_CTA.PessoaFJ.SQL.Add('Where CGC = :xCGC'); DM_CTA.PessoaFJ.Params[0].AsString := DM_CNT.NotasCNPJRet.AsString; DM_CTA.PessoaFJ.Active := True; DM_CTA.PessoaFJ.Open; if copy(DM_CTA.PessoaFJCGC.AsString,10,4) <> '0000' then Rem.locColeta.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 2, 14) else Rem.locColeta.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 1, 9) + Copy(DM_CTA.PessoaFJCGC.AsString, 14, 2); Rem.locColeta.xNome := DM_CTA.PessoaFJRSocial.AsString; Rem.locColeta.xLgr := DM_CTA.PessoaFJEndereco.AsString; Rem.locColeta.nro := DM_CTA.PessoaFJNumero.AsString; Rem.locColeta.xCpl := DM_CTA.PessoaFJComplemento.AsString; Rem.locColeta.xBairro := DM_CTA.PessoaFJBairro.AsString; CodigoMunicipio := DM_CTA.PessoaFJCodigoEstado.AsInteger * 100000 + DM_CTA.PessoaFJCodigoMunicipio.AsInteger; Rem.locColeta.cMun := CodigoMunicipio; Rem.locColeta.xMun := DM_CTA.PessoaFJCidade.AsString; Rem.locColeta.UF := DM_CTA.PessoaFJEstado.AsString; end; {$ENDIF} if copy(DM_CTA.PessoaFJCGC.AsString,10,4) <> '0000' then begin Rem.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 2, 14); IE := DM_CTA.PessoaFJIEstadual.AsString; end else begin Rem.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 1, 9) + Copy(DM_CTA.PessoaFJCGC.AsString, 14, 2); IE := 'ISENTO'; end; Rem.IE := IE; Rem.fone := DM_CTA.PessoaFJTelefone.AsString; // // Relação das Notas Fiscais // DM_CNT.Notas.Close; DM_CNT.Notas.SQL.Clear; DM_CNT.Notas.SQL.Add('Select * From Cnt_Notas'); DM_CNT.Notas.SQL.Add('Where Codigo = :xCodigo'); DM_CNT.Notas.SQL.Add('and Serie = :xSerie'); DM_CNT.Notas.SQL.Add('and Numero = :xNumero'); DM_CNT.Notas.SQL.Add('Order By Tipo'); DM_CNT.Notas.Params[0].AsInteger := DM_CNT.ConhecimentoCodigo.AsInteger; DM_CNT.Notas.Params[1].AsInteger := DM_CNT.ConhecimentoSerie.AsInteger; DM_CNT.Notas.Params[2].AsInteger := DM_CNT.ConhecimentoNumero.AsInteger; DM_CNT.Notas.Active := True; DM_CNT.Notas.Open; DM_CNT.Notas.Last; j := DM_CNT.Notas.RecordCount; if j > 0 then begin DM_CNT.Notas.First; for i := 1 to j do begin case DM_CNT.NotasTipo.AsInteger of 0: begin // Nota Fiscal {$IFDEF PL_200} with infCTeNorm.infDoc.infNF.Add do {$ELSE} with Rem.InfNF.Add do {$ENDIF} begin nRoma := DM_CNT.NotasRomaneioNF.AsString; nPed := DM_CNT.NotasPedidoNF.AsString; Modelo := moNF011AAvulsa; serie := DM_CNT.NotasSerieNF.AsString; nDoc := DM_CNT.NotasNumeroNF.AsString; dEmi := DM_CNT.NotasEmissaoNF.AsDateTime; vBC := RoundTo(DM_CNT.NotasValorBCICMS.AsFloat, -2); vICMS := RoundTo(DM_CNT.NotasValorICMS.AsFloat, -2); vBCST := RoundTo(DM_CNT.NotasValorBCICMSST.AsFloat, -2); vST := RoundTo(DM_CNT.NotasValorICMSST.AsFloat, -2); vProd := RoundTo(DM_CNT.NotasValorProdutos.AsFloat, -2); vNF := RoundTo(DM_CNT.NotasValorNF.AsFloat, -2); nCFOP := DM_CNT.NotasCFOPNF.AsInteger; nPeso := RoundTo(DM_CNT.NotasPesoKg.AsFloat, -2); PIN := DM_CNT.NotasPinSuframa.AsString; {$IFNDEF PL_200} // Local de Retirada if trim(DM_CNT.NotasCNPJRet.AsString) <> '' then begin DM_CTA.PessoaFJ.Close; DM_CTA.PessoaFJ.SQL.Clear; DM_CTA.PessoaFJ.SQL.Add('Select * From Sis_PessoaFJ'); DM_CTA.PessoaFJ.SQL.Add('Where CGC = :xCGC'); DM_CTA.PessoaFJ.Params[0].AsString := DM_CNT.NotasCNPJRet.AsString; DM_CTA.PessoaFJ.Active := True; DM_CTA.PessoaFJ.Open; if copy(DM_CTA.PessoaFJCGC.AsString,10,4) <> '0000' then locRet.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 2, 14) else locRet.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 1, 9) + Copy(DM_CTA.PessoaFJCGC.AsString, 14, 2); locRet.xNome := DM_CTA.PessoaFJRSocial.AsString; locRet.xLgr := DM_CTA.PessoaFJEndereco.AsString; locRet.nro := DM_CTA.PessoaFJNumero.AsString; locRet.xCpl := DM_CTA.PessoaFJComplemento.AsString; locRet.xBairro := DM_CTA.PessoaFJBairro.AsString; CodigoMunicipio := DM_CTA.PessoaFJCodigoEstado.AsInteger * 100000 + DM_CTA.PessoaFJCodigoMunicipio.AsInteger; locRet.cMun := CodigoMunicipio; locRet.xMun := DM_CTA.PessoaFJCidade.AsString; locRet.UF := DM_CTA.PessoaFJEstado.AsString; end; {$ENDIF} end; end; 1: begin // Nota Fiscal Eletrônica {$IFDEF PL_200} with infCTeNorm.infDoc.infNFe.Add do {$ELSE} with Rem.InfNFe.Add do {$ENDIF} begin chave := DM_CNT.NotasChaveNFe.AsString; PIN := DM_CNT.NotasPinSuframa.AsString; end; end; 2: begin // Outros Tipos de Documentos {$IFDEF PL_200} with infCTeNorm.infDoc.infOutros.Add do {$ELSE} with Rem.InfOutros.Add do {$ENDIF} begin if DM_CNT.NotasTipoOutros.AsInteger = 0 then tpDoc := tdDeclaracao else begin tpDoc := tdOutros; descOutros := DM_CNT.NotasDescricao.AsString; end; nDoc := DM_CNT.NotasNumeroNF.AsString; dEmi := DM_CNT.NotasEmissaoNF.AsDateTime; vDocFisc := RoundTo(DM_CNT.NotasValorNF.AsFloat, -2); end; end; end; DM_CNT.Notas.Next; end; end; end; // // Dados do Destinatario // if trim(DM_CNT.ConhecimentoDestinatario.AsString) <> '' then begin DM_CTA.PessoaFJ.Close; DM_CTA.PessoaFJ.SQL.Clear; DM_CTA.PessoaFJ.SQL.Add('Select * From Sis_PessoaFJ'); DM_CTA.PessoaFJ.SQL.Add('Where CGC = :xCGC'); DM_CTA.PessoaFJ.Params[0].AsString := DM_CNT.ConhecimentoDestinatario.AsString; DM_CTA.PessoaFJ.Active := True; DM_CTA.PessoaFJ.Open; DM_CTA.PessoaFJE.Close; DM_CTA.PessoaFJE.SQL.Clear; DM_CTA.PessoaFJE.SQL.Add('Select * From Sis_PessoaFJEmail'); DM_CTA.PessoaFJE.SQL.Add('Where CNPJ = :xCNPJ'); DM_CTA.PessoaFJE.SQL.Add('and Modulo = :xModulo'); DM_CTA.PessoaFJE.Params[0].AsString := DM_CTA.PessoaFJCGC.AsString; DM_CTA.PessoaFJE.Params[1].AsString := 'DFE'; DM_CTA.PessoaFJE.Active := True; DM_CTA.PessoaFJE.Open; if (DM_CTA.PessoaFJECNPJ.AsString = DM_CTA.PessoaFJCGC.AsString) and (DM_CTA.PessoaFJEModulo.AsString = 'DFE') then Dest.email := Trim(DM_CTA.PessoaFJEEmail.AsString) else Dest.email := ''; Dest.xNome := DM_CTA.PessoaFJRSocial.AsString; Dest.EnderDest.xLgr := DM_CTA.PessoaFJEndereco.AsString; Dest.EnderDest.nro := DM_CTA.PessoaFJNumero.AsString; Dest.EnderDest.xCpl := DM_CTA.PessoaFJComplemento.AsString; Dest.EnderDest.xBairro := DM_CTA.PessoaFJBairro.AsString; CodigoMunicipio := DM_CTA.PessoaFJCodigoEstado.AsInteger * 100000 + DM_CTA.PessoaFJCodigoMunicipio.AsInteger; Dest.EnderDest.cMun := CodigoMunicipio; Dest.EnderDest.xMun := DM_CTA.PessoaFJCidade.AsString; Dest.EnderDest.CEP := StrToIntDef(DM_CTA.PessoaFJCEP.AsString, 0); Dest.EnderDest.UF := DM_CTA.PessoaFJEstado.AsString; Dest.EnderDest.cPais := DM_CTA.PessoaFJCodigoPais.AsInteger; Dest.EnderDest.xPais := DM_CTA.PessoaFJPais.AsString; if copy(DM_CTA.PessoaFJCGC.AsString,10,4) <> '0000' then begin Dest.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 2, 14); IE := DM_CTA.PessoaFJIEstadual.AsString; end else begin Dest.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 1, 9) + Copy(DM_CTA.PessoaFJCGC.AsString, 14, 2); IE := 'ISENTO'; end; Dest.IE := IE; Dest.fone := DM_CTA.PessoaFJTelefone.AsString; Dest.ISUF := Trim(DM_CTA.PessoaFJInscSUF.AsString); // Local de Entrega if trim(DM_CNT.ConhecimentoCNPJEnt.AsString) <> '' then begin DM_CTA.PessoaFJ.Close; DM_CTA.PessoaFJ.SQL.Clear; DM_CTA.PessoaFJ.SQL.Add('Select * From Sis_PessoaFJ'); DM_CTA.PessoaFJ.SQL.Add('Where CGC = :xCGC'); DM_CTA.PessoaFJ.Params[0].AsString := DM_CNT.ConhecimentoCNPJEnt.AsString; DM_CTA.PessoaFJ.Active := True; DM_CTA.PessoaFJ.Open; if copy(DM_CTA.PessoaFJCGC.AsString,10,4) <> '0000' then Dest.locEnt.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 2, 14) else Dest.locEnt.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 1, 9) + Copy(DM_CTA.PessoaFJCGC.AsString, 14, 2); Dest.locEnt.xNome := DM_CTA.PessoaFJRSocial.AsString; Dest.locEnt.xLgr := DM_CTA.PessoaFJEndereco.AsString; Dest.locEnt.nro := DM_CTA.PessoaFJNumero.AsString; Dest.locEnt.xCpl := DM_CTA.PessoaFJComplemento.AsString; Dest.locEnt.xBairro := DM_CTA.PessoaFJBairro.AsString; CodigoMunicipio := DM_CTA.PessoaFJCodigoEstado.AsInteger * 100000 + DM_CTA.PessoaFJCodigoMunicipio.AsInteger; Dest.locEnt.cMun := CodigoMunicipio; Dest.locEnt.xMun := DM_CTA.PessoaFJCidade.AsString; Dest.locEnt.UF := DM_CTA.PessoaFJEstado.AsString; end; end; // // Dados do Expedidor // if trim(DM_CNT.ConhecimentoExpedidor.AsString) <> '' then begin DM_CTA.PessoaFJ.Close; DM_CTA.PessoaFJ.SQL.Clear; DM_CTA.PessoaFJ.SQL.Add('Select * From Sis_PessoaFJ'); DM_CTA.PessoaFJ.SQL.Add('Where CGC = :xCGC'); DM_CTA.PessoaFJ.Params[0].AsString := DM_CNT.ConhecimentoExpedidor.AsString; DM_CTA.PessoaFJ.Active := True; DM_CTA.PessoaFJ.Open; DM_CTA.PessoaFJE.Close; DM_CTA.PessoaFJE.SQL.Clear; DM_CTA.PessoaFJE.SQL.Add('Select * From Sis_PessoaFJEmail'); DM_CTA.PessoaFJE.SQL.Add('Where CNPJ = :xCNPJ'); DM_CTA.PessoaFJE.SQL.Add('and Modulo = :xModulo'); DM_CTA.PessoaFJE.Params[0].AsString := DM_CTA.PessoaFJCGC.AsString; DM_CTA.PessoaFJE.Params[1].AsString := 'DFE'; DM_CTA.PessoaFJE.Active := True; DM_CTA.PessoaFJE.Open; if (DM_CTA.PessoaFJECNPJ.AsString = DM_CTA.PessoaFJCGC.AsString) and (DM_CTA.PessoaFJEModulo.AsString = 'DFE') then Exped.email := Trim(DM_CTA.PessoaFJEEmail.AsString) else Exped.email := ''; Exped.xNome := DM_CTA.PessoaFJRSocial.AsString; Exped.EnderExped.xLgr := DM_CTA.PessoaFJEndereco.AsString; Exped.EnderExped.nro := DM_CTA.PessoaFJNumero.AsString; Exped.EnderExped.xCpl := DM_CTA.PessoaFJComplemento.AsString; Exped.EnderExped.xBairro := DM_CTA.PessoaFJBairro.AsString; CodigoMunicipio := DM_CTA.PessoaFJCodigoEstado.AsInteger * 100000 + DM_CTA.PessoaFJCodigoMunicipio.AsInteger; Exped.EnderExped.cMun := CodigoMunicipio; Exped.EnderExped.xMun := DM_CTA.PessoaFJCidade.AsString; Exped.EnderExped.CEP := StrToIntDef(DM_CTA.PessoaFJCEP.AsString, 0); Exped.EnderExped.UF := DM_CTA.PessoaFJEstado.AsString; Exped.EnderExped.cPais := DM_CTA.PessoaFJCodigoPais.AsInteger; Exped.EnderExped.xPais := DM_CTA.PessoaFJPais.AsString; if copy(DM_CTA.PessoaFJCGC.AsString,10,4) <> '0000' then begin Exped.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 2, 14); IE := DM_CTA.PessoaFJIEstadual.AsString; end else begin Exped.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 1, 9) + Copy(DM_CTA.PessoaFJCGC.AsString, 14, 2); IE := 'ISENTO'; end; Exped.IE := IE; Exped.fone := DM_CTA.PessoaFJTelefone.AsString; end; // // Dados do Recebedor // if trim(DM_CNT.ConhecimentoExpedidor.AsString) <> '' then begin DM_CTA.PessoaFJ.Close; DM_CTA.PessoaFJ.SQL.Clear; DM_CTA.PessoaFJ.SQL.Add('Select * From Sis_PessoaFJ'); DM_CTA.PessoaFJ.SQL.Add('Where CGC = :xCGC'); DM_CTA.PessoaFJ.Params[0].AsString := DM_CNT.ConhecimentoExpedidor.AsString; DM_CTA.PessoaFJ.Active := True; DM_CTA.PessoaFJ.Open; DM_CTA.PessoaFJE.Close; DM_CTA.PessoaFJE.SQL.Clear; DM_CTA.PessoaFJE.SQL.Add('Select * From Sis_PessoaFJEmail'); DM_CTA.PessoaFJE.SQL.Add('Where CNPJ = :xCNPJ'); DM_CTA.PessoaFJE.SQL.Add('and Modulo = :xModulo'); DM_CTA.PessoaFJE.Params[0].AsString := DM_CTA.PessoaFJCGC.AsString; DM_CTA.PessoaFJE.Params[1].AsString := 'DFE'; DM_CTA.PessoaFJE.Active := True; DM_CTA.PessoaFJE.Open; if (DM_CTA.PessoaFJECNPJ.AsString = DM_CTA.PessoaFJCGC.AsString) and (DM_CTA.PessoaFJEModulo.AsString = 'DFE') then Receb.email := Trim(DM_CTA.PessoaFJEEmail.AsString) else Receb.email := ''; Receb.xNome := DM_CTA.PessoaFJRSocial.AsString; Receb.EnderReceb.xLgr := DM_CTA.PessoaFJEndereco.AsString; Receb.EnderReceb.nro := DM_CTA.PessoaFJNumero.AsString; Receb.EnderReceb.xCpl := DM_CTA.PessoaFJComplemento.AsString; Receb.EnderReceb.xBairro := DM_CTA.PessoaFJBairro.AsString; CodigoMunicipio := DM_CTA.PessoaFJCodigoEstado.AsInteger * 100000 + DM_CTA.PessoaFJCodigoMunicipio.AsInteger; Receb.EnderReceb.cMun := CodigoMunicipio; Receb.EnderReceb.xMun := DM_CTA.PessoaFJCidade.AsString; Receb.EnderReceb.CEP := StrToIntDef(DM_CTA.PessoaFJCEP.AsString, 0); Receb.EnderReceb.UF := DM_CTA.PessoaFJEstado.AsString; Receb.EnderReceb.cPais := DM_CTA.PessoaFJCodigoPais.AsInteger; Receb.EnderReceb.xPais := DM_CTA.PessoaFJPais.AsString; if copy(DM_CTA.PessoaFJCGC.AsString,10,4) <> '0000' then begin Receb.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 2, 14); IE := DM_CTA.PessoaFJIEstadual.AsString; end else begin Receb.CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 1, 9) + Copy(DM_CTA.PessoaFJCGC.AsString, 14, 2); IE := 'ISENTO'; end; Receb.IE := IE; Receb.fone := DM_CTA.PessoaFJTelefone.AsString; end; // // Valores da Prestação de Serviço // vPrest.vTPrest := RoundTo(DM_CNT.ConhecimentoValorTotal.AsFloat, -2); vPrest.vRec := RoundTo(DM_CNT.ConhecimentoValorTotal.AsFloat, -2); // // Relação dos Componentes da Prestação de Serviço // DM_CNT.Componentes.Close; DM_CNT.Componentes.SQL.Clear; DM_CNT.Componentes.SQL.Add('Select * From Cnt_Componentes'); DM_CNT.Componentes.SQL.Add('Where Codigo = :xCodigo'); DM_CNT.Componentes.SQL.Add('and Serie = :xSerie'); DM_CNT.Componentes.SQL.Add('and Numero = :xNumero'); DM_CNT.Componentes.SQL.Add('and Complemento <> :xComplemento'); DM_CNT.Componentes.SQL.Add('Order By Item'); DM_CNT.Componentes.Params[0].AsInteger := DM_CNT.ConhecimentoCodigo.AsInteger; DM_CNT.Componentes.Params[1].AsInteger := DM_CNT.ConhecimentoSerie.AsInteger; DM_CNT.Componentes.Params[2].AsInteger := DM_CNT.ConhecimentoNumero.AsInteger; DM_CNT.Componentes.Params[3].AsString := 'S'; DM_CNT.Componentes.Active := True; DM_CNT.Componentes.Open; DM_CNT.Componentes.Last; j := DM_CNT.Componentes.RecordCount; if j > 0 then begin DM_CNT.Componentes.First; for i := 1 to j do begin if DM_CNT.ComponentesValor.AsFloat > 0.0 then begin with vPrest.comp.Add do begin xNome := DM_CNT.ComponentesDescricao.AsString; vComp := RoundTo(DM_CNT.ComponentesValor.AsFloat, -2); end; end; DM_CNT.Componentes.Next; end; end; // // Valores dos Impostos // // TpcnCSTIcms = (cst00, cst10, cst20, cst30, cst40, cst41, cst45, cst50, cst51, // cst60, cst70, cst80, cst81, cst90, cstPart10, cstPart90, // cstRep41, cstVazio, cstICMSOutraUF, cstICMSSN); //80 e 81 apenas para CTe case DM_CNT.ConhecimentoCSTICMS.AsInteger of 00: begin Imp.ICMS.SituTrib := cst00; Imp.ICMS.ICMS00.CST := cst00; // Tributação Normal ICMS Imp.ICMS.ICMS00.vBC := RoundTo(DM_CNT.ConhecimentoBaseCalc.AsFloat, -2); Imp.ICMS.ICMS00.pICMS := RoundTo(DM_CNT.ConhecimentoAliqICMS.AsFloat, -2); Imp.ICMS.ICMS00.vICMS := RoundTo(DM_CNT.ConhecimentoValorICMS.AsFloat, -2); end; 20: begin Imp.ICMS.SituTrib := cst20; Imp.ICMS.ICMS20.CST := cst20; // Tributação com BC reduzida do ICMS Imp.ICMS.ICMS20.pRedBC := RoundTo(DM_CNT.ConhecimentoReducaoICMS.AsFloat, -2); Imp.ICMS.ICMS20.vBC := RoundTo(DM_CNT.ConhecimentoBaseCalc.AsFloat, -2); Imp.ICMS.ICMS20.pICMS := RoundTo(DM_CNT.ConhecimentoAliqICMS.AsFloat, -2); Imp.ICMS.ICMS20.vICMS := RoundTo(DM_CNT.ConhecimentoValorICMS.AsFloat, -2); end; 40: begin Imp.ICMS.SituTrib := cst40; Imp.ICMS.ICMS45.CST := cst40; // ICMS Isento end; 41: begin Imp.ICMS.SituTrib := cst41; Imp.ICMS.ICMS45.CST := cst41; // ICMS não Tributada end; 51: begin Imp.ICMS.SituTrib := cst51; Imp.ICMS.ICMS45.CST := cst51; // ICMS diferido end; 60: begin Imp.ICMS.SituTrib := cst60; Imp.ICMS.ICMS60.CST := cst60; // Tributação atribuida ao tomador ou 3. por ST Imp.ICMS.ICMS60.vBCSTRet := RoundTo(DM_CNT.ConhecimentoBaseCalc.AsFloat, -2); Imp.ICMS.ICMS60.pICMSSTRet := RoundTo(DM_CNT.ConhecimentoAliqICMS.AsFloat, -2); Imp.ICMS.ICMS60.vICMSSTRet := RoundTo(DM_CNT.ConhecimentoValorICMS.AsFloat, -2); Imp.ICMS.ICMS60.vCred := RoundTo(DM_CNT.ConhecimentoCreditoICMS.AsFloat, -2); // Recalcula o valor a receber quando existe ICMS/ST retido vPrest.vRec := RoundTo(vPrest.vTPrest - Imp.ICMS.ICMS60.vCred, -2); // ou // vPrest.vRec := RoundTo(vPrest.vTPrest - Imp.ICMS.ICMS60.vICMSSTRet, -2); end; 90: begin Imp.ICMS.SituTrib := cst90; Imp.ICMS.ICMS90.CST := cst90; // ICMS Outros Imp.ICMS.ICMS90.pRedBC := RoundTo(DM_CNT.ConhecimentoReducaoICMS.AsFloat, -2); Imp.ICMS.ICMS90.vBC := RoundTo(DM_CNT.ConhecimentoBaseCalc.AsFloat, -2); Imp.ICMS.ICMS90.pICMS := RoundTo(DM_CNT.ConhecimentoAliqICMS.AsFloat, -2); Imp.ICMS.ICMS90.vICMS := RoundTo(DM_CNT.ConhecimentoValorICMS.AsFloat, -2); Imp.ICMS.ICMS90.vCred := RoundTo(DM_CNT.ConhecimentoCreditoICMS.AsFloat, -2); end; end; // Valor Total dos Tributos imp.vTotTrib := RoundTo(DM_CNT.ConhecimentovTotTrib.AsFloat, -2); // Obs do Contribuinte if (imp.vTotTrib <> 0.0) then begin with compl.ObsCont.Add do begin xCampo := 'Lei da Transparencia'; xTexto := 'O valor aproximado de tributos incidentes sobre o preco deste servico e de R$ ' + FormatFloat('##0.00', imp.vTotTrib) + ' (' + FormatFloat('#0.00', DM_CNT.ConhecimentoAliqNBS.AsFloat) + '%) ' + 'Fonte: IBPT'; end; end; case DM_CNT.ConhecimentoTipoCTe.AsInteger of 0,3: begin // // Informações do Detalhamento do CTe Normal // // // Informações da Carga // {$IFDEF PL_200} infCTeNorm.infCarga.vCarga := RoundTo(DM_CNT.ConhecimentoValorTotalNF.AsFloat, -2); infCTeNorm.infCarga.proPred := DM_CNT.ConhecimentoEspecie.AsString; infCTeNorm.infCarga.xOutCat := DM_CNT.ConhecimentoNatureza.AsString; // UnidMed = (uM3,uKG, uTON, uUNIDADE, uLITROS); with infCTeNorm.infCarga.InfQ.Add do begin cUnid := uKg; tpMed := 'Kg'; qCarga := RoundTo(DM_CNT.ConhecimentoPesoTotal.AsFloat, -2); end; with infCTeNorm.infCarga.InfQ.Add do begin cUnid := uUnidade; tpMed := DM_CNT.ConhecimentoNatureza.AsString; qCarga := RoundTo(DM_CNT.ConhecimentoVolume.AsFloat, -2); end; {$ELSE} infCarga.vCarga := RoundTo(DM_CNT.ConhecimentoValorTotalNF.AsFloat, -2); infCarga.proPred := DM_CNT.ConhecimentoEspecie.AsString; infCarga.xOutCat := DM_CNT.ConhecimentoNatureza.AsString; // UnidMed = (uM3,uKG, uTON, uUNIDADE, uLITROS); with infCarga.InfQ.Add do begin cUnid := uKg; tpMed := 'Kg'; qCarga := RoundTo(DM_CNT.ConhecimentoPesoTotal.AsFloat, -2); end; with infCarga.InfQ.Add do begin cUnid := uUnidade; tpMed := DM_CNT.ConhecimentoNatureza.AsString; qCarga := RoundTo(DM_CNT.ConhecimentoVolume.AsFloat, -2); end; {$ENDIF} // // Informações dos Containers // // // Informações de Documentos de Transporte Anterior // // TpcteTipoServico = (tsNormal, tsSubcontratacao, tsRedespacho, tsIntermediario); if ide.tpServ <> tsNormal then begin {$IFDEF PL_200} with infCTeNorm.docAnt.emiDocAnt.Add do {$ELSE} with infCTeNorm.emiDocAnt.Add do {$ENDIF} begin // Dados do Emissor do Documento Anterior CNPJCPF := Exped.CNPJCPF; IE := Exped.IE; UF := Exped.EnderExped.UF; xNome := Exped.xNome; with idDocAnt.Add do begin // // Relação de Documentos Anteriores // DM_CNT.Notas.Close; DM_CNT.Notas.SQL.Clear; DM_CNT.Notas.SQL.Add('Select * From Cnt_Notas'); DM_CNT.Notas.SQL.Add('Where Codigo = :xCodigo'); DM_CNT.Notas.SQL.Add('and Serie = :xSerie'); DM_CNT.Notas.SQL.Add('and Numero = :xNumero'); DM_CNT.Notas.SQL.Add('Order By Tipo'); DM_CNT.Notas.Params[0].AsInteger := DM_CNT.ConhecimentoCodigo.AsInteger; DM_CNT.Notas.Params[1].AsInteger := DM_CNT.ConhecimentoSerie.AsInteger; DM_CNT.Notas.Params[2].AsInteger := DM_CNT.ConhecimentoNumero.AsInteger; DM_CNT.Notas.Active := True; DM_CNT.Notas.Open; DM_CNT.Notas.Last; j := DM_CNT.Notas.RecordCount; if j > 0 then begin DM_CNT.Notas.First; for i := 1 to j do begin case DM_CNT.NotasTipo.AsInteger of 3: begin // Documentos Anteriores de Papel with idDocAntPap.Add do begin // TpcteTipoDocumentoAnterior = (daCTRC, daCTAC, daACT, daNF7, daNF27, daCAN, daCTMC, daATRE, daDTA, daCAI, daCCPI, daCA, daTIF, daOutros); case DM_CNT.NotasModelo.AsInteger of 00: tpDoc := daCTRC; 01: tpDoc := daCTAC; 02: tpDoc := daACT; 03: tpDoc := daNF7; 04: tpDoc := daNF27; 05: tpDoc := daCAN; 06: tpDoc := daCTMC; 07: tpDoc := daATRE; 08: tpDoc := daDTA; 09: tpDoc := daCAI; 10: tpDoc := daCCPI; 11: tpDoc := daCA; 12: tpDoc := daTIF; 99: tpDoc := daOutros; end; serie := DM_CNT.NotasSerieNF.AsString; subser := DM_CNT.NotasSubSerie.AsString; nDoc := DM_CNT.NotasNumeroNF.AsInteger; dEmi := DM_CNT.NotasEmissaoNF.AsDateTime; end; end; 4: begin // Documentos Anteriores Eletrônico with idDocAntEle.Add do begin chave := DM_CNT.NotasChaveNFe.AsString; end; end; end; DM_CNT.Notas.Next; end; end; end; end; end; // // Informações da Seguradora // {$IFDEF PL_200} with infCTeNorm.seg.Add do {$ELSE} with infseg.Add do {$ENDIF} begin case DM_CNT.ConhecimentoRespSeguro.AsInteger of 0: respSeg := rsRemetente; 1: respSeg := rsExpedidor; 2: respSeg := rsRecebedor; 3: respSeg := rsDestinatario; 4: respSeg := rsEmitenteCTe; 5: respSeg := rsTomadorServico; end; if (trim(DM_CTA.ParamDFeSeguradora.AsString) <> '') and (DM_CTA.ParamDFeSeguradora.AsString <> '000000000000000') then begin DM_CTA.PessoaFJ.Close; DM_CTA.PessoaFJ.SQL.Clear; DM_CTA.PessoaFJ.SQL.Add('Select * From Sis_PessoaFJ'); DM_CTA.PessoaFJ.SQL.Add('Where CGC = :xCGC'); DM_CTA.PessoaFJ.Params[0].AsString := DM_CTA.ParamDFeSeguradora.AsString; DM_CTA.PessoaFJ.Active := True; DM_CTA.PessoaFJ.Open; xSeg := Copy(trim(DM_CTA.PessoaFJRSocial.AsString), 1, 30); end; nApol := Copy(trim(DM_CTA.ParamDFeNumApolice.AsString), 1, 20); nAver := DM_CTA.ParamDFeNumAverbacao.AsString; end; // // Dados do Modal Rodoviário // {$IFDEF PL_200} infCTeNorm.rodo.RNTRC := DM_CTA.ParamDFeRNTRC.AsString; infCTeNorm.Rodo.dPrev := (DM_CNT.ConhecimentoData.AsDateTime+1); // TpcteLotacao = (ltNao, ltSim); case DM_CNT.ConhecimentoLotacao.AsInteger of 0: infCTeNorm.Rodo.Lota := ltNao; 1: infCTeNorm.Rodo.Lota := ltSim; end; infCTeNorm.Rodo.CIOT := Trim(DM_CTA.ParamDFeCIOT.AsString); {$ELSE} Rodo.RNTRC := DM_CTA.ParamDFeRNTRC.AsString; Rodo.dPrev := (DM_CNT.ConhecimentoData.AsDateTime+1); // TpcteLotacao = (ltNao, ltSim); case DM_CNT.ConhecimentoLotacao.AsInteger of 0: Rodo.Lota := ltNao; 1: Rodo.Lota := ltSim; end; Rodo.CIOT := Trim(DM_CTA.ParamDFeCIOT.AsString); {$ENDIF} // // Ordem de Coleta Associados (OCC) // // // Lacres // if DM_CNT.ConhecimentoLotacao.AsInteger = 1 // Lotação then begin // // Informações do Vale Pedágio // // // Dados dos Veículos // DM_CNT.LotaVeic.Close; DM_CNT.LotaVeic.SQL.Clear; DM_CNT.LotaVeic.SQL.Add('Select * From Cnt_LotaVeic'); DM_CNT.LotaVeic.SQL.Add('Where Codigo = :xCodigo'); DM_CNT.LotaVeic.SQL.Add('and Serie = :xSerie'); DM_CNT.LotaVeic.SQL.Add('and Numero = :xNumero'); DM_CNT.LotaVeic.SQL.Add('Order By Item'); DM_CNT.LotaVeic.Params[0].AsInteger := DM_CNT.ConhecimentoCodigo.AsInteger; DM_CNT.LotaVeic.Params[1].AsInteger := DM_CNT.ConhecimentoSerie.AsInteger; DM_CNT.LotaVeic.Params[2].AsInteger := DM_CNT.ConhecimentoNumero.AsInteger; DM_CNT.LotaVeic.Active := True; DM_CNT.LotaVeic.Open; DM_CNT.LotaVeic.Last; j := DM_CNT.LotaVeic.RecordCount; if j > 0 then begin DM_CNT.LotaVeic.First; for i := 1 to j do begin {$IFDEF PL_200} with infCTeNorm.rodo.veic.Add do {$ELSE} with rodo.veic.Add do {$ENDIF} begin DM_CNT.Frota.Close; DM_CNT.Frota.SQL.Clear; DM_CNT.Frota.SQL.Add('Select * From Cnt_Frota'); DM_CNT.Frota.SQL.Add('Where Codigo = :xCodigo'); DM_CNT.Frota.Params[0].AsInteger:=DM_CNT.LotaVeicVeiculo.AsInteger; DM_CNT.Frota.Active:=True; DM_CNT.Frota.Open; cInt := IntToStr(DM_CNT.LotaVeicVeiculo.AsInteger); RENAVAM := DM_CNT.FrotaRENAVAM.AsString; placa := DM_CNT.FrotaPlaca.AsString; tara := DM_CNT.FrotaTara.AsInteger; capKg := DM_CNT.FrotaCapKg.AsInteger; capM3 := DM_CNT.FrotaCapM3.AsInteger; // TpcteTipoVeiculo = (tvTracao, tvReboque); case DM_CNT.FrotaTipo.AsInteger of 0: tpVeic := tvTracao; 1: tpVeic := tvReboque; end; // TpcteTipoRodado = (trNaoAplicavel, trTruck, trToco, trCavaloMecanico, trVAN, trUtilitario, trOutros); case DM_CNT.FrotaTipoRodado.AsInteger of 0: tpRod := trNaoAplicavel; 1: tpRod := trTruck; 2: tpRod := trToco; 3: tpRod := trCavaloMecanico; 4: tpRod := trVAN; 5: tpRod := trUtilitario; 6: tpRod := trOutros; end; // TpcteTipoCarroceria = (tcNaoAplicavel, tcAberta, tcFechada, tcGraneleira, tcPortaContainer, tcSider); case DM_CNT.FrotaTipoCarroceria.AsInteger of 0: tpCar := tcNaoAplicavel; 1: tpCar := tcAberta; 2: tpCar := tcFechada; 3: tpCar := tcGraneleira; 4: tpCar := tcPortaContainer; 5: tpCar := tcSider; end; UF := DM_CNT.FrotaUF.AsString; // TpcteTipoPropriedade = (tpProprio, tpTerceiro); if DM_CNT.FrotaProprietario.AsString = 'S' then tpProp := tpProprio else begin tpProp := tpTerceiro; DM_CTA.PessoaFJ.Close; DM_CTA.PessoaFJ.SQL.Clear; DM_CTA.PessoaFJ.SQL.Add('Select * From Sis_PessoaFJ'); DM_CTA.PessoaFJ.SQL.Add('Where CGC = :xCGC'); DM_CTA.PessoaFJ.Params[0].AsString:=DM_CNT.FrotaCNPJProp.AsString; DM_CTA.PessoaFJ.Active:=True; DM_CTA.PessoaFJ.Open; if copy(DM_CNT.FrotaCNPJProp.AsString,10,4) <> '0000' then begin prop.CNPJCPF := Copy(DM_CNT.FrotaCNPJProp.AsString, 2, 14); prop.IE := DM_CTA.PessoaFJIEstadual.AsString; end else begin prop.CNPJCPF := Copy(DM_CNT.FrotaCNPJProp.AsString, 1, 9) + Copy(DM_CNT.FrotaCNPJProp.AsString, 14, 2); prop.IE := 'ISENTO'; end; prop.RNTRC := DM_CNT.FrotaRNTRC.AsString; prop.xNome := DM_CTA.PessoaFJRSocial.AsString; prop.UF := DM_CTA.PessoaFJEstado.AsString; // TpcteProp = (tpTACAgregado, tpTACIndependente, tpOutros); case DM_CNT.FrotaTipoProp.AsInteger of 0: prop.tpProp := tpTACAgregado; 1: prop.tpProp := tpTACIndependente; 2: prop.tpProp := tpOutros; end; end; end; DM_CNT.LotaVeic.Next; end; end; // // Informações do(s) Motorista(s) // DM_CNT.LotaMoto.Close; DM_CNT.LotaMoto.SQL.Clear; DM_CNT.LotaMoto.SQL.Add('Select * From Cnt_LotaMoto'); DM_CNT.LotaMoto.SQL.Add('Where Codigo = :xCodigo'); DM_CNT.LotaMoto.SQL.Add('and Serie = :xSerie'); DM_CNT.LotaMoto.SQL.Add('and Numero = :xNumero'); // DM_CNT.LotaMoto.SQL.Add('Order By Item'); DM_CNT.LotaMoto.Params[0].AsInteger := DM_CNT.ConhecimentoCodigo.AsInteger; DM_CNT.LotaMoto.Params[1].AsInteger := DM_CNT.ConhecimentoSerie.AsInteger; DM_CNT.LotaMoto.Params[2].AsInteger := DM_CNT.ConhecimentoNumero.AsInteger; DM_CNT.LotaMoto.Active := True; DM_CNT.LotaMoto.Open; DM_CNT.LotaMoto.Last; j := DM_CNT.LotaMoto.RecordCount; if j > 0 then begin DM_CNT.LotaMoto.First; for i := 1 to j do begin {$IFDEF PL_200} with infCTeNorm.rodo.moto.Add do {$ELSE} with rodo.moto.Add do {$ENDIF} begin DM_CNT.Condutores.Close; DM_CNT.Condutores.SQL.Clear; DM_CNT.Condutores.SQL.Add('Select * From Cnt_Condutores'); DM_CNT.Condutores.SQL.Add('Where CPF = :xCPF'); DM_CNT.Condutores.Params[0].AsString:=DM_CNT.LotaMotoCPF.AsString; DM_CNT.Condutores.Active:=True; DM_CNT.Condutores.Open; xNome := DM_CNT.CondutoresNome.AsString; CPF := DM_CNT.CondutoresCPF.AsString; end; DM_CNT.LotaMoto.Next; end; end; end; // // Informações sobre os produtos Perigosos // DM_CNT.Produtos.Close; DM_CNT.Produtos.SQL.Clear; DM_CNT.Produtos.SQL.Add('Select * From Cnt_Produtos'); DM_CNT.Produtos.SQL.Add('Where Codigo = :xCodigo'); DM_CNT.Produtos.SQL.Add('and Serie = :xSerie'); DM_CNT.Produtos.SQL.Add('and Numero = :xNumero'); DM_CNT.Produtos.SQL.Add('Order By Item'); DM_CNT.Produtos.Params[0].AsInteger := DM_CNT.ConhecimentoCodigo.AsInteger; DM_CNT.Produtos.Params[1].AsInteger := DM_CNT.ConhecimentoSerie.AsInteger; DM_CNT.Produtos.Params[2].AsInteger := DM_CNT.ConhecimentoNumero.AsInteger; DM_CNT.Produtos.Active := True; DM_CNT.Produtos.Open; DM_CNT.Produtos.Last; j := DM_CNT.Produtos.RecordCount; if j > 0 then begin DM_CNT.Produtos.First; for i := 1 to j do begin {$IFDEF PL_200} with infCTeNorm.peri.Add do {$ELSE} with peri.Add do {$ENDIF} begin nONU := DM_CNT.ProdutosNumeroONU.AsString; xNomeAE := Trim(DM_CNT.ProdutosNome.AsString); xClaRisco := Trim(DM_CNT.ProdutosClasse.AsString); grEmb := Trim(DM_CNT.ProdutosGrupo.AsString); qTotProd := Trim(DM_CNT.ProdutosQtde.AsString); qVolTipo := Trim(DM_CNT.ProdutosVolumes.AsString); PontoFulgor := Trim(DM_CNT.ProdutosFulgor.AsString); end; DM_CNT.Produtos.Next; end; end; // // Informações sobre os veículos novos transportados // DM_CNT.Veiculos.Close; DM_CNT.Veiculos.SQL.Clear; DM_CNT.Veiculos.SQL.Add('Select * From Cnt_VeiculosNovos'); DM_CNT.Veiculos.SQL.Add('Where Codigo = :xCodigo'); DM_CNT.Veiculos.SQL.Add('and Serie = :xSerie'); DM_CNT.Veiculos.SQL.Add('and Numero = :xNumero'); DM_CNT.Veiculos.SQL.Add('Order By Item'); DM_CNT.Veiculos.Params[0].AsInteger := DM_CNT.ConhecimentoCodigo.AsInteger; DM_CNT.Veiculos.Params[1].AsInteger := DM_CNT.ConhecimentoSerie.AsInteger; DM_CNT.Veiculos.Params[2].AsInteger := DM_CNT.ConhecimentoNumero.AsInteger; DM_CNT.Veiculos.Active := True; DM_CNT.Veiculos.Open; DM_CNT.Veiculos.Last; j := DM_CNT.Veiculos.RecordCount; if j > 0 then begin DM_CNT.Veiculos.First; for i := 1 to j do begin {$IFDEF PL_200} with infCTeNorm.veicNovos.Add do {$ELSE} with veicNovos.Add do {$ENDIF} begin chassi := DM_CNT.VeiculosChassi.AsString; cCor := Trim(DM_CNT.VeiculosCodigoCor.AsString); xCor := Trim(DM_CNT.VeiculosDescCor.AsString); cMod := Trim(DM_CNT.VeiculosModelo.AsString); vUnit := RoundTo(DM_CNT.VeiculosValor.AsFloat, -2); vFrete := RoundTo(DM_CNT.VeiculosFrete.AsFloat, -2); end; DM_CNT.Veiculos.Next; end; end; if DM_CNT.ConhecimentoTipoCTe.AsInteger = 3 then begin // // Informações do Detalhamento do CTe Substituto // {$IFDEF PL_200} with infCTeNorm.infCteSub do {$ELSE} with infCTeSub do {$ENDIF} begin chCte := DM_CNT.ConhecimentoSubstituiCTe.AsString; if trim(DM_CNT.ConhecimentoNFeTomador.AsString) <> '' then tomaICMS.refNFe := DM_CNT.ConhecimentoNFeTomador.AsString else begin if DM_CNT.ConhecimentoNFNumero.AsInteger>0 then begin {$IFDEF PL_200} tomaICMS.refNF.CNPJCPF := DM_CNT.ConhecimentoNFCNPJ.AsString; {$ELSE} tomaICMS.refNF.CNPJ := DM_CNT.ConhecimentoNFCNPJ.AsString; {$ENDIF} tomaICMS.refNF.modelo := DM_CNT.ConhecimentoNFMod.AsString; tomaICMS.refNF.serie := DM_CNT.ConhecimentoNFSerie.AsInteger; tomaICMS.refNF.subserie := DM_CNT.ConhecimentoNFSubSerie.AsInteger; tomaICMS.refNF.nro := DM_CNT.ConhecimentoNFNumero.AsInteger; tomaICMS.refNF.valor := DM_CNT.ConhecimentoNFValor.AsFloat; tomaICMS.refNF.dEmi := DM_CNT.ConhecimentoNFEmissao.AsDateTime; end else begin if trim(DM_CNT.ConhecimentoCTeTomador.AsString) <> '' then tomaICMS.refCte := DM_CNT.ConhecimentoCTeTomador.AsString else tomaNaoICMS.refCteAnu := DM_CNT.ConhecimentoAnuladoCTe.AsString; end; end; end; end; end; 1: begin // // Informações do Detalhamento do CTe Complementado // DM_CNT.Complemento.Close; DM_CNT.Complemento.SQL.Clear; DM_CNT.Complemento.SQL.Add('Select * From Cnt_Complemento'); DM_CNT.Complemento.SQL.Add('Where Codigo = :xCodigo'); DM_CNT.Complemento.SQL.Add('and Serie = :xSerie'); DM_CNT.Complemento.SQL.Add('and Numero = :xNumero'); DM_CNT.Complemento.SQL.Add('Order By Item'); DM_CNT.Complemento.Params[0].AsInteger := DM_CNT.ConhecimentoCodigo.AsInteger; DM_CNT.Complemento.Params[1].AsInteger := DM_CNT.ConhecimentoSerie.AsInteger; DM_CNT.Complemento.Params[2].AsInteger := DM_CNT.ConhecimentoNumero.AsInteger; DM_CNT.Complemento.Active := True; DM_CNT.Complemento.Open; DM_CNT.Complemento.Last; j := DM_CNT.Complemento.RecordCount; if j > 0 then begin DM_CNT.Complemento.First; for i := 1 to j do begin if DM_CNT.ComplementoValorTotal.AsFloat > 0.0 then begin {$IFDEF PL_200} with infCTeComp do // 1-1 {$ELSE} with infCTeComp.Add do // 1-10 {$ENDIF} begin chave := DM_CNT.ComplementoCTe.AsString; {$IFNDEF PL_200} vPresComp.vTPrest := RoundTo(DM_CNT.ComplementoValorTotal.AsFloat, -2); // // Informações dos Componentes Complementados // DM_CNT.Componentes.Close; DM_CNT.Componentes.SQL.Clear; DM_CNT.Componentes.SQL.Add('Select * From Cnt_Componentes'); DM_CNT.Componentes.SQL.Add('Where Codigo = :xCodigo'); DM_CNT.Componentes.SQL.Add('and Serie = :xSerie'); DM_CNT.Componentes.SQL.Add('and Numero = :xNumero'); DM_CNT.Componentes.SQL.Add('and Complemento = :xComplemento'); DM_CNT.Componentes.SQL.Add('Order By Item'); DM_CNT.Componentes.Params[0].AsInteger := DM_CNT.ConhecimentoCodigo.AsInteger; DM_CNT.Componentes.Params[1].AsInteger := DM_CNT.ConhecimentoSerie.AsInteger; DM_CNT.Componentes.Params[2].AsInteger := DM_CNT.ConhecimentoNumero.AsInteger; DM_CNT.Componentes.Params[3].AsString := 'S'; DM_CNT.Componentes.Active := True; DM_CNT.Componentes.Open; DM_CNT.Componentes.Last; l := DM_CNT.Componentes.RecordCount; if l > 0 then begin DM_CNT.Componentes.First; for k := 1 to l do begin if DM_CNT.ComponentesValor.AsFloat > 0.0 then begin with vPresComp.compComp.Add do begin xNome := DM_CNT.ComponentesDescricao.AsString; vComp := RoundTo(DM_CNT.ComponentesValor.AsFloat, -2); end; end; DM_CNT.Componentes.Next; end; end; // // Informações relativas ao Impostos Complementados // // TpcnCSTIcms = (cst00, cst10, cst20, cst30, cst40, cst41, cst45, cst50, cst51, cst60, cst70, cst80, cst81, cst90); // 80 e 81 apenas para CTe case DM_CNT.ComplementoCST.AsInteger of 00: begin ImpComp.ICMSComp.SituTrib := cst00; ImpComp.ICMSComp.ICMS00.CST := cst00; // Tributação Normal ICMS ImpComp.ICMSComp.ICMS00.vBC := RoundTo(DM_CNT.ComplementoBC.AsFloat, -2); ImpComp.ICMSComp.ICMS00.pICMS := RoundTo(DM_CNT.ComplementoAliq.AsFloat, -2); ImpComp.ICMSComp.ICMS00.vICMS := RoundTo(DM_CNT.ComplementoValorICMS.AsFloat, -2); end; 20: begin ImpComp.ICMSComp.SituTrib := cst20; ImpComp.ICMSComp.ICMS20.CST := cst20; // Tributação com BC reduzida do ICMS ImpComp.ICMSComp.ICMS20.pRedBC := RoundTo(DM_CNT.ComplementoReducao.AsFloat, -2); ImpComp.ICMSComp.ICMS20.vBC := RoundTo(DM_CNT.ComplementoBC.AsFloat, -2); ImpComp.ICMSComp.ICMS20.pICMS := RoundTo(DM_CNT.ComplementoAliq.AsFloat, -2); ImpComp.ICMSComp.ICMS20.vICMS := RoundTo(DM_CNT.ComplementoValorICMS.AsFloat, -2); end; 40: begin ImpComp.ICMSComp.SituTrib := cst40; ImpComp.ICMSComp.ICMS45.CST := cst40; // ICMS Isento end; 41: begin ImpComp.ICMSComp.SituTrib := cst41; ImpComp.ICMSComp.ICMS45.CST := cst41; // ICMS não Tributada end; 51: begin ImpComp.ICMSComp.SituTrib := cst51; ImpComp.ICMSComp.ICMS45.CST := cst51; // ICMS diferido end; 60: begin ImpComp.ICMSComp.SituTrib := cst60; ImpComp.ICMSComp.ICMS60.CST := cst60; // Tributação atribuida ao tomador ou 3. por ST ImpComp.ICMSComp.ICMS60.vBCSTRet := RoundTo(DM_CNT.ComplementoBC.AsFloat, -2); ImpComp.ICMSComp.ICMS60.pICMSSTRet := RoundTo(DM_CNT.ComplementoAliq.AsFloat, -2); ImpComp.ICMSComp.ICMS60.vICMSSTRet := RoundTo(DM_CNT.ComplementoValorICMS.AsFloat, -2); ImpComp.ICMSComp.ICMS60.vCred := RoundTo(DM_CNT.ComplementoCredito.AsFloat, -2); end; 90: begin ImpComp.ICMSComp.SituTrib := cst90; ImpComp.ICMSComp.ICMS90.CST := cst90; // ICMS Outros ImpComp.ICMSComp.ICMS90.pRedBC := RoundTo(DM_CNT.ComplementoReducao.AsFloat, -2); ImpComp.ICMSComp.ICMS90.vBC := RoundTo(DM_CNT.ComplementoBC.AsFloat, -2); ImpComp.ICMSComp.ICMS90.pICMS := RoundTo(DM_CNT.ComplementoAliq.AsFloat, -2); ImpComp.ICMSComp.ICMS90.vICMS := RoundTo(DM_CNT.ComplementoValorICMS.AsFloat, -2); ImpComp.ICMSComp.ICMS90.vCred := RoundTo(DM_CNT.ComplementoCredito.AsFloat, -2); end; end; // Valor Total dos Tributos ImpComp.vTotTrib := RoundTo(DM_CNT.ConhecimentovTotTrib.AsFloat, -2); // Obs do Contribuinte if (impComp.vTotTrib <> 0.0) then begin with compl.ObsCont.Add do begin xCampo := 'Lei da Transparencia'; xTexto := 'O valor aproximado de tributos incidentes sobre o preco deste servico e de R$ ' + FormatFloat('##0.00', impComp.vTotTrib) + ' (' + FormatFloat('#0.00', DM_CNT.ConhecimentoAliqNBS.AsFloat) + '%) ' + 'Fonte: IBPT'; end; end; {$ENDIF} end; end; DM_CNT.Complemento.Next; end; end; end; 2: begin // // Informações do Detalhamento do CTe do tipo Anulação de Valores // {$IFDEF PL_200} infCTeAnu.chCTe := DM_CNT.ConhecimentoAnuladoCTe.AsString; infCTeAnu.dEmi := DM_CNT.ConhecimentoAnuladoData.AsDateTime; {$ELSE} infCTeAnuEnt.chCTe := DM_CNT.ConhecimentoAnuladoCTe.AsString; infCTeAnuEnt.dEmi := DM_CNT.ConhecimentoAnuladoData.AsDateTime; {$ENDIF} end; end; // fim do case {$IFDEF PL_200} DM_CTA.PessoaFJ.Close; DM_CTA.PessoaFJ.SQL.Clear; DM_CTA.PessoaFJ.SQL.Add('Select * From Sis_PessoaFJ'); DM_CTA.PessoaFJ.SQL.Add('Where DownloadXML = :xXML'); DM_CTA.PessoaFJ.Params[0].AsString := 'S'; DM_CTA.PessoaFJ.Active := True; DM_CTA.PessoaFJ.Open; DM_CTA.PessoaFJ.Last; j := DM_CTA.PessoaFJ.RecordCount; if j > 0 then begin DM_CTA.PessoaFJ.First; for i := 1 to j do begin with autXML.Add do begin if copy(DM_CTA.PessoaFJCGC.AsString,10,4) <> '0000' then CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 2, 14) else CNPJCPF := Copy(DM_CTA.PessoaFJCGC.AsString, 1, 9) + Copy(DM_CTA.PessoaFJCGC.AsString, 14, 2); end; DM_CTA.PessoaFJ.Next; end; end; {$ENDIF} end; // fim da adição do conhecimento ao compomente end; // ============================================================================= // // Enviar Lote // // ============================================================================= procedure TfrmMovEmitirCNT.EnviarLote(Lote: Integer); var i: integer; begin MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' => Enviando Lote de Conhecimento(s)'); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add('XMLs:'); MemoStatus.Lines.Add(' Gerado(s)'); MemoStatus.Lines.Add(' Salvo(s) em: '+DMDFe.PathMensal); // Gera o XML, Assina e Salva o Arquivo DMDFe.CTe.Conhecimentos.Assinar; MemoStatus.Lines.Add(' Assinado(s)'); // Valida os dados do XML salvo DMDFe.CTe.Conhecimentos.Valida; MemoStatus.Lines.Add(' Validado(s)'); // EnviaSefaz = variável booleana // Se False, apenas gera o XML, assina, valida e Imprime o DACTe não protocolado // Se True, gera o XML, assina, valida, envia para o SEFAZ e Imprime o DACTe protocolado if DM_CTA.EnviaSefaz and (rgTipoEmissao.ItemIndex <> 1) then begin MemoStatus.Lines.Add(' Aguarde Enviando...'); for i := 0 to (DMDFe.CTe.Conhecimentos.Count - 1) do begin DM_CNT.IncAltDel.SQL.Clear; DM_CNT.IncAltDel.SQL.Add('Update Cnt_Conhecimento Set'); DM_CNT.IncAltDel.SQL.Add('Emissao = :xEmissao,'); DM_CNT.IncAltDel.SQL.Add('TipoEmissao = :xTipoEmissao,'); DM_CNT.IncAltDel.SQL.Add('DtHrContingencia = :xDataHora,'); DM_CNT.IncAltDel.SQL.Add('Justificativa = :xJustificativa,'); DM_CNT.IncAltDel.SQL.Add('ChaveCTe = :xChave,'); DM_CNT.IncAltDel.SQL.Add('cStatus = :xcStatus,'); DM_CNT.IncAltDel.SQL.Add('xMotivo = :xxMotivo,'); DM_CNT.IncAltDel.SQL.Add('Anulado = :xAnulado,'); DM_CNT.IncAltDel.SQL.Add('Substituido = :xSubstituido,'); DM_CNT.IncAltDel.SQL.Add('Complementado = :xComplementado,'); DM_CNT.IncAltDel.SQL.Add('SerieMDFe = :xSerieMDFe,'); DM_CNT.IncAltDel.SQL.Add('NumeroMDFe = :xNumeroMDFe'); DM_CNT.IncAltDel.SQL.Add('Where Serie = :xSerie'); DM_CNT.IncAltDel.SQL.Add('and Numero = :xNumero'); DM_CNT.IncAltDel.Params[0].AsDateTime:=DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.dhEmi; if rgCondicao.ItemIndex=0 then begin DM_CNT.IncAltDel.Params[1].AsInteger:=0; // Tipo de Emissao Normal DM_CNT.IncAltDel.Params[2].AsDateTime:=0; DM_CNT.IncAltDel.Params[3].AsString:=''; end else begin DM_CNT.IncAltDel.Params[1].AsInteger:=2; // Tipo de Emissao SEFAZ-Virtual de Contingencia DM_CNT.IncAltDel.Params[2].AsDateTime:=DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.dhCont; DM_CNT.IncAltDel.Params[3].AsString:=DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.xJust; end; DM_CNT.IncAltDel.Params[4].AsString:=Copy(DMDFe.CTe.Conhecimentos.Items[i].CTe.inFCTe.ID,4,44); DM_CNT.IncAltDel.Params[5].AsInteger:=0; // cStatus DM_CNT.IncAltDel.Params[6].AsString:=''; // xMotivo DM_CNT.IncAltDel.Params[7].AsString:='N'; // Anulado DM_CNT.IncAltDel.Params[8].AsString:='N'; // Substituido DM_CNT.IncAltDel.Params[9].AsString:='N'; // Complementado DM_CNT.IncAltDel.Params[10].AsInteger:=0; // Serie MDFe DM_CNT.IncAltDel.Params[11].AsInteger:=0; // Numero MDFe DM_CNT.IncAltDel.Params[12].AsInteger:=DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.serie; DM_CNT.IncAltDel.Params[13].AsInteger:=DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.nCT; DM_CNT.IncAltDel.ExecSQL; end; // Envia o lote de Conhecimentos para o SEFAZ e Imprime o DACTe // Lote = variável que contem o numero do lote (numero sequencial) DMDFe.CTe.Enviar(Lote); sRecibo := DMDFe.CTe.WebServices.Enviar.Recibo; sStat := IntToStr(DMDFe.CTe.WebServices.Enviar.cStat); sMotivo := DMDFe.CTe.WebServices.Enviar.xMotivo; sDataHora := DateTimeToStr(DMDFe.CTe.WebServices.Enviar.dhRecbto); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' => Recibo nº ' + sRecibo); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' Data / Hora Status do CT-e'); MemoStatus.Lines.Add(' ---------------- ---------------------------------------'); MemoStatus.Lines.Add(' ' + sDataHora + ' ' + sStat + ' - ' + sMotivo); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' '); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' => Consultando Recibo nº ' + sRecibo); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); DMDFe.CTe.WebServices.Retorno.Recibo := sRecibo; if DMDFe.CTe.WebServices.Retorno.Executar then begin MemoStatus.Lines.Add(' Conhecimento Número do protocolo Status do CT-e'); MemoStatus.Lines.Add(' ------------ ----------------------------------- ---------------------------------------'); for i := 0 to (DMDFe.Cte.WebServices.Retorno.CteRetorno.ProtCTe.Count - 1) do begin sDataHora := DateTimeToStr(DMDFe.CTe.WebServices.Retorno.CteRetorno.ProtCTe.Items[i].dhRecbto); sProtocolo := DMDFe.CTe.WebServices.Retorno.CteRetorno.ProtCTe.Items[i].nProt; sStat := IntToStr(DMDFe.CTe.WebServices.Retorno.CteRetorno.ProtCTe.Items[i].cStat); sMotivo := DMDFe.CTe.WebServices.Retorno.CteRetorno.ProtCTe.Items[i].xMotivo; sConhecimento := Copy(DMDFe.CTe.WebServices.Retorno.CteRetorno.ProtCTe.Items[i].chCTe, 26, 3) + '.' + Copy(DMDFe.CTe.WebServices.Retorno.CteRetorno.ProtCTe.Items[i].chCTe, 29, 3) + '.' + Copy(DMDFe.CTe.WebServices.Retorno.CteRetorno.ProtCTe.Items[i].chCTe, 32, 3); MemoStatus.Lines.Add(' ' + sConhecimento + ' ' + sProtocolo + ' ' + sDataHora + ' ' + sStat + ' - ' + sMotivo); end; for i := 0 to (DMDFe.CTe.Conhecimentos.Count - 1) do begin DM_CNT.IncAltDel.SQL.Clear; DM_CNT.IncAltDel.SQL.Add('Update Cnt_Conhecimento Set'); DM_CNT.IncAltDel.SQL.Add('Enviado = :xEnviado,'); DM_CNT.IncAltDel.SQL.Add('Protocolado = :xProtocolado,'); DM_CNT.IncAltDel.SQL.Add('Protocolo = :xProtocolo,'); DM_CNT.IncAltDel.SQL.Add('Impresso = :xImpresso,'); DM_CNT.IncAltDel.SQL.Add('cStatus = :xcStatus,'); DM_CNT.IncAltDel.SQL.Add('xMotivo = :xxMotivo'); DM_CNT.IncAltDel.SQL.Add('Where Serie = :xSerie'); DM_CNT.IncAltDel.SQL.Add('and Numero = :xNumero'); DM_CNT.IncAltDel.Params[0].AsString:='S'; if DMDFe.CTe.Conhecimentos.Items[i].CTe.procCTe.cStat=100 then begin DM_CNT.IncAltDel.Params[1].AsString:='S'; DM_CNT.IncAltDel.Params[2].AsString:=DMDFe.CTe.Conhecimentos.Items[i].CTe.procCTe.nProt; end else begin DM_CNT.IncAltDel.Params[1].AsString:='N'; DM_CNT.IncAltDel.Params[2].AsString:=''; end; DM_CNT.IncAltDel.Params[3].AsString:='S'; DM_CNT.IncAltDel.Params[4].AsInteger:=DMDFe.CTe.Conhecimentos.Items[i].CTe.procCTe.cStat; // cStatus DM_CNT.IncAltDel.Params[5].AsString:=DMDFe.CTe.Conhecimentos.Items[i].CTe.procCTe.xMotivo; // xMotivo DM_CNT.IncAltDel.Params[6].AsInteger:=DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.serie; DM_CNT.IncAltDel.Params[7].AsInteger:=DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.nCT; DM_CNT.IncAltDel.ExecSQL; end; MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); end else begin MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' => Erro: '); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' '+DMDFe.CTe.WebServices.Retorno.Msg); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); end; end else begin MemoStatus.Lines.Add(' Aguarde Imprimindo...'); // Imprime o CTe DMDFe.CTe.Conhecimentos.Imprimir; MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' => Impressão Concluida'); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); for i := 0 to (DMDFe.CTe.Conhecimentos.Count - 1) do begin DM_CNT.IncAltDel.SQL.Clear; DM_CNT.IncAltDel.SQL.Add('Update Cnt_Conhecimento Set'); DM_CNT.IncAltDel.SQL.Add('Emissao = :xEmissao,'); DM_CNT.IncAltDel.SQL.Add('TipoEmissao = :xTipoEmissao,'); DM_CNT.IncAltDel.SQL.Add('DtHrContingencia = :xDataHora,'); DM_CNT.IncAltDel.SQL.Add('Justificativa = :xJustificativa,'); DM_CNT.IncAltDel.SQL.Add('ChaveCTe = :xChave,'); DM_CNT.IncAltDel.SQL.Add('Impresso = :xImpresso,'); DM_CNT.IncAltDel.SQL.Add('cStatus = :xcStatus,'); DM_CNT.IncAltDel.SQL.Add('xMotivo = :xxMotivo,'); DM_CNT.IncAltDel.SQL.Add('Anulado = :xAnulado,'); DM_CNT.IncAltDel.SQL.Add('Substituido = :xSubstituido,'); DM_CNT.IncAltDel.SQL.Add('Complementado = :xComplementado,'); DM_CNT.IncAltDel.SQL.Add('SerieMDFe = :xSerieMDFe,'); DM_CNT.IncAltDel.SQL.Add('NumeroMDFe = :xNumeroMDFe'); DM_CNT.IncAltDel.SQL.Add('Where Serie = :xSerie'); DM_CNT.IncAltDel.SQL.Add('and Numero = :xNumero'); DM_CNT.IncAltDel.Params[0].AsDateTime:=DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.dhEmi; if (rgTipoEmissao.ItemIndex = 1) then begin DM_CNT.IncAltDel.Params[1].AsInteger:=1; // Tipo de Emissao Contingência FS-DA DM_CNT.IncAltDel.Params[2].AsDateTime:=DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.dhCont; DM_CNT.IncAltDel.Params[3].AsString:=DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.xJust; end else begin DM_CNT.IncAltDel.Params[1].AsInteger:=0; // Tipo de Emissao Normal DM_CNT.IncAltDel.Params[2].AsDateTime:=0; DM_CNT.IncAltDel.Params[3].AsString:=''; end; DM_CNT.IncAltDel.Params[4].AsString:=Copy(DMDFe.CTe.Conhecimentos.Items[i].CTe.inFCTe.ID,4,44); DM_CNT.IncAltDel.Params[5].AsString:='S'; DM_CNT.IncAltDel.Params[6].AsInteger:=0; // cStatus DM_CNT.IncAltDel.Params[7].AsString:=''; // xMotivo DM_CNT.IncAltDel.Params[8].AsString:='N'; // Anulado DM_CNT.IncAltDel.Params[9].AsString:='N'; // Substituido DM_CNT.IncAltDel.Params[10].AsString:='N'; // Complementado DM_CNT.IncAltDel.Params[11].AsInteger:=0; // Serie MDFe DM_CNT.IncAltDel.Params[12].AsInteger:=0; // Numero MDFe DM_CNT.IncAltDel.Params[13].AsInteger:=DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.serie; DM_CNT.IncAltDel.Params[14].AsInteger:=DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.nCT; DM_CNT.IncAltDel.ExecSQL; end; end; end; // ============================================================================= // // Enviar XML e DACTE por Email // // ============================================================================= procedure TfrmMovEmitirCNT.EnviarEmail; var i, j: Integer; sRemetente: String; begin DM_CTA.TabHost.Close; DM_CTA.TabHost.SQL.Clear; DM_CTA.TabHost.SQL.Add('Select * From Sis_TabHost'); DM_CTA.TabHost.SQL.Add('Where Empresa = :xEmpresa'); DM_CTA.TabHost.SQL.Add('Order By Sequencia'); DM_CTA.TabHost.Params[0].AsString:=DM_CTA.xCodEmpresa; DM_CTA.TabHost.Active:=True; DM_CTA.TabHost.Open; Mensagem:=TStringList.Create; sCC:=TStringList.Create; j:=lstChave3.Items.Count-1; // Caso a quantidade de Conhecimentos não impressos seja superior o // limite estabelecido pelo SEFAZ (50 Conhecimentos por lote) a // geração dos CT-e ficara limitada nos 50 primeiros, portanto novos // lotes deverão ser gerados até compreender todos os Conhecimentos não // impressas. if j>LimiteCTe then j:=LimiteCTe-1; pbProgresso.Min:=0; pbProgresso.Max:=j; if (j>=0) and Selecionado then begin Ok:=False; MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' => Enviando Conhecimento(s) por e-mail'); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); for i:=pbProgresso.Min to pbProgresso.Max do begin pbProgresso.Position:=i; DM_CNT.Conhecimento.Close; DM_CNT.Conhecimento.SQL.Clear; DM_CNT.Conhecimento.SQL.Add('Select * From Cnt_Conhecimento'); DM_CNT.Conhecimento.SQL.Add('Where Codigo = :xUnidade'); DM_CNT.Conhecimento.SQL.Add('and Serie = :xSerie'); DM_CNT.Conhecimento.SQL.Add('and Numero = :xNumero'); DM_CNT.Conhecimento.Params[0].AsInteger:=StrToInt(lstChave3.Items.Strings[i]); DM_CNT.Conhecimento.Params[1].AsInteger:=StrToInt(lstChaveS2.Items.Strings[i]); DM_CNT.Conhecimento.Params[2].AsInteger:=StrToInt(lstChave4.Items.Strings[i]); DM_CNT.Conhecimento.Active:=True; DM_CNT.Conhecimento.Open; //====================================================================== // Enviar email para o Cliente //====================================================================== DM_CTA.PessoaFJE.Close; DM_CTA.PessoaFJE.SQL.Clear; DM_CTA.PessoaFJE.SQL.Add('Select * From Sis_PessoaFJEmail'); DM_CTA.PessoaFJE.SQL.Add('Where CNPJ = :xCNPJ'); DM_CTA.PessoaFJE.SQL.Add('and Modulo = :xModulo'); DM_CTA.PessoaFJE.Params[0].AsString:=DM_CNT.ConhecimentoPagador.AsString; DM_CTA.PessoaFJE.Params[1].AsString:='DFE'; DM_CTA.PessoaFJE.Active:=True; DM_CTA.PessoaFJE.Open; if (DM_CTA.PessoaFJECNPJ.AsString=DM_CNT.ConhecimentoPagador.AsString) and (DM_CTA.PessoaFJEModulo.AsString='DFE') then Endereco:=Trim(DM_CTA.PessoaFJEEmail.AsString) else Endereco:=''; Ok:=False; MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' CT-e: '+Copy(DMDFe.CTe.Conhecimentos.Items[i].CTe.inFCTe.ID,4,44)); if Endereco<>'' then begin Mensagem.Clear; sCC.Clear; if DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.tpEmis = teFSDA then begin Mensagem.Add('Comunicamos por este e-mail que o Conhecimento Eletrônico (Chave='+ Copy(DMDFe.CTe.Conhecimentos.Items[i].CTe.inFCTe.ID,4,44)+ ') não foi enviado para SEFAZ pelo motivo: '+DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.xJust); Mensagem.Add('DACTE em Contingência - Impresso em decorrência de problemas técnicos.'); Mensagem.Add(' '); Mensagem.Add('Enviamos em anexo o arquivo XML em Contingência do respectivo CT-e.'); Mensagem.Add(' '); Mensagem.Add('Um novo e-mail será enviado, com o XML autorizado assim que os problemas forem sanados.'); end else begin Mensagem.Add('Prezado cliente,'); Mensagem.Add(' '); Mensagem.Add('Você está recebendo o Conhecimento de Transporte Eletrônico número ' + FormatFloat('###,###,##0', DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.nCT) + ', série ' + FormatFloat('000', DMDFe.CTe.Conhecimentos.Items[i].CTe.Ide.serie) + ' de ' + DM_CTA.EmpresaNome.AsString + ', no valor de R$ '+ FormatFloat('###,##0.00', DMDFe.CTe.Conhecimentos.Items[i].CTe.vPrest.vTPrest) + '. Além disso, junto com a(s) mercadoria(s) seguirá o DACTE ' + '(Documento Auxiliar do Conhecimento de Transporte Eletrônico), ' + 'impresso em papel que acompanha o transporte da(s) mesma(s).'); Mensagem.Add(' '); Mensagem.Add('Anexo à este e-mail você está recebendo também o arquivo ' + 'XML do Conhecimento de Transporte Eletrônico. Este arquivo deve ser armazenado ' + 'eletronicamente por sua empresa pelo prazo de 05 (cinco) anos, ' + 'conforme previsto na legislação tributária (Art. 173 do Código ' + 'Tributário Nacional e § 4º da Lei 5.172 de 25/10/1966).'); Mensagem.Add(' '); Mensagem.Add('O DACTE em papel pode ser arquivado para apresentação ao fisco ' + 'quando solicitado. Todavia, se sua empresa também for emitente ' + 'de NF-e ou CT-e, o arquivamento eletrônico do XML de seus fornecedores ' + 'é obrigatório, sendo passível de fiscalização.'); Mensagem.Add(' '); Mensagem.Add('Para se certificar que este CT-e é válido, queira por favor ' + 'consultar sua autenticidade no site nacional do projeto CT-e ' + '(www.cte.fazenda.gov.br), utilizando a chave de acesso contida no DACTE.'); Mensagem.Add(' '); Mensagem.Add('Chave: ' + Copy(DMDFe.CTe.Conhecimentos.Items[i].CTe.inFCTe.ID,4,44)); Mensagem.Add(' '); Mensagem.Add('Protocolo de Autorização: ' + DMDFe.CTe.Conhecimentos.Items[i].CTe.procCTe.nProt); end; Mensagem.Add(' '); Mensagem.Add('Atenciosamente,'); Mensagem.Add(' '); Mensagem.Add(DM_CTA.EmpresaNome.AsString); Mensagem.Add(' '); Mensagem.Add('>>> Este e-mail, bem como seu(s) anexo(s), foi gerado pelo sistema ERPJr <<<'); if Trim(DM_CTA.EmpresaEmail.AsString)<>'' then sCC.Add(Trim(DM_CTA.EmpresaEmail.AsString)); if DMDFe.CopiaPara<>'' then sCC.Add(DMDFe.CopiaPara); DM_CTA.TabHost.Last; jHost:=DM_CTA.TabHost.RecordCount; iHost:=1; DM_CTA.TabHost.First; Falha:=False; MemoStatus.Lines.Add(' Endereço: ' + Endereco); if DMDFe.CTe.Conhecimentos.Items[i].CTe.procCTe.cStat = 100 then begin repeat MemoStatus.Lines.Add(' Aguarde... Enviando CT-e por e-mail. Host ['+ IntToStr(iHost)+'] '); if DM_CTA.TabHostRequerConta.AsString='S' then sRemetente := Trim(DM_CTA.TabHostContaemail.AsString) else sRemetente := Trim(DM_CTA.EmpresaEmail.AsString); try DMDFe.CTe.Conhecimentos.Items[i].EnviarEmail(DM_CTA.TabHostServidorSMTP.AsString, DM_CTA.TabHostPortaSMTP.AsString, DM_CTA.TabHostUsuario.AsString, DM_CTA.TabHostSenha.AsString, sRemetente, Endereco, 'CT-e', Mensagem, DMDFe.ConexaoSSL, // SSL DMDFe.EnviaPDF, // Enviar em PDF sCC, // sCC nil, // Anexos DMDFe.PedeConfirmacao, // Pede Confirmação de Recebimento DMDFe.AguardarEnvio, // Aguarda o Envio DM_CTA.EmpresaNome.AsString, // Nome do remetente DMDFe.ConexaoTLS, DMDFe.UsarThread); Ok:=True; except DM_CTA.TabHost.Next; Inc(iHost); if iHost>jHost then Falha:=True; end; until Ok or Falha; end; if Falha then MessageDlg('e-mail não enviado por motivo de Falha.', mtError, [mbOk], 0); end else MemoStatus.Lines.Add(' Endereço: Tomador do Serviço não possui, e-mail Cadastrado.'); end; if Ok then begin MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' e-mail(s) enviado(s) com Sucesso.'); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); end; end else MessageDlg('Nenhum Conhecimento Selecionado.', mtInformation, [mbOk], 0); end; // ============================================================================= // // Conhecimentos não Emitidos // // ============================================================================= procedure TfrmMovEmitirCNT.NaoEmitidos; var i, j, Lote, CTeInicial, CTeFinal: Integer; begin DM_CTA.Empresa.Close; DM_CTA.Empresa.SQL.Clear; DM_CTA.Empresa.SQL.Add('Select * From Sis_Empresa'); DM_CTA.Empresa.SQL.Add('Where Codigo = :xCodigo'); DM_CTA.Empresa.Params[0].AsString:=DM_CTA.ParamDFeEmitente.AsString; DM_CTA.Empresa.Active:=True; DM_CTA.Empresa.Open; DMDFe.CTe.Conhecimentos.Clear; // Lote de Conhecimentos DM_CNT.LoteCTe.Close; DM_CNT.LoteCTe.SQL.Clear; DM_CNT.LoteCTe.SQL.Add('Select * From Cnt_LoteCTe'); DM_CNT.LoteCTe.SQL.Add('Order By Lote'); DM_CNT.LoteCTe.Active:=True; DM_CNT.LoteCTe.Open; DM_CNT.LoteCTe.Last; if DM_CNT.LoteCTe.RecordCount>0 then begin if (DM_CNT.LoteCTeCTInicial.AsInteger=0) and (DM_CNT.LoteCTeCTFinal.AsInteger=0) then Lote:=DM_CNT.LoteCTeLote.AsInteger else begin Lote:=(DM_CNT.LoteCTeLote.AsInteger+1); DM_CNT.IncAltDel.SQL.Clear; DM_CNT.IncAltDel.SQL.Add('Insert into Cnt_LoteCTe'); DM_CNT.IncAltDel.SQL.Add('(Lote, CTInicial, CTFinal, Data)'); DM_CNT.IncAltDel.SQL.Add('Values (:xLote, :xCTInicial, :xCTFinal, :xData)'); DM_CNT.IncAltDel.Params[0].AsInteger:=Lote; DM_CNT.IncAltDel.Params[1].AsInteger:=0; DM_CNT.IncAltDel.Params[2].AsInteger:=0; DM_CNT.IncAltDel.Params[3].AsDateTime:=Date; DM_CNT.IncAltDel.ExecSQL; end; end else begin Lote:=1; DM_CNT.IncAltDel.SQL.Clear; DM_CNT.IncAltDel.SQL.Add('Insert into Cnt_LoteCTe'); DM_CNT.IncAltDel.SQL.Add('(Lote, CTInicial, CTFinal, Data)'); DM_CNT.IncAltDel.SQL.Add('Values (:xLote, :xCTInicial, :xCTFinal, :xData)'); DM_CNT.IncAltDel.Params[0].AsInteger:=Lote; DM_CNT.IncAltDel.Params[1].AsInteger:=0; DM_CNT.IncAltDel.Params[2].AsInteger:=0; DM_CNT.IncAltDel.Params[3].AsDateTime:=Date; DM_CNT.IncAltDel.ExecSQL; end; j:=lstChave3.Items.Count-1; // Caso a quantidade de Conhecimentos não impressos seja superior o // limite estabelecido pelo SEFAZ (50 Conhecimentos por lote) a // geração dos CT-e ficara limitada nos 50 primeiros, portanto novos // lotes deverão ser gerados até compreender todos os Conhecimentos não // impressas. if j>LimiteCTe then j:=LimiteCTe-1; pbProgresso.Min:=0; pbProgresso.Max:=j; if (j>=0) and Selecionado then begin CTeInicial:=0; CTeFinal:=0; pbProgresso.Min:=0; pbProgresso.Max:=j; for i:=pbProgresso.Min to pbProgresso.Max do begin pbProgresso.Position:=i; DM_CNT.Conhecimento.Close; DM_CNT.Conhecimento.SQL.Clear; DM_CNT.Conhecimento.SQL.Add('Select * From Cnt_Conhecimento'); DM_CNT.Conhecimento.SQL.Add('Where Codigo = :xUnidade'); DM_CNT.Conhecimento.SQL.Add('and Serie = :xSerie'); DM_CNT.Conhecimento.SQL.Add('and Numero = :xNumero'); DM_CNT.Conhecimento.Params[0].AsInteger:=StrToInt(lstChave3.Items.Strings[i]); DM_CNT.Conhecimento.Params[1].AsInteger:=StrToInt(lstChaveS2.Items.Strings[i]); DM_CNT.Conhecimento.Params[2].AsInteger:=StrToInt(lstChave4.Items.Strings[i]); DM_CNT.Conhecimento.Active:=True; DM_CNT.Conhecimento.Open; if CTeInicial=0 then CTeInicial:=DM_CNT.ConhecimentoNumero.AsInteger; CTeFinal:=DM_CNT.ConhecimentoNumero.AsInteger; GerarLoteCTe; end; EnviarLote(Lote); DM_CNT.IncAltDel.SQL.Clear; DM_CNT.IncAltDel.SQL.Add('Update Cnt_LoteCTe Set'); DM_CNT.IncAltDel.SQL.Add('CTInicial = :xCTInicial,'); DM_CNT.IncAltDel.SQL.Add('CTFinal = :xCTFinal,'); DM_CNT.IncAltDel.SQL.Add('Data = :xData'); DM_CNT.IncAltDel.SQL.Add('Where Lote = :xLote'); DM_CNT.IncAltDel.Params[0].AsInteger:=CTeInicial; DM_CNT.IncAltDel.Params[1].AsInteger:=CTeFinal; DM_CNT.IncAltDel.Params[2].AsDateTime:=Date; DM_CNT.IncAltDel.Params[3].AsInteger:=Lote; DM_CNT.IncAltDel.ExecSQL; if DMDFe.EnviaEmail then EnviarEmail; end else MessageDlg('Nenhum Conhecimento Selecionado.', mtInformation, [mbOk], 0); if (DM_CTA.ParamDFeResumo.AsString='S') // and (PrintDialog.Execute) then MemoStatus.Print(Texto); end; // ============================================================================= // // Conhecimentos Emitidos // // ============================================================================= procedure TfrmMovEmitirCNT.Emitidos; var PathArquivo, NomeArquivo, PathEvento, NomeEvento: String; i, j: Integer; begin DMDFe.DACTe.MostrarPreview := chkVisualizar.Checked; DMDFe.DACTe.NumCopias := edtNumCopias.ValueInt; DM_CTA.Empresa.Close; DM_CTA.Empresa.SQL.Clear; DM_CTA.Empresa.SQL.Add('Select * From Sis_Empresa'); DM_CTA.Empresa.SQL.Add('Where Codigo = :xCodigo'); DM_CTA.Empresa.Params[0].AsString:=DM_CTA.ParamDFeEmitente.AsString; DM_CTA.Empresa.Active:=True; DM_CTA.Empresa.Open; j:=lstChave3.Items.Count-1; if (j>=0) // and Selecionado then begin pbProgresso.Min:=0; pbProgresso.Max:=j; DMDFe.CTe.Conhecimentos.Clear; MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' => Impressão de Conhecimento'); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add('XML:'); MemoStatus.Lines.Add(' Carregado'); for i:=pbProgresso.Min to pbProgresso.Max do begin pbProgresso.Position:=i; DM_CNT.Conhecimento.Close; DM_CNT.Conhecimento.SQL.Clear; DM_CNT.Conhecimento.SQL.Add('Select * From Cnt_Conhecimento'); DM_CNT.Conhecimento.SQL.Add('Where Codigo = :xUnidade'); DM_CNT.Conhecimento.SQL.Add('and Serie = :xSerie'); DM_CNT.Conhecimento.SQL.Add('and Numero = :xNumero'); DM_CNT.Conhecimento.Params[0].AsInteger:=StrToInt(lstChave3.Items.Strings[i]); DM_CNT.Conhecimento.Params[1].AsInteger:=StrToInt(lstChaveS2.Items.Strings[i]); DM_CNT.Conhecimento.Params[2].AsInteger:=StrToInt(lstChave4.Items.Strings[i]); DM_CNT.Conhecimento.Active:=True; DM_CNT.Conhecimento.Open; PathArquivo:=DMDFe.CTe.Configuracoes.Arquivos.GetPathCTe(DM_CNT.ConhecimentoEmissao.AsDateTime); NomeArquivo:=PathArquivo+'\'+DM_CNT.ConhecimentoChaveCTe.AsString+'-cte.xml'; // Carrega no Componente o CTe salvo em Arquivo XML DMDFe.CTe.Conhecimentos.Clear; DMDFe.CTe.Conhecimentos.LoadFromFile(NomeArquivo); MemoStatus.Lines.Add(' CT-e : '+Copy(DMDFe.CTe.Conhecimentos.Items[0].CTe.inFCTe.ID,4,44)); // Imprime o DACTe DMDFe.DACTe.CTeCancelada := (DM_CNT.ConhecimentoCancelado.AsString = 'S'); DMDFe.CTe.Conhecimentos.Imprimir; DMDFe.DACTe.CTeCancelada := False; if DM_CNT.ConhecimentoCancelado.AsString = 'S' then begin DM_CNT.Eventos.Close; DM_CNT.Eventos.SQL.Clear; DM_CNT.Eventos.SQL.Add('Select * From Cnt_Eventos'); DM_CNT.Eventos.SQL.Add('Where Codigo = :xUnidade'); DM_CNT.Eventos.SQL.Add('and Serie = :xSerie'); DM_CNT.Eventos.SQL.Add('and Numero = :xNumero'); DM_CNT.Eventos.SQL.Add('and Evento = :xEvento'); DM_CNT.Eventos.SQL.Add('Order By Sequencia'); DM_CNT.Eventos.Params[0].AsInteger:=StrToInt(lstChave3.Items.Strings[i]); DM_CNT.Eventos.Params[1].AsInteger:=StrToInt(lstChaveS2.Items.Strings[i]); DM_CNT.Eventos.Params[2].AsInteger:=StrToInt(lstChave4.Items.Strings[i]); DM_CNT.Eventos.Params[3].AsInteger:=110111; DM_CNT.Eventos.Active:=True; DM_CNT.Eventos.Open; DM_CNT.Eventos.Last; PathEvento := DMDFe.CTe.Configuracoes.Arquivos.GetPathEvento(teCancelamento, DM_CNT.EventosDataEvento.AsDateTime); NomeEvento := PathEvento + '\' + DM_CNT.ConhecimentoChaveCTe.AsString + '110111' + '01' + '-procEventoCTe.xml'; // Carrega no Componente o Evento salvo em Arquivo XML DMDFe.CTe.EventoCTe.Evento.Clear; DMDFe.CTe.EventoCTe.LerXML(NomeEvento); MemoStatus.Lines.Add(' Evento: ' + DM_CNT.ConhecimentoChaveCTe.AsString + '110111' + '01'); // Imprime o Evento DMDFe.CTe.ImprimirEvento; end; end; MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' => Impressão Concluida'); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); end else MessageDlg('Nenhum Conhecimento Selecionado.', mtInformation, [mbOk], 0); end; // ============================================================================= // // Conhecimentos não Protocolados // // ============================================================================= procedure TfrmMovEmitirCNT.NaoProtocolados; var PathArquivo, NomeArquivo, sRemetente, xProtocoloCanc, xMotivoCanc: String; dhDataCanc: TDateTime; i, j, cStatusAtual: Integer; begin Mensagem:=TStringList.Create; sCC:=TStringList.Create; DMDFe.DACTe.NumCopias:=edtNumCopias.ValueInt; DM_CTA.Empresa.Close; DM_CTA.Empresa.SQL.Clear; DM_CTA.Empresa.SQL.Add('Select * From Sis_Empresa'); DM_CTA.Empresa.SQL.Add('Where Codigo = :xCodigo'); DM_CTA.Empresa.Params[0].AsString:=DM_CTA.ParamDFeEmitente.AsString; DM_CTA.Empresa.Active:=True; DM_CTA.Empresa.Open; j:=lstChave3.Items.Count-1; if (j>=0) // and Selecionado then begin pbProgresso.Min:=0; pbProgresso.Max:=j; for i:=pbProgresso.Min to pbProgresso.Max do begin pbProgresso.Position:=i; DM_CNT.Conhecimento.Close; DM_CNT.Conhecimento.SQL.Clear; DM_CNT.Conhecimento.SQL.Add('Select * From Cnt_Conhecimento'); DM_CNT.Conhecimento.SQL.Add('Where Codigo = :xUnidade'); DM_CNT.Conhecimento.SQL.Add('and Serie = :xSerie'); DM_CNT.Conhecimento.SQL.Add('and Numero = :xNumero'); DM_CNT.Conhecimento.Params[0].AsInteger:=StrToInt(lstChave3.Items.Strings[i]); DM_CNT.Conhecimento.Params[1].AsInteger:=StrToInt(lstChaveS2.Items.Strings[i]); DM_CNT.Conhecimento.Params[2].AsInteger:=StrToInt(lstChave4.Items.Strings[i]); DM_CNT.Conhecimento.Active:=True; DM_CNT.Conhecimento.Open; PathArquivo:=DMDFe.CTe.Configuracoes.Arquivos.GetPathCTe(DM_CNT.ConhecimentoEmissao.AsDateTime); NomeArquivo:=PathArquivo+'\'+DM_CNT.ConhecimentoChaveCTe.AsString+'-cte.xml'; // Carrega no Componente o CTe salvo em Arquivo XML DMDFe.CTe.Conhecimentos.Clear; DMDFe.CTe.Conhecimentos.LoadFromFile(NomeArquivo); MemoStatus.Lines.Add('XML:'); MemoStatus.Lines.Add(' Carregado'); MemoStatus.Lines.Add(' CT-e: '+Copy(DMDFe.CTe.Conhecimentos.Items[0].CTe.inFCTe.ID,4,44)); DMDFe.CTe.Consultar; cStatusAtual := DMDFe.CTe.WebServices.Consulta.cStat; xProtocoloCanc := DMDFe.CTe.WebServices.Consulta.Protocolo; xMotivoCanc := DMDFe.CTe.WebServices.Consulta.XMotivo; dhDataCanc := DMDFe.CTe.WebServices.Consulta.DhRecbto; DMDFe.CTe.Conhecimentos.Clear; DMDFe.CTe.Conhecimentos.LoadFromFile(NomeArquivo); DM_CNT.IncAltDel.SQL.Clear; DM_CNT.IncAltDel.SQL.Add('Update Cnt_Conhecimento Set'); DM_CNT.IncAltDel.SQL.Add('Emissao = :xEmissao,'); DM_CNT.IncAltDel.SQL.Add('ChaveCTe = :xChave,'); DM_CNT.IncAltDel.SQL.Add('Enviado = :xEnviado,'); DM_CNT.IncAltDel.SQL.Add('Protocolado = :xProtocolado,'); DM_CNT.IncAltDel.SQL.Add('Protocolo = :xProtocolo,'); DM_CNT.IncAltDel.SQL.Add('Impresso = :xImpresso,'); DM_CNT.IncAltDel.SQL.Add('cStatus = :xcStatus,'); DM_CNT.IncAltDel.SQL.Add('xMotivo = :xxMotivo,'); DM_CNT.IncAltDel.SQL.Add('Anulado = :xAnulado,'); DM_CNT.IncAltDel.SQL.Add('Substituido = :xSubstituido,'); DM_CNT.IncAltDel.SQL.Add('Complementado = :xComplementado,'); DM_CNT.IncAltDel.SQL.Add('SerieMDFe = :xSerieMDFe,'); DM_CNT.IncAltDel.SQL.Add('NumeroMDFe = :xNumeroMDFe'); DM_CNT.IncAltDel.SQL.Add('Where Codigo = :xCodigo'); DM_CNT.IncAltDel.SQL.Add('and Numero = :xNumero'); DM_CNT.IncAltDel.SQL.Add('and Serie = :xSerie'); DM_CNT.IncAltDel.Params[0].AsDateTime:=DateOf(DMDFe.CTe.Conhecimentos.Items[0].CTe.Ide.dhEmi); DM_CNT.IncAltDel.Params[1].AsString:=Copy(DMDFe.CTe.Conhecimentos.Items[0].CTe.inFCTe.ID,4,44); DM_CNT.IncAltDel.Params[2].AsString:='S'; if DMDFe.CTe.Conhecimentos.Items[0].CTe.procCTe.cStat=100 then begin DM_CNT.IncAltDel.Params[3].AsString:='S'; DM_CNT.IncAltDel.Params[4].AsString:=DMDFe.CTe.Conhecimentos.Items[0].CTe.procCTe.nProt; end else begin DM_CNT.IncAltDel.Params[3].AsString:='N'; DM_CNT.IncAltDel.Params[4].AsString:=''; end; DM_CNT.IncAltDel.Params[5].AsString:='S'; DM_CNT.IncAltDel.Params[6].AsInteger:=DMDFe.CTe.Conhecimentos.Items[0].CTe.procCTe.cStat; // cStatus DM_CNT.IncAltDel.Params[7].AsString:=DMDFe.CTe.Conhecimentos.Items[0].CTe.procCTe.xMotivo; // xMotivo DM_CNT.IncAltDel.Params[8].AsString:='N'; // Anulado DM_CNT.IncAltDel.Params[9].AsString:='N'; // Substituido DM_CNT.IncAltDel.Params[10].AsString:='N'; // Complementado DM_CNT.IncAltDel.Params[11].AsInteger:=0; // Serie MDFe DM_CNT.IncAltDel.Params[12].AsInteger:=0; // Numero MDFe DM_CNT.IncAltDel.Params[13].AsInteger:=StrToInt(lstChave3.Items.Strings[i]); DM_CNT.IncAltDel.Params[14].AsInteger:=StrToInt(lstChave4.Items.Strings[i]); DM_CNT.IncAltDel.Params[15].AsInteger:=DMDFe.CTe.Conhecimentos.Items[0].CTe.Ide.serie; DM_CNT.IncAltDel.ExecSQL; if cStatusAtual = 101 // Cancelado then begin 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.SQL.Add('and Serie = :xSerie'); DM_CNT.IncAltDel.Params[0].AsString:='S'; DM_CNT.IncAltDel.Params[1].AsString:=xProtocoloCanc; DM_CNT.IncAltDel.Params[2].AsString:=xMotivoCanc; DM_CNT.IncAltDel.Params[3].AsInteger:=StrToInt(lstChave3.Items.Strings[i]); DM_CNT.IncAltDel.Params[4].AsInteger:=StrToInt(lstChave4.Items.Strings[i]); DM_CNT.IncAltDel.Params[5].AsInteger:=DMDFe.CTe.Conhecimentos.Items[0].CTe.Ide.serie; DM_CNT.IncAltDel.ExecSQL; end; MemoStatus.Lines.Add(' Aguarde a Impressão...'); DMDFe.DACTe.CTeCancelada := (cStatusAtual = 101); DMDFe.CTe.Conhecimentos.Imprimir; DM_CTA.PessoaFJE.Close; DM_CTA.PessoaFJE.SQL.Clear; DM_CTA.PessoaFJE.SQL.Add('Select * From Sis_PessoaFJEmail'); DM_CTA.PessoaFJE.SQL.Add('Where CNPJ = :xCNPJ'); DM_CTA.PessoaFJE.SQL.Add('and Modulo = :xModulo'); DM_CTA.PessoaFJE.Params[0].AsString:=DM_CNT.ConhecimentoPagador.AsString; DM_CTA.PessoaFJE.Params[1].AsString:='DFE'; DM_CTA.PessoaFJE.Active:=True; DM_CTA.PessoaFJE.Open; if (DM_CTA.PessoaFJECNPJ.AsString=DM_CNT.ConhecimentoPagador.AsString) and (DM_CTA.PessoaFJEModulo.AsString='DFE') then Endereco:=Trim(DM_CTA.PessoaFJEEmail.AsString) else Endereco:=''; if Endereco<>'' then begin Mensagem.Clear; sCC.Clear; Mensagem.Add('Comunicamos por este e-mail que o Conhecimento Eletrônico (Chave='+ Copy(DMDFe.CTe.Conhecimentos.Items[0].CTe.inFCTe.ID,4,44)+ ') obteve autorização de uso sob o protocolo: '+DMDFe.CTe.Conhecimentos.Items[0].CTe.procCTe.nProt); Mensagem.Add(' '); Mensagem.Add('Enviamos em anexo o arquivo XML do respectivo CT-e.'); Mensagem.Add(' '); Mensagem.Add('Atenciosamente,'); Mensagem.Add(' '); Mensagem.Add(DM_CTA.EmpresaNome.AsString); Mensagem.Add(' '); Mensagem.Add('>>> Sistema ERPJr <<<'); if Trim(DM_CTA.EmpresaEmail.AsString)<>'' then sCC.Add(Trim(DM_CTA.EmpresaEmail.AsString)); if DMDFe.CopiaPara<>'' then sCC.Add(DMDFe.CopiaPara); DM_CTA.TabHost.Close; DM_CTA.TabHost.SQL.Clear; DM_CTA.TabHost.SQL.Add('Select * From Sis_TabHost'); DM_CTA.TabHost.SQL.Add('Where Empresa = :xEmpresa'); DM_CTA.TabHost.SQL.Add('Order By Sequencia'); DM_CTA.TabHost.Params[0].AsString:=DM_CTA.xCodEmpresa; DM_CTA.TabHost.Active:=True; DM_CTA.TabHost.Open; DM_CTA.TabHost.Last; jHost:=DM_CTA.TabHost.RecordCount; iHost:=1; DM_CTA.TabHost.First; Ok:=False; Falha:=False; MemoStatus.Lines.Add(' CT-e: '+Copy(DMDFe.CTe.Conhecimentos.Items[0].CTe.inFCTe.ID,4,44)); MemoStatus.Lines.Add(' Destinatário: ' + Endereco); repeat MemoStatus.Lines.Add(' Aguarde... Enviando CT-e por e-mail. Host ['+ IntToStr(iHost)+'] '); if DM_CTA.TabHostRequerConta.AsString='S' then sRemetente := Trim(DM_CTA.TabHostContaemail.AsString) else sRemetente := Trim(DM_CTA.EmpresaEmail.AsString); try DMDFe.CTe.Conhecimentos.Items[0].EnviarEmail(DM_CTA.TabHostServidorSMTP.AsString, DM_CTA.TabHostPortaSMTP.AsString, DM_CTA.TabHostUsuario.AsString, DM_CTA.TabHostSenha.AsString, sRemetente, Endereco, 'CT-e', Mensagem, DMDFe.ConexaoSSL, // SSL DMDFe.EnviaPDF, // Enviar em PDF sCC, // sCC nil, // Anexos DMDFe.PedeConfirmacao, // Pede Confirmação de Recebimento DMDFe.AguardarEnvio, // Aguarda o Envio DM_CTA.EmpresaNome.AsString, // Nome do remetente DMDFe.ConexaoTLS, DMDFe.UsarThread); Ok:=True; except DM_CTA.TabHost.Next; Inc(iHost); if iHost>jHost then Falha:=True; end; until Ok or Falha; if Falha then MessageDlg('e-mail não enviado por motivo de Falha.', mtError, [mbOk], 0); end else MemoStatus.Lines.Add(' Endereço: Tomador do Serviço não possui, e-mail Cadastrado.'); DMDFe.DACTe.CTeCancelada := False; end; if Ok then begin MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' e-mail(s) enviado(s) com Sucesso.'); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); end; end else MessageDlg('Nenhum Conhecimento Selecionado.', mtInformation, [mbOk], 0); if (DM_CTA.ParamDFeResumo.AsString='S') // and (PrintDialog.Execute) then MemoStatus.Print(Texto); Mensagem.Free; sCC.Free; end; // ============================================================================= // // Conhecimentos não Protocolados por motivo de Contingencia // // ============================================================================= procedure TfrmMovEmitirCNT.NaoProtocoladosContingencia; var i, j, Lote, CTeInicial, CTeFinal: Integer; PathArquivo, NomeArquivo: String; begin DM_CTA.Empresa.Close; DM_CTA.Empresa.SQL.Clear; DM_CTA.Empresa.SQL.Add('Select * From Sis_Empresa'); DM_CTA.Empresa.SQL.Add('Where Codigo = :xCodigo'); DM_CTA.Empresa.Params[0].AsString:=DM_CTA.ParamDFeEmitente.AsString; DM_CTA.Empresa.Active:=True; DM_CTA.Empresa.Open; DMDFe.CTe.Conhecimentos.Clear; // Lote de Conhecimentos DM_CNT.LoteCTe.Close; DM_CNT.LoteCTe.SQL.Clear; DM_CNT.LoteCTe.SQL.Add('Select * From Cnt_LoteCTe'); DM_CNT.LoteCTe.SQL.Add('Order By Lote'); DM_CNT.LoteCTe.Active:=True; DM_CNT.LoteCTe.Open; DM_CNT.LoteCTe.Last; if DM_CNT.LoteCTe.RecordCount>0 then begin if (DM_CNT.LoteCTeCTInicial.AsInteger=0) and (DM_CNT.LoteCTeCTFinal.AsInteger=0) then Lote:=DM_CNT.LoteCTeLote.AsInteger else begin Lote:=(DM_CNT.LoteCTeLote.AsInteger+1); DM_CNT.IncAltDel.SQL.Clear; DM_CNT.IncAltDel.SQL.Add('Insert into Cnt_LoteCTe'); DM_CNT.IncAltDel.SQL.Add('(Lote, CTInicial, CTFinal, Data)'); DM_CNT.IncAltDel.SQL.Add('Values (:xLote, :xCTInicial, :xCTFinal, :xData)'); DM_CNT.IncAltDel.Params[0].AsInteger:=Lote; DM_CNT.IncAltDel.Params[1].AsInteger:=0; DM_CNT.IncAltDel.Params[2].AsInteger:=0; DM_CNT.IncAltDel.Params[3].AsDateTime:=Date; DM_CNT.IncAltDel.ExecSQL; end; end else begin Lote:=1; DM_CNT.IncAltDel.SQL.Clear; DM_CNT.IncAltDel.SQL.Add('Insert into Cnt_LoteCTe'); DM_CNT.IncAltDel.SQL.Add('(Lote, CTInicial, CTFinal, Data)'); DM_CNT.IncAltDel.SQL.Add('Values (:xLote, :xCTInicial, :xCTFinal, :xData)'); DM_CNT.IncAltDel.Params[0].AsInteger:=Lote; DM_CNT.IncAltDel.Params[1].AsInteger:=0; DM_CNT.IncAltDel.Params[2].AsInteger:=0; DM_CNT.IncAltDel.Params[3].AsDateTime:=Date; DM_CNT.IncAltDel.ExecSQL; end; j:=lstChave3.Items.Count-1; // Caso a quantidade de Conhecimentos não impressos seja superior o // limite estabelecido pelo SEFAZ (50 Conhecimentos por lote) a // geração dos CT-e ficara limitada nos 50 primeiros, portanto novos // lotes deverão ser gerados até compreender todos os Conhecimentos não // impressas. if j>LimiteCTe then j:=LimiteCTe-1; pbProgresso.Min:=0; pbProgresso.Max:=j; if (j>=0) and Selecionado then begin CTeInicial:=0; CTeFinal:=0; pbProgresso.Min:=0; pbProgresso.Max:=j; MemoStatus.Lines.Add('XML:'); MemoStatus.Lines.Add(' Carregado'); for i:=pbProgresso.Min to pbProgresso.Max do begin pbProgresso.Position:=i; DM_CNT.Conhecimento.Close; DM_CNT.Conhecimento.SQL.Clear; DM_CNT.Conhecimento.SQL.Add('Select * From Cnt_Conhecimento'); DM_CNT.Conhecimento.SQL.Add('Where Codigo = :xUnidade'); DM_CNT.Conhecimento.SQL.Add('and Serie = :xSerie'); DM_CNT.Conhecimento.SQL.Add('and Numero = :xNumero'); DM_CNT.Conhecimento.Params[0].AsInteger:=StrToInt(lstChave3.Items.Strings[i]); DM_CNT.Conhecimento.Params[1].AsInteger:=StrToInt(lstChaveS2.Items.Strings[i]); DM_CNT.Conhecimento.Params[2].AsInteger:=StrToInt(lstChave4.Items.Strings[i]); DM_CNT.Conhecimento.Active:=True; DM_CNT.Conhecimento.Open; if CTeInicial=0 then CTeInicial:=DM_CNT.ConhecimentoNumero.AsInteger; CTeFinal:=DM_CNT.ConhecimentoNumero.AsInteger; PathArquivo:=DMDFe.CTe.Configuracoes.Arquivos.GetPathCTe(DM_CNT.ConhecimentoEmissao.AsDateTime); NomeArquivo:=PathArquivo+'\'+DM_CNT.ConhecimentoChaveCTe.AsString+'-cte.xml'; // Carrega no Componente o CTe salvo em Arquivo XML DMDFe.CTe.Conhecimentos.LoadFromFile(NomeArquivo); MemoStatus.Lines.Add(' CT-e: '+Copy(DMDFe.CTe.Conhecimentos.Items[i].CTe.inFCTe.ID,4,44)); end; EnviarLote(Lote); DM_CNT.IncAltDel.SQL.Clear; DM_CNT.IncAltDel.SQL.Add('Update Cnt_LoteCTe Set'); DM_CNT.IncAltDel.SQL.Add('CTInicial = :xCTInicial,'); DM_CNT.IncAltDel.SQL.Add('CTFinal = :xCTFinal,'); DM_CNT.IncAltDel.SQL.Add('Data = :xData'); DM_CNT.IncAltDel.SQL.Add('Where Lote = :xLote'); DM_CNT.IncAltDel.Params[0].AsInteger:=CTeInicial; DM_CNT.IncAltDel.Params[1].AsInteger:=CTeFinal; DM_CNT.IncAltDel.Params[2].AsDateTime:=Date; DM_CNT.IncAltDel.Params[3].AsInteger:=Lote; DM_CNT.IncAltDel.ExecSQL; if DMDFe.EnviaEmail then EnviarEmail; end else MessageDlg('Nenhum Conhecimento Selecionado.', mtInformation, [mbOk], 0); if (DM_CTA.ParamDFeResumo.AsString='S') // and (PrintDialog.Execute) then MemoStatus.Print(Texto); end; procedure TfrmMovEmitirCNT.btnImprimirClick(Sender: TObject); begin DMDFe.DACTe.NumCopias:=edtNumCopias.ValueInt; MontaListadosSelecionados; DMDFe.DACTe.MostrarPreview:=chkVisualizar.Checked; case rgCondicao.ItemIndex of 0: NaoEmitidos; 1: Emitidos; 2: NaoProtocolados; 3: NaoProtocoladosContingencia; end; LimpaCampos; end; procedure TfrmMovEmitirCNT.btnLimparClick(Sender: TObject); begin MemoStatus.Clear; MemoStatus.Lines.Clear; LimpaCampos; end; procedure TfrmMovEmitirCNT.btnEmailClick(Sender: TObject); var PathArquivo, NomeArquivo: String; i, j: Integer; begin MontaListadosSelecionados; j:=lstChave3.Items.Count-1; DMDFe.CTe.Conhecimentos.Clear; if (j>=0) and Selecionado then begin pbProgresso.Min:=0; pbProgresso.Max:=j; MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' => Enviando Conhecimento(s) por e-mail'); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add('XML:'); MemoStatus.Lines.Add(' Carregado'); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); for i:=pbProgresso.Min to pbProgresso.Max do begin pbProgresso.Position:=i; DM_CNT.Conhecimento.Close; DM_CNT.Conhecimento.SQL.Clear; DM_CNT.Conhecimento.SQL.Add('Select * From Cnt_Conhecimento'); DM_CNT.Conhecimento.SQL.Add('Where Codigo = :xUnidade'); DM_CNT.Conhecimento.SQL.Add('and Serie = :xSerie'); DM_CNT.Conhecimento.SQL.Add('and Numero = :xNumero'); DM_CNT.Conhecimento.Params[0].AsInteger:=StrToInt(lstChave3.Items.Strings[i]); DM_CNT.Conhecimento.Params[1].AsInteger:=StrToInt(lstChaveS2.Items.Strings[i]); DM_CNT.Conhecimento.Params[2].AsInteger:=StrToInt(lstChave4.Items.Strings[i]); DM_CNT.Conhecimento.Active:=True; DM_CNT.Conhecimento.Open; PathArquivo:=DMDFe.CTe.Configuracoes.Arquivos.GetPathCTe(DM_CNT.ConhecimentoEmissao.AsDateTime); NomeArquivo:=PathArquivo+'\'+DM_CNT.ConhecimentoChaveCTe.AsString+'-cte.xml'; DMDFe.CTe.Conhecimentos.LoadFromFile(NomeArquivo); MemoStatus.Lines.Add(' CT-e: '+Copy(DMDFe.CTe.Conhecimentos.Items[i].CTe.inFCTe.ID,4,44)); end; if DMDFe.EnviaEmail then EnviarEmail; end else MessageDlg('Nenhum Conhecimento Selecionado.', mtInformation, [mbOk], 0); if (DM_CTA.ParamDFeResumo.AsString='S') // and (PrintDialog.Execute) then MemoStatus.Print(Texto); LimpaCampos; end; procedure TfrmMovEmitirCNT.btnConsultaStatusClick(Sender: TObject); var sAmbiente: String; begin DMDFe.CTe.WebServices.StatusServico.Executar; sAmbiente := trim(TpAmbToStr( DMDFe.CTe.WebServices.StatusServico.tpAmb )); if sAmbiente = '1' then sAmbiente := sAmbiente + ' - Produção' else sAmbiente := sAmbiente + ' - Homologação'; MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' => Consultando Status do Serviço'); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' Ambiente : ' + sAmbiente); MemoStatus.Lines.Add(' Versão Aplicativo : ' + DMDFe.CTe.WebServices.StatusServico.verAplic); MemoStatus.Lines.Add(' Status Código : ' + IntToStr( DMDFe.CTe.WebServices.StatusServico.cStat )); MemoStatus.Lines.Add(' Status Descrição : ' + DMDFe.CTe.WebServices.StatusServico.xMotivo); MemoStatus.Lines.Add(' UF : ' + CodigoParaUF( DMDFe.CTe.WebServices.StatusServico.cUF )); MemoStatus.Lines.Add(' Recebimento : ' + DFeUtil.SeSenao(DMDFe.CTe.WebServices.StatusServico.dhRecbto = 0, '', DateTimeToStr(DMDFe.CTe.WebServices.StatusServico.dhRecbto))); MemoStatus.Lines.Add(' Tempo Médio : ' + IntToStr( DMDFe.CTe.WebServices.StatusServico.TMed )); MemoStatus.Lines.Add(' Retorno : ' + DFeUtil.SeSenao(DMDFe.CTe.WebServices.StatusServico.dhRetorno = 0, '', DateTimeToStr(DMDFe.CTe.WebServices.StatusServico.dhRetorno))); MemoStatus.Lines.Add(' Observação : ' + DMDFe.CTe.WebServices.StatusServico.xObs); MemoStatus.Lines.Add('-------------------------------------------------------------------------------------------'); MemoStatus.Lines.Add(' '); end; procedure TfrmMovEmitirCNT.btnInformacoesClick(Sender: TObject); begin frmACBrCTe.ShowModal; end; end.