private void lvMonthStatement_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (lvMonthStatement.Items.Count == 0 || lvMonthStatement.SelectedIndex == -1) { return; } Transaction currTrans = (Transaction)lvMonthStatement.SelectedItem; Receipt receiptDlg = new Receipt(currentAccount, 0, currTrans, currentUser, false); receiptDlg.Owner = this; receiptDlg.ShowDialog(); }
public void CloseAccount() { if (lvAccounts.Items.Count != 0 && lvAccounts.SelectedIndex != -1) { if (currentAccount.IsActive == false) { MessageBox.Show("This account is already closed."); return; } string closingAcctMessage = null; if (currentAccount.Balance > 0) { closingAcctMessage = "Before closing the account, please confirm the withdrawal of the remaining balance of $" + currentAccount.Balance; MessageBoxResult res = MessageBox.Show(closingAcctMessage, "Withdrawal of funds required", MessageBoxButton.YesNo, MessageBoxImage.Warning); Transaction transac; if (res == MessageBoxResult.Yes) { transac = new Transaction(); transac.Date = DateTime.Today; transac.Amount = currentAccount.Balance; transac.Type = "Withdrawal"; transac.AccountId = currentAccount.Id; decimal previousBalance = currentAccount.Balance; //balance before transaction try { EFData.context.Transactions.Add(transac); currentAccount.Balance = 0; //new balance EFData.context.SaveChanges(); } catch (DbEntityValidationException ex) { var error = ex.EntityValidationErrors.First().ValidationErrors.First(); MessageBox.Show(error.ErrorMessage); EFData.context.Entry(transac).State = EntityState.Detached; return; } catch (SystemException ex) { MessageBox.Show("Database error: " + ex.Message, "Database operation failed", MessageBoxButton.OK, MessageBoxImage.Error); } Receipt withdrawalReceipt = new Receipt(currentAccount, previousBalance, transac, currentClient, true); // withdrawalReceipt.Owner = this; bool?dlgResult = withdrawalReceipt.ShowDialog(); } if (res == MessageBoxResult.No) { MessageBox.Show("Account cannot be closed before withdrawal of remaining funds", "Warning", MessageBoxButton.OK, MessageBoxImage.Error); return; } } string message = string.Format("Confirm closure of account number {0} with remaining balance of {1} $ ?", currentAccount.Id, currentAccount.Balance); MessageBoxResult result = MessageBox.Show(message, "Confirmation of account closure required", MessageBoxButton.YesNo, MessageBoxImage.Information); if (result == MessageBoxResult.Yes) { currentAccount.IsActive = false; currentAccount.CloseDate = DateTime.Today; AccountClosureStatement closureStatementDlg = new AccountClosureStatement(currentAccount); // closureStatementDlg.Owner = this; closureStatementDlg.ShowDialog(); } try { EFData.context.SaveChanges(); } catch (DbEntityValidationException ex) { var error = ex.EntityValidationErrors.First().ValidationErrors.First(); MessageBox.Show(error.ErrorMessage); // EFData.context.Entry(transac).State = EntityState.Detached; return; } catch (SystemException ex) { MessageBox.Show("Database error: " + ex.Message, "Database operation failed", MessageBoxButton.OK, MessageBoxImage.Error); } //FIX : confirm closing account //FIX: generate statement about closing account LoadFoundAccounts(); } else { MessageBox.Show("Customer and account to close must be selected first."); return; } }
//make transaction private void MakeTransaction() { Transaction transac = null; Transaction depositToBenefAccount = null; try { decimal amount = decimal.Parse(tbAmount.Text); transac = new Transaction(); transac.Date = DateTime.Today; transac.Amount = amount; transac.Type = currentTransType; transac.AccountId = currentAccount.Id; if (currentTransType == "Transfer") { int destinationAccNo = int.Parse(tbBenefAccNo.Text); transac.ToAccount = destinationAccNo; } if (currentTransType == "Payment") { User payee = (User)comboPayees.SelectedItem; transac.ToAccount = (from a in EFData.context.Accounts where a.UserId == payee.Id && a.AccountType.Id == 4 select a.Id).FirstOrDefault(); transac.PaymentCategory = comboPayCategory.Text; } EFData.context.Transactions.Add(transac); decimal previousBalance = currentAccount.Balance; //balance before transaction currentAccount.Balance = DeductAddMoneyToAccount(currentTransType, previousBalance, amount); //new balance if (currentTransType == "Transfer" || currentTransType == "Payment") { Account beneficiaryAcc = EFData.context.Accounts.SingleOrDefault(a => a.Id == transac.ToAccount); depositToBenefAccount = new Transaction { Date = DateTime.Today, Amount = amount, Type = "Deposit", AccountId = beneficiaryAcc.Id }; EFData.context.Transactions.Add(depositToBenefAccount); beneficiaryAcc.Balance = beneficiaryAcc.Balance + Math.Round(amount, 2); //add money to beneficiary } EFData.context.SaveChanges(); lblBalance.Content = "$ " + currentAccount.Balance; string message = string.Format("The {0} was completed successfully", currentTransType.ToLower()); MessageBox.Show(message, "Success", MessageBoxButton.OK, MessageBoxImage.Information); tbAmount.Text = ""; tbBenefAccNo.Text = ""; Receipt receiptDlg = new Receipt(currentAccount, previousBalance, transac, currentUser, true); receiptDlg.Owner = this; bool?result = receiptDlg.ShowDialog(); if (result == true) { MessageBoxResult answer = MessageBox.Show("Would you like to perform another " + currentTransType.ToLower() + " ?", "Choice required", MessageBoxButton.YesNo, MessageBoxImage.Question); if (answer == MessageBoxResult.No) { DialogResult = true; } } } catch (DbEntityValidationException ex) { var error = ex.EntityValidationErrors.First().ValidationErrors.First(); MessageBox.Show(error.ErrorMessage); EFData.context.Entry(transac).State = EntityState.Detached; EFData.context.Entry(depositToBenefAccount).State = EntityState.Detached; return; } catch (SystemException ex) { MessageBox.Show("Database error: " + ex.Message, "Database operation failed", MessageBoxButton.OK, MessageBoxImage.Error); return; } }