Beispiel #1
0
        private async void btnEjecutar_Click(object sender, RoutedEventArgs e)
        {
            lblNombre.Content = validador.validarNombre(txtNombre.Text);
            lblPrecio.Content = validador.validarPrecio(txtPrecio.Text);
            lblCodigo.Content = validador.validarCodigoInterno(txtCod.Text);

            Producto p = new Producto();

            ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();


            if (lblNombre.Content.Equals("OK") && lblPrecio.Content.Equals("OK") && lblCodigo.Content.Equals("OK"))
            {
                if (cbRubro.SelectedIndex == -1)
                {
                    lblRubro.Content    = "Seleccione una opcion";
                    lblRubro.Visibility = Visibility.Visible;
                }
                else
                {
                    string json;
                    switch (btnEjecutar.Content.ToString())
                    {
                    case "Agregar":

                        json = proxy.ReadAllRubros();
                        RubroCollections reCol = new RubroCollections(json);
                        string           aux   = string.Empty;

                        p.IdRubro = (int)cbRubro.SelectedValue;
                        foreach (var item in reCol)
                        {
                            if (p.IdRubro == item.IdRubro)
                            {
                                aux = item.TipoRubro;
                            }
                        }

                        int precio;
                        int.TryParse(txtPrecio.Text, out precio);
                        p.Precio        = precio;
                        p.CodigoInterno = txtCod.Text;
                        p.Nombre        = txtNombre.Text;
                        p.Sku           = txtNombre.Text.Substring(0, 4) + aux.Substring(0, 4);
                        p.Descripcion   = txtDescripcion.Text;



                        string jsons = p.Serializar();

                        if (proxy.CrearProducto(jsons))
                        {
                            await this.ShowMessageAsync("Exito", "Producto agregado!");

                            LimpiarControles();
                        }
                        else
                        {
                            await this.ShowMessageAsync("Error", "No se pudo agregar el producto");
                        };

                        break;

                    case "Modificar":

                        json = proxy.ReadAllRubros();
                        RubroCollections ruCol = new RubroCollections(json);
                        string           au    = string.Empty;

                        p.IdRubro = (int)cbRubro.SelectedValue;
                        foreach (var item in ruCol)
                        {
                            if (p.IdRubro == item.IdRubro)
                            {
                                au = item.TipoRubro;
                            }
                        }
                        int preci;
                        int.TryParse(txtPrecio.Text, out preci);
                        p.Precio        = preci;
                        p.CodigoInterno = txtCod.Text;
                        p.Nombre        = txtNombre.Text;
                        p.Sku           = txtNombre.Text.Substring(0, 4) + au.Substring(0, 4);
                        p.Descripcion   = txtDescripcion.Text;


                        Producto pr = (Producto)dgProd.SelectedValue;
                        p.IdProducto = pr.IdProducto;
                        jsons        = p.Serializar();

                        if (proxy.ActualizarProducto(jsons))
                        {
                            await this.ShowMessageAsync("Exito", "Producto Modificado!");

                            LimpiarControles();
                        }
                        else
                        {
                            await this.ShowMessageAsync("Error", "No se pudo agregar el producto");
                        };

                        break;

                    case "Eliminar":

                        Producto pro = (Producto)dgProd.SelectedValue;
                        json = pro.Serializar();
                        if (proxy.EliminarProducto(json))
                        {
                            await this.ShowMessageAsync("Exito", "Producto eliminado");

                            LimpiarControles();
                        }
                        else
                        {
                            await this.ShowMessageAsync("Error", "No se a podido eliminar");

                            LimpiarControles();
                        }

                        break;

                    default:
                        break;
                    }
                }
            }
            else
            {
                if (lblPrecio.Content.Equals("OK"))
                {
                    lblPrecio.Visibility = Visibility.Hidden;
                }
                else
                {
                    lblPrecio.Visibility = Visibility.Visible;
                }

                if (lblNombre.Content.Equals("OK"))
                {
                    lblNombre.Visibility = Visibility.Hidden;
                }
                else
                {
                    lblNombre.Visibility = Visibility.Visible;
                }

                if (lblCodigo.Content.Equals("OK"))
                {
                    lblCodigo.Visibility = Visibility.Hidden;
                }
                else
                {
                    lblCodigo.Visibility = Visibility.Visible;
                }
            }
        }