示例#1
0
 private void UpdateBtn_Click(object sender, RoutedEventArgs e)
 {
     if (!(ProductsDgd.SelectedItem is Products product))
     {
         return;
     }
     ParentView.Show_NewProductView(product);
 }
        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            var name              = ProductTxt.Text;
            var categoryParsed    = Enum.TryParse(CategoryCmb.Text, out ProductCategory category);
            var priceParsed       = decimal.TryParse(PriceTxt.Text, out decimal price);
            var packageUnitParsed = Enum.TryParse(UnitCmb.Text, out PackageUnit packageUnit);
            var weightParsed      = decimal.TryParse(WeightTxt.Text, out decimal weight);
            var wasteParsed       = decimal.TryParse(WasteTxt.Text, out decimal waste);
            var yieldParsed       = decimal.TryParse(YieldTxt.Text, out decimal yield);

            if (name == "" || name == string.Empty)
            {
                WarningTbk.Text       = "Ingrese el nombre del producto";
                WarningTbk.Visibility = Visibility.Visible;
            }
            else if (!categoryParsed || CategoryCmb.Text == "-Seleccione una opción-")
            {
                WarningTbk.Text       = "Verifique que haya seleccionado una categoría";
                WarningTbk.Visibility = Visibility.Visible;
            }
            else if (!priceParsed)
            {
                WarningTbk.Text       = "Ingrese el precio del producto";
                WarningTbk.Visibility = Visibility.Visible;
            }
            else if (!packageUnitParsed || UnitCmb.Text == "-Seleccione una opción-")
            {
                WarningTbk.Text       = "Verifique que haya seleccionado una unidad de medida";
                WarningTbk.Visibility = Visibility.Visible;
            }
            else if (!weightParsed)
            {
                WarningTbk.Text       = "Ingrese el peso del producto";
                WarningTbk.Visibility = Visibility.Visible;
            }
            else if (!wasteParsed)
            {
                WarningTbk.Text       = "Ingrese el porcentaje de merma";
                WarningTbk.Visibility = Visibility.Visible;
            }
            else if (!yieldParsed)
            {
                WarningTbk.Text       = "Verifique que todos los datos se hayan ingresado";
                WarningTbk.Visibility = Visibility.Visible;
            }
            else if (Cost <= 0)
            {
                WarningTbk.Text       = "Verifique que todos los datos se hayan ingresado";
                WarningTbk.Visibility = Visibility.Visible;
            }
            else
            {
                if (_product != null)
                {
                    try
                    {
                        if (weight != 1)
                        {
                            var yieldPercent  = yield / 100;
                            var yieldByWeight = 1 / yieldPercent;
                            var priceByOne    = Math.Round(price / weight, 2);
                            var costByOne     = Math.Round(yieldByWeight * priceByOne, 2);
                            using (var db = App.DbFactory.Open())
                            {
                                db.Update(new Products
                                {
                                    Id       = _product.Id,
                                    Name     = name,
                                    Category = category,
                                    Price    = priceByOne,
                                    Unit     = packageUnit,
                                    Weight   = 1,
                                    Waste    = waste,
                                    Yield    = yield,
                                    Cost     = costByOne
                                });
                            }
                        }
                        else
                        {
                            using (var db = App.DbFactory.Open())
                            {
                                db.Update(new Products
                                {
                                    Id       = _product.Id,
                                    Name     = name,
                                    Category = category,
                                    Price    = price,
                                    Unit     = packageUnit,
                                    Weight   = weight,
                                    Waste    = waste,
                                    Yield    = yield,
                                    Cost     = Cost
                                });
                            }
                        }
                        ParentView.Show_MessageView("El producto se ha actualizado con éxito\n¿Deseas guardar uno nuevo?",
                                                    //affirmative action
                                                    delegate
                        {
                            ParentView.Show_NewProductView();
                        },
                                                    null,
                                                    //negative action
                                                    delegate
                        {
                            ParentView.Show_ProductsView();
                        },
                                                    null,
                                                    FontAwesome.WPF.FontAwesomeIcon.CheckCircle
                                                    );
                    }
                    catch (Exception)
                    {
                        ParentView.Show_MessageView("Hubo un problema al actualizar el producto\nComuniquese a soporte técnico",
                                                    //affirmative action
                                                    delegate
                        {
                            ParentView.Show_NewProductView();
                        },
                                                    "Aceptar",
                                                    //negative action
                                                    null,
                                                    null,
                                                    FontAwesome.WPF.FontAwesomeIcon.ExclamationCircle
                                                    );
                    }
                }
                else
                {
                    Products product;
                    if (weight != 1)
                    {
                        var yieldPercent  = yield / 100;
                        var yieldByWeight = 1 / yieldPercent;
                        var priceByOne    = Math.Round(price / weight, 2);
                        var costByOne     = Math.Round(yieldByWeight * priceByOne, 2);
                        product = new Products
                        {
                            Name     = name,
                            Category = category,
                            Price    = priceByOne,
                            Unit     = packageUnit,
                            Weight   = 1,
                            Waste    = waste,
                            Yield    = yield,
                            Cost     = costByOne
                        };
                    }
                    else
                    {
                        product = new Products
                        {
                            Name     = name,
                            Category = category,
                            Price    = price,
                            Unit     = packageUnit,
                            Weight   = weight,
                            Waste    = waste,
                            Yield    = yield,
                            Cost     = Cost
                        };
                    }

                    try
                    {
                        using (var db = App.DbFactory.Open())
                        {
                            db.Save(product);
                        }
                        ParentView.Show_MessageView("El producto se ha guardado con éxito\n¿Deseas guardar otro?",
                                                    //affirmative action
                                                    delegate
                        {
                            ParentView.Show_NewProductView();
                        },
                                                    null,
                                                    //negative action
                                                    delegate
                        {
                            ParentView.Show_ProductsView();
                        },
                                                    null,
                                                    FontAwesome.WPF.FontAwesomeIcon.CheckCircle
                                                    );
                    }
                    catch (Exception)
                    {
                        ParentView.Show_MessageView("Hubo un problema al agregar el producto\nComuniquese a soporte técnico",
                                                    //affirmative action
                                                    delegate
                        {
                            ParentView.Show_NewProductView();
                        },
                                                    "Aceptar",
                                                    //negative action
                                                    null,
                                                    null,
                                                    FontAwesome.WPF.FontAwesomeIcon.ExclamationCircle
                                                    );
                    }
                }
            }
        }
示例#3
0
 private void AddBtn_Click(object sender, RoutedEventArgs e)
 {
     ParentView.Show_NewProductView();
 }