public ActionResult SubscribePackage(int packageid, int cartype) { //Check the car type and Pakcage of user may be later AdhocUserPackageAddOnViewModel packageDetails = new AdhocUserPackageAddOnViewModel(); if (Session["LinkCarId"] != null) { var linkCarId = Convert.ToInt32(Session["LinkCarId"]); packageDetails.LinkCarId = linkCarId; if (Session["SelectedCar"] != null) { var car = Session["SelectedCar"] as CarUser; packageDetails.SelectedCar = car.CarId.ToString(); ViewBag.SelectedCarName = car.DisplayName; } var userid = User.Identity.GetUserId(); //var carUserList = db.CarUsers.Where(a => a.UserId == userid && a.CarType.Id == cartype); var carUserList = db.CarUsers.Where(a => a.UserId == userid); var packageCarUserList = db.UserPackages.Where(a => a.UserId == userid).Select(a => a.CarId).ToList(); var list = new List <CarUser>(); foreach (var carUser in carUserList) { if (!packageCarUserList.Contains(carUser.CarId)) { list.Add(carUser); } } packageDetails.UserCars = list; //int latestCar = packageDetails.UserCars.ToList().OrderByDescending(a => a.CarId).Select(a => a.CarId).FirstOrDefault(); //ViewBag.LatestCar = latestCar; int latestCar = list.OrderByDescending(a => a.CarId).Select(a => a.CarId).FirstOrDefault(); ViewBag.LatestCar = latestCar; //Store packageid in view packageDetails.PackageID = packageid; if (packageid == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } else { var package = db.Packages.Where(a => a.PackageId == packageid).FirstOrDefault(); if (package == null) { return(HttpNotFound()); } packageDetails.Packages = package; //packageDetails.Services = db.Services.ToList(); foreach (var item in db.Services.ToList()) { bool flag = true; foreach (var subItem in package.Package_Services) { if (item.ServiceID == subItem.ServiceID) { flag = false; break; } } if (flag) { if (packageDetails.Services == null) { packageDetails.Services = new List <Service>(); } (packageDetails.Services as List <Service>).Add(item); } } //packageDetails.Services = (from s in package.Package_Services // join from a in db.Services // where s.ServiceID != a.ServiceID // select a).ToList(); } } return(View(packageDetails)); }
public ActionResult SubscribePackage(AdhocUserPackageAddOnViewModel model, string[] services) { var package = db.Packages.Where(a => a.PackageId == model.PackageID).FirstOrDefault(); if (package == null) { return(View()); } var userId = User.Identity.GetUserId(); //var carList = db.CarUsers.Where(a => a.UserId == userId).FirstOrDefault(); //if (carList == null) // return View(); int existing_user_package_id = 0; if (Session["LinkCarId"] != null) { var linkCarId = Convert.ToInt32(Session["LinkCarId"]); existing_user_package_id = db.UserPackages.Where(a => a.CarId == linkCarId && a.PaymentRecieved == true).OrderByDescending(a => a.CreatedDt).Select(a => a.Id).FirstOrDefault(); } else { return(View()); } int caridfromuserpage = Convert.ToInt32(model.SelectedCar); if (Session["SelectedCar"] != null) { var car = Session["SelectedCar"] as CarUser; caridfromuserpage = car.CarId; } //Save User Packges var savingEntity = new AdhocUserPackage { UserId = userId, PackageId = package.PackageId, CarId = caridfromuserpage, SubscribedDate = DateTime.Now, ActualPrice = package.Package_Price, TotalPrice = model.Packages.Package_Price, PriceWithAddOns = model.Packages.Package_Price - package.Package_Price, DiscountPrice = 0, CreatedDt = DateTime.Now, ExistingPackageId = existing_user_package_id, PaymentRecieved = false }; db.AdhocUserPackages.Add(savingEntity); db.SaveChanges(); //Save uer addson if (services != null) { //Save user addons AdhocUserPackagesAddon addon = new AdhocUserPackagesAddon(); foreach (var item in services) { addon.ServiceID = Convert.ToInt32(item); addon.UserPackageID = savingEntity.Id; addon.ActualPrice = 0; addon.DiscountPrice = 0; addon.CreatedDt = DateTime.Now; db.AdhocUserPackagesAddons.Add(addon); db.SaveChanges(); } } if (Session["NewServiceGarageId"] != null) { var car = db.CarUsers.Where(a => a.CarId == caridfromuserpage).FirstOrDefault(); car.GarageId = Convert.ToInt32(Session["NewServiceGarageId"]); db.SaveChanges(); } //save car auto renewal //if (model.AutoRenewalSubscription) //{ // if (Session["SelectedCar"] != null) // { // var carTemp = Session["SelectedCar"] as CarUser; // var car = db.CarUsers.Where(a => a.CarId == carTemp.CarId).FirstOrDefault(); // car.AutoRenewal = model.AutoRenewalSubscription; // db.SaveChanges(); // } // else // { // var carId = Convert.ToInt32(model.SelectedCar); // var car = db.CarUsers.Where(a => a.CarId == carId).FirstOrDefault(); // car.AutoRenewal = model.AutoRenewalSubscription; // db.SaveChanges(); // } //} return(RedirectToAction("PrepaymentConfirmation", new { id = savingEntity.Id })); }