public void DeleteCashTransaction() { if (SelectedCashTransaction == null) { return; } MessageBoxResult answer = MessageBox.Show("Удалить выбранную транзакцию?", "Question", MessageBoxButton.YesNo); if (answer != MessageBoxResult.Yes) { return; } var transactionToDelete = _mainViewModel.Context.Transactions.ToList() .LastOrDefault(x => x.ID == SelectedCashTransaction.ID); try { _mainViewModel.Context.Transactions.Remove(transactionToDelete); _mainViewModel.Context.SaveChanges(); MessageBox.Show("Транзакция удалена.", "Confirmation", MessageBoxButton.OK); } catch (Exception e) { MessageBox.Show("Не удалось удалить транзакцию", "Error", MessageBoxButton.OK); Logging.WriteToLog("Failed to remove cash transaction. " + e.Message); } Update(); _mainViewModel.UpdateCashInHand(); }
/// <summary> /// Find all transactions with SelectedProduct and delete all of them /// </summary> protected void DeleteProduct() { MessageBoxResult answer = MessageBox.Show("Удалить выбранный товар?", "Question", MessageBoxButton.YesNo); if (answer != MessageBoxResult.Yes) { return; } var transactionsToDelete = MainViewModel.Context.Transactions.Where(x => x.ProductID != null && SelectedProductIDs.Contains(x.ProductID.Value)).ToList(); foreach (var transaction in transactionsToDelete) { MainViewModel.Context.Transactions.Remove(transaction); } var productsToDelete = MainViewModel.Context.Products.ToList() .Where(x => SelectedProductIDs.Contains(x.ID)).ToList(); foreach (var productToDelete in productsToDelete) { MainViewModel.Context.Products.Remove(productToDelete); } try { MainViewModel.Context.SaveChanges(); //Update CashInHand and Products value MainViewModel.UpdateCashInHand(); } catch (Exception e) { MessageBox.Show(("Не удалось удалить товар"), "Error", MessageBoxButton.OK, MessageBoxImage.Error); Logging.WriteToLog("Failed delete product. " + e.InnerException.Message); } MessageBox.Show("Товар удален", "Confirmation", MessageBoxButton.OK); Update(); }