Пример #1
0
        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            if (txtCódigo.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Debe ingresar la identificación del cliente");
                txtCódigo.Focus();
                return;
            }

            if (txtNombre.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Debe ingresar el nombre del cliente");
                txtNombre.Focus();
                return;
            }

            if (txtApellidos.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Debe ingresar los apellidos del cliente");
                txtApellidos.Focus();
                return;
            }
            if (chkFacturaElectronica.IsChecked == true)
            {
                if (txtEmail.Text == "")
                {
                    MessageBox.Show("Debe ingresar el correo electroníco. Ej: [email protected]");
                    txtApellidos.Focus();
                    return;
                }
                else if (!check_email(txtEmail.Text))
                {
                    MessageBox.Show("Formato incorrecto en el campo de correo electroníco. Ej: [email protected]");
                    txtApellidos.Focus();
                    return;
                }
            }


            string caseSwitch = cmbTipoId.Text;
            short  tipoid;

            switch (caseSwitch)
            {
            case "Física":
                tipoid = 1;
                break;

            case "Jurídica":
                tipoid = 2;
                break;

            case "DIMEX":
                tipoid = 3;
                break;

            case "NITE":
                tipoid = 4;
                break;

            default:
                tipoid = 5;
                break;
            }

            using (var bd = new EmpeñosDataContext())
            {
                Cliente = bd.Clientes.SingleOrDefault(c => c.Código == txtCódigo.Value.ToString());

                if (Cliente == null && códigoCliente2 == "")
                {
                    Cliente = new Cliente {
                        TipoIdentificación = tipoid, Código = txtCódigo.Value.ToString()
                    };
                    bd.Clientes.InsertOnSubmit(Cliente);
                }
                else if (códigoCliente2 != "")
                {
                    try
                    {
                        bd.updateIDs(txtCódigo.Value.ToString(), códigoCliente2);
                        códigoCliente2 = "";
                        Cliente        = bd.Clientes.SingleOrDefault(c => c.Código == txtCódigo.Value.ToString());
                    }
                    catch (Exception t)
                    {
                        System.Windows.MessageBox.Show("Error al actualizar.  Ya existe un cliente con esta identificación.\n" + t.Message, "Error", MessageBoxButton.OK);
                    }
                }

                Cliente.TipoIdentificación = tipoid;
                Cliente.Nombre             = txtNombre.Text.Trim();
                Cliente.Apellidos          = txtApellidos.Text.Trim();
                Cliente.Género             = rbMasculino.IsChecked.Value ? 'M' : 'F';
                Cliente.Teléfono           = txtTeléfono.Text;
                Cliente.Email = txtEmail.Text;
                Cliente.RecibirNotificaciones  = chkRecibirNotificaciones.IsChecked.Value;
                Cliente.FacturacionElectronica = chkFacturaElectronica.IsChecked.Value;
                Cliente.Dirección = txtDirección.Text;
                Cliente.Notas     = txtNotas.Text;

                if (imgFoto.Source != null && !imgFoto.Source.ToString().StartsWith("pack"))
                {
                    using (var stream = new MemoryStream())
                    {
                        var encoder = new PngBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(imgFoto.Source as BitmapSource));
                        encoder.Save(stream);
                        Cliente.Foto = stream.ToArray();
                    }
                }
                else
                {
                    Cliente.Foto = null;
                }

                this.DialogResult = true;


                bd.SubmitChanges();
            }
        }