public int Add(VmCustomerAdd model, out int?id) { int isSaved = 0; if (model.CustomerHeaderId == 0) { var cus = new Customer { CustomerHeaderId = 0, CustomerName = model.CustomerName, Address = model.Address, EmalAddress = model.EmailAddress, MobileNumber = model.MobileNumber }; db.Customer.Add(cus); isSaved = db.SaveChanges(); id = cus.CustomerHeaderId; } else { var cus = db.Customer.Find(model.CustomerHeaderId); cus.CustomerHeaderId = 0; cus.CustomerName = model.CustomerName; cus.Address = model.Address; cus.EmalAddress = model.EmailAddress; cus.MobileNumber = model.MobileNumber; id = cus.CustomerHeaderId; isSaved = db.SaveChanges(); } return(isSaved); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { db = new ProjectDbContext(); if (db.Customer.Any(a => a.EmalAddress == model.Email)) { ModelState.AddModelError("Email", "This email address is already registered"); return(View(model)); } var cus = new VmCustomerAdd { Address = model.Address, CustomerHeaderId = 0, CustomerName = model.Name, EmailAddress = model.Email, MobileNumber = model.Mobile }; int?id; customerService = new CustomerService(db); customerService.Add(cus, out id); var user = new ApplicationUser { UserName = model.Email, Email = model.Email, UserType = (int)UserType.Customer, ReferrenceId = id }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult Add(VmOrderAdd model) { var loggedInUser = UserManager.FindById <ApplicationUser, int>(User.Identity.GetUserId <int>()); var customer = new Customer(); if ((model.Customer == null || model.Customer.CustomerName == null) && loggedInUser != null) { customer = db.Customer.Where(w => w.CustomerHeaderId == loggedInUser.ReferrenceId).FirstOrDefault(); } else { var cus = new VmCustomerAdd { Address = model.Customer.Address, CustomerHeaderId = model.Customer.CustomerHeaderId, CustomerName = model.Customer.CustomerName, EmailAddress = model.Customer.EmailAddress, MobileNumber = model.Customer.MobileNumber }; int?id; customerService.Add(cus, out id); customer = db.Customer.Find(id); var user = new ApplicationUser { UserName = model.Customer.EmailAddress, Email = model.Customer.EmailAddress, UserType = (int)UserType.Customer, ReferrenceId = customer.CustomerHeaderId, CreationDate = DateTime.Now, LastUpdatedDate = DateTime.Now }; var result = UserManager.Create(user, model.Customer.Password); if (result.Succeeded) { SignInManager.SignIn(user, isPersistent: false, rememberBrowser: false); // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link //string code = UserManager.GenerateEmailConfirmationToken(user.Id); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); } } var order = new Order { CustomerHeaderId = customer.CustomerHeaderId, OrderDate = DateTime.Now, OrderHeaderId = 0, Status = (int)OrderStatus.Pending, OrderNo = GenerateOrderNo() }; db.Order.Add(order); db.SaveChanges(); foreach (var item in model.Purchases) { var orderItem = new OrderItem { OrderHeaderId = order.OrderHeaderId, OrderItemHeaderId = 0, ProductHeaderId = item.ProductHeaderId, Quantity = item.Quantity, Rate = db.Product.Find(item.ProductHeaderId).Rate, Total = db.Product.Find(item.ProductHeaderId).Rate *item.Quantity }; db.OrderItem.Add(orderItem); } db.SaveChanges(); return(Json(customer.CustomerHeaderId, JsonRequestBehavior.AllowGet)); }