private void SyncButton_Click(object sender, EventArgs e) { using (var context = new WarehouseContext()) { var sales = from sale in context.Sales orderby sale.Id descending select sale; foreach (var sale in sales) { this.saleTableAdapter.Insert(sale.Product.WarehouseId, sale.Salesman.WarehouseId, sale.Date, sale.Quantity); context.Sales.Remove(sale); } context.SaveChanges(); var products = from product in context.Products select product; foreach (var product in products) { context.Products.Remove(product); } context.SaveChanges(); foreach (var product in this.productsSaledProducedAvailableTableAdapter.GetData()) { Debug.WriteLine(product.ToString()); var localProduct = new Product { Name = product.Name, Price = product.Price, Description = product.Description, AvailableQuantity = product.QAvailable, WarehouseId = product.Id }; context.Products.Add(localProduct); } context.SaveChanges(); var salesmans = from salseman in context.SalesTeam select salseman; foreach (var salesman in salesmans) { context.SalesTeam.Remove(salesman); } context.SaveChanges(); foreach (var salesman in this.salesmanTableAdapter.GetData()) { var localSalesman = new Salesman { Name = salesman.Name, WarehouseId = salesman.Id }; context.SalesTeam.Add(localSalesman); } context.SaveChanges(); } RefreshSales(); RefreshProducts(); RefreshSalesTeam(); }
private void Sale_Click(object sender, EventArgs e) { if (ProductList.SelectedItems.Count > 0) { var quantity = 0; if (!Int32.TryParse(SaleQuantity.Text, out quantity)) { MessageBox.Show("Incorrect Quantity value. Not a number.", "Sale Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (quantity <= 0) { MessageBox.Show("Incorrect Quantity value. Non positive numebr.", "Sale Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } var item = ProductList.SelectedItems[ProductList.SelectedItems.Count - 1]; var item_id = Int32.Parse(item.SubItems[0].Text); Debug.WriteLine("Sale of product ID: " + item_id.ToString()); using (var context = new WarehouseContext()) { var salesman = GetSalesman(context); if (salesman == null) { MessageBox.Show("Please select salesman first.", "Sale Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } var product_to_sale = from product in context.Products where product.Id == item_id select product; if (product_to_sale.First().AvailableQuantity < quantity) { MessageBox.Show("Incorrect Quantity value. Not enough available items left.", "Sale Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } var sale_item = new Sale { Product = product_to_sale.First(), Quantity = quantity, Salesman = salesman, Date = DateTime.Now }; context.Sales.Add(sale_item); product_to_sale.First().AvailableQuantity = product_to_sale.First().AvailableQuantity - quantity; context.SaveChanges(); } SaleProductName.Text = ""; SaleQuantity.Text = ""; RefreshSales(); RefreshProducts(); } else { Debug.WriteLine("No product selected"); MessageBox.Show("You must select product.", "Sale Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } }