public ActionResult Create([Bind(Include = "BankAccountTypeID")] BankAccount bankAccount, string CurrencyCode)
        {
            if ((db.BankAccountTypes.Find(bankAccount.BankAccountTypeID).Type == "FOR_CUR_ACC" && CurrencyCode == "PLN") ||
                (db.BankAccountTypes.Find(bankAccount.BankAccountTypeID).Type != "FOR_CUR_ACC" && CurrencyCode != "PLN")
                )
            {
                ModelState.AddModelError("CurrencyCode", "Nieprawidłowa waluta");
            }

            if (ModelState.IsValid)
            {
                bankAccount.Balance           = 0.0m;
                bankAccount.AvailableFounds   = 0.0m;
                bankAccount.Lock              = 0.0m;
                bankAccount.BankAccountNumber = NewBankAcocuntNumber();
                bankAccount.CreationDate      = DateTime.Today;
                bankAccount.Currency          = db.Currencies.Single(c => c.Code == CurrencyCode);

                PaymentCard paymentCard = new PaymentCard
                {
                    PaymentCardNumber = PaymentCardsController.NewPaymentCardNumber(),
                    Code       = new Random().Next(0, 9999).ToString("D4"),
                    Blocked    = false,
                    SecureCard = true,
                };

                db.BankAccounts.Add(bankAccount);
                paymentCard.BankAccount = bankAccount;
                db.PaymentCards.Add(paymentCard);
                db.Profiles.Single(p => p.Email == User.Identity.Name).BankAccounts.Add(bankAccount);

                db.SaveChanges();
                return(RedirectToAction("Index", "Home"));
            }

            ViewBag.BankAccountTypes = db.BankAccountTypes.ToList();
            ViewBag.Currencies       = db.Currencies.ToList();
            return(View(bankAccount));
        }
示例#2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (db.Profiles.Any(p => p.PESEL == model.PESEL))
            {
                ModelState.AddModelError("PESEL", "Widnieje już taki numer PESEL w bazie");
            }

            if (db.Profiles.Any(p => p.Email == model.Email))
            {
                ModelState.AddModelError("Email", "Adres email jest już zajęty");
            }

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    UserManager.AddToRole(user.Id, "User");

                    Profile profile = new Profile
                    {
                        Email        = model.Email,
                        FirstName    = model.FirstName,
                        LastName     = model.LastName,
                        PESEL        = model.PESEL,
                        BankAccounts = new List <BankAccount>()
                    };

                    BankAccount bankAccount = new BankAccount()
                    {
                        Balance           = 0.0m,
                        AvailableFounds   = 0.0m,
                        Lock              = 0.0m,
                        BankAccountNumber = BankAccountsController.NewBankAcocuntNumber(),
                        CreationDate      = DateTime.Today,
                        BankAccountTypeID = model.BankAccountTypeID,
                        CurrencyID        = db.Currencies.Single(c => c.Code == "PLN").ID
                    };

                    PaymentCard paymentCard = new PaymentCard
                    {
                        PaymentCardNumber = PaymentCardsController.NewPaymentCardNumber(),
                        Code       = new Random().Next(0, 9999).ToString("D4"),
                        Blocked    = false,
                        SecureCard = true,
                    };

                    db.BankAccounts.Add(bankAccount);
                    profile.BankAccounts.Add(bankAccount);
                    paymentCard.BankAccount = bankAccount;
                    db.Profiles.Add(profile);
                    db.PaymentCards.Add(paymentCard);
                    db.SaveChanges();

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            ViewBag.BankAccountTypes = db.BankAccountTypes.ToList();
            return(View(model));
        }