public void CompleteCurrentOrder() { CurrentSalesOrder.CompleteSalesOrder(); SalesOrders.Add(CurrentSalesOrder); CurrentSalesOrder = new SalesOrder(new Location(Country.BOLIVIA, City.COCHABAMBA)); }
//saves details of new sales order to database private void btnSave_Click(object sender, RoutedEventArgs e) { this.Closing -= new System.ComponentModel.CancelEventHandler(SalesOrder_Closing); //add new salesorder to database if (SelectedSalesOrder == null) { if (cbCustomer.SelectedItem is Customer && dpOrderDate.SelectedDate != null && ProductsOrdered.Count != 0) { SalesOrder newSalesOrder = new SalesOrder() { OrderDate = (DateTime)dpOrderDate.SelectedDate, TotalPrice = Convert.ToDouble(tbPrice.Text), Customer = cbCustomer.SelectedItem as Customer, Employee = MainMenu.User.Employee, Paid = (bool)chkPaid.IsChecked, OrderStatus = OrderStatus.Confirmed, Problem = false, Active = true, }; SalesOrders.Add(newSalesOrder); Ctx.SaveChanges(); //method 1 foreach (var product in ProductsOrdered) { SalesOrderProduct salesOrderProduct = new SalesOrderProduct() { SalesOrderId = newSalesOrder.SalesOrderId, ProductId = product.ProductId }; Ctx.SalesOrderProducts.Add(salesOrderProduct); } Ctx.SaveChanges(); /*method 2 * foreach (var item in ProductsOrdered) * { * newSalesOrder.SalesOrderProducts.Add(new SalesOrderProduct() * { * SalesOrder = newSalesOrder, * Product = item * }); * }*/ this.Close(); this.Closing += new System.ComponentModel.CancelEventHandler(SalesOrder_Closing); } else { MessageBox.Show("Not all fields are completed."); } } else //update selectedSales order in database { SelectedSalesOrder.TotalPrice = Convert.ToDouble(tbPrice.Text); SelectedSalesOrder.Paid = (bool)chkPaid.IsChecked; //Update the products in the order var salesOrderProducts = Ctx.SalesOrderProducts .Where(so => so.SalesOrderId == SelectedSalesOrder.SalesOrderId).ToList(); Ctx.SalesOrderProducts.RemoveRange(salesOrderProducts); Ctx.SaveChanges(); foreach (var product in ProductsOrdered) { SalesOrderProduct salesOrderProduct = new SalesOrderProduct() { SalesOrderId = SelectedSalesOrder.SalesOrderId, ProductId = product.ProductId }; Ctx.SalesOrderProducts.Add(salesOrderProduct); } Ctx.SaveChanges(); CollectionViewSource.GetDefaultView(SalesOrders).Refresh(); Ctx.SaveChanges(); } this.Close(); this.Closing += new System.ComponentModel.CancelEventHandler(SalesOrder_Closing); }