private void Guardar(object sender, RoutedEventArgs e)
        {
            try
            {
                bool ExisteContribuyente = true;
                using (db_FacturaDigital db = new db_FacturaDigital())
                {
                    DataModel.EF.Contribuyente Contribuyente = db.Contribuyente.FirstOrDefault();
                    if (Contribuyente == null)
                    {
                        ExisteContribuyente = false;
                        Contribuyente       = new DataModel.EF.Contribuyente();
                    }

                    #region DatosGenerales
                    if (string.IsNullOrEmpty(txt_Identificacion.Text))
                    {
                        MessageBox.Show("Favor llenar el campo de identificacion", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.Identificacion_Numero = txt_Identificacion.Text;
                    Contribuyente.Identificacion_Tipo   = (cb_Cedula.SelectedItem as ComboBoxItem).Tag.ToString();


                    if (string.IsNullOrEmpty(txt_Nombre.Text))
                    {
                        MessageBox.Show("Favor llenar el campo de nombre", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.Nombre = txt_Nombre.Text;



                    if (string.IsNullOrEmpty(txt_comercial.Text))
                    {
                        MessageBox.Show("Favor llenar el campo de nombre comercial", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.NombreComercial = txt_comercial.Text;

                    if (string.IsNullOrEmpty(txt_Correo.Text))
                    {
                        MessageBox.Show("Favor llenar el campo de correo", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.CorreoElectronico = txt_Correo.Text;
                    int Region;
                    if (!int.TryParse(txt_TelefonoRegion.Text, out Region))
                    {
                        MessageBox.Show("Favor llenar el campo de region (solamente numeros) ", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.Telefono_Codigo = Region;

                    int telefono;
                    if (!int.TryParse(txt_TelefonoNumero.Text, out telefono))
                    {
                        MessageBox.Show("Favor llenar el campo de telefono (solamente numeros)", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.Telefono_Numero = telefono;
                    #endregion

                    #region Ubicacion
                    UbicacionesType provincia = cb_Provincia.SelectedItem as UbicacionesType;
                    if (provincia == null)
                    {
                        MessageBox.Show("Favor seleccionar una provincia", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.Provincia = provincia.Id;


                    UbicacionesType canton = cb_canton.SelectedItem as UbicacionesType;
                    if (canton == null)
                    {
                        MessageBox.Show("Favor seleccionar una Canton", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.Canton = canton.Id;


                    UbicacionesType Distrito = cb_distrito.SelectedItem as UbicacionesType;
                    if (Distrito == null)
                    {
                        MessageBox.Show("Favor seleccionar una Distrito", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.Distrito = Distrito.Id;


                    UbicacionesType Barrio = cb_barrio.SelectedItem as UbicacionesType;
                    if (Distrito == null)
                    {
                        MessageBox.Show("Favor seleccionar una Barrio", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.Barrio = Barrio.Id;


                    if (string.IsNullOrEmpty(txt_otrasSenas.Text))
                    {
                        MessageBox.Show("Favor llenar el campo de otras senas", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.OtrasSenas = txt_otrasSenas.Text;
                    #endregion

                    #region Datos Hacienda
                    if (string.IsNullOrEmpty(txt_UsuarioHacienda.Text))
                    {
                        MessageBox.Show("Favor llenar el campo de usuario de hacienda ", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.UsuarioHacienda = txt_UsuarioHacienda.Text;


                    if (string.IsNullOrEmpty(txt_contrasena.Text))
                    {
                        MessageBox.Show("Favor llenar el campo de contrasena de hacienda", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.ContrasenaHacienda = txt_contrasena.Text;
                    #endregion

                    #region Certificado
                    if (string.IsNullOrEmpty(CertificadoUrl) && (Contribuyente.Certificado == null || Contribuyente.Certificado.Length == 0))
                    {
                        MessageBox.Show("Favor seleccionar un certificado", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }

                    if (!string.IsNullOrEmpty(CertificadoUrl))
                    {
                        Contribuyente.Certificado = File.ReadAllBytes(CertificadoUrl);
                    }

                    if (string.IsNullOrEmpty(txt_contrasenaCertificado.Text))
                    {
                        MessageBox.Show("Favor llenar el campo de contrasena de certificado", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }
                    Contribuyente.Contrasena_Certificado = txt_contrasenaCertificado.Text;


                    try
                    {
                        X509Certificate2 x509 = new X509Certificate2(Contribuyente.Certificado, Contribuyente.Contrasena_Certificado, X509KeyStorageFlags.Exportable);
                        x509.Verify();
                        string Values       = x509.Subject.Split(',').First(q => q.ToUpper().Contains("SERIALNUMBER"));
                        string SerialNumber = Values.Split('=')[1];
                        string OnlyNumber   = new string(SerialNumber.Where(c => char.IsDigit(c)).ToArray());
                        if (!OnlyNumber.Contains(Contribuyente.Identificacion_Numero) && !Contribuyente.Identificacion_Numero.Contains(OnlyNumber))
                        {
                            if (MessageBox.Show("El numero de identificacion registrado en el certificado no concuerda con el ingresado por el usuario en el campo [Identificacion]. Aun asi desea continuar?", "Validacion", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
                            {
                                return;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        this.LogError(ex);
                        MessageBox.Show("No se pudo abrir el ceritificado. Esto se puede deber a que la contrasena indicada no es valida o que el contenido del certificado es invalido", "Error", MessageBoxButton.OK, MessageBoxImage.Stop);
                        return;
                    }


                    #endregion

                    if (!ExisteContribuyente)
                    {
                        db.Contribuyente.Add(Contribuyente);
                    }

                    db.SaveChanges();

                    Contribuyente_Consecutivos conse = db.Contribuyente_Consecutivos.FirstOrDefault(q => q.Id_Contribuyente == Contribuyente.Id_Contribuyente);

                    if (conse == null)
                    {
                        conse = new Contribuyente_Consecutivos()
                        {
                            Consecutivo_Facturas            = 1,
                            Consecutivo_NotasCredito        = 1,
                            Consecutivo_Tiquete_Electrónico = 1,
                            Consecutivo_Confirmacion        = 1,
                            Id_Contribuyente = Contribuyente.Id_Contribuyente
                        };
                        db.Contribuyente_Consecutivos.Add(conse);
                        db.SaveChanges();
                    }

                    MessageBox.Show("Perfil actualizado correctamente", "Informacion", MessageBoxButton.OK, MessageBoxImage.Information);
                    try
                    {
                        RecursosSistema.Contribuyente = Contribuyente;
                        RecursosSistema.OnStartMain_Load();
                    }catch (Exception ex)
                    {
                        this.LogError(ex);
                        MessageBox.Show("Reinicie el sistema para continuar");
                    }
                }
            }
            catch (Exception ex)
            {
                this.LogError(ex);
                MessageBox.Show("Ocurrio al guardar el perfil del contribuyente", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Ejemplo n.º 2
0
        private void Save(object sender, RoutedEventArgs e)
        {
            try
            {
                #region Validaciones
                if (string.IsNullOrEmpty(txt_host.Text))
                {
                    MessageBox.Show("Error debe de ingresar la dirección del servidor", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                    return;
                }

                if (string.IsNullOrEmpty(txt_Usuario.Text))
                {
                    MessageBox.Show("Error debe de ingresar el usuario", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                    return;
                }

                if (string.IsNullOrEmpty(txt_contrasena.Password))
                {
                    MessageBox.Show("Error debe de ingresar la contraseña", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                    return;
                }

                if (string.IsNullOrEmpty(txt_Puerto.Text))
                {
                    MessageBox.Show("Error debe de ingresar el numero de puerto", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                    return;
                }

                int puerto;
                if (!int.TryParse(txt_Puerto.Text, out puerto) || puerto <= 0)
                {
                    MessageBox.Show("El puerto debe de ser un dato numerico mayor a 0", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop);
                    return;
                }
                #endregion

                using (db_FacturaDigital db = new db_FacturaDigital())
                {
                    SMTP value = db.SMTP.FirstOrDefault();
                    if (value == null)
                    {
                        value = new SMTP();
                        db.SMTP.Add(value);
                    }


                    value.Contrasena       = txt_contrasena.Password;
                    value.Puerto           = puerto;
                    value.Url_Servidor     = txt_host.Text;
                    value.Usuario          = txt_Usuario.Text;
                    value.SSL              = chk_SSL.IsChecked.Value;
                    value.Detalle_Email    = txt_EmailDetails.Text;
                    value.Id_Contribuyente = RecursosSistema.Contribuyente.Id_Contribuyente;
                    db.SaveChanges();
                }
            }catch (Exception ex)
            {
                this.LogError(ex);
                MessageBox.Show("Error al guardar los datos del SMPT", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            MessageBox.Show("Datos de smtp almacenados correctamente", "Informacion", MessageBoxButton.OK, MessageBoxImage.Information);

            try
            {
                RecursosSistema.OnStartMain_Load();
            }
            catch (Exception ex)
            {
                this.LogError(ex);
                MessageBox.Show("Reinicie el sistema para continuar");
            }
        }