private void btnDeleteProduct_Click(object sender, EventArgs e) { int[] rowHandles = gridView1.GetSelectedRows(); if (rowHandles.Count() == 0) { MessageBox.Show("Please select a product.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string productName = gridView1.GetRowCellValue(rowHandles[0], gridView1.Columns[0]).ToString(); Products productToDelete = dbContext.Products.Where(p => p.product_name == productName).FirstOrDefault(); if (productToDelete != null) { if (dbContext.History.Any(h => h.product_name == productToDelete.product_name) || dbContext.StockCharge.Any(sc => sc.product_id == productToDelete.id)) { MessageBox.Show( string.Format("Unable to delete the product [{0}] because it has already been bought or sold." , productToDelete.product_name), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { dbContext.Products.Remove(productToDelete); EntityFrameworkTransactionsControl.Commit(dbContext); btnShowProducts.PerformClick(); } } }
private void btnEditProduct_Click(object sender, EventArgs e) { int[] rowHandles = gridView1.GetSelectedRows(); if (rowHandles.Count() == 0) { MessageBox.Show("Please select a product.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string oldProductName = gridView1.GetRowCellValue(rowHandles[0], gridView1.Columns[0]).ToString(); string newProductName = Microsoft.VisualBasic.Interaction. InputBox("Please, enter the new name of the product.", "Edit product"); if (newProductName == string.Empty) { return; } if (newProductName.Length < 3) { MessageBox.Show("Invalid product name. Product names should be longer than 3 symbols.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else if (newProductName == oldProductName) { return; } else { Products modifiedProduct = dbContext.Products.Where(p => p.product_name == oldProductName).FirstOrDefault(); if (modifiedProduct != null) { modifiedProduct.product_name = newProductName; EntityFrameworkTransactionsControl.Commit(dbContext); btnShowProducts.PerformClick(); } } }
private void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e) { if ("Load Cash".Equals(e.Button.Tag)) { try { long lastStateChangeID = dbContext.Cashbox.Max(cb => cb.id); Cashbox lastStateInfo = dbContext.Cashbox.Where(cb => cb.id == lastStateChangeID) .Select(stateInfo => stateInfo).First(); Cashbox cashLoad = new Cashbox(lastStateInfo.amount_after, lastStateInfo.amount_after + 1000, DateTime.Now); dbContext.Cashbox.Add(cashLoad); EntityFrameworkTransactionsControl.Commit(dbContext); UpdateState(); } catch (Exception) { MessageBox.Show("Cashbox State table is empty, please provide a starting amount." , "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if ("Process".Equals(e.Button.Tag)) { #region SaleMode if (saleMode == true) { List <History> processableSales = new List <History>(); int totalSalesQuantity = 0; decimal cashGains = 0M; string recipeHelper = ""; int recipeCount = 1; DateTime dateOfSale = DateTime.Now; foreach (var sale in bsSalesHistory) { if (sale.quantity <= 0) { MessageBox.Show(string.Format("Skipping product {0}, because selected quantity is invalid.", sale.product_name == null ? "[Unnamed]" : sale.product_name), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); continue; } sale.Date = dateOfSale; if (dbContext.Products.Any(p => p.product_name == sale.product_name)) { var anonymousProductDetails = dbContext.Products. Where(p => p.product_name == sale.product_name). Select(s => new { Price = s.price_sell, Id = s.id }).ToList(); long quantityId = anonymousProductDetails[0].Id; StockQuantities quantityAccumulator = dbContext.StockQuantities .Where(sq => sq.product_id == quantityId) .FirstOrDefault(); if (quantityAccumulator != null) { if (quantityAccumulator.quantity - sale.quantity < 0) { MessageBox.Show( string.Format("There are not enough items from product [{0}] in stock to finish the sale.", sale.product_name), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); continue; } quantityAccumulator.quantity -= sale.quantity; quantityAccumulator.reference_date = dateOfSale; EntityFrameworkTransactionsControl.Commit(dbContext); } else { MessageBox.Show( string.Format("There is no information about the quantity of product {0}. Unable to complete the sale of the current product.", sale.product_name), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); continue; } totalSalesQuantity += (int)sale.quantity; cashGains += sale.quantity * anonymousProductDetails[0].Price; recipeHelper += Environment.NewLine + string.Format("{0,-3})", recipeCount++) + string.Format("{0,-27}", sale.product_name) + string.Format("{0,-13}", anonymousProductDetails[0].Price) + sale.quantity + " items."; processableSales.Add(sale); } else { MessageBox.Show(string.Format("Product [{0}] is invalid. Please, register it first.", sale.product_name), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } if (processableSales.Count > 0) { dbContext.History.AddRange(processableSales); try { long lastStateChangeID = dbContext.Cashbox.Max(cb => cb.id); decimal lastStateAmount = dbContext.Cashbox. Where(cb => cb.id == lastStateChangeID). Select(stateInfo => stateInfo). First().amount_after; dbContext.Cashbox.Add(new Cashbox(lastStateAmount, lastStateAmount + cashGains, DateTime.Now)); EntityFrameworkTransactionsControl.Commit(dbContext); } catch (Exception) { MessageBox.Show("Cashbox State table is empty, please provide a starting amount.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); EntityFrameworkTransactionsControl.Rollback(dbContext); return; } MessageBox.Show("Sale complete!" + Environment.NewLine + Environment.NewLine + "Product Price Quantity" + Environment.NewLine + "---------------------------------------------------" + recipeHelper + Environment.NewLine + "---------------------------------------------------" + Environment.NewLine + string.Format("Total Items Sold {0}.", totalSalesQuantity) + Environment.NewLine + string.Format("Total Cash Earned {0}$.", cashGains), "Sales information", MessageBoxButtons.OK, MessageBoxIcon.Information); UpdateState(); } } #endregion #region BuyMode else { List <StockCharge> processChargeItems = new List <StockCharge>(); int totalBoughtQuantity = 0; decimal cashToPay = 0M; string recipeHelper = ""; int recipeCount = 1; DateTime dateOfBuying = DateTime.Now; foreach (var charge in bsSalesHistory) { if (charge.quantity <= 0) { MessageBox.Show(string.Format("Skipping product {0}, because the selected quantity is invalid.", charge.product_name == null ? "[Unnamed]" : charge.product_name), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); continue; } charge.Date = dateOfBuying; if (dbContext.Products.Any(p => p.product_name == charge.product_name)) { var anonymousProductDetails = dbContext.Products. Where(p => p.product_name == charge.product_name). Select(s => new { Id = s.id, Price = s.price_charge }).ToList(); long quantityId = anonymousProductDetails[0].Id; StockQuantities quantityAccumulator = dbContext.StockQuantities .Where(sq => sq.product_id == quantityId) .FirstOrDefault(); if (quantityAccumulator != null) { quantityAccumulator.quantity += charge.quantity; quantityAccumulator.reference_date = dateOfBuying; EntityFrameworkTransactionsControl.Commit(dbContext); } else { MessageBox.Show( string.Format("There is no information about the quantity of product {0}. Unable to complete the purchase of the current product.", charge.product_name), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); continue; } totalBoughtQuantity += (int)charge.quantity; cashToPay += charge.quantity * anonymousProductDetails[0].Price; recipeHelper += Environment.NewLine + string.Format("{0,-3})", recipeCount++) + string.Format("{0,-27}", charge.product_name) + string.Format("{0,-13}", anonymousProductDetails[0].Price) + charge.quantity + " items."; processChargeItems.Add(new StockCharge(DateTime.Now, anonymousProductDetails[0].Id, charge.quantity)); } else { MessageBox.Show(string.Format("Product [{0}] is invalid. Please, register it first.", charge.product_name), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } if (processChargeItems.Count > 0) { dbContext.StockCharge.AddRange(processChargeItems); try { long lastStateChangeID = dbContext.Cashbox.Max(cb => cb.id); decimal lastStateAmount = dbContext.Cashbox. Where(cb => cb.id == lastStateChangeID). Select(stateInfo => stateInfo). First().amount_after; decimal remainingCash = lastStateAmount - cashToPay; if (remainingCash > 0) { dbContext.Cashbox.Add(new Cashbox(lastStateAmount, remainingCash, DateTime.Now)); EntityFrameworkTransactionsControl.Commit(dbContext); } else { MessageBox.Show("There is not enough cash to pay the charge! Aborting the operation.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); EntityFrameworkTransactionsControl.Rollback(dbContext); return; } } catch (Exception) { MessageBox.Show("Cashbox State table is empty, please provide a starting amount.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); EntityFrameworkTransactionsControl.Rollback(dbContext); return; } MessageBox.Show("Purchases complete!" + Environment.NewLine + Environment.NewLine + "Product Price Quantity" + Environment.NewLine + "---------------------------------------------------" + recipeHelper + Environment.NewLine + "---------------------------------------------------" + Environment.NewLine + string.Format("Total Items Bought {0}.", totalBoughtQuantity) + Environment.NewLine + string.Format("Total Cash Payed {0}$.", cashToPay), "Purchases information", MessageBoxButtons.OK, MessageBoxIcon.Information); UpdateState(); } } #endregion } }