private void button2_Click_1(object sender, EventArgs e) { string fileXml, nomeXml; XmlDocument xml = new XmlDocument(); config = new Utils.ReadConfigure().GetConfig(); GetCertificado(); enviaSeFaz = new EnviaSeFaz.ManagerSeFaz(config, cert); try { openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); openFileDialog1.InitialDirectory = @"C:\"; openFileDialog1.Filter = "XML Files (*.xml)|*.xml"; openFileDialog1.ShowDialog(); fileXml = openFileDialog1.FileName; xml.Load(openFileDialog1.FileName); nomeXml = Path.GetFileName(fileXml); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return; } try { enviaSeFaz.verificaStatusSefaz(); } catch (Exception exc) { Utils.Logger.getInstance.error(exc.ToString()); return; } try { Id_db = ManagerDB.Instance.SelectMaxNFCEidDB(config.configNFCe.Serie, nomeXml.Substring(3)); enviaSeFaz.enviaSefaz(xml); ManagerDB.Instance.InsertNfceStatus(Id_db, (int)StatusCupom.CupomEnviadoContigencia, "Enviado Sefaz Contingencia", ""); ManagerDB.Instance.UpdateReciboNFCe(Id_db, enviaSeFaz.GetRecibo()); if (ConsultaEnviado()) { ChangedFile(nomeXml, false); ManagerDB.Instance.InsertNfceStatus(Id_db, (int)StatusCupom.AprovadoContingencia, "Aprovado Uso da NFCe (Cont)", ""); } else { ChangedFile(nomeXml, true); ManagerDB.Instance.InsertNfceStatus(Id_db, (int)StatusCupom.CupomRejeitadoContigencia, "Rejeitado em contingencia ", enviaSeFaz.GetStatus()); } } catch (ApplicationException appEx) { Utils.Logger.getInstance.error(appEx.ToString()); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } MessageBox.Show("arquivo processado com sucesso: " + nomeXml); }
private void button3_Click(object sender, EventArgs e) { MessageQueue mq = new MessageQueue(@".\Private$\nfce_contingencia", false); MessageQueueTransaction transaction = new MessageQueueTransaction(); string nomeXml = ""; try { bool findFile = true; transaction.Begin(); System.Messaging.Message msg = mq.Receive(transaction); msg.Formatter = new XmlMessageFormatter(new String[] { "System.String,mscorlib" }); nomeXml = msg.Body.ToString(); lblArquivo.Text = nomeXml; XmlDocument xml = new XmlDocument(); try { string dir = String.Format("{0}", config.configMaquina.pathFiles + "\\contingencia\\" + nomeXml + ".xml"); xml.Load(dir); findFile = false; } catch (Exception ex) { MessageBox.Show("Arquivo nao encontrado: "+nomeXml+".xml"); } if(findFile) { try { openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); openFileDialog1.InitialDirectory = @"C:\"; openFileDialog1.Filter = "XML Files (*.xml)|*.xml"; openFileDialog1.ShowDialog(); string fileXml = openFileDialog1.FileName; if (fileXml.Contains(nomeXml)) { MessageBox.Show("Arquivo diferente do que deve ser buscado: "+nomeXml); return; } xml.Load(openFileDialog1.FileName); } catch (Exception ex) { MessageBox.Show("Arquivo nao encontrado: " + nomeXml + ".xml"); return; } } enviaSeFaz = new EnviaSeFaz.ManagerSeFaz(config, cert); try { enviaSeFaz.verificaStatusSefaz(); } catch (Exception exc) { Utils.Logger.getInstance.error(exc.ToString()); transaction.Abort(); return; } Id_db = ManagerDB.Instance.SelectMaxNFCEidDB(config.configNFCe.Serie, nomeXml.Substring(3)); enviaSeFaz.enviaSefaz(xml); ManagerDB.Instance.InsertNfceStatus(Id_db, (int)StatusCupom.CupomEnviadoContigencia, "Enviado Sefaz Contingencia", ""); ManagerDB.Instance.UpdateReciboNFCe(Id_db, enviaSeFaz.GetRecibo()); if (ConsultaEnviado()) { ChangedFile(nomeXml, false); ManagerDB.Instance.InsertNfceStatus(Id_db, (int)StatusCupom.AprovadoContingencia, "Aprovado Uso da NFCe (Cont)", ""); } else { ChangedFile(nomeXml, true); ManagerDB.Instance.InsertNfceStatus(Id_db, (int)StatusCupom.CupomRejeitadoContigencia, "Rejeitado em contingencia ", enviaSeFaz.GetStatus()); } transaction.Commit(); } catch (ApplicationException appEx) { transaction.Commit(); Utils.Logger.getInstance.error(appEx.ToString()); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } MessageBox.Show("arquivo processado com sucesso: " + nomeXml); }
private void worker_DoWork(object sender, DoWorkEventArgs e) { while (!e.Cancel) { try { if (mq.GetAllMessages().Count() > 0) { MessageQueueTransaction transaction = new MessageQueueTransaction(); string nomeXml = ""; try { enviar = new EnviaSeFaz.ManagerSeFaz(config, cert); transaction.Begin(); System.Messaging.Message msg = mq.Receive(transaction); msg.Formatter = new XmlMessageFormatter(new String[] { "System.String,mscorlib" }); nomeXml = msg.Body.ToString(); Utils.Logger.getInstance.error("nota: " + nomeXml); string dir = String.Format("{0}", config.configMaquina.pathFiles + "\\contingencia\\" + nomeXml + ".xml"); XmlDocument xml = new XmlDocument(); xml.Load(dir); try { enviar.verificaStatusSefaz(); } catch (Exception exc) { Utils.Logger.getInstance.error(exc.ToString()); transaction.Abort(); Thread.Sleep(10000); continue; } Id_db = ManagerDB.Instance.SelectMaxNFCEidDB(config.configNFCe.Serie, nomeXml.Substring(3)); enviar.enviaSefaz(xml); ManagerDB.Instance.InsertNfceStatus(Id_db, (int)StatusCupom.CupomEnviadoContigencia, "Enviado Sefaz Contingencia", ""); ManagerDB.Instance.UpdateReciboNFCe(Id_db, enviar.GetRecibo()); if (ConsultaEnviado()) { XmlDocument xmlfinal = enviar.GetXmlOK(); xmlfinal.Save(String.Format("{0}\\{1}.xml", config.configMaquina.pathFiles + "\\contingencia\\", nomeXml)); ChangedFile(nomeXml, false); ManagerDB.Instance.InsertNfceStatus(Id_db, (int)StatusCupom.AprovadoContingencia, "Aprovado Uso da NFCe (Cont)", ""); } else { ChangedFile(nomeXml, true); ManagerDB.Instance.InsertNfceStatus(Id_db, (int)StatusCupom.CupomRejeitadoContigencia, "Rejeitado em contingencia ", enviar.GetStatus()); } transaction.Commit(); } catch (WebException sEX) { transaction.Abort(); Utils.Logger.getInstance.error("Problema na Sefaz: "+sEX.ToString()); } catch (ApplicationException appEx) { ManagerDB.Instance.InsertNfceStatus(Id_db, (int)StatusCupom.CupomRejeitadoContigencia, "Rejeitado em contingencia ", enviar.GetStatus()); ChangedFile(nomeXml, true); transaction.Commit(); Utils.Logger.getInstance.error(appEx.ToString()); } catch (Exception ex) { transaction.Abort(); Utils.Logger.getInstance.error(ex.ToString()); } } else { Thread.Sleep(60000); } } catch (Exception ex) { Utils.Logger.getInstance.error(ex.ToString()); } } _resetEvent.Set(); // signal that worker is done }