procedure TfrmMovManifesto.GerarLoteMDFe; var i, j, k, l: Integer; begin with DMDFe.MDFe.Manifestos.Add.MDFe do begin // // Dados de Identificação do MDF-e // Ide.cUF := DM_CTA.EmpresaCodigoEstado.AsInteger; // TpcnTipoAmbiente = (taProducao, taHomologacao); case DM_CTA.ParamDFeWSAmbiente.AsInteger of 0: Ide.tpAmb := taHomologacao; 1: Ide.tpAmb := taProducao; end; // TMDFeTpEmitente = ( teTransportadora, teTranspCargaPropria ); if TipoDoc = 1 then Ide.tpEmit := teTransportadora else Ide.tpEmit := teTranspCargaPropria; Ide.modelo := '58'; Ide.serie := DM_MDF.ManifestoSerie.AsInteger; Ide.nMDF := DM_MDF.ManifestoNumero.AsInteger; Ide.cMDF := DM_MDF.ManifestoCodigo.AsInteger; // Código Aleatório // TMDFeModal = ( moRodoviario, moAereo, moAquaviario, moFerroviario ); Ide.modal := moRodoviario; Ide.dhEmi := Now; // TpcnTipoEmissao = (teNormal, teContingencia, teSCAN, teDPEC, teFSDA); case rgTipoEmissao.ItemIndex of 0: Ide.tpEmis := teNormal; 1: Ide.tpEmis := teFSDA; end; // TpcnProcessoEmissao = (peAplicativoContribuinte, peAvulsaFisco, peAvulsaContribuinte, peContribuinteAplicativoFisco); Ide.procEmi := peAplicativoContribuinte; Ide.verProc := '4.0'; Ide.UFIni := DM_MDF.ManifestoUFInicio.AsString; Ide.UFFim := DM_MDF.ManifestoUFFim.AsString; // ======== Dados do Carregamento =========================================== DM_MDF.Carrega.Close; DM_MDF.Carrega.SQL.Clear; DM_MDF.Carrega.SQL.Add('Select * From MDF_CarrDesc'); DM_MDF.Carrega.SQL.Add('Where Modulo = :xModulo'); DM_MDF.Carrega.SQL.Add('and Empresa = :xEmpresa'); DM_MDF.Carrega.SQL.Add('and Serie = :xSerie'); DM_MDF.Carrega.SQL.Add('and Numero = :xNumero'); DM_MDF.Carrega.SQL.Add('and Tipo = :xTipo'); DM_MDF.Carrega.Params[0].AsString:=DM_CTA.xModulo; DM_MDF.Carrega.Params[1].AsString:=DM_CTA.xCodEmpresa; DM_MDF.Carrega.Params[2].AsInteger:=DM_MDF.ManifestoSerie.AsInteger; DM_MDF.Carrega.Params[3].AsInteger:=DM_MDF.ManifestoNumero.AsInteger; DM_MDF.Carrega.Params[4].AsInteger:=0; DM_MDF.Carrega.Active:=True; DM_MDF.Carrega.Open; DM_MDF.Carrega.Last; j := DM_MDF.Carrega.RecordCount -1; if j>=0 then begin DM_MDF.Carrega.First; for i:=0 to j do begin with Ide.infMunCarrega.Add do begin cMunCarrega := DM_MDF.CarregaCodMun.AsInteger; xMunCarrega := DM_MDF.CarregaDescMun.AsString; end; DM_MDF.Carrega.Next; end; end; // ======== Dados do Percurso =========================================== DM_MDF.Percursos.Close; DM_MDF.Percursos.SQL.Clear; DM_MDF.Percursos.SQL.Add('Select * From MDF_Percursos'); DM_MDF.Percursos.SQL.Add('Where Modulo = :xModulo'); DM_MDF.Percursos.SQL.Add('and Empresa = :xEmpresa'); DM_MDF.Percursos.SQL.Add('and Serie = :xSerie'); DM_MDF.Percursos.SQL.Add('and Numero = :xNumero'); DM_MDF.Percursos.SQL.Add('Order By Ordem'); DM_MDF.Percursos.Params[0].AsString:=DM_CTA.xModulo; DM_MDF.Percursos.Params[1].AsString:=DM_CTA.xCodEmpresa; DM_MDF.Percursos.Params[2].AsInteger:=DM_MDF.ManifestoSerie.AsInteger; DM_MDF.Percursos.Params[3].AsInteger:=DM_MDF.ManifestoNumero.AsInteger; DM_MDF.Percursos.Active:=True; DM_MDF.Percursos.Open; DM_MDF.Percursos.Last; j := DM_MDF.Percursos.RecordCount -1; if j>=0 then begin DM_MDF.Percursos.First; for i:=0 to j do begin with Ide.infPercurso.Add do begin UFPer := DM_MDF.PercursosUFPerc.AsString; end; DM_MDF.Percursos.Next; 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 := Trim(DM_CTA.EmpresaNome.AsString); Emit.xFant := Trim(DM_CTA.EmpresaFantasia.AsString); Emit.EnderEmit.xLgr := Trim(DM_CTA.EmpresaEndereco.AsString); Emit.EnderEmit.nro := Trim(DM_CTA.EmpresaNumero.AsString); Emit.EnderEmit.xCpl := Trim(DM_CTA.EmpresaComplemento.AsString); Emit.EnderEmit.xBairro := Trim(DM_CTA.EmpresaBairro.AsString); Emit.EnderEmit.cMun := DM_CTA.EmpresaCodigoEstado.AsInteger * 100000 + DM_CTA.EmpresaCodigoMunicipio.AsInteger; Emit.EnderEmit.xMun := Trim(DM_CTA.EmpresaCidade.AsString); Emit.EnderEmit.UF := DM_CTA.EmpresaEstado.AsString; Emit.EnderEmit.CEP := StrToIntDef(Trim(DM_CTA.EmpresaCEP.AsString), 0); Emit.EnderEmit.Fone := Trim(DM_CTA.EmpresaTelefone.AsString); Emit.enderEmit.email := Trim(DM_CTA.EmpresaEmail.AsString); // Dados do Modal Rodoviário rodo.RNTRC := DM_CTA.ParamDFeRNTRC.AsString; rodo.CIOT := ''; // ======== Dados dos Veículos =========================================== DM_MDF.Veiculos.Close; DM_MDF.Veiculos.SQL.Clear; DM_MDF.Veiculos.SQL.Add('Select * From MDF_Veiculos'); DM_MDF.Veiculos.SQL.Add('Where Modulo = :xModulo'); DM_MDF.Veiculos.SQL.Add('and Empresa = :xEmpresa'); DM_MDF.Veiculos.SQL.Add('and Serie = :xSerie'); DM_MDF.Veiculos.SQL.Add('and Numero = :xNumero'); DM_MDF.Veiculos.SQL.Add('Order By Item'); DM_MDF.Veiculos.Params[0].AsString:=DM_CTA.xModulo; DM_MDF.Veiculos.Params[1].AsString:=DM_CTA.xCodEmpresa; DM_MDF.Veiculos.Params[2].AsInteger:=DM_MDF.ManifestoSerie.AsInteger; DM_MDF.Veiculos.Params[3].AsInteger:=DM_MDF.ManifestoNumero.AsInteger; DM_MDF.Veiculos.Active:=True; DM_MDF.Veiculos.Open; DM_MDF.Veiculos.Last; j := DM_MDF.Veiculos.RecordCount -1; if j>=0 then begin DM_MDF.Veiculos.First; for i:=0 to j do 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_MDF.VeiculosCodigo.AsInteger; DM_CNT.Frota.Active:=True; DM_CNT.Frota.Open; // Dados do Veiculo Principal if DM_MDF.VeiculosItem.AsInteger = 0 then begin rodo.veicTracao.cInt := IntToStr(DM_MDF.VeiculosCodigo.AsInteger); rodo.veicTracao.placa := DM_MDF.VeiculosPlaca.AsString; rodo.veicTracao.tara := DM_MDF.VeiculosTara.AsInteger; rodo.veicTracao.capKG := DM_MDF.VeiculosCapKg.AsInteger; rodo.veicTracao.capM3 := DM_MDF.VeiculosCapM3.AsInteger; rodo.veicTracao.UF := DM_CNT.FrotaUF.AsString; if DM_CNT.FrotaProprietario.AsString = 'N' then begin rodo.veicTracao.prop.CNPJCPF := DM_CNT.FrotaCNPJProp.AsString; rodo.veicTracao.prop.RNTRC := DM_CNT.FrotaRNTRC.AsString; 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; rodo.veicTracao.prop.xNome := DM_CTA.PessoaFJRSocial.AsString; rodo.veicTracao.prop.IE := DM_CTA.PessoaFJIEstadual.AsString; rodo.veicTracao.prop.UF := DM_CTA.PessoaFJEstado.AsString; // TpcteProp = (tpTACAgregado, tpTACIndependente, tpOutros); case DM_CNT.FrotaTipoProp.AsInteger of 0: rodo.veicTracao.prop.tpProp := tpTACAgregado; 1: rodo.veicTracao.prop.tpProp := tpTACIndependente; 2: rodo.veicTracao.prop.tpProp := tpOutros; end; end; // TpcteTipoRodado = (trNaoAplicavel, trTruck, trToco, trCavaloMecanico, trVAN, trUtilitario, trOutros); case DM_CNT.FrotaTipoRodado.AsInteger of 0: rodo.veicTracao.tpRod := trNaoAplicavel; 1: rodo.veicTracao.tpRod := trTruck; 2: rodo.veicTracao.tpRod := trToco; 3: rodo.veicTracao.tpRod := trCavaloMecanico; 4: rodo.veicTracao.tpRod := trVAN; 5: rodo.veicTracao.tpRod := trUtilitario; 6: rodo.veicTracao.tpRod := trOutros; end; // TpcteTipoCarroceria = (tcNaoAplicavel, tcAberta, tcFechada, tcGraneleira, tcPortaContainer, tcSider); case DM_CNT.FrotaTipoCarroceria.AsInteger of 0: rodo.veicTracao.tpCar := tcNaoAplicavel; 1: rodo.veicTracao.tpCar := tcAberta; 2: rodo.veicTracao.tpCar := tcFechada; 3: rodo.veicTracao.tpCar := tcGraneleira; 4: rodo.veicTracao.tpCar := tcPortaContainer; 5: rodo.veicTracao.tpCar := tcSider; end; end else begin with rodo.veicReboque.Add do begin cInt := IntToStr(DM_MDF.VeiculosCodigo.AsInteger); placa := DM_MDF.VeiculosPlaca.AsString; tara := DM_MDF.VeiculosTara.AsInteger; capKG := DM_MDF.VeiculosCapKg.AsInteger; capM3 := DM_MDF.VeiculosCapM3.AsInteger; if DM_CNT.FrotaProprietario.AsString = 'N' then begin prop.CNPJCPF := DM_CNT.FrotaCNPJProp.AsString; prop.RNTRC := DM_CNT.FrotaRNTRC.AsString; 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; prop.xNome := DM_CTA.PessoaFJRSocial.AsString; prop.IE := DM_CTA.PessoaFJIEstadual.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; // 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; end; end; DM_MDF.Veiculos.Next; end; end; // ======== Dados dos Condutores =========================================== DM_MDF.Condutores.Close; DM_MDF.Condutores.SQL.Clear; DM_MDF.Condutores.SQL.Add('Select * From MDF_Condutores'); DM_MDF.Condutores.SQL.Add('Where Modulo = :xModulo'); DM_MDF.Condutores.SQL.Add('and Empresa = :xEmpresa'); DM_MDF.Condutores.SQL.Add('and Serie = :xSerie'); DM_MDF.Condutores.SQL.Add('and Numero = :xNumero'); DM_MDF.Condutores.SQL.Add('Order By Nome'); DM_MDF.Condutores.Params[0].AsString:=DM_CTA.xModulo; DM_MDF.Condutores.Params[1].AsString:=DM_CTA.xCodEmpresa; DM_MDF.Condutores.Params[2].AsInteger:=DM_MDF.ManifestoSerie.AsInteger; DM_MDF.Condutores.Params[3].AsInteger:=DM_MDF.ManifestoNumero.AsInteger; DM_MDF.Condutores.Active:=True; DM_MDF.Condutores.Open; DM_MDF.Condutores.Last; j := DM_MDF.Condutores.RecordCount -1; if j>=0 then begin DM_MDF.Condutores.First; for i:=0 to j do begin with rodo.veicTracao.condutor.Add do begin xNome := DM_MDF.CondutoresNome.AsString; CPF := DM_MDF.CondutoresCPF.AsString; end; DM_MDF.Condutores.Next; end; end; // ======== Dados dos Vale Pedagios ========================================= DM_MDF.ValePedagio.Close; DM_MDF.ValePedagio.SQL.Clear; DM_MDF.ValePedagio.SQL.Add('Select * From MDF_ValePedagio'); DM_MDF.ValePedagio.SQL.Add('Where Modulo = :xModulo'); DM_MDF.ValePedagio.SQL.Add('and Empresa = :xEmpresa'); DM_MDF.ValePedagio.SQL.Add('and Serie = :xSerie'); DM_MDF.ValePedagio.SQL.Add('and Numero = :xNumero'); DM_MDF.ValePedagio.Params[0].AsString:=DM_CTA.xModulo; DM_MDF.ValePedagio.Params[1].AsString:=DM_CTA.xCodEmpresa; DM_MDF.ValePedagio.Params[2].AsInteger:=DM_MDF.ManifestoSerie.AsInteger; DM_MDF.ValePedagio.Params[3].AsInteger:=DM_MDF.ManifestoNumero.AsInteger; DM_MDF.ValePedagio.Active:=True; DM_MDF.ValePedagio.Open; DM_MDF.ValePedagio.Last; j := DM_MDF.ValePedagio.RecordCount -1; if j>=0 then begin DM_MDF.ValePedagio.First; for i:=0 to j do begin with rodo.valePed.disp.Add do begin if copy(DM_MDF.ValePedagioCNPJFor.AsString, 10, 4) <> '0000' then CNPJForn := Copy(DM_MDF.ValePedagioCNPJFor.AsString, 2, 14) else CNPJForn := Copy(DM_MDF.ValePedagioCNPJFor.AsString, 1, 9) + Copy(DM_MDF.ValePedagioCNPJFor.AsString, 14, 2); if copy(DM_MDF.ValePedagioCNPJPag.AsString, 10, 4) <> '0000' then CNPJPg := Copy(DM_MDF.ValePedagioCNPJPag.AsString, 2, 14) else CNPJPg := Copy(DM_MDF.ValePedagioCNPJPag.AsString, 1, 9) + Copy(DM_MDF.ValePedagioCNPJPag.AsString, 14, 2); nCompra := DM_MDF.ValePedagioNumCompra.AsString; end; DM_MDF.ValePedagio.Next; end; end; // ======== Dados dos Descarregamentos ====================================== DM_MDF.Descarrega.Close; DM_MDF.Descarrega.SQL.Clear; DM_MDF.Descarrega.SQL.Add('Select * From MDF_CarrDesc'); DM_MDF.Descarrega.SQL.Add('Where Modulo = :xModulo'); DM_MDF.Descarrega.SQL.Add('and Empresa = :xEmpresa'); DM_MDF.Descarrega.SQL.Add('and Serie = :xSerie'); DM_MDF.Descarrega.SQL.Add('and Numero = :xNumero'); DM_MDF.Descarrega.SQL.Add('and Tipo = :xTipo'); DM_MDF.Descarrega.Params[0].AsString:=DM_CTA.xModulo; DM_MDF.Descarrega.Params[1].AsString:=DM_CTA.xCodEmpresa; DM_MDF.Descarrega.Params[2].AsInteger:=DM_MDF.ManifestoSerie.AsInteger; DM_MDF.Descarrega.Params[3].AsInteger:=DM_MDF.ManifestoNumero.AsInteger; DM_MDF.Descarrega.Params[4].AsInteger:=1; DM_MDF.Descarrega.Active:=True; DM_MDF.Descarrega.Open; DM_MDF.Descarrega.Last; j := DM_MDF.Descarrega.RecordCount -1; if j>=0 then begin DM_MDF.Descarrega.First; for i:=0 to j do begin // Local do Descarregamento with infDoc.infMunDescarga.Add do begin cMunDescarga := DM_MDF.DescarregaCodMun.AsInteger; xMunDescarga := DM_MDF.DescarregaDescMun.AsString; // Chaves dos Documentos DM_MDF.Documentos.Close; DM_MDF.Documentos.SQL.Clear; DM_MDF.Documentos.SQL.Add('Select * From MDF_Documentos'); DM_MDF.Documentos.SQL.Add('Where Modulo = :xModulo'); DM_MDF.Documentos.SQL.Add('and Empresa = :xEmpresa'); DM_MDF.Documentos.SQL.Add('and Serie = :xSerie'); DM_MDF.Documentos.SQL.Add('and Numero = :xNumero'); DM_MDF.Documentos.SQL.Add('Order By Item'); DM_MDF.Documentos.Params[0].AsString:=DM_CTA.xModulo; DM_MDF.Documentos.Params[1].AsString:=DM_CTA.xCodEmpresa; DM_MDF.Documentos.Params[2].AsInteger:=DM_MDF.ManifestoSerie.AsInteger; DM_MDF.Documentos.Params[3].AsInteger:=DM_MDF.ManifestoNumero.AsInteger; DM_MDF.Documentos.Active:=True; DM_MDF.Documentos.Open; DM_MDF.Documentos.Last; l := DM_MDF.Documentos.RecordCount -1; if l >=0 then begin DM_MDF.Documentos.First; for k:=0 to l do begin if DM_MDF.DescarregaCodMun.AsInteger = DM_MDF.DocumentosCodMunDesc.AsInteger then begin case TipoDoc of 1: begin // CTe with infCTe.Add do begin chCTe := DM_MDF.DocumentosChave.AsString; end; end; 3: begin // NFe with infNFe.Add do begin chNFe := DM_MDF.DocumentosChave.AsString; end; end; end; end; DM_MDF.Documentos.Next; end; end; end; DM_MDF.Descarrega.Next; end; end; // Totais case TipoDoc of 1: tot.qCTe := DM_MDF.ManifestoQtdeCTe.AsInteger; 3: tot.qNFe := DM_MDF.ManifestoQtdeNFe.AsInteger end; tot.vCarga := RoundTo(DM_MDF.ManifestoValorTotal.AsFloat, -2); // UnidMed = (uM3,uKG, uTON, uUNIDADE, uLITROS); if DM_MDF.ManifestoUnidade.AsInteger = 1 then tot.cUnid := uKg else tot.cUnid := uTon; tot.qCarga := RoundTo(DM_MDF.ManifestoPesoBruto.AsFloat, -4); // ======== Dados dos Lacres =========================================== DM_MDF.Lacres.Close; DM_MDF.Lacres.SQL.Clear; DM_MDF.Lacres.SQL.Add('Select * From MDF_Lacres'); DM_MDF.Lacres.SQL.Add('Where Modulo = :xModulo'); DM_MDF.Lacres.SQL.Add('and Empresa = :xEmpresa'); DM_MDF.Lacres.SQL.Add('and Serie = :xSerie'); DM_MDF.Lacres.SQL.Add('and Numero = :xNumero'); DM_MDF.Lacres.SQL.Add('Order By Item'); DM_MDF.Lacres.Params[0].AsString:=DM_CTA.xModulo; DM_MDF.Lacres.Params[1].AsString:=DM_CTA.xCodEmpresa; DM_MDF.Lacres.Params[2].AsInteger:=DM_MDF.ManifestoSerie.AsInteger; DM_MDF.Lacres.Params[3].AsInteger:=DM_MDF.ManifestoNumero.AsInteger; DM_MDF.Lacres.Active:=True; DM_MDF.Lacres.Open; DM_MDF.Lacres.Last; j := DM_MDF.Lacres.RecordCount -1; if j>=0 then begin DM_MDF.Lacres.First; for i:=0 to j do begin with lacres.Add do begin nLacre := DM_MDF.LacresLacre.AsString; end; DM_MDF.Lacres.Next; end; end; infAdic.infCpl := DM_MDF.ManifestoInfComplementar.AsString; infAdic.infAdFisco := DM_MDF.ManifestoInfFisco.AsString; end; end;