//// GET: SupplierWithAgreements/Details/5 //public ActionResult Details(int? id) //{ // if (id == null) // { // return new HttpStatusCodeResult(HttpStatusCode.BadRequest); // } // SupplierWithAgreements supplierWithAgreements = db.SupplierWithAgreements.Find(id); // if (supplierWithAgreements == null) // { // return HttpNotFound(); // } // return View(supplierWithAgreements); //} /// <summary> /// Ariel Sigo /// /// Created: /// 2017/04/29 /// /// GET: SupplierWithAgreements/Create /// </summary> /// <returns>View of Dummy Product List</returns> public ActionResult Create() { //if (User.IsInRole("Supplier")) //{ // ViewBag.Account = "supplier"; // return View("AlreadyAccepted"); //} List <Product> products = _productManager.ListProducts(); //SupplierWithAgreements dummy = new SupplierWithAgreements() SupplierApplicantViewModel dummy = new SupplierApplicantViewModel() { Agreements = new List <AgreementWithProductName>() }; foreach (Product p in products) { dummy.Agreements.Add(new AgreementWithProductName() { ProductId = p.ProductId, ProductName = p.Name }); } ViewBag.Message = ""; return(View(dummy)); }
//public ActionResult Create([Bind(Include = "FarmTaxID,UserId,FarmName,FarmAddress,FarmCity,FarmState,Agreements")] SupplierWithAgreements supplierWithAgreements) public async Task <ActionResult> Create([Bind(Include = "FirstName,LastName,Phone,AddressLineOne,AddressLineTwo,City,State,Zip,EmailAddress,UserName,Password,ConfirmPassword,FarmTaxID,FarmName,FarmAddress,FarmCity,FarmState,ProductIDs")] SupplierApplicantViewModel supplierApplicant) { supplierApplicant.Agreements = new List <AgreementWithProductName>(); if (supplierApplicant.ProductIDs != null) { for (int i = 0; i < supplierApplicant.ProductIDs.Length; i++) { supplierApplicant.Agreements.Add(new AgreementWithProductName() { ProductId = supplierApplicant.ProductIDs[i], ProductName = _productManager.RetrieveProductById(supplierApplicant.ProductIDs[i]).Name }); } } if (ModelState.IsValid) { //Need to first make a user with the information, then the supplier User newUser = new User() { FirstName = supplierApplicant.FirstName, LastName = supplierApplicant.LastName, City = supplierApplicant.City, State = supplierApplicant.State, AddressLineOne = supplierApplicant.AddressLineOne, AddressLineTwo = supplierApplicant.AddressLineTwo, Zip = supplierApplicant.Zip, EmailAddress = supplierApplicant.EmailAddress, EmailPreferences = true, Active = true, Phone = supplierApplicant.Phone, UserName = supplierApplicant.UserName }; try { string result = _userManager.CreateNewUser(newUser, supplierApplicant.Password, supplierApplicant.ConfirmPassword); if (!("Created".Equals(result))) { ViewBag.Message = result; return(View(supplierApplicant)); } } catch (Exception) { return(new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable)); } // If we are here, it means we created the user User newlyCreated = null; try { newlyCreated = _userManager.RetrieveUserByUserName(newUser.UserName); } catch (Exception) { return(new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable)); } if (null == newlyCreated) // this shouldn't ever be true { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } // Make a supplier and store the associated data to the db SupplierWithAgreements supplierWithAgreements = new SupplierWithAgreements() { FarmName = supplierApplicant.FarmName, FarmAddress = supplierApplicant.FarmAddress, FarmCity = supplierApplicant.FarmCity, FarmState = supplierApplicant.FarmState, FarmTaxID = supplierApplicant.FarmTaxID, UserId = newlyCreated.UserId, ProductIDs = supplierApplicant.ProductIDs, Agreements = supplierApplicant.Agreements, Active = true, IsApproved = false }; try { if (_supplierManager.ApplyForSupplierAccount(supplierWithAgreements)) { supplierWithAgreements.SupplierID = _supplierManager.RetrieveSupplierByUserId(supplierWithAgreements.UserId).SupplierID; foreach (Agreement a in supplierWithAgreements.Agreements) { if (!_agreementManager.CreateAgreementsForSupplier(supplierWithAgreements, _productManager.RetrieveProductById(a.ProductId))) { return(new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable)); } } RegisterViewModel registerModel = new RegisterViewModel() { Email = newUser.EmailAddress, Password = supplierApplicant.Password, ConfirmPassword = supplierApplicant.ConfirmPassword }; var controller = DependencyResolver.Current.GetService <AccountController>(); controller.ControllerContext = new ControllerContext(this.Request.RequestContext, controller); var result = await controller.Register(registerModel); return(RedirectToAction("ApplicationSuccess", "Home", new { username = newlyCreated.UserName, supOrCom = true })); //return RedirectToAction("Register", "Account", registerModel); } else { return(new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable)); } } catch (Exception) { return(new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable)); } } List <Product> products = _productManager.ListProducts(); supplierApplicant.Agreements = new List <AgreementWithProductName>(); foreach (Product p in products) { supplierApplicant.Agreements.Add(new AgreementWithProductName() { ProductId = p.ProductId, ProductName = p.Name }); } return(View(supplierApplicant)); }