Пример #1
0
        private void metroButton1_Click(object sender, EventArgs e)
        {
            this.makebackup();

            try
            {
                this.edtUF.Text = this.edtUF.Text.ToUpper();

                if (string.IsNullOrEmpty(this.edtNome.Text))
                    throw new Exception("Nome deve ser informado");

                if (string.IsNullOrEmpty(this.edtUF.Text))
                    throw new Exception("UF deve ser informada");

                if (this.uf == null)
                {
                    this.edtID.Text = this.edtID.Text.PadLeft(this._tipo == NFe.Components.TipoAplicativo.Nfse ? 7 : 2, '0');

                    if (this.edtUF.Text == "XX")
                        throw new Exception("Não atualize nada nesta UF");

                    if (Convert.ToInt32(this.edtID.Text) < 900 && this._tipo == NFe.Components.TipoAplicativo.Nfe)
                    {
                        if (!NFe.Components.Functions.UFParaCodigo(this.edtUF.Text).Equals(Convert.ToInt32(this.edtID.Text)))
                        {
                            throw new Exception("Divergência entre 'UF' e 'ID'");
                        }
                    }

                    if (this._tipo == NFe.Components.TipoAplicativo.Nfse)
                    {
                        int cUF = NFe.Components.Functions.UFParaCodigo(this.edtUF.Text);
                        if (cUF != Convert.ToInt32(this.edtID.Text.Substring(0, 2)) || cUF == 0)
                        {
                            throw new Exception("Divergência entre 'UF' e 'ID'");
                        }
                    }
                }
                string key = this.edtUF.Text + " - " +
                            this.edtID.Text +
                            (this._tipo == NFe.Components.TipoAplicativo.Nfse ? (" - " + this.edtPadrao.SelectedItem.ToString()) : "");

                Estado item = null;
                try
                {
                    item = listaEstados[edtEstados.SelectedIndex];
                }
                catch
                {
                    item = new Estado();
                }

                if (item != null)
                    if (!key.Equals(item.key))  //é diferente da selecionada?
                    {
                        if (listageral.Contains(key))   //a nova chave existe?
                            throw new Exception("Já existe uma configuração com os parâmetros informados");
                    }

                StringBuilder erro = new StringBuilder();
                for (int vs = 0; vs < xlabels.Count; ++vs)
                {
                    string c = (this.tpProd.Controls["P_" + xlabels[vs]] as UserControl2).metroTextBox1.Text;
                    dummy.ArquivoExiste(erro, configname, c);

                    c = (this.tpHomo.Controls["H_" + xlabels[vs]] as UserControl2).metroTextBox1.Text;
                    dummy.ArquivoExiste(erro, configname, c);
                }
                if (erro.Length > 0)
                {
                    if (!(MetroFramework.MetroMessageBox.Show(null, erro.ToString() + "\r\n\r\nConfirma mesmo assim?", "Erro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
                        return;
                }

                if (this._tipo == NFe.Components.TipoAplicativo.Nfse && this.uf == null)
                    if (!this.edtNome.Text.Trim().EndsWith(this.edtUF.Text))
                        this.edtNome.Text += " - " + this.edtUF.Text;

                XElement[] eleProducao = new XElement[this.xlabels.Count];
                XElement[] eleHomologa = new XElement[this.xlabels.Count];
                for (int vs = 0; vs < xlabels.Count; ++vs)
                {
                    eleProducao[vs] = new XElement(xlabels[vs], (this.tpProd.Controls["P_" + xlabels[vs]] as UserControl2).metroTextBox1.Text);
                    eleHomologa[vs] = new XElement(xlabels[vs], (this.tpHomo.Controls["H_" + xlabels[vs]] as UserControl2).metroTextBox1.Text);
                }

                XElement users = XElement.Load(this.configname);
                IEnumerable<XElement> elements = users.Elements();

                if (item != null)
                {
                    if (!string.IsNullOrEmpty(item.ID))
                    {
                        /*if (this._tipo == NFe.Components.TipoAplicativo.Nfse)
                            elements.AncestorsAndSelf(NFe.Components.NFeStrConstants.Estado).
                                Where(x1 => x1.Attribute(NFe.Components.TpcnResources.ID.ToString()).Value == item.ID &&
                                            x1.Attribute(NFe.Components.NFeStrConstants.Padrao).Value == item.Padrao &&
                                            x1.Attribute(NFe.Components.TpcnResources.UF.ToString()).Value == item.UF).Remove();
                        else*/
                        elements.AncestorsAndSelf(NFe.Components.NFeStrConstants.Estado).
                            Where(x1 => x1.Attribute(NFe.Components.TpcnResources.ID.ToString()).Value == item.ID &&
                                        x1.Attribute(NFe.Components.TpcnResources.UF.ToString()).Value == item.UF).Remove();
                    }
                }
                XElement ele = new XElement(NFe.Components.NFeStrConstants.Estado,
                    new XAttribute(NFe.Components.TpcnResources.ID.ToString(), this.edtID.Text),
                    new XAttribute(NFe.Components.NFeStrConstants.Nome, this.edtNome.Text),
                    new XAttribute(NFe.Components.TpcnResources.UF.ToString(), this.edtUF.Text),
                    new XAttribute((this._tipo == NFe.Components.TipoAplicativo.Nfse ? NFe.Components.NFeStrConstants.Padrao : NFe.Components.NFeStrConstants.SVC), this.edtPadrao.SelectedItem.ToString()),
                    new XElement(NFe.Components.NFeStrConstants.LocalHomologacao, eleHomologa),
                    new XElement(NFe.Components.NFeStrConstants.LocalProducao, eleProducao)
                );
                users.Add(ele);

                users.Save(this.configname);

                /********** tirado pq quando terminar, é lido tudo novamente
                 * 
                if (dummy.listageral.ContainsKey(key))   //a nova chave existe?
                {
                    item.Nome = this.edtNome.Text;
                    item.ID = this.edtID.Text;
                    item.UF = this.edtUF.Text;
                    if (this._tipo == NFe.Components.TipoAplicativo.Nfse)
                        item.Padrao = this.edtPadrao.SelectedItem.ToString();
                    else
                        item.svc = this.edtPadrao.SelectedItem.ToString();
                }
                else
                {
                    Estado estado = new Estado();
                    estado.Nome = this.edtNome.Text;
                    estado.ID = this.edtID.Text;
                    estado.UF = this.edtUF.Text;
                    if (this._tipo == NFe.Components.TipoAplicativo.Nfse)
                        estado.Padrao = this.edtPadrao.SelectedItem.ToString();
                    else
                        estado.svc = this.edtPadrao.SelectedItem.ToString();

                    dummy.listageral.Add(key, (int)this._tipo);

                    listaEstados.Add(estado);
                }
                *********/

                RefreshDados();

                MetroFramework.MetroMessageBox.Show(null, "OK Atualizado com sucesso!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                this.restorebackup();
                MetroFramework.MetroMessageBox.Show(null, ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Пример #2
0
        private void metroButton1_Click(object sender, EventArgs e)
        {
            this.makebackup();

            try
            {
                this.edtUF.Text = this.edtUF.Text.ToUpper();

                if (string.IsNullOrEmpty(this.edtNome.Text))
                {
                    throw new Exception("Nome deve ser informado");
                }

                if (string.IsNullOrEmpty(this.edtUF.Text))
                {
                    throw new Exception("UF deve ser informada");
                }

                if (this.uf == null)
                {
                    this.edtID.Text = this.edtID.Text.PadLeft(this._tipo == NFe.Components.TipoAplicativo.Nfse ? 7 : 2, '0');

                    if (this.edtUF.Text == "XX")
                    {
                        throw new Exception("Não atualize nada nesta UF");
                    }

                    if (Convert.ToInt32(this.edtID.Text) < 900 && this._tipo == NFe.Components.TipoAplicativo.Nfe)
                    {
                        if (!NFe.Components.Functions.UFParaCodigo(this.edtUF.Text).Equals(Convert.ToInt32(this.edtID.Text)))
                        {
                            throw new Exception("Divergência entre 'UF' e 'ID'");
                        }
                    }

                    if (this._tipo == NFe.Components.TipoAplicativo.Nfse)
                    {
                        int cUF = NFe.Components.Functions.UFParaCodigo(this.edtUF.Text);
                        if (cUF != Convert.ToInt32(this.edtID.Text.Substring(0, 2)) || cUF == 0)
                        {
                            throw new Exception("Divergência entre 'UF' e 'ID'");
                        }
                    }
                }
                string key = this.edtUF.Text + " - " +
                             this.edtID.Text +
                             (this._tipo == NFe.Components.TipoAplicativo.Nfse ? (" - " + this.edtPadrao.SelectedItem.ToString()) : "");

                Estado item = null;
                try
                {
                    item = listaEstados[edtEstados.SelectedIndex];
                }
                catch
                {
                    item = new Estado();
                }

                if (item != null)
                {
                    if (!key.Equals(item.key))        //é diferente da selecionada?
                    {
                        if (listageral.Contains(key)) //a nova chave existe?
                        {
                            throw new Exception("Já existe uma configuração com os parâmetros informados");
                        }
                    }
                }

                StringBuilder erro = new StringBuilder();
                for (int vs = 0; vs < xlabels.Count; ++vs)
                {
                    string c = (this.tpProd.Controls["P_" + xlabels[vs]] as UserControl2).metroTextBox1.Text;
                    dummy.ArquivoExiste(erro, configname, c);

                    c = (this.tpHomo.Controls["H_" + xlabels[vs]] as UserControl2).metroTextBox1.Text;
                    dummy.ArquivoExiste(erro, configname, c);
                }
                if (erro.Length > 0)
                {
                    if (!(MetroFramework.MetroMessageBox.Show(null, erro.ToString() + "\r\n\r\nConfirma mesmo assim?", "Erro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
                    {
                        return;
                    }
                }

                if (this._tipo == NFe.Components.TipoAplicativo.Nfse && this.uf == null)
                {
                    if (!this.edtNome.Text.Trim().EndsWith(this.edtUF.Text))
                    {
                        this.edtNome.Text += " - " + this.edtUF.Text;
                    }
                }

                XElement[] eleProducao = new XElement[this.xlabels.Count];
                XElement[] eleHomologa = new XElement[this.xlabels.Count];
                for (int vs = 0; vs < xlabels.Count; ++vs)
                {
                    eleProducao[vs] = new XElement(xlabels[vs], (this.tpProd.Controls["P_" + xlabels[vs]] as UserControl2).metroTextBox1.Text);
                    eleHomologa[vs] = new XElement(xlabels[vs], (this.tpHomo.Controls["H_" + xlabels[vs]] as UserControl2).metroTextBox1.Text);
                }

                XElement users = XElement.Load(this.configname);
                IEnumerable <XElement> elements = users.Elements();

                if (item != null)
                {
                    if (!string.IsNullOrEmpty(item.ID))
                    {
                        /*if (this._tipo == NFe.Components.TipoAplicativo.Nfse)
                         *  elements.AncestorsAndSelf(NFe.Components.NFeStrConstants.Estado).
                         *      Where(x1 => x1.Attribute(NFe.Components.TpcnResources.ID.ToString()).Value == item.ID &&
                         *                  x1.Attribute(NFe.Components.NFeStrConstants.Padrao).Value == item.Padrao &&
                         *                  x1.Attribute(NFe.Components.TpcnResources.UF.ToString()).Value == item.UF).Remove();
                         * else*/
                        elements.AncestorsAndSelf(NFe.Components.NFeStrConstants.Estado).
                        Where(x1 => x1.Attribute(NFe.Components.TpcnResources.ID.ToString()).Value == item.ID &&
                              x1.Attribute(NFe.Components.TpcnResources.UF.ToString()).Value == item.UF).Remove();
                    }
                }
                XElement ele = new XElement(NFe.Components.NFeStrConstants.Estado,
                                            new XAttribute(NFe.Components.TpcnResources.ID.ToString(), this.edtID.Text),
                                            new XAttribute(NFe.Components.NFeStrConstants.Nome, this.edtNome.Text),
                                            new XAttribute(NFe.Components.TpcnResources.UF.ToString(), this.edtUF.Text),
                                            new XAttribute((this._tipo == NFe.Components.TipoAplicativo.Nfse ? NFe.Components.NFeStrConstants.Padrao : NFe.Components.NFeStrConstants.SVC), this.edtPadrao.SelectedItem.ToString()),
                                            new XElement(NFe.Components.NFeStrConstants.LocalHomologacao, eleHomologa),
                                            new XElement(NFe.Components.NFeStrConstants.LocalProducao, eleProducao)
                                            );
                users.Add(ele);

                users.Save(this.configname);

                /********** tirado pq quando terminar, é lido tudo novamente
                 *
                 * if (dummy.listageral.ContainsKey(key))   //a nova chave existe?
                 * {
                 *  item.Nome = this.edtNome.Text;
                 *  item.ID = this.edtID.Text;
                 *  item.UF = this.edtUF.Text;
                 *  if (this._tipo == NFe.Components.TipoAplicativo.Nfse)
                 *      item.Padrao = this.edtPadrao.SelectedItem.ToString();
                 *  else
                 *      item.svc = this.edtPadrao.SelectedItem.ToString();
                 * }
                 * else
                 * {
                 *  Estado estado = new Estado();
                 *  estado.Nome = this.edtNome.Text;
                 *  estado.ID = this.edtID.Text;
                 *  estado.UF = this.edtUF.Text;
                 *  if (this._tipo == NFe.Components.TipoAplicativo.Nfse)
                 *      estado.Padrao = this.edtPadrao.SelectedItem.ToString();
                 *  else
                 *      estado.svc = this.edtPadrao.SelectedItem.ToString();
                 *
                 *  dummy.listageral.Add(key, (int)this._tipo);
                 *
                 *  listaEstados.Add(estado);
                 * }
                 *********/

                RefreshDados();

                MetroFramework.MetroMessageBox.Show(null, "OK Atualizado com sucesso!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                this.restorebackup();
                MetroFramework.MetroMessageBox.Show(null, ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }