// **************************************
        // URL: /Transaction/Balance
        // **************************************
        public ActionResult Balance(string userName = "")
        {
            if (userName != "")
            {
                var accountNumber = "";
                var oldUser = MembershipService.GetUser(userName, false);
                var userID = oldUser.ProviderUserKey.ToString();
                List<TransactionModel> trans = new List<TransactionModel> { };
                using (ATMEntities db = new ATMEntities())
                {
                    var customer = from c in db.customers
                                   where c.idCustomer == userID
                                   select c;
                    accountNumber = customer.Single().account;

                    var dbtrans = from t in db.transactions
                                  where t.account == accountNumber
                                  select t;
                    var balance = getAccountBalance(accountNumber);

                    foreach (transaction dbtran in dbtrans)
                    {
                        TransactionModel tempInstance = new TransactionModel();
                        tempInstance.account = dbtran.account;
                        tempInstance.debit = Convert.ToSingle(dbtran.debit);
                        tempInstance.credit = Convert.ToSingle(dbtran.credit);
                        tempInstance.tStamp = dbtran.tStamp;
                        trans.Add(tempInstance);
                    }

                    Session.Add("accountNumber", accountNumber);
                    TempData["balance"] = balance;

                    return View(trans);
                }
            }
            else
            {
                return View();
            }
        }
 public ActionResult Deposit(TransactionModel model)
 {
     if (ModelState.IsValid && model.credit > 0)
     {
         string dtStamp = DateTime.Now.ToString();
         var dbATM = new ATMEntities();
         var transaction = new transaction
         {
             tStamp = dtStamp,
             credit = model.credit,
             account = model.account,
             debit = 0
         };
         dbATM.transactions.AddObject(transaction);
         dbATM.SaveChanges();
         TempData["successD"] = "Y";
     }
     else
     {
         ModelState.AddModelError("", "The deposit value provided is incorrect.");
     }
     return View(model);
 }
 private double getAccountBalance(string account)
 {
     var balance = 0.0;
     using (ATMEntities db = new ATMEntities())
     {
         var trans = from t in db.transactions
                     where t.account == account
                     select t;
         foreach (transaction tran in trans)
         {
             balance += (tran.credit - tran.debit);
         }
     }
     return balance;
 }
 public ActionResult Withdraw(TransactionModel model)
 {
     if (ModelState.IsValid && model.debit > 0)
     {
         string dtStamp = DateTime.Now.ToString();
         var balance = getAccountBalance(model.account);
         if (balance <= model.debit)
         {
             TempData["successW"] = "N";
         }
         else
         {
             var dbATM = new ATMEntities();
             var transaction = new transaction
             {
                 tStamp = dtStamp,
                 debit = model.debit,
                 account = model.account,
                 credit = 0
             };
             dbATM.transactions.AddObject(transaction);
             dbATM.SaveChanges();
             TempData["successW"] = "Y";
         }
         return View(model);
     }
     else
     {
         ModelState.AddModelError("", "The withdrawal value provided is incorrect.");
     }
     return View(model);
 }
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, false);

                    var accountNumber = "";
                    var oldUser = MembershipService.GetUser(model.UserName, false);
                    var userID = oldUser.ProviderUserKey.ToString();
                    using (ATMEntities db = new ATMEntities())
                    {
                        var customer = from c in db.customers
                                    where c.idCustomer == userID
                                    select c;
                        accountNumber = customer.Single().account;
                    }
                    Session.Add("accountNumber", accountNumber);

                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
            return View(model);
        }
 public ActionResult Register(RegisterModel model)
 {
     if (ModelState.IsValid)
     {
         // Attempt to register the user
         MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
         if (createStatus == MembershipCreateStatus.Success)
         { // On success create a new customer data
             MembershipUser newUser = MembershipService.GetUser(model.UserName, false);
             string newUserID = newUser.ProviderUserKey.ToString();
             string dtStamp = DateTime.Now.ToString();
             string accountNo = Guid.NewGuid().ToString();
             var dbATM = new ATMEntities();
             var customer = new customer
             {
                 idCustomer = newUserID,
                 firstName = model.FirstName,
                 lastName = model.LastName,
                 account = accountNo,
                 cDate = dtStamp
             };
             dbATM.customers.AddObject(customer);
             dbATM.SaveChanges();
             FormsService.SignIn(model.UserName, false);
             Session.Add("accountNumber", accountNo);
             TempData["justRegistered"] = "Y";
             return RedirectToAction("Index", "Home");
         }
         else
         {
             ModelState.AddModelError("", CustomerValidation.ErrorCodeToString(createStatus));
         }
     }
     ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
     return View(model);
 }