コード例 #1
0
        private void button5_Click(object sender, EventArgs e)
        {
            ClientEnvironment manager = null;
            Parametro         oParam  = null;

            try
            {
                manager = Conexao.CreateManager(Program.ConAux);
                oParam  = Program.GetParametro(Program.empresaSelecionada, manager);

                if (oNFe.codigoSituacao == TipoSituacaoNota.Cancelada)
                {
                    //verificar se foi cancelada por evento
                    if (oNFe.CanceladaPorEvento(manager))
                    {
                        #region cancelamento por evento
                        MessageBox.Show("Nota cancelada por evento");
                        //todo : enviar para DANFE.exe XML do evento.
                        #endregion
                    }
                    else
                    {
                        #region cancelamento por webservice
                        //tratar envio de xml para destinatário.
                        String nomeArquivo = oNFe.nProtCancelamento + "_v2.00-procCancNFe.xml";

                        //enviar email para destinatário ?
                        ITNFe oNFeXML = (ITNFe)XMLUtils.CarregaXML_STR(oNFe.xmlNota, oNFe.versao, "TNFe");
                        if (!String.IsNullOrEmpty(oNFeXML.infNFe.dest.email))
                        {
                            if (File.Exists(oParam.pastaImpressao + nomeArquivo.Replace(".xml", ".txt")))
                            {
                                File.Delete(oParam.pastaImpressao + nomeArquivo.Replace(".xml", ".txt"));
                            }

                            //gerar arquivo TXT com o email do destinatário
                            using (StreamWriter oSW = File.CreateText(oParam.pastaImpressao + nomeArquivo.Replace(".xml", ".txt")))
                            {
                                oSW.WriteLine(oNFeXML.infNFe.dest.email);
                                oSW.WriteLine(oNFeXML.infNFe.Id.Replace("NFe", ""));
                                oSW.WriteLine(oNFeXML.infNFe.ide.nNF);
                                oSW.WriteLine(oNFeXML.infNFe.ide.serie);
                                oSW.WriteLine(oNFeXML.infNFe.emit.xNome);
                                oSW.WriteLine(oNFe.nProtCancelamento);
                                oSW.WriteLine(oNFeXML.infNFe.dest.xNome);
                                oSW.Close();
                            }
                            GC.Collect();

                            //gerar arquivo txt com o email do destinatário
                            NFeUtils.GeraArquivoProcCancNFe(oNFe, oParam.pastaImpressao + nomeArquivo, oParam.versaoDados);

                            MessageBox.Show("Arquivo enviado para servidor de impressão.");
                        }
                        else
                        {
                            MessageBox.Show("Não existe email para enviar arquivo.");
                        }
                        oNFeXML = null;
                        #endregion
                    }
                }
                else
                {
                    MessageBox.Show("Nota não esta cancelada");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                oParam = null;
                Conexao.DisposeManager(manager);
            }
        }
コード例 #2
0
        private void dgvEventos_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex.Equals(2))
            {
                try
                {
                    FrmVisualizaXML oFrmXML = new FrmVisualizaXML(
                        ((Evento)dgvEventos.Rows[e.RowIndex].DataBoundItem).XMLPedido);
                    oFrmXML.Show();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else if (e.ColumnIndex.Equals(3))
            {
                try
                {
                    FrmVisualizaXML oFrmXML = new FrmVisualizaXML(
                        ((Evento)dgvEventos.Rows[e.RowIndex].DataBoundItem).XMLResposta);
                    oFrmXML.Show();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else if (e.ColumnIndex.Equals(4))
            {
                try
                {
                    oEvento = (Evento)dgvEventos.Rows[e.RowIndex].DataBoundItem;

                    if (oEvento.codigoSituacao != TipoSituacaoEvento.FinalizadoAprovado135 &&
                        oEvento.codigoSituacao != TipoSituacaoEvento.FinalizadoAprovado136 &&
                        oEvento.codigoSituacao != TipoSituacaoEvento.FinalizadoAprovado155)
                    {
                        throw new Exception("Evento não aprovado");
                    }


                    //solicitar usuario local para save
                    sfdProcNFeCCe.Filter = "Arquivo XML|*.xml";
                    sfdProcNFeCCe.Title  = "Salvar arquivo";

                    string sufixo = "";
                    if (oEvento.tpEvento == TEventoInfEventoTpEvento.CartaCorrecao)
                    {
                        sufixo = "_v1.00-procCCe.xml";
                    }
                    else if (oEvento.tpEvento == TEventoInfEventoTpEvento.Cancelamento)
                    {
                        sufixo = "_v1.00-procEventoCancNFe.xml";
                    }
                    else
                    {
                        sufixo = ".xml";
                    }

                    sfdProcNFeCCe.FileName = ((ITEvento)XMLUtils.CarregaXML_STR(oEvento.XMLPedido,
                                                                                oNFe.versao,
                                                                                "TEvento")).infEvento.Id + sufixo;

                    sfdProcNFeCCe.ShowDialog();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    oEvento = null;
                }
            }
            else if (e.ColumnIndex.Equals(5))
            {
                //gerar arquivo para danfe.exe
                #region imprimir evento

                ClientEnvironment manager = null;
                Parametro         oParam  = null;

                try
                {
                    manager = Conexao.CreateManager(Program.ConAux);
                    oParam  = Program.GetParametro(Program.empresaSelecionada, manager);
                    oEvento = (Evento)dgvEventos.Rows[e.RowIndex].DataBoundItem;

                    if (oEvento.codigoSituacao != TipoSituacaoEvento.FinalizadoAprovado135 &&
                        oEvento.codigoSituacao != TipoSituacaoEvento.FinalizadoAprovado136 &&
                        oEvento.codigoSituacao != TipoSituacaoEvento.FinalizadoAprovado155)
                    {
                        throw new Exception("Evento não aprovado");
                    }

                    //tratar envio de xml para destinatário.
                    string sufixo = "";
                    if (oEvento.tpEvento == TEventoInfEventoTpEvento.CartaCorrecao)
                    {
                        sufixo = "_v1.00-procCCe.xml";
                    }
                    else if (oEvento.tpEvento == TEventoInfEventoTpEvento.Cancelamento)
                    {
                        sufixo = "_v1.00-procEventoCancNFe.xml";
                    }
                    else
                    {
                        sufixo = ".xml";
                    }

                    String nomeArquivo = ((ITEvento)XMLUtils.CarregaXML_STR(oEvento.XMLPedido,
                                                                            oNFe.versao,
                                                                            "TEvento")).infEvento.Id + sufixo;

                    ITNFe oNFeXML = (ITNFe)
                                    XMLUtils.CarregaXML_STR(oNFe.xmlNota,
                                                            oNFe.versao,
                                                            "TNFe");

                    //salvar TXT com dados complementares
                    if (File.Exists(oParam.pastaImpressao + nomeArquivo.Replace(".xml", ".txt")))
                    {
                        File.Delete(oParam.pastaImpressao + nomeArquivo.Replace(".xml", ".txt"));
                    }

                    //gerar arquivo TXT com o email do destinatário
                    using (StreamWriter oSW = File.CreateText(oParam.pastaImpressao + nomeArquivo.Replace(".xml", ".txt")))
                    {
                        //dados destinatarios
                        oSW.WriteLine(oNFeXML.infNFe.dest.xNome);
                        oSW.WriteLine(oNFeXML.infNFe.dest.enderDest.xLgr + ", " + oNFeXML.infNFe.dest.enderDest.nro);
                        oSW.WriteLine(oNFeXML.infNFe.dest.enderDest.xBairro);
                        oSW.WriteLine(oNFeXML.infNFe.dest.enderDest.CEP);
                        oSW.WriteLine(oNFeXML.infNFe.dest.enderDest.xMun);
                        oSW.WriteLine(oNFeXML.infNFe.dest.enderDest.fone);
                        oSW.WriteLine(oNFeXML.infNFe.dest.enderDest.UF);
                        oSW.WriteLine(oNFeXML.infNFe.dest.IE);

                        //dados emitente
                        oSW.WriteLine(oNFeXML.infNFe.emit.xNome);
                        oSW.WriteLine(oNFeXML.infNFe.emit.enderEmit.xLgr + ", " + oNFeXML.infNFe.emit.enderEmit.nro);
                        oSW.WriteLine(oNFeXML.infNFe.emit.enderEmit.xBairro);
                        oSW.WriteLine(oNFeXML.infNFe.emit.enderEmit.CEP);
                        oSW.WriteLine(oNFeXML.infNFe.emit.enderEmit.xMun);
                        oSW.WriteLine(oNFeXML.infNFe.emit.enderEmit.fone);
                        oSW.WriteLine(oNFeXML.infNFe.emit.enderEmit.UF);
                        oSW.WriteLine(oNFeXML.infNFe.emit.IE);

                        oSW.WriteLine(oNFeXML.infNFe.ide.nNF);
                        oSW.WriteLine(oNFeXML.infNFe.ide.serie);
                        oSW.WriteLine(oNFeXML.infNFe.ide.dEmi);

                        oSW.Close();
                    }
                    GC.Collect();

                    NFeUtils.GeraArquivoProcEventoNFe(oEvento, oParam.pastaImpressao + nomeArquivo, oNFe.versao);

                    MessageBox.Show("Arquivo enviado para servidor de impressão.");

                    oNFeXML = null;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    oEvento = null;
                    Conexao.DisposeManager(manager);
                    oParam = null;
                }
                #endregion
            }
        }