Example #1
0
        public bool Transaction(decimal Amount, string Description)
        {
            if (Balance + Amount > 0)
            {
                using (var db = new BankingEntities1())
                {
                    using (var transaction = db.Database.BeginTransaction())
                    {
                        try
                        {
                            // Amount is the amount of money being withdrawn or deposited
                            // Positive is a Deposit
                            // Negative is a Withdraw

                            if (Balance + Amount >= 0)
                            {
                                var Transaction = new Transaction
                                {
                                    Amount      = Amount,
                                    AccountType = Text,
                                    DateTime    = DateTime.Now,
                                    Description = Description,
                                    AccountId   = Id
                                };
                                Debug.WriteLine("Transaction insertion.");
                                db.Transactions.Add(Transaction);
                                db.CheckingAccounts.Find(Id).Balance += Amount;
                            }
                            Debug.WriteLine("CheckingAccounts balance updated. Saving changes.s");
                            db.SaveChanges();
                            transaction.Commit();
                            return(true);
                        }
                        catch (System.Data.Entity.Infrastructure.DbUpdateException e)
                        {
                            Debug.WriteLine(e.InnerException);
                            Debug.WriteLine(e.Message);
                            Debug.WriteLine(e.Source);
                            foreach (var eve in e.Data)
                            {
                                Debug.WriteLine(eve);
                            }
                            throw;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            return(false);
                        }
                    }
                }
            }
            return(false);
        }
Example #2
0
        public bool Transaction(decimal Amount, string Description)
        {
            Debug.WriteLine("TRANSACTION");
            using (var db = new BankingEntities1())
            {
                Balance += Amount;

                var  OverdraftFee = -30;
                bool OverDraft    = false;
                var  ODFee        = new Transaction
                {
                    Amount      = OverdraftFee,
                    AccountType = Text,
                    DateTime    = DateTime.Now,
                    Description = "Overdraft charge",
                    AccountId   = Id
                };
                var Transaction = new Transaction
                {
                    Amount      = Amount,
                    AccountType = Text,
                    DateTime    = DateTime.Now,
                    Description = Description,
                    AccountId   = Id
                };
                db.Transactions.Add(Transaction);

                if (Balance < 0 && Amount < 0)
                {
                    // If the account balance is less than zero
                    // and the amount is negative it's a withdrawl
                    // apply a fee

                    OverDraft = true;
                    db.SaveChanges();
                }

                db.Transactions.Add(Transaction);
                if (OverDraft)
                {
                    db.Transactions.Add(ODFee);
                    Balance += OverdraftFee;
                }
                var BusAccount = db.BusinessAccounts.Find(Id);
                BusAccount.Balance = Balance;
                db.SaveChanges();
                return(true);

                /*
                 * using (var transaction = db.Database.BeginTransaction())
                 * {
                 *  try
                 *  {
                 *      // Amount is the amount of money being withdrawn or deposited
                 *      // Positive is a Deposit
                 *      // Negative is a Withdraw
                 *      Balance += Amount;
                 *
                 *      var Transaction = new Transaction
                 *      {
                 *          Amount = Amount,
                 *          AccountType = Text,
                 *          DateTime = DateTime.Now,
                 *          Description = Description,
                 *          AccountId = Id
                 *      };
                 *      db.Transactions.Add(Transaction);
                 *
                 *      if (Balance < 0 && Amount < 0)
                 *      {
                 *          // If the account balance is less than zero
                 *          // and the amount is negative it's a withdrawl
                 *          // apply a fee
                 *          var OverdraftFee = -30;
                 *
                 *          var ODFee = new Transaction
                 *          {
                 *              Amount = OverdraftFee,
                 *              AccountType = Text,
                 *              DateTime = DateTime.Now,
                 *              Description = "Overdraft charge",
                 *              AccountId = Id
                 *          };
                 *          Balance += OverdraftFee;
                 *          db.Transactions.Add(ODFee);
                 *      }
                 *      db.SaveChanges();
                 *      transaction.Commit();
                 *      Debug.WriteLine("END TRANSACTION");
                 *  }
                 *  catch (Exception)
                 *  {
                 *      transaction.Rollback();
                 *      return false;
                 *  }
                 *  return true;
                 * }*/
            }
        }