public void ComputePrice() { decimal price = produto.Cost; foreach (PedidoProdutoIngrediente ppi in pedido.ingredientes) { ProdutoIngrediente p = Array.Find(produto.ingredientes, i => i.PIngredientID == ppi.ProdutoIngredienteID); price += p.Price * ppi.Quantidade; } price *= pedido.pedidoProdutoQtde; pedido.computatedPrice = price; materialLabel3.Text = "Preço Final: R$ " + price; }
public Produto Get(int id) { using ( SqlConnection connection = new SqlConnection(string.Format("User ID={0}; Password={1}; Initial Catalog={2}; Persist Security Info=True;Data Source={3}", Program.dbLogin, Program.dbPass, "dbSblenders", Program.dbEnv)) ) using ( SqlCommand produtoQueryCommand = new SqlCommand("SELECT produtoID, produtoNome, produtoCusto, produtoDescricao FROM tbProduto WHERE produtoID = @id", connection) ) { produtoQueryCommand.Parameters.Add(new SqlParameter("@id", id)); using (SqlDataAdapter productAdapter = new SqlDataAdapter(produtoQueryCommand)) { DataTable product = new DataTable(); connection.Open(); productAdapter.Fill(product); if (product.Rows.Count < 1) { Response.StatusCode = StatusCodes.Status400BadRequest; return(null); } else { using ( SqlCommand produtoIngQueryCommand = new SqlCommand("SELECT tbIngrediente.ingredienteID, ingredienteNome, produtoIngredienteID, ingredienteCusto, novoPreco, quantidadePadrao, ingredienteDescricao, categoriaIngredienteID FROM tbProdutoIngrediente INNER JOIN tbIngrediente ON tbIngrediente.ingredienteID = tbProdutoIngrediente.ingredienteID WHERE tbProdutoIngrediente.produtoID = @id", connection) ) { produtoIngQueryCommand.Parameters.Add(new SqlParameter("@id", id)); DataTable ingredients = new DataTable(); using (SqlDataAdapter ingredientsAdapter = new SqlDataAdapter(produtoIngQueryCommand)) { ingredientsAdapter.Fill(ingredients); List <ProdutoIngrediente> ingList = new List <ProdutoIngrediente>(); foreach (DataRow r in ingredients.Rows) { decimal preco; if (r["novoPreco"] == DBNull.Value) { preco = (decimal)r["ingredienteCusto"]; } else { preco = (decimal)r["novoPreco"]; } ProdutoIngrediente ingrediente = new ProdutoIngrediente((int)r["quantidadePadrao"], (int)r["ingredienteID"], preco, (string)r["ingredienteNome"], (string)r["ingredienteDescricao"], (int)r["produtoIngredienteID"], (int)r["categoriaIngredienteID"]); ingList.Add(ingrediente); } List <Produto.InformacaoNutricional> nutriInfo = new List <Produto.InformacaoNutricional>(); using ( SqlCommand produtoInfoNutrQuery = new SqlCommand("SELECT tbInformacaoNutricional.informacaoNutricionalValor, tbInformacaoNutricionalTipo.informacaoNutriTipoDescricao FROM tbInformacaoNutricional INNER JOIN tbInformacaoNutricionalTipo ON tbInformacaoNutricional.informacaoNutriTipoID = tbInformacaoNutricionalTipo.informacaoNutriTipoID WHERE tbInformacaoNutricional.produtoID = @id", connection) ){ using (SqlDataAdapter nutriAdapter = new SqlDataAdapter(produtoInfoNutrQuery)){ DataTable nutriInfoTable = new DataTable(); produtoInfoNutrQuery.Parameters.Add(new SqlParameter("@id", id)); nutriAdapter.Fill(nutriInfoTable); foreach (DataRow r in nutriInfoTable.Rows) { nutriInfo.Add(new Produto.InformacaoNutricional((string)r["informacaoNutriTipoDescricao"], (int)r["informacaoNutricionalValor"])); } } } Produto produto = new Produto(id, (decimal)product.Rows[0]["produtoCusto"], product.Rows[0]["produtoNome"].ToString(), product.Rows[0]["produtoDescricao"].ToString(), ingList.ToArray(), nutriInfo.ToArray()); return(produto); } } } } } }
public Ingredientes(ProdutoParcial p) { InitializeComponent(); pictureBox1.LoadAsync("https://localhost:44323/api/ProdutoFoto/" + p.ID); pedido = new PedidoProduto(1, p.ID, new PedidoProdutoIngrediente[0]); string URL = $"https://localhost:44323/api/Produtos/{p.ID}"; string urlParameters = ""; HttpClient client = new HttpClient(); client.BaseAddress = new Uri(URL); // Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); // List data response. HttpResponseMessage response = client.GetAsync(urlParameters).Result; // Blocking call! Program will wait here until a response is received or a timeout occurs. JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); Produto resultado = serializer.Deserialize <Produto>(response.Content.ReadAsStringAsync().Result); if (response.IsSuccessStatusCode) { produto = resultado; materialLabel4.Text = "Descrição: " + produto.Desc; List <int> categorias = new List <int> { }; bool hasPages = false; foreach (ProdutoIngrediente pI in produto.ingredientes) { if (categorias.Contains(pI.CategoriaIngredienteID)) { string[] rowStrings = { pI.Name, "R$ " + pI.Price, pI.DefaultQuantity.ToString(), pI.Desc }; ListViewItem row = new ListViewItem(rowStrings); ((MaterialListView)materialTabControl1.Controls[categorias.IndexOf(pI.CategoriaIngredienteID)].Controls[0]).Items.Add(row); if (pI.DefaultQuantity != 0) { List <PedidoProdutoIngrediente> ppis = new List <PedidoProdutoIngrediente>(pedido.ingredientes); ppis.Add(new PedidoProdutoIngrediente(pI.IngredientID, pI.DefaultQuantity)); pedido.ingredientes = ppis.ToArray(); } } else { categorias.Add(pI.CategoriaIngredienteID); MaterialListView l = new MaterialListView(); l.Dock = DockStyle.Fill; ColumnHeader nameHeader = new ColumnHeader(); nameHeader.Text = "Ingrediente"; nameHeader.Width = 300; ColumnHeader costHeader = new ColumnHeader(); costHeader.Text = "Custo"; costHeader.Width = 140; ColumnHeader descHeader = new ColumnHeader(); descHeader.Text = "Descrição"; descHeader.Width = 400; ColumnHeader qHeader = new ColumnHeader(); qHeader.Text = "Quantidade"; qHeader.Width = 100; l.Columns.AddRange(new ColumnHeader[] { nameHeader, costHeader, qHeader, descHeader }); l.HeaderStyle = ColumnHeaderStyle.Nonclickable; TabPage categoriaPage = new TabPage(); categoriaPage.Text = getCategoriaNome(pI.CategoriaIngredienteID); //transformar issso em uma chamada de API pra pegar o nome dps categoriaPage.Controls.Add(l); if (pI.DefaultQuantity != 0) { List <PedidoProdutoIngrediente> ppis = new List <PedidoProdutoIngrediente>(pedido.ingredientes); ppis.Add(new PedidoProdutoIngrediente(pI.IngredientID, pI.DefaultQuantity)); pedido.ingredientes = ppis.ToArray(); } string[] rowStrings = { pI.Name, "R$ " + pI.Price, pI.DefaultQuantity.ToString(), pI.Desc }; ListViewItem row = new ListViewItem(rowStrings); l.Items.Add(row); materialTabControl1.TabPages.Add(categoriaPage); l.SelectedIndexChanged += new System.EventHandler((object o, EventArgs e) => { if (l.SelectedIndices.Count == 0) { return; } ProdutoIngrediente[] ingredientes = (from ingrediente in produto.ingredientes where ingrediente.CategoriaIngredienteID == categorias[materialTabControl1.SelectedIndex] select ingrediente).ToArray(); ProdutoIngrediente selectedIngredient = ingredientes[l.SelectedIndices[0]]; int index = Array.FindIndex(pedido.ingredientes, ing => ing.ProdutoIngredienteID == selectedIngredient.PIngredientID); if (index != -1) { AddIngrediente query = new AddIngrediente(pedido.ingredientes[index].Quantidade); query.ShowDialog(); pedido.ingredientes[index].Quantidade = query.Quantidade; ComputePrice(); l.Items[l.SelectedIndices[0]].SubItems[2].Text = query.Quantidade.ToString(); } else { List <PedidoProdutoIngrediente> ppis = new List <PedidoProdutoIngrediente>(pedido.ingredientes); ppis.Add(new PedidoProdutoIngrediente(selectedIngredient.IngredientID, selectedIngredient.DefaultQuantity)); pedido.ingredientes = ppis.ToArray(); AddIngrediente query = new AddIngrediente(pedido.ingredientes[pedido.ingredientes.Length - 1].Quantidade); query.ShowDialog(); pedido.ingredientes[pedido.ingredientes.Length - 1].Quantidade = query.Quantidade; ComputePrice(); l.Items[l.SelectedIndices[0]].SubItems[2].Text = pedido.ingredientes[pedido.ingredientes.Length - 1].Quantidade.ToString(); } }); hasPages = true; } if (hasPages) { materialTabSelector1.BaseTabControl = materialTabControl1; materialTabSelector1.Invalidate(); materialTabSelector1.Update(); } } ComputePrice(); } else { MessageBox.Show("Erro em obter os detalhes do produto da API. Contate um funcionário."); Close(); } client.Dispose(); }