/// <summary> /// Acción al añadir un producto en una venta. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddProduct_Click(object sender, RoutedEventArgs e) { // Obtener contenedor. DataTemplate auxDT = (FormGrid.Children[0] as ContentPresenter).ContentTemplate; // Obtener elementos de la GUI TextBox searchProducts = (TextBox)auxDT.FindName("ProductSearch", (ContentPresenter)FormGrid.Children[0]); StackPanel productsSP = (StackPanel)auxDT.FindName("Products", (ContentPresenter)FormGrid.Children[0]); // Crear variable y conexión para el producto. ProductsConn pc = new ProductsConn(); // Product product; try { selProd = pc.Get(int.Parse(searchProducts.Text)); // Intentar buscar el ID transformado. if (selProd == null) { throw new Exception(); } relationProd[(ActivityList.SelectedItem as ContentPresenter)] = selProd; } catch { Error error = new Error("El ID del producto introducido no existe."); error.Show(); // Si da algún error mostrar que ha fallado. return; } // Crear y añadir elemento a la lista visual. ContentPresenter productItem = new ContentPresenter(); productItem.ContentTemplate = (DataTemplate)Resources["ProductItem"]; productItem.Content = searchProducts.Text; productsSP.Children.Clear(); // Solo se permite uno, limpiar antes siempre. productsSP.Children.Add(productItem); // Refrescar contador de Total. float paying = selProd.ProdPrice; // Si es null Product ser 0. float discounting; if (selDisc == null) { discounting = 0; } else { discounting = selDisc.PricePerc; } float result = paying - (paying * discounting / 100); (auxDT.FindName("TotalCost", (ContentPresenter)FormGrid.Children[0]) as TextBlock).Text = "Total: " + result + "€"; }
private void Update() { ActivityList.Items.Clear(); FormGrid.Children.Clear(); SalesConn sc = new SalesConn(); RefundsConn rc = new RefundsConn(); ProductsConn pc = new ProductsConn(); DiscountsConn dc = new DiscountsConn(); List <Sale> sales = sc.List; List <Refund> refunds = rc.List; history.Clear(); relationProd.Clear(); relationDisc.Clear(); selProd = null; selDisc = null; // Crear elementos. Sale selSale; Product selProduct; Discount selDiscount; // Recuperar todas las ventas. for (int j = 0; j < sales.Count; j++) { ContentPresenter conItem = new ContentPresenter(); conItem.Content = sales[j].Id.ToString(); conItem.ContentTemplate = (DataTemplate)Resources["SaleMenuItem"]; int max = refunds.Count; // Si coincide con alguna devolución convertir elemento de menu en Return. for (int k = 0; k < max; k++) { if (sales[j].Id == refunds[k].SaleId) { conItem.ContentTemplate = (DataTemplate)Resources["ReturnMenuItem"]; } } // Añadimos el elemento. ActivityList.Items.Add(conItem); // Obtener Sale, Product y Discount. selSale = sales[j]; try { selProduct = pc.Get(selSale.ProdId.Value); } catch { selProduct = null; } try { selDiscount = dc.Get(selSale.DiscId.Value); } catch { selDiscount = null; } // Se crea su formulario ContentPresenter conForm = new ContentPresenter(); conForm.IsEnabled = false; conForm.ContentTemplate = (DataTemplate)Resources["SaleForm"]; conForm.ApplyTemplate(); // Obtener contenedor. DataTemplate auxDT = conForm.ContentTemplate; // Obtener elementos de la GUI StackPanel products = (StackPanel)auxDT.FindName("Products", conForm); StackPanel discounts = (StackPanel)auxDT.FindName("Discounts", conForm); float paying = 0; // Agregar producto if (selSale.ProdId != null && selSale.ProdId != 0) { ContentPresenter productItem = new ContentPresenter(); productItem.Content = selSale.ProdId; productItem.ContentTemplate = (DataTemplate)Resources["ProductItem"]; products.Children.Clear(); products.Children.Add(productItem); Product tmpProduct = pc.Get(int.Parse(productItem.Content.ToString())); paying = tmpProduct.ProdPrice; } float discounting = 0; // Agregar descuento if (selSale.DiscId != null && selSale.DiscId != 0) { ContentPresenter discountItem = new ContentPresenter(); discountItem.Content = selSale.DiscId; discountItem.ContentTemplate = (DataTemplate)Resources["DiscountItem"]; discounts.Children.Clear(); discounts.Children.Add(discountItem); Discount tmpDiscount = dc.Get(int.Parse(discountItem.Content.ToString())); discounting = tmpDiscount.PricePerc; (auxDT.FindName("Discounted", conForm) as TextBox).Text = discounting.ToString(); } // INSERTAR TODOS LOS DATOS NUMERICOS float result = paying - (paying * discounting / 100); (auxDT.FindName("TotalCost", conForm) as TextBlock).Text = "Total: " + result + "€"; // Dar de alta en History y Relations history.Add(conItem, conForm); relationProd.Add(conItem, selProduct); relationDisc.Add(conItem, selDiscount); } }