public static string sMontaStringConexao() { try { Globais LeRegWin = new Globais(); belGlobais MontaStringConexao = new belGlobais(); StringBuilder sbConexao = new StringBuilder(); sbConexao.Append("User ="******"SYSDBA"); sbConexao.Append(";"); sbConexao.Append("Password="******"masterkey"); sbConexao.Append(";"); sbConexao.Append("Database="); string sdatabase = LeRegWin.LeRegConfig("BancoDados"); sbConexao.Append(sdatabase); sbConexao.Append(";"); sbConexao.Append("DataSource="); sbConexao.Append(LeRegWin.LeRegConfig("Servidor")); sbConexao.Append(";"); sbConexao.Append("Port=3050;Dialect=3; Charset=NONE;Role=;Connection lifetime=15;Pooling=true; MinPoolSize=0;MaxPoolSize=2000;Packet Size=8192;ServerType=0;"); return (string)sbConexao.ToString(); } catch (Exception ex) { throw ex; } }
public belPopulaObjetos(string sEmp, List<string> objlConhec, string cUf, X509Certificate2 cert) { this.sEmp = sEmp; this.objlConhec = objlConhec; this.cUf = cUf; this.cert = cert; belGlobais objGlobais = new belGlobais(); sNomeArq = NomeArqCte(); sPath = util.RetCaminhoPastaPadrao("Envio") + @sNomeArq; }
private void btnEnvio_Click(object sender, EventArgs e) { try { objCriaXml = new belCriaXml(); bool bContingencia = false; if (!belStatic.bModoContingencia) { if (Operacao) { #region Verifica se Item Selecionado já foi enviado for (int i = 0; i < dgvArquivos.RowCount; i++) { if (dgvArquivos["cl_assina", i].Value != null) { if (dgvArquivos["cl_assina", i].Value.ToString().Equals("True")) { if (objGerais.VerificaCampoReciboPreenchido(belStatic.CodEmpresaCte, dgvArquivos["nr_lanc", i].Value.ToString()) != "") { throw new Exception("O Conhecimento de Sequência " + dgvArquivos["nr_lanc", i].Value.ToString() + " Já tem um recibo Salvo no Banco de Dados, tente Buscar Retorno."); } } } } #endregion lblStatus.Text = "Carregando Informações..."; #region Pega Notas Selecionadas na Grid string sCanceladas = ""; slistaConhec = new List<string>(); for (int i = 0; i < dgvArquivos.RowCount; i++) { try { if (dgvArquivos["cl_assina", i].Value != null) { if (((dgvArquivos["cl_assina", i].Value != null) && (dgvArquivos["cl_assina", i].Value.ToString().Equals("True"))) && ((dgvArquivos["ds_cancelamento", i].Value.ToString() == "")) && (dgvArquivos["st_cte", i].Value.ToString().Equals("0"))) { if (Convert.ToBoolean(dgvArquivos["st_cte", i].Value) == false && Convert.ToBoolean(dgvArquivos["st_contingencia", i].Value) == true) { bContingencia = true; if (slistaConhec.Count() > 0) { throw new Exception("Os Conhecimentos Pendentes devem ser Enviados um por vez."); } } slistaConhec.Add((string)dgvArquivos["nr_lanc", i].Value); } if (dgvArquivos["ds_cancelamento", i].Value.ToString() != "") { if (Convert.ToBoolean(dgvArquivos["cl_assina", i].Value.ToString()) == true) { sCanceladas += "Conhecimento de Transp. " + dgvArquivos["cd_conheci", i].Value.ToString() + " - Esta Cancelado e não é Permitido o Reenvio do mesmo!" + Environment.NewLine + Environment.NewLine; } } } } catch (Exception) { throw; } } #endregion if (slistaConhec.Count == 0) { KryptonMessageBox.Show("Nenhuma nota Válida foi Selecionada!", "A T E N Ç Ã O", MessageBoxButtons.OK, MessageBoxIcon.Error); if (sCanceladas != "") { throw new Exception(sCanceladas); } lblStatus.Text = ""; } else { if (!bContingencia) { #region Envio Normal //verifica no banco se as sequencias são existentes. List<string> objLGerarSeq = objGerais.ValidaSeqNoBanco(belStatic.CodEmpresaCte, slistaConhec); if (objLGerarSeq.Count > 0) { frmGerarNumeroCte objfrmGerarNum = new frmGerarNumeroCte(belStatic.CodEmpresaCte, objLGerarSeq); objfrmGerarNum.ShowDialog(); } cert = new X509Certificate2(); cert = belCertificadoDigital.BuscaNome(""); if (!belCertificadoDigital.ValidaCertificado(cert)) { lblStatus.Text = ""; throw new Exception("Certificado não Selecionado."); } #region Popula as Classes e abre form Visualização belPopulaObjetos objObjetos = new belPopulaObjetos(belStatic.CodEmpresaCte, slistaConhec, objbelUfEmp.CUF, cert); daoInfCte objdaoInfCte = new daoInfCte(); objdaoInfCte.ImportaConhecInfCte(objObjetos, belStatic.CodEmpresaCte); lblStatus.Text = "Aguardando Envio"; frmVisualizaCte objFrm = new frmVisualizaCte(objObjetos); objFrm.ShowDialog(); #endregion if (objFrm.bCancela) { lblStatus.Text = ""; throw new Exception("Envio do(s) Conhecimento(s) Cancelado"); } else { #region Envia Lote WebService lblStatus.Text = "Enviando Lote para WebService..."; daoGenerator objGerator = new daoGenerator(); int iNumLote = Convert.ToInt32(objGerator.RetornaProximoValorGenerator("GEN_LOTE_CTE")); objGravaDadosRetorno.GravarChave(objFrm.objObjetosAlter); string sRecibo = objCriaXml.GerarXml(objFrm.objObjetosAlter, iNumLote); List<belStatusCte> ListaStatus = objCriaXml.ConsultaLoteEnviado(sRecibo); // if (sRecibo != "") // sicupira { objGravaDadosRetorno.GravarRecibo(objFrm.objObjetosAlter, sRecibo); } foreach (belStatusCte cte in ListaStatus.Where(C => C.Enviado == true)) { if (cte.CodRetorno != "218" && cte.CodRetorno != "101" && cte.CodRetorno != "103" && cte.CodRetorno != "104" && cte.CodRetorno != "105" && cte.CodRetorno != "100" && cte.CodRetorno != "204") { objGravaDadosRetorno.ApagarRecibo(sRecibo); } else { objGravaDadosRetorno.GravarProtocoloEnvio(cte); } } KryptonMessageBox.Show(belTrataMensagem.RetornaMensagem(ListaStatus, belTrataMensagem.Tipo.Envio), "CT-e - Retorno WebService", MessageBoxButtons.OK, MessageBoxIcon.Information); objGerais = new daoBuscaDadosGerais(); foreach (belStatusCte cte in ListaStatus.Where(C => C.Enviado == true)) { lblStatus.Text = "Salvando Arquivos"; objGravaDadosRetorno.AlterarStatusCte(cte); string sChave = objGravaDadosRetorno.BuscaChave(cte.NumeroSeq); objCriaXml.SalvaArquivoPastaEnviado(objGerais.BuscaNumeroConhecimento(cte.NumeroSeq), sChave); if (cte.CodRetorno == "218" || cte.CodRetorno == "101") { objCriaXml.SalvaArquivoPastaCancelado(sChave); } } lblStatus.Text = ""; Pendencias = objGerais.VerificaPendenciasdeEnvio(); if (Pendencias.Count > 0) { txtPendencias.Text = ""; txtPendencias.Visible = true; btnPendencias.Visible = true; foreach (string item in Pendencias) { txtPendencias.Text += "Seq. " + item + Environment.NewLine; } } else { txtPendencias.Visible = false; btnPendencias.Visible = false; } btnPesquisa_Click(sender, e); #endregion } #endregion } else { #region Envio Contingencia cert = new X509Certificate2(); cert = belCertificadoDigital.BuscaNome(""); if (!belCertificadoDigital.ValidaCertificado(cert)) { lblStatus.Text = ""; throw new Exception("Certificado não Selecionado."); } objCriaXml.cert = cert; objGerais = new daoBuscaDadosGerais(); belGlobais objGlobais = new belGlobais(); XmlDocument doc = new XmlDocument(); string sChave = objGerais.BuscaChaveRetornoCteSeq(slistaConhec[0]); DirectoryInfo dPastaContingencia = new DirectoryInfo(belStaticPastas.CONTINGENCIA); FileInfo[] finfo = dPastaContingencia.GetFiles("*.xml", SearchOption.AllDirectories); bool ArquivoPastaEnvio = false; bool ArquivoPastaEnvioMesAtual = false; string sCaminho = ""; foreach (FileInfo arq in finfo) { if (arq.Name.Contains("Lote") && ArquivoPastaEnvio == false) { doc.Load(@arq.FullName); if (doc.GetElementsByTagName("infCte")[0].Attributes["Id"].Value.ToString().Replace("CTe", "").Equals(sChave)) { sCaminho = @arq.FullName; string sPathDest = belStaticPastas.ENVIO + "\\" + arq.Name; string sPathOrigem = belStaticPastas.CONTINGENCIA + "\\" + arq.Name; if (File.Exists(sPathDest)) { File.Delete(sPathDest); } File.Copy(sPathOrigem, sPathDest); ArquivoPastaEnvio = true; } } else if (!arq.Name.Contains("Lote") && ArquivoPastaEnvioMesAtual == false) { string sData = HLP.Util.Util.GetDateServidor().Date.ToString("dd-MM-yyyy"); doc.Load(@arq.FullName); if (doc.GetElementsByTagName("infCte")[0].Attributes["Id"].Value.ToString().Replace("CTe", "").Equals(sChave)) { string sPathDest = belStaticPastas.ENVIO + sData.Substring(3, 2) + "-" + sData.Substring(8, 2) + @"\\" + arq.Name; string sPathOrigem = belStaticPastas.CONTINGENCIA + sData.Substring(3, 2) + "-" + sData.Substring(8, 2) + @"\\" + arq.Name; if (File.Exists(sPathDest)) { File.Delete(sPathDest); } File.Copy(sPathOrigem, sPathDest); ArquivoPastaEnvioMesAtual = true; } } if (ArquivoPastaEnvioMesAtual && ArquivoPastaEnvio) { lblStatus.Text = "Enviando Lote para WebService..."; doc.Load(sCaminho); string sRetorno = objCriaXml.TransmitirLote(doc); string sRecibo = objCriaXml.BuscaReciboRetornoEnvio(sRetorno); List<belStatusCte> ListaStatus = objCriaXml.ConsultaLoteEnviado(sRecibo); if (sRecibo != "") { objGravaDadosRetorno.GravarRecibo(slistaConhec[0], sRecibo); } foreach (belStatusCte cte in ListaStatus) { if (cte.CodRetorno != "103" && cte.CodRetorno != "104" && cte.CodRetorno != "105" && cte.CodRetorno != "100") { objGravaDadosRetorno.ApagarRecibo(sRecibo); } else { objGravaDadosRetorno.GravarProtocoloEnvio(cte); } } KryptonMessageBox.Show(belTrataMensagem.RetornaMensagem(ListaStatus, belTrataMensagem.Tipo.Envio), "CT-e - Retorno WebService", MessageBoxButtons.OK, MessageBoxIcon.Information); objGerais = new daoBuscaDadosGerais(); foreach (belStatusCte cte in ListaStatus.Where(C => C.Enviado == true)) { lblStatus.Text = "Salvando Arquivos"; objGravaDadosRetorno.AlterarStatusCte(cte); string sprot = objGerais.BuscaNumProtocolo(cte.NumeroSeq); objCriaXml.SalvaArquivoPastaEnviado(objGerais.BuscaNumeroConhecimento(cte.NumeroSeq), cte.Chave); } lblStatus.Text = ""; btnPendencias_Click(sender, e); break; } } if (!ArquivoPastaEnvioMesAtual && !ArquivoPastaEnvio) { lblStatus.Text = ""; KryptonMessageBox.Show("Arquivo para Envio não Encontrado", "CONHECIMENTO DE TRANSP. ELETRÔNICO", MessageBoxButtons.OK, MessageBoxIcon.Information); } #endregion } } } else { KryptonMessageBox.Show("Sistema está Indisponível!", "CT-e - AVISO", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { KryptonMessageBox.Show("Sistema se encontra em Modo de Contingência", "CT-e - AVISO", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { lblStatus.Text = ""; KryptonMessageBox.Show(null, _sMessageException + (ex.InnerException != null ? ex.InnerException.Message : ex.Message).ToString(), "CT-e - AVISO", MessageBoxButtons.OK, MessageBoxIcon.Information); ; } }
private void btnGeraPdf_Click(object sender, EventArgs e) { try { List<DadosImpressao> objListDados = new List<DadosImpressao>(); FileInfo[] fArquivoImprimir = null; belGlobais objGlobais = new belGlobais(); DirectoryInfo dPasta = null; if (!belStatic.bModoContingencia) { #region Verifica Selecionadas for (int i = 0; i < dgvArquivos.RowCount; i++) { if (dgvArquivos["cl_imprime", i].Value != null) { if (dgvArquivos["cl_imprime", i].Value.ToString().Equals("True")) { string sProtEnvio = objGerais.VerificaCampoProtocoloEnvio(dgvArquivos["cd_conheci", i].Value.ToString()); if (sProtEnvio != "") { DadosImpressao objDados = new DadosImpressao(); objDados.sNumeroCte = dgvArquivos["cd_conheci", i].Value.ToString(); objDados.sProtocolo = sProtEnvio; if (!(dgvArquivos["ds_cancelamento", i].Value).ToString().Equals("")) { objDados.Cancelado = true; } objListDados.Add(objDados); } } } } #endregion } else { #region Verifica Notas em Contingencia for (int i = 0; i < dgvArquivos.RowCount; i++) { if (dgvArquivos["cl_imprime", i].Value != null) { if (dgvArquivos["cl_imprime", i].Value.ToString().Equals("True") && Convert.ToBoolean(dgvArquivos["st_cte", i].Value) == false && Convert.ToBoolean(dgvArquivos["st_contingencia", i].Value) == true) { DadosImpressao objDados = new DadosImpressao(); objDados.sNumeroCte = dgvArquivos["cd_conheci", i].Value.ToString(); objListDados.Add(objDados); } } } #endregion } if (objListDados.Count() > 0) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { #region Busca Arquivos XML for (int i = 0; i < objListDados.Count; i++) { if (!belStatic.bModoContingencia) { string sChaveCteRet = objGerais.BuscaChaveRetornoCte(objListDados[i].sNumeroCte); string sPasta = sChaveCteRet.Substring(4, 2) + "-" + sChaveCteRet.Substring(2, 2); if (!objListDados[i].Cancelado) { dPasta = new DirectoryInfo(belStaticPastas.ENVIADOS + @"\\" + sPasta); } else { dPasta = new DirectoryInfo(belStaticPastas.CANCELADOS + @"\\" + sPasta); } if (dPasta.Exists) { fArquivoImprimir = dPasta.GetFiles("Cte_" + sChaveCteRet + ".xml"); if (fArquivoImprimir.Count() == 1) { objListDados[i].bArquivoEncontrado = true; objListDados[i].sCaminhoXml = dPasta.ToString() + "\\Cte_" + sChaveCteRet + ".xml"; } else { throw new Exception("Arquivo Xml não Encontrado"); } } } else { XmlDocument doc = new XmlDocument(); string sChaveCteRet = objGerais.BuscaChaveRetornoCte(objListDados[i].sNumeroCte); string sPasta = sChaveCteRet.Substring(4, 2) + "-" + sChaveCteRet.Substring(2, 2); dPasta = new DirectoryInfo(belStaticPastas.CONTINGENCIA + @"\\" + sPasta); fArquivoImprimir = dPasta.GetFiles("*.xml", SearchOption.AllDirectories); foreach (FileInfo arq in fArquivoImprimir) { doc.Load(@arq.FullName); if (doc.GetElementsByTagName("infCte")[0].Attributes["Id"].Value.ToString().Replace("CTe", "").Equals(sChaveCteRet)) { objListDados[i].bArquivoEncontrado = true; objListDados[i].sCaminhoXml = arq.FullName; break; } } } } #endregion belPopulaDataSet objDataSet = new belPopulaDataSet(); dsCTe dsPadrao = new dsCTe(); dsCTe dsLotacao = new dsCTe(); dsCTe dsPadraoCancelado = new dsCTe(); dsCTe dsLotacaoCancelado = new dsCTe(); int iCount = 0; for (int i = 0; i < objListDados.Count; i++) { if (objListDados[i].bArquivoEncontrado == true) { if (objDataSet.VerificaLotacao(objListDados[i].sCaminhoXml)) { if (!objListDados[i].Cancelado) { dsLotacao = new dsCTe(); objDataSet.PopulaDataSet(dsLotacao, objListDados[i].sCaminhoXml, 1, objListDados[i].sProtocolo); GeraPDF(dsLotacao, TipoPDF.LOTACAO, objListDados[i].sNumeroCte, folderBrowserDialog1.SelectedPath); iCount++; } else { dsLotacaoCancelado = new dsCTe(); objDataSet.PopulaDataSet(dsLotacaoCancelado, objListDados[i].sCaminhoXml, 1, objListDados[i].sProtocolo); GeraPDF(dsLotacaoCancelado, TipoPDF.LOTACAO_CANCELADO, objListDados[i].sNumeroCte, folderBrowserDialog1.SelectedPath); iCount++; } } else { if (!objListDados[i].Cancelado) { dsPadrao = new dsCTe(); objDataSet.PopulaDataSet(dsPadrao, objListDados[i].sCaminhoXml, 1, objListDados[i].sProtocolo); GeraPDF(dsPadrao, TipoPDF.PADRAO, objListDados[i].sNumeroCte, folderBrowserDialog1.SelectedPath); iCount++; } else { dsPadraoCancelado = new dsCTe(); objDataSet.PopulaDataSet(dsPadraoCancelado, objListDados[i].sCaminhoXml, 1, objListDados[i].sProtocolo); GeraPDF(dsPadraoCancelado, TipoPDF.PADRAO_CANCELADO, objListDados[i].sNumeroCte, folderBrowserDialog1.SelectedPath); iCount++; } } } } if (iCount > 0) { KryptonMessageBox.Show("Arquivos PDF gerados com sucesso!", "CT-e - AVISO", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } catch (Exception ex) { KryptonMessageBox.Show(null, _sMessageException + (ex.InnerException != null ? ex.InnerException.Message : ex.Message).ToString(), "CT-e - AVISO", MessageBoxButtons.OK, MessageBoxIcon.Information); ; } }
private void btnImpressao_Click(object sender, EventArgs e) { try { List<DadosImpressao> objListDados = new List<DadosImpressao>(); belGlobais objGlobais = new belGlobais(); DirectoryInfo dPasta = null; FileInfo[] fArquivoImprimir = null; if (!belStatic.bModoContingencia) { #region Verifica Selecionadas for (int i = 0; i < dgvArquivos.RowCount; i++) { if (dgvArquivos["cl_imprime", i].Value != null) { if (dgvArquivos["cl_imprime", i].Value.ToString().Equals("True")) { //string sProtEnvio = objGerais.VerificaCampoProtocoloEnvio(dgvArquivos["cd_conheci", i].Value.ToString()); //if (sProtEnvio != "") //{ DadosImpressao objDados = new DadosImpressao(); objDados.sNumeroCte = dgvArquivos["cd_conheci", i].Value.ToString(); objDados.sProtocolo = "";// sProtEnvio; if (!dgvArquivos["ds_cancelamento", i].Value.ToString().Equals("")) { objDados.Cancelado = true; } objListDados.Add(objDados); //} } } } #endregion } else { #region Verifica Notas em Contingencia for (int i = 0; i < dgvArquivos.RowCount; i++) { if (dgvArquivos["cl_imprime", i].Value != null) { if (dgvArquivos["cl_imprime", i].Value.ToString().Equals("True") && Convert.ToBoolean(dgvArquivos["st_cte", i].Value) == false && Convert.ToBoolean(dgvArquivos["st_contingencia", i].Value) == true) { DadosImpressao objDados = new DadosImpressao(); objDados.sNumeroCte = dgvArquivos["cd_conheci", i].Value.ToString(); objListDados.Add(objDados); } } } #endregion } #region Popula Dataset com Cte Validos if (objListDados.Count > 0) { for (int i = 0; i < objListDados.Count; i++) { if (!belStatic.bModoContingencia) { string sChaveCteRet = objGerais.BuscaChaveRetornoCte(objListDados[i].sNumeroCte); string sPasta = sChaveCteRet.Substring(4, 2) + "-" + sChaveCteRet.Substring(2, 2); if (!objListDados[i].Cancelado) { dPasta = new DirectoryInfo(belStaticPastas.ENVIADOS + @"\\" + sPasta); } else { dPasta = new DirectoryInfo(belStaticPastas.CANCELADOS + @"\\" + sPasta); } if (dPasta.Exists) { fArquivoImprimir = dPasta.GetFiles("Cte_" + sChaveCteRet + ".xml"); if (fArquivoImprimir.Count() == 1) { objListDados[i].bArquivoEncontrado = true; objListDados[i].sCaminhoXml = dPasta.ToString() + "\\Cte_" + sChaveCteRet + ".xml"; } else { throw new Exception("Arquivo Xml não Encontrado"); } } } else { XmlDocument doc = new XmlDocument(); string sChaveCteRet = objGerais.BuscaChaveRetornoCte(objListDados[i].sNumeroCte); string sPasta = sChaveCteRet.Substring(4, 2) + "-" + sChaveCteRet.Substring(2, 2); dPasta = new DirectoryInfo(belStaticPastas.CONTINGENCIA + @"\\" + sPasta); fArquivoImprimir = dPasta.GetFiles("*.xml", SearchOption.AllDirectories); foreach (FileInfo arq in fArquivoImprimir) { doc.Load(@arq.FullName); if (doc.GetElementsByTagName("infCte")[0].Attributes["Id"].Value.ToString().Replace("CTe", "").Equals(sChaveCteRet)) { objListDados[i].bArquivoEncontrado = true; objListDados[i].sCaminhoXml = arq.FullName; break; } } } } belPopulaDataSet objDataSet = new belPopulaDataSet(); dsCTe dsPadrao = new dsCTe(); dsCTe dsLotacao = new dsCTe(); dsCTe dsPadraoCancelado = new dsCTe(); dsCTe dsLotacaoCancelado = new dsCTe(); for (int i = 0; i < objListDados.Count; i++) { dsCTe dsPDF = new dsCTe(); if (objListDados[i].bArquivoEncontrado == true) { if (objDataSet.VerificaLotacao(objListDados[i].sCaminhoXml)) { if (!objListDados[i].Cancelado) { objDataSet.PopulaDataSet(dsLotacao, objListDados[i].sCaminhoXml, i + 1, objListDados[i].sProtocolo); objDataSet.PopulaDataSet(dsPDF, objListDados[i].sCaminhoXml, 1, objListDados[i].sProtocolo); GeraPDF(dsPDF, TipoPDF.LOTACAO, objListDados[i].sNumeroCte); } else { objDataSet.PopulaDataSet(dsLotacaoCancelado, objListDados[i].sCaminhoXml, i + 1, objListDados[i].sProtocolo); objDataSet.PopulaDataSet(dsPDF, objListDados[i].sCaminhoXml, 1, objListDados[i].sProtocolo); GeraPDF(dsPDF, TipoPDF.LOTACAO_CANCELADO, objListDados[i].sNumeroCte); } } else { if (!objListDados[i].Cancelado) { objDataSet.PopulaDataSet(dsPadrao, objListDados[i].sCaminhoXml, i + 1, objListDados[i].sProtocolo); objDataSet.PopulaDataSet(dsPDF, objListDados[i].sCaminhoXml, 1, objListDados[i].sProtocolo); GeraPDF(dsPDF, TipoPDF.PADRAO, objListDados[i].sNumeroCte); } else { objDataSet.PopulaDataSet(dsPadraoCancelado, objListDados[i].sCaminhoXml, i + 1, objListDados[i].sProtocolo); objDataSet.PopulaDataSet(dsPDF, objListDados[i].sCaminhoXml, 1, objListDados[i].sProtocolo); GeraPDF(dsPDF, TipoPDF.PADRAO_CANCELADO, objListDados[i].sNumeroCte); } } } } if (objbelGlobais.LeRegWin("EmailAutomatico").ToString() == "True") { EnviaEmail(objListDados); } if (dsPadrao.infCte.Count() > 0) { ReportDocument rpt = new ReportDocument(); rpt.Load(Application.StartupPath + "\\Relatorios\\rptCtePadrao.rpt"); rpt.SetDataSource(dsPadrao); rpt.Refresh(); frmRelatorioCte frm = new frmRelatorioCte(rpt, "Impressão de DACTE - Carga Fracionada"); frm.Show(); } if (dsPadraoCancelado.infCte.Count() > 0) { ReportDocument rpt = new ReportDocument(); rpt.Load(Application.StartupPath + "\\Relatorios\\rptCtePadraoCancelado.rpt"); rpt.SetDataSource(dsPadraoCancelado); rpt.Refresh(); frmRelatorioCte frm = new frmRelatorioCte(rpt, "Impressão de DACTE - Carga Fracionada(Cancelados)"); frm.Show(); } if (dsLotacao.infCte.Count() > 0) { ReportDocument rpt = new ReportDocument(); rpt.Load(Application.StartupPath + "\\Relatorios\\rptCteLotacao.rpt"); rpt.SetDataSource(dsLotacao); rpt.Refresh(); frmRelatorioCte frm = new frmRelatorioCte(rpt, "Impressão de DACTE - Lotação"); frm.Show(); } if (dsLotacaoCancelado.infCte.Count() > 0) { ReportDocument rpt = new ReportDocument(); rpt.Load(Application.StartupPath + "\\Relatorios\\rptCteLotacaoCancelado.rpt"); rpt.SetDataSource(dsLotacaoCancelado); rpt.Refresh(); frmRelatorioCte frm = new frmRelatorioCte(rpt, "Impressão de DACTE - Lotação(Cancelados)"); frm.Show(); } } else { KryptonMessageBox.Show("Nenhum Conhecimento Válido foi Selecionado para Impressão", "CT-e - AVISO", MessageBoxButtons.OK, MessageBoxIcon.Information); ; } } catch (Exception ex) { KryptonMessageBox.Show(null, _sMessageException + (ex.InnerException != null ? ex.InnerException.Message : ex.Message).ToString(), "CT-e - AVISO", MessageBoxButtons.OK, MessageBoxIcon.Information); ; } #endregion CarregaGrid(); }
private string MontaXmlBuscaRetorno(string sRecibo) { try { XNamespace pf = "http://www.portalfiscal.inf.br/cte"; XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance"; XContainer consReciCTe = new XElement(pf + "consReciCTe", new XAttribute("versao", "1.04"), new XAttribute(ns + "schemaLocation", "http://www.portalfiscal.inf.br/consReciCte_v1.04.xsd"), new XAttribute("xmlns", "http://www.portalfiscal.inf.br/cte"), new XAttribute(XNamespace.Xmlns + "ds", "http://www.w3.org/2000/09/xmldsig#"), new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"), new XElement(pf + "tpAmb", belStatic.TpAmb), new XElement(pf + "nRec", sRecibo)); #region Valida_Xml belGlobais getschema = new belGlobais(); XmlSchemaCollection myschema = new XmlSchemaCollection(); XmlValidatingReader reader; try { XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None); reader = new XmlValidatingReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + consReciCTe.ToString(), XmlNodeType.Element, context); myschema.Add("http://www.portalfiscal.inf.br/cte", belStaticPastas.SCHEMA_CTE + "\\consReciCte_v1.04.xsd"); reader.ValidationType = ValidationType.Schema; reader.Schemas.Add(myschema); while (reader.Read()) { } } catch (XmlException x) { throw new Exception(x.Message); } catch (XmlSchemaException x) { throw new Exception(x.Message); } #endregion return consReciCTe.ToString(); } catch (Exception ex) { throw ex; } }
public List<belStatusCte> GerarXmlConsultaStatus() { XNamespace pf = "http://www.portalfiscal.inf.br/cte"; XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance"; XContainer consStatServ = new XElement(pf + "consStatServCte", new XAttribute("versao", "1.04"), new XAttribute(ns + "schemaLocation", "http://www.portalfiscal.inf.br/consStatServCte_v1.04.xsd"), new XAttribute("xmlns", "http://www.portalfiscal.inf.br/cte"), new XAttribute(XNamespace.Xmlns + "ds", "http://www.w3.org/2000/09/xmldsig#"), new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance")); XContainer tpAmb = new XElement(pf + "tpAmb", belStatic.TpAmb); XContainer status = new XElement(pf + "xServ", "STATUS"); consStatServ.Add(tpAmb); consStatServ.Add(status); #region Valida Xml belGlobais getschema = new belGlobais(); XmlSchemaCollection myschema = new XmlSchemaCollection(); XmlValidatingReader reader; try { XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None); reader = new XmlValidatingReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + consStatServ.ToString(), XmlNodeType.Element, context); myschema.Add("http://www.portalfiscal.inf.br/cte", belStaticPastas.SCHEMA_CTE + "\\consStatServCte_v1.04.xsd"); reader.ValidationType = ValidationType.Schema; reader.Schemas.Add(myschema); while (reader.Read()) { } } catch (XmlException x) { throw new Exception(x.Message); } catch (XmlSchemaException x) { throw new Exception(x.Message); } #endregion #region Consultar Status XmlDocument doc = new XmlDocument(); doc.LoadXml(consStatServ.ToString()); string sRetorno = ConsultarStatus(doc); List<belStatusCte> ListaStatus = TrataDadosRetorno(sRetorno); #endregion return ListaStatus; }
public List<belStatusCte> GerarXmlInutilizacao(belInutilizaFaixaCte objInutiliza) { XNamespace pf = "http://www.portalfiscal.inf.br/cte"; XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance"; XContainer inutCTe = new XElement(pf + "inutCTe", new XAttribute("versao", "1.04"), new XAttribute(ns + "schemaLocation", "http://www.portalfiscal.inf.br/inutCte_v1.04.xsd"), new XAttribute("xmlns", "http://www.portalfiscal.inf.br/cte"), new XAttribute(XNamespace.Xmlns + "ds", "http://www.w3.org/2000/09/xmldsig#"), new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance")); XContainer infInut = new XElement(pf + "infInut", new XAttribute("Id", objInutiliza.Id), new XElement(pf + "tpAmb", objInutiliza.tpAmb), new XElement(pf + "xServ", objInutiliza.xServ), new XElement(pf + "cUF", objInutiliza.cUF), new XElement(pf + "ano", objInutiliza.ano), new XElement(pf + "CNPJ", objInutiliza.CNPJ), new XElement(pf + "mod", objInutiliza.mod), new XElement(pf + "serie", objInutiliza.serie), new XElement(pf + "nCTIni", objInutiliza.nCTIni), new XElement(pf + "nCTFin", objInutiliza.nCTFin), new XElement(pf + "xJust", objInutiliza.xJust)); inutCTe.Add(infInut); #region Assinatura belAssinaCte Assinatura = new belAssinaCte(); string sInutCte = Assinatura.ConfigurarArquivo(inutCTe.ToString(), "infInut", this.cert); XElement xInutCTe = XElement.Parse(sInutCte); #endregion #region Valida_Xml belGlobais getschema = new belGlobais(); XmlSchemaCollection myschema = new XmlSchemaCollection(); XmlValidatingReader reader; try { XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None); reader = new XmlValidatingReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + xInutCTe.ToString(), XmlNodeType.Element, context); myschema.Add("http://www.portalfiscal.inf.br/cte", belStaticPastas.SCHEMA_CTE + "\\inutCte_v1.04.xsd"); reader.ValidationType = ValidationType.Schema; reader.Schemas.Add(myschema); while (reader.Read()) { } } catch (XmlException x) { throw new Exception(x.Message); } catch (XmlSchemaException x) { throw new Exception(x.Message); } #endregion #region Salva Xml string sData = HLP.Util.Util.GetDateServidor().Date.ToString("dd-MM-yyyy"); belGlobais objGlobais = new belGlobais(); DirectoryInfo dPastaProtocolo = new DirectoryInfo(belStaticPastas.PROTOCOLOS); if (!dPastaProtocolo.Exists) { dPastaProtocolo.Create(); } DirectoryInfo dPastaMesAtual = new DirectoryInfo(dPastaProtocolo + @"\\" + sData.Substring(3, 2) + "-" + sData.Substring(8, 2)); if (!dPastaMesAtual.Exists) { dPastaMesAtual.Create(); } string sCaminho = dPastaMesAtual.ToString() + "\\" + "Inut_" + objInutiliza.nCTIni + "_" + objInutiliza.nCTFin + ".xml"; XDocument XmlInut = new XDocument(xInutCTe); XmlInut.Save(sCaminho); #endregion #region Inutilizar Cte XmlDocument doc = new XmlDocument(); doc.Load(sCaminho); string sRetorno = InutilizaFaixaCte(doc); List<belStatusCte> ListaStatus = TrataDadosRetorno(sRetorno); foreach (belStatusCte cte in ListaStatus) { if (cte.CodRetorno == "102") { XDocument xRet = XDocument.Parse(sRetorno); xRet.Save(sCaminho); } else { File.Delete(sCaminho); } } #endregion return ListaStatus; }
public string GerarXml(belPopulaObjetos objObjetos, int iNumLote) { this.cert = objObjetos.cert; try { List<string> listXml = new List<string>(); DirectoryInfo dPastaMesAtual = null; DirectoryInfo dPastaEnvio = null; string sData = HLP.Util.Util.GetDateServidor().Date.ToString("dd-MM-yyyy"); string sRecibo = ""; XContainer CTe = null; XContainer infCte = null; XContainer ide = null; XContainer toma03 = null; XContainer toma4 = null; XContainer enderToma = null; XContainer emit = null; XContainer compl = null; XContainer enderEmit = null; XContainer rem = null; XContainer enderReme = null; XContainer infNF = null; XContainer infNFe = null; XContainer infOutros = null; XContainer exped = null; XContainer enderExped = null; XContainer receb = null; XContainer enderReceb = null; XContainer dest = null; XContainer enderDest = null; XContainer vPrest = null; XContainer Comp = null; XContainer imp = null; XContainer ICMS = null; XContainer ICMS00 = null; XContainer ICMS20 = null; XContainer ICMS45 = null; XContainer ICMS60 = null; XContainer ICMS90 = null; XContainer ICMSOutraUF = null; XContainer infCTeNorm = null; XContainer seg = null; XContainer infModal = null; XContainer rodo = null; XContainer veic = null; XContainer prop = null; XContainer moto = null; #region Monta Cabecalho XNamespace pf = "http://www.portalfiscal.inf.br/cte"; XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance"; #endregion for (int i = 0; i < objObjetos.objLinfCte.Count; i++) { CTe = new XElement(pf + "CTe"); belinfCte obj = objObjetos.objLinfCte[i]; infCte = (new XElement(pf + "infCte", new XAttribute("versao", "1.04"), new XAttribute("Id", obj.id))); #region ide ide = (new XElement(pf + "ide", new XElement(pf + "cUF", obj.ide.cUF), new XElement(pf + "cCT", obj.ide.cCT), new XElement(pf + "CFOP", obj.ide.CFOP), new XElement(pf + "natOp", obj.ide.natOp), new XElement(pf + "forPag", obj.ide.forPag), new XElement(pf + "mod", obj.ide.mod), new XElement(pf + "serie", obj.ide.serie), new XElement(pf + "nCT", Convert.ToInt32(obj.ide.nCT)), new XElement(pf + "dhEmi", (HLP.Util.Util.GetDateServidor()).ToString("yyyy-MM-ddTHH:MM:ss")), new XElement(pf + "tpImp", obj.ide.tpImp), new XElement(pf + "tpEmis", obj.ide.tpEmis), new XElement(pf + "cDV", obj.ide.cDV), new XElement(pf + "tpAmb", obj.ide.tpAmb), new XElement(pf + "tpCTe", obj.ide.tpCTe), new XElement(pf + "procEmi", obj.ide.procEmi), new XElement(pf + "verProc", obj.ide.verProc), new XElement(pf + "cMunEnv", obj.ide.cMunEnv), new XElement(pf + "xMunEnv", obj.ide.xMunEnv), new XElement(pf + "UFEnv", obj.ide.UFEnv), new XElement(pf + "modal", obj.ide.modal), new XElement(pf + "tpServ", obj.ide.tpServ), new XElement(pf + "cMunIni", obj.ide.cMunIni), new XElement(pf + "xMunIni", obj.ide.xMunIni), new XElement(pf + "UFIni", obj.ide.UFIni), new XElement(pf + "cMunFim", obj.ide.cMunFim), new XElement(pf + "xMunFim", obj.ide.xMunFim), new XElement(pf + "UFFim", obj.ide.UFFim), new XElement(pf + "retira", obj.ide.retira))); if (obj.ide.toma03 != null) { toma03 = (new XElement(pf + "toma03", new XElement(pf + "toma", obj.ide.toma03.toma))); ide.Add(toma03); } else if (obj.ide.toma04 != null) { toma4 = (new XElement(pf + "toma4", new XElement(pf + "toma", obj.ide.toma04.toma), obj.ide.toma04.CNPJ != "" ? new XElement(pf + "CNPJ", obj.ide.toma04.CNPJ) : null, obj.ide.toma04.CPF != "" ? new XElement(pf + "CPF", obj.ide.toma04.CPF) : null, obj.ide.toma04.IE != "" ? new XElement(pf + "IE", obj.ide.toma04.IE) : null, new XElement(pf + "xNome", obj.ide.toma04.xNome), obj.ide.toma04.xFant != "" ? new XElement(pf + "xFant", obj.ide.toma04.xFant) : null, obj.ide.toma04.fone != "" ? new XElement(pf + "fone", obj.ide.toma04.fone) : null)); enderToma = (new XElement(pf + "enderToma", new XElement(pf + "xLgr", obj.ide.toma04.enderToma.xLgr), new XElement(pf + "nro", obj.ide.toma04.enderToma.nro), obj.ide.toma04.enderToma.xCpl != "" ? new XElement(pf + "xCpl", obj.ide.toma04.enderToma.xCpl) : null, new XElement(pf + "xBairro", obj.ide.toma04.enderToma.xBairro), new XElement(pf + "cMun", obj.ide.toma04.enderToma.cMun), new XElement(pf + "xMun", obj.ide.toma04.enderToma.xMun), obj.ide.toma04.enderToma.CEP != "" ? new XElement(pf + "CEP", obj.ide.toma04.enderToma.CEP) : null, new XElement(pf + "UF", obj.ide.toma04.enderToma.UF), obj.ide.toma04.enderToma.cPais != "" ? new XElement(pf + "cPais", obj.ide.toma04.enderToma.cPais) : null, obj.ide.toma04.enderToma.xPais != null ? new XElement(pf + "xPais", obj.ide.toma04.enderToma.xPais) : null)); toma4.Add(enderToma); ide.Add(toma4); } #endregion #region Comp compl = (obj.compl != null ? new XElement(pf + "compl", new XElement(pf + "ObsCont", new XAttribute("xCampo", obj.compl.ObsCont.Xcampo), new XElement(pf + "xTexto", obj.compl.ObsCont.Xtexto))) : null); #endregion #region emit emit = (new XElement(pf + "emit", new XElement(pf + "CNPJ", obj.emit.CNPJ), new XElement(pf + "IE", obj.emit.IE), new XElement(pf + "xNome", obj.emit.xNome), obj.emit.xFant != "" ? new XElement(pf + "xFant", obj.emit.xFant) : null)); enderEmit = (new XElement(pf + "enderEmit", new XElement(pf + "xLgr", obj.emit.enderEmit.xLgr), new XElement(pf + "nro", obj.emit.enderEmit.nro), obj.emit.enderEmit.xCpl != "" ? new XElement(pf + "xCpl", obj.emit.enderEmit.xCpl) : null, new XElement(pf + "xBairro", obj.emit.enderEmit.xBairro), new XElement(pf + "cMun", obj.emit.enderEmit.cMun), new XElement(pf + "xMun", obj.emit.enderEmit.xMun), obj.emit.enderEmit.CEP != "" ? new XElement(pf + "CEP", obj.emit.enderEmit.CEP) : null, new XElement(pf + "UF", obj.emit.enderEmit.UF), obj.emit.enderEmit.fone != "" ? new XElement(pf + "fone", obj.emit.enderEmit.fone) : null)); emit.Add(enderEmit); #endregion #region rem rem = (new XElement(pf + "rem", obj.rem.CNPJ != "" ? new XElement(pf + "CNPJ", obj.rem.CNPJ) : null, obj.rem.CPF != "" ? new XElement(pf + "CPF", obj.rem.CPF) : null, new XElement(pf + "IE", obj.rem.IE), new XElement(pf + "xNome", obj.rem.xNome), obj.rem.xFant != "" ? new XElement(pf + "xFant", obj.rem.xFant) : null, obj.rem.fone != "" ? new XElement(pf + "fone", obj.rem.fone) : null)); enderReme = (new XElement(pf + "enderReme", new XElement(pf + "xLgr", obj.rem.enderReme.xLgr), new XElement(pf + "nro", obj.rem.enderReme.nro), obj.rem.enderReme.xCpl != "" ? new XElement(pf + "xCpl", obj.rem.enderReme.xCpl) : null, new XElement(pf + "xBairro", obj.rem.enderReme.xBairro), new XElement(pf + "cMun", obj.rem.enderReme.cMun), new XElement(pf + "xMun", obj.rem.enderReme.xMun), obj.rem.enderReme.CEP != "" ? new XElement(pf + "CEP", obj.rem.enderReme.CEP) : null, new XElement(pf + "UF", obj.rem.enderReme.UF), obj.rem.enderReme.cPais != "" ? new XElement(pf + "cPais", obj.rem.enderReme.cPais) : null, obj.rem.enderReme.xPais != "" ? new XElement(pf + "xPais", obj.rem.enderReme.xPais) : null)); rem.Add(enderReme); for (int j = 0; j < obj.rem.infNF.Count; j++) { infNF = new XElement(pf + "infNF", new XElement(pf + "mod", obj.rem.infNF[j].mod), new XElement(pf + "serie", obj.rem.infNF[j].serie), new XElement(pf + "nDoc", obj.rem.infNF[j].nDoc.Replace(",", ".")), new XElement(pf + "dEmi", (Convert.ToDateTime(obj.rem.infNF[j].dEmi)).ToString("yyyy-MM-dd")), new XElement(pf + "vBC", obj.rem.infNF[j].vBC.Replace(",", ".")), new XElement(pf + "vICMS", obj.rem.infNF[j].vICMS.Replace(",", ".")), new XElement(pf + "vBCST", obj.rem.infNF[j].vBCST.Replace(",", ".")), new XElement(pf + "vST", obj.rem.infNF[j].vST.Replace(",", ".")), new XElement(pf + "vProd", obj.rem.infNF[j].vProd.Replace(",", ".")), new XElement(pf + "vNF", obj.rem.infNF[j].vNF.Replace(",", ".")), new XElement(pf + "nCFOP", obj.rem.infNF[j].nCFOP.Replace(",", "."))); rem.Add(infNF); } for (int nfe = 0; nfe < obj.rem.infNFe.Count; nfe++) { infNFe = new XElement(pf + "infNFe", new XElement(pf + "chave", obj.rem.infNFe[nfe].chave)); rem.Add(infNFe); } for (int j = 0; j < obj.rem.infOutros.Count; j++) { infOutros = new XElement(pf + "infOutros", new XElement(pf + "tpDoc", obj.rem.infOutros[j].tpDoc), obj.rem.infOutros[j].descOutros != "" ? new XElement(pf + "descOutros", obj.rem.infOutros[j].descOutros) : null, new XElement(pf + "nDoc", obj.rem.infOutros[j].nDoc), new XElement(pf + "dEmi", Convert.ToDateTime(obj.rem.infOutros[j].dEmi).ToString("yyyy-MM-dd")), new XElement(pf + "vDocFisc", obj.rem.infOutros[j].vDocFisc.Replace(",", "."))); rem.Add(infOutros); } #endregion #region exped exped = null; if (obj.exped != null) { exped = (new XElement(pf + "exped", obj.exped.CNPJ != "" ? new XElement(pf + "CNPJ", obj.exped.CNPJ) : null, obj.exped.CPF != "" ? new XElement(pf + "CPF", obj.exped.CPF) : null, new XElement(pf + "IE", obj.exped.IE), new XElement(pf + "xNome", obj.exped.xNome), obj.exped.fone != "" ? new XElement(pf + "fone", obj.exped.fone) : null)); enderExped = (new XElement(pf + "enderExped", new XElement(pf + "xLgr", obj.exped.enderExped.xLgr), new XElement(pf + "nro", obj.exped.enderExped.nro), obj.exped.enderExped.xCpl != "" ? new XElement(pf + "xCpl", obj.exped.enderExped.xCpl) : null, new XElement(pf + "xBairro", obj.exped.enderExped.xBairro), new XElement(pf + "cMun", obj.exped.enderExped.cMun), new XElement(pf + "xMun", obj.exped.enderExped.xMun), obj.exped.enderExped.CEP != "" ? new XElement(pf + "CEP", obj.exped.enderExped.CEP) : null, new XElement(pf + "UF", obj.exped.enderExped.UF), obj.exped.enderExped.cPais != "" ? new XElement(pf + "cPais", obj.exped.enderExped.cPais) : null, obj.exped.enderExped.xPais != "" ? new XElement(pf + "xPais", obj.exped.enderExped.xPais) : null)); exped.Add(enderExped); } #endregion #region receb receb = null; if (obj.receb != null) { receb = (new XElement(pf + "receb", obj.receb.CNPJ != "" ? new XElement(pf + "CNPJ", obj.receb.CNPJ) : null, obj.receb.CPF != "" ? new XElement(pf + "CPF", obj.receb.CPF) : null, new XElement(pf + "IE", obj.receb.IE), new XElement(pf + "xNome", obj.receb.xNome), obj.receb.fone != "" ? new XElement(pf + "fone", obj.receb.fone) : null)); enderReceb = (new XElement(pf + "enderReceb", new XElement(pf + "xLgr", obj.receb.enderReceb.xLgr), new XElement(pf + "nro", obj.receb.enderReceb.nro), obj.receb.enderReceb.xCpl != "" ? new XElement(pf + "xCpl", obj.receb.enderReceb.xCpl) : null, new XElement(pf + "xBairro", obj.receb.enderReceb.xBairro), new XElement(pf + "cMun", obj.receb.enderReceb.cMun), new XElement(pf + "xMun", obj.receb.enderReceb.xMun), obj.receb.enderReceb.CEP != "" ? new XElement(pf + "CEP", obj.receb.enderReceb.CEP) : null, new XElement(pf + "UF", obj.receb.enderReceb.UF), obj.receb.enderReceb.cPais != "" ? new XElement(pf + "cPais", obj.receb.enderReceb.cPais) : null, obj.receb.enderReceb.xPais != "" ? new XElement(pf + "xPais", obj.receb.enderReceb.xPais) : null)); receb.Add(enderReceb); } #endregion #region dest dest = (new XElement(pf + "dest", obj.dest.CNPJ != "" ? new XElement(pf + "CNPJ", obj.dest.CNPJ) : null, obj.dest.CPF != "" ? new XElement(pf + "CPF", obj.dest.CPF) : null, obj.dest.IE != "" ? new XElement(pf + "IE", obj.dest.IE) : null, new XElement(pf + "xNome", obj.dest.xNome), obj.dest.fone != "" ? new XElement(pf + "fone", obj.dest.fone) : null, obj.dest.ISUF != "" ? new XElement(pf + "ISUF", obj.dest.ISUF) : null)); enderDest = (new XElement(pf + "enderDest", new XElement(pf + "xLgr", obj.dest.enderDest.xLgr), new XElement(pf + "nro", obj.dest.enderDest.nro), obj.dest.enderDest.xCpl != "" ? new XElement(pf + "xCpl", obj.dest.enderDest.xCpl) : null, new XElement(pf + "xBairro", obj.dest.enderDest.xBairro), new XElement(pf + "cMun", obj.dest.enderDest.cMun), new XElement(pf + "xMun", obj.dest.enderDest.xMun), obj.dest.enderDest.CEP != "" ? new XElement(pf + "CEP", obj.dest.enderDest.CEP) : null, new XElement(pf + "UF", obj.dest.enderDest.UF), obj.dest.enderDest.cPais != "" ? new XElement(pf + "cPais", obj.dest.enderDest.cPais) : null, obj.dest.enderDest.xPais != "" ? new XElement(pf + "xPais", obj.dest.enderDest.xPais) : null)); dest.Add(enderDest); #endregion #region vPrest vPrest = (new XElement(pf + "vPrest", new XElement(pf + "vTPrest", obj.vPrest.vTPrest.Replace(",", ".")), new XElement(pf + "vRec", obj.vPrest.vRec.Replace(",", ".")))); for (int j = 0; j < obj.vPrest.Comp.Count; j++) { Comp = new XElement(pf + "Comp", new XElement(pf + "xNome", obj.vPrest.Comp[j].xNome), new XElement(pf + "vComp", obj.vPrest.Comp[j].vComp.Replace(",", "."))); vPrest.Add(Comp); } #endregion #region imp imp = new XElement(pf + "imp"); //ICMS = new XElement(pf + "ICMS", new XElement(pf + "vTotTrib", "200.06")); ICMS = new XElement(pf + "ICMS"); if (obj.imp.ICMS.ICMS00 != null) { belICMS00 objICMS00 = obj.imp.ICMS.ICMS00; ICMS00 = (new XElement(pf + "ICMS00", new XElement(pf + "CST", objICMS00.CST), new XElement(pf + "vBC", objICMS00.vBC), new XElement(pf + "pICMS", objICMS00.pICMS), new XElement(pf + "vICMS", objICMS00.vICMS))); ICMS.Add(ICMS00); } else if (obj.imp.ICMS.ICMS20 != null) { belICMS20 objICMS20 = obj.imp.ICMS.ICMS20; ICMS20 = (new XElement(pf + "ICMS20", new XElement(pf + "CST", objICMS20.CST), new XElement(pf + "pRedBC", objICMS20.pRedBC), new XElement(pf + "vBC", objICMS20.vBC), new XElement(pf + "pICMS", objICMS20.pICMS), new XElement(pf + "vICMS", objICMS20.vICMS))); ICMS.Add(ICMS20); } else if (obj.imp.ICMS.ICMS45 != null) { ICMS45 = (new XElement(pf + "ICMS45", new XElement(pf + "CST", obj.imp.ICMS.ICMS45.CST))); ICMS.Add(ICMS45); } else if (obj.imp.ICMS.ICMS60 != null) { belICMS60 objICMS60 = obj.imp.ICMS.ICMS60; ICMS60 = (new XElement(pf + "ICMS60", new XElement(pf + "CST", objICMS60.CST), new XElement(pf + "vBCSTRet", objICMS60.vBCSTRet), new XElement(pf + "vICMSSTRet", objICMS60.vICMSSTRet), new XElement(pf + "pICMSSTRet", objICMS60.pICMSSTRet))); ICMS.Add(ICMS60); } else if (obj.imp.ICMS.ICMS90 != null) { belICMS90 objICMS90 = obj.imp.ICMS.ICMS90; ICMS60 = (new XElement(pf + "ICMS90", new XElement(pf + "CST", objICMS90.CST), new XElement(pf + "vBC", objICMS90.vBC), new XElement(pf + "pICMS", objICMS90.pICMS), new XElement(pf + "vICMS", objICMS90.vICMS))); ICMS.Add(ICMS90); } else if (obj.imp.ICMS.ICMSOutraUF != null) { belICMSOutraUF objICMSOutraUF = obj.imp.ICMS.ICMSOutraUF; ICMSOutraUF = (new XElement(pf + "ICMSOutraUF", new XElement(pf + "CST", objICMSOutraUF.CST), objICMSOutraUF.pRedBCOutraUF != "" ? new XElement(pf + "pRedBCOutraUF", objICMSOutraUF.pRedBCOutraUF) : null, new XElement(pf + "vBCOutraUF", objICMSOutraUF.vBCOutraUF), new XElement(pf + "pICMSOutraUF", objICMSOutraUF.pICMSOutraUF), new XElement(pf + "vICMSOutraUF", objICMSOutraUF.vICMSOutraUF))); ICMS.Add(ICMSOutraUF); } imp.Add(ICMS); #endregion #region infCTeNorm infCTeNorm = new XElement(pf + "infCTeNorm", new XElement(pf + "infCarga", new XElement(pf + "vCarga", obj.infCTeNorm.infCarga.vCarga), new XElement(pf + "proPred", obj.infCTeNorm.infCarga.proPred), obj.infCTeNorm.infCarga.xOutCat != "" ? new XElement(pf + "xOutCat", obj.infCTeNorm.infCarga.xOutCat) : null, obj.infCTeNorm.infCarga.infQ != null ? (from c in obj.infCTeNorm.infCarga.infQ select new XElement(pf + "infQ", new XElement(pf + "cUnid", c.cUnid), new XElement(pf + "tpMed", c.tpMed), new XElement(pf + "qCarga", retornaqCarga(c.qCarga)))) : null)); seg = new XElement(pf + "seg", new XElement(pf + "respSeg", obj.infCTeNorm.seg.respSeg), new XElement(pf + "nApol", obj.infCTeNorm.seg.nApol)); infCTeNorm.Add(seg); #region Modal Rodoviario infModal = new XElement(pf + "infModal", new XAttribute("versaoModal", "1.04")); rodo = new XElement(pf + "rodo", new XElement(pf + "RNTRC", obj.infCTeNorm.rodo.RNTRC), new XElement(pf + "dPrev", Convert.ToDateTime(obj.infCTeNorm.rodo.dPrev).ToString("yyyy-MM-dd")), new XElement(pf + "lota", obj.infCTeNorm.rodo.lota)); for (int v = 0; v < obj.infCTeNorm.rodo.veic.Count; v++) { veic = new XElement(pf + "veic", new XElement(pf + "RENAVAM", obj.infCTeNorm.rodo.veic[v].RENAVAM), new XElement(pf + "placa", obj.infCTeNorm.rodo.veic[v].placa), new XElement(pf + "tara", obj.infCTeNorm.rodo.veic[v].tara), new XElement(pf + "capKG", obj.infCTeNorm.rodo.veic[v].capKG), new XElement(pf + "capM3", obj.infCTeNorm.rodo.veic[v].capM3), new XElement(pf + "tpProp", obj.infCTeNorm.rodo.veic[v].tpProp), new XElement(pf + "tpVeic", obj.infCTeNorm.rodo.veic[v].tpVeic), new XElement(pf + "tpRod", obj.infCTeNorm.rodo.veic[v].tpRod), new XElement(pf + "tpCar", obj.infCTeNorm.rodo.veic[v].tpCar), new XElement(pf + "UF", obj.infCTeNorm.rodo.veic[v].UF)); if (obj.infCTeNorm.rodo.veic[v].prop != null) { prop = new XElement(pf + "prop", new XElement(pf + (obj.infCTeNorm.rodo.veic[v].prop.CPFCNPJ.Length == 11 ? "CPF" : "CNPJ"), obj.infCTeNorm.rodo.veic[v].prop.CPFCNPJ), new XElement(pf + "RNTRC", obj.infCTeNorm.rodo.veic[v].prop.RNTRC), new XElement(pf + "xNome", obj.infCTeNorm.rodo.veic[v].prop.xNome), new XElement(pf + "IE", obj.infCTeNorm.rodo.veic[v].prop.IE), new XElement(pf + "UF", obj.infCTeNorm.rodo.veic[v].prop.UF), new XElement(pf + "tpProp", obj.infCTeNorm.rodo.veic[v].prop.tpProp)); veic.Add(prop); } rodo.Add(veic); } if (obj.infCTeNorm.rodo.moto != null) { moto = new XElement(pf + "moto", new XElement(pf + "xNome", obj.infCTeNorm.rodo.moto.xNome), new XElement(pf + "CPF", obj.infCTeNorm.rodo.moto.CPF)); rodo.Add(moto); } #endregion infModal.Add(rodo); infCTeNorm.Add(infModal); #endregion #region Monta CTe infCte.Add(ide); infCte.Add(compl); infCte.Add(emit); infCte.Add(rem); infCte.Add(exped); infCte.Add(receb); infCte.Add(dest); infCte.Add(vPrest); infCte.Add(imp); infCte.Add(infCTeNorm); CTe.Add(infCte); #endregion #region Assinatura belAssinaCte Assinatura = new belAssinaCte(); string sCte = Assinatura.ConfigurarArquivo(CTe.ToString(), "infCte", objObjetos.cert); listXml.Add(sCte); #endregion #region SalvaCteIndividual XDocument XmlCte = XDocument.Parse(sCte); string sNumCte = obj.ide.nCT.PadLeft(6, '0'); belGlobais objGlobais = new belGlobais(); if (belStatic.bModoContingencia) { dPastaEnvio = new DirectoryInfo(belStaticPastas.CONTINGENCIA); } else { dPastaEnvio = new DirectoryInfo(belStaticPastas.ENVIO); } if (!dPastaEnvio.Exists) { dPastaEnvio.Create(); } dPastaMesAtual = new DirectoryInfo(dPastaEnvio + @"\\" + sData.Substring(3, 2) + "-" + sData.Substring(8, 2)); if (!dPastaMesAtual.Exists) { dPastaMesAtual.Create(); } XmlCte.Save(dPastaMesAtual.ToString() + "\\" + "Cte_" + sNumCte + ".xml"); #endregion } #region Salva Lote string sXml = ""; foreach (var xml in listXml) { sXml = sXml + xml; } string sXmlCte = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><enviCTe versao=\"1.04\" " + "xsi:schemaLocation=\"http://www.portalfiscal.inf.br/cte enviCte_v1.04.xsd\" " + "xmlns=\"http://www.portalfiscal.inf.br/cte\" xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><idLote>" + iNumLote + "</idLote>" + sXml + "</enviCTe>"); StreamWriter sw = new StreamWriter(dPastaEnvio.ToString() + "\\" + "Cte_Lote_" + iNumLote + "_" + sData + ".xml"); sw.Write(sXmlCte); sw.Close(); #endregion #region Valida_Xml belGlobais getschema = new belGlobais(); XmlSchemaCollection myschema = new XmlSchemaCollection(); XmlValidatingReader reader; try { XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None); reader = new XmlValidatingReader(sXmlCte.ToString(), XmlNodeType.Element, context); myschema.Add("http://www.portalfiscal.inf.br/cte", belStaticPastas.SCHEMA_CTE + "\\enviCte_v1.04.xsd"); reader.ValidationType = ValidationType.Schema; reader.Schemas.Add(myschema); while (reader.Read()) { } } catch (XmlException x) { throw new Exception(x.Message); } catch (XmlSchemaException x) { throw new Exception(x.Message); } #endregion #region Envia e Busca Recibo Lote WebService if (!belStatic.bModoContingencia) { XmlDocument doc = new XmlDocument(); doc.Load(dPastaEnvio.ToString() + "\\" + "Cte_Lote_" + iNumLote + "_" + sData + ".xml"); //busca retorno string sRetorno = TransmitirLote(doc); sRecibo = BuscaReciboRetornoEnvio(sRetorno); } #endregion return sRecibo; } catch (Exception ex) { throw ex; } }
public void SalvaArquivoPastaCancelado(string sChaveCte) { try { string sData = HLP.Util.Util.GetDateServidor().Date.ToString("dd-MM-yyyy"); belGlobais objGlobais = new belGlobais(); DirectoryInfo dPastaCancelados = new DirectoryInfo(belStaticPastas.CANCELADOS); if (!dPastaCancelados.Exists) { dPastaCancelados.Create(); } DirectoryInfo dPastaCanceladosMesAtual = new DirectoryInfo(dPastaCancelados + @"\\" + sData.Substring(3, 2) + "-" + sData.Substring(8, 2)); if (!dPastaCanceladosMesAtual.Exists) { dPastaCanceladosMesAtual.Create(); } DirectoryInfo dPastaEnviados = new DirectoryInfo(belStaticPastas.ENVIADOS); if (dPastaEnviados.Exists) { // select conhecim.cd_nprotcte from conhecim where conhecim.nr_lanc and conhecim.cd_empresa = '' DirectoryInfo dPastaEnviadosMesAtual = new DirectoryInfo(dPastaEnviados + @"\\" + sData.Substring(3, 2) + "-" + sData.Substring(8, 2)); XDocument XmlCte = XDocument.Load(dPastaEnviadosMesAtual.ToString() + "\\" + "Cte_" + sChaveCte + ".xml"); if (XmlCte != null) { XmlCte.Save(dPastaCanceladosMesAtual + "\\Cte_" + sChaveCte + ".xml"); File.Delete(dPastaEnviadosMesAtual.ToString() + "\\" + "Cte_" + sChaveCte + ".xml"); } } } catch (Exception) { throw new Exception("Erro ao Salvar o CT-e de Chave " + sChaveCte + " na pasta Cancelados"); } }
public void SalvaArquivoPastaEnviado(string sNumCte, string sChaveCte) { try { string sData = HLP.Util.Util.GetDateServidor().Date.ToString("dd-MM-yyyy"); belGlobais objGlobais = new belGlobais(); DirectoryInfo dPastaEnviados = new DirectoryInfo(belStaticPastas.ENVIADOS); if (!dPastaEnviados.Exists) { dPastaEnviados.Create(); } DirectoryInfo dPastaEnviadosMesAtual = new DirectoryInfo(dPastaEnviados + @"\\" + sData.Substring(3, 2) + "-" + sData.Substring(8, 2)); if (!dPastaEnviadosMesAtual.Exists) { dPastaEnviadosMesAtual.Create(); } DirectoryInfo dPastaEnvio = new DirectoryInfo(belStaticPastas.ENVIO); if (dPastaEnvio.Exists) { DirectoryInfo dPastaEnvioMesAtual = new DirectoryInfo(dPastaEnvio.FullName + "\\" + sData.Substring(3, 2) + "-" + sData.Substring(8, 2)); XDocument XmlCte = XDocument.Load(dPastaEnvioMesAtual.FullName.ToString() + "\\" + "Cte_" + sNumCte.PadLeft(6, '0') + ".xml"); //sicupira if (XmlCte != null) { XmlDocument doc = MontaXmlConsultaSituacao(sChaveCte); string sRetorno = ConsultarSituacao(doc, true, sChaveCte); sRetorno = sRetorno.Substring(sRetorno.IndexOf("<protCTe"), sRetorno.IndexOf("</retConsSitCTe>") - sRetorno.IndexOf("<protCTe")); // MessageBox.Show("retorno = " + sRetorno); string sX = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<cteProc versao=\"1.04\" xmlns=\"http://www.portalfiscal.inf.br/cte\">" + XmlCte.ToString() + sRetorno + "</cteProc>"; StreamWriter sw = new StreamWriter(dPastaEnviadosMesAtual.FullName + "\\Cte_" + sChaveCte + ".xml"); sw.Write(sX); sw.Close(); // MessageBox.Show(dPastaEnviadosMesAtual.FullName + "\\Cte_" + sChaveCte + ".xml"); } else { MessageBox.Show("XmlCte nullo - " + dPastaEnvioMesAtual.FullName.ToString() + "\\" + "Cte_" + sNumCte.PadLeft(6, '0') + ".xml"); } } } catch (Exception ex) { throw new Exception("Erro ao Salvar o CT-e " + sNumCte + " na pasta Enviados" + Environment.NewLine + ex.Message); } }
public string ConsultarSituacao(XmlDocument doc, bool SalvarXml, string sChave) { string sRetorno = ""; try { belUF objUf = new belUF(); objUf.SiglaUF = "SP"; //Homologação if (belStatic.TpAmb == 2) { HLP.WebService.CTe_Homologacao_cteConsulta.cteCabecMsg objCabecStatus = new HLP.WebService.CTe_Homologacao_cteConsulta.cteCabecMsg(); objCabecStatus.cUF = objUf.CUF; objCabecStatus.versaoDados = "1.04"; HLP.WebService.CTe_Homologacao_cteConsulta.CteConsulta objConsulta = new HLP.WebService.CTe_Homologacao_cteConsulta.CteConsulta(); objConsulta.cteCabecMsgValue = objCabecStatus; objConsulta.ClientCertificates.Add(cert); sRetorno = objConsulta.cteConsultaCT(doc).OuterXml; } //Produção else if (belStatic.TpAmb == 1) { HLP.WebService.CTe_Producao_cteConsulta.cteCabecMsg objCabecStatus = new HLP.WebService.CTe_Producao_cteConsulta.cteCabecMsg(); objCabecStatus.cUF = objUf.CUF; objCabecStatus.versaoDados = "1.04"; HLP.WebService.CTe_Producao_cteConsulta.CteConsulta objConsulta = new HLP.WebService.CTe_Producao_cteConsulta.CteConsulta(); objConsulta.cteCabecMsgValue = objCabecStatus; objConsulta.ClientCertificates.Add(cert); sRetorno = objConsulta.cteConsultaCT(doc).OuterXml; } if (SalvarXml) { string sData = HLP.Util.Util.GetDateServidor().Date.ToString("dd-MM-yyyy"); belGlobais objGlobais = new belGlobais(); DirectoryInfo dPastaProtocolo = new DirectoryInfo(belStaticPastas.PROTOCOLOS); if (!dPastaProtocolo.Exists) { dPastaProtocolo.Create(); } DirectoryInfo dPastaMesAtual = new DirectoryInfo(dPastaProtocolo + @"\\" + sData.Substring(3, 2) + "-" + sData.Substring(8, 2)); if (!dPastaMesAtual.Exists) { dPastaMesAtual.Create(); } XDocument docRet = XDocument.Parse(sRetorno); docRet.Save(dPastaMesAtual + "\\Env_" + sChave + ".xml"); } return sRetorno; } catch (Exception ex) { throw ex; } }