private void btInutiliza_Click(object sender, EventArgs e) { ClientEnvironment manager = null; Parametro oParam = null; FuncaoAutomacao oFuncao = null; try { manager = Conexao.CreateManager(Program.ConAux); oParam = Program.GetParametro(Program.empresaSelecionada, manager); oFuncao = new FuncaoAutomacao(Program.empresaSelecionada, manager, Program.enviarInformacoesSobreErros); if (Int32.Parse(tbNotaInicial.Text) > Int32.Parse(tbNotaFinal.Text)) { throw new Exception("Nota Inicial deve ser menor que Nota Final."); } //Código da UF + CNPJ + modelo + série + nro inicial e nro final precedida do literal “ID” ITInutNFe oInutilizacao = (ITInutNFe)XMLUtils.XMLFactory(oParam.versao, "TInutNFe"); oInutilizacao.versao = oParam.versaoDados; oInutilizacao.infInut = (ITInutNFeInfInut)XMLUtils.XMLFactory(oParam.versao, "TInutNFeInfInut"); oInutilizacao.infInut.ano = tbAno.Text; oInutilizacao.infInut.CNPJ = tbCNPJ.Text; oInutilizacao.infInut.cUF = NFeUtils.DefineUF(cbUF.SelectedValue.ToString()); oInutilizacao.infInut.Id = "ID" + cbUF.SelectedValue.ToString() + tbAno.Text.PadLeft(2, '0') + tbCNPJ.Text + tbMod.Text + tbSerie.Text.PadLeft(3, '0') + tbNotaInicial.Text.PadLeft(9, '0') + tbNotaFinal.Text.PadLeft(9, '0'); oInutilizacao.infInut.mod = oParam.conexao == TipoConexao.NFCe ? TMod.Item65 : TMod.Item55; oInutilizacao.infInut.nNFIni = tbNotaInicial.Text; oInutilizacao.infInut.nNFFin = tbNotaFinal.Text; oInutilizacao.infInut.serie = tbSerie.Text; oInutilizacao.infInut.tpAmb = (TAmb)NFeUtils.DefineEnum(cbTipoAmbiente.SelectedValue.ToString(), typeof(TAmb)); oInutilizacao.infInut.xJust = tbJust.Text; oInutilizacao.infInut.xServ = TInutNFeInfInutXServ.INUTILIZAR; String cStat = String.Empty; String xMotivo = String.Empty; oFuncao.InutilizaNumeracao(oInutilizacao, ref cStat, ref xMotivo, oParam.versao); if (cStat == String.Empty && xMotivo == String.Empty) //recebeu resposta da sefaz { throw new Exception("Não foi possível executar Inutilização. Consulte o LOG do sistema."); } if (cStat != "102") { throw new Exception(xMotivo); } var oRetInut = (ITRetInutNFe)XMLUtils.LoadXMLFile(oParam.pastaRecibo + oInutilizacao.infInut.Id + "-inu.xml", oParam.versao, "TRetInutNFe"); oInutilizacao = (ITInutNFe)XMLUtils.LoadXMLFile(oParam.pastaRecibo + oInutilizacao.infInut.Id + "-ped-inu.xml", oParam.versao, "TInutNFe"); Int32 notaInicial = Int32.Parse(oInutilizacao.infInut.nNFIni); Int32 notaFinal = Int32.Parse(oInutilizacao.infInut.nNFFin); while (notaInicial <= notaFinal) { NotaInutilizada oNota = new NotaInutilizada(); oNota.numeroNota = notaInicial.ToString(); //setar a serie da nota oNota.serieNota = oInutilizacao.infInut.serie; oNota.data = DateTime.Today; oNota.empresa = oParam.empresa; oNota.XMLResposta = XMLUtils.GetXML(oRetInut, oParam.versao); oNota.XMLPedido = XMLUtils.GetXML(oInutilizacao, oParam.versao); oNota.Save(manager); notaInicial++; } MessageBox.Show(xMotivo); } catch (Exception ex) { if (oFuncao != null) { oFuncao.CriaLog(999, "Inutilização de Notas", ex); } MessageBox.Show(ex.Message); } finally { oParam = null; oFuncao = null; Conexao.DisposeManager(manager); } }
public static void ThreadMasterExecution(Object oThreadIdetificada) { ThreadIdetificada oTh = (ThreadIdetificada)oThreadIdetificada; String ConAux = (String)oTh.args[0]; String empresa = (String)oTh.args[1]; bool enviarErro = false; if (oTh.args.Length > 2) { enviarErro = (bool)oTh.args[2]; } ClientEnvironment manager = null; FuncaoAutomacao oFuncao = null; try { oTh.executando = true; oTh.DoOnStart(null, new StatusEventArgs() { empresa = empresa }); manager = Conexao.CreateManager(ConAux); oFuncao = new FuncaoAutomacao(empresa, manager, enviarErro); oFuncao.CriaLog(998, "Serviço de monitoramento inicializado com sucesso."); DateTime dataUltimaExecucao = DateTime.Now; Int32 totalMilliSecondsTime = 1000; while (!oTh.finalizar) { TimeSpan elapsed = DateTime.Now.Subtract(dataUltimaExecucao); if (elapsed.TotalMilliseconds < totalMilliSecondsTime) { Double delay = totalMilliSecondsTime - elapsed.TotalMilliseconds; try { Thread.Sleep((int)delay); } catch { } } else { //verificar estado da conexao if (manager.connection.State != System.Data.ConnectionState.Open) { //conexao não esta aberta manager = Conexao.CreateManager(ConAux); oFuncao.UpdateManager(manager); } dataUltimaExecucao = DateTime.Now; NFeUtils.GeraHeartBeat(true, empresa); oFuncao.DoOnLoop(false, ""); } } oTh.executando = false; oTh.DoOnStop(null, new StatusEventArgs() { empresa = empresa }); oFuncao.CriaLog(997, "Serviço de monitoramento paralizado com sucesso."); } catch (Exception ex) { ExceptionEventArgs ev = new ExceptionEventArgs(); ev.error = ex; oTh.DoOnError(null, ev); } finally { oFuncao = null; oTh.executando = false; oTh.finalizado = true; Conexao.DisposeManager(manager); } }