public ActionResult ViewPackageDetail(int packageId) { using (var db = new TourEntities()) { var package = db.Package.Where(x => x.PackageId == packageId).FirstOrDefault(); ViewPackageViewModel packageViewModel = new ViewPackageViewModel(); packageViewModel.PackageId = package.PackageId; packageViewModel.PackageName = package.PackageName; packageViewModel.Description = package.Description; packageViewModel.IteneraryDetail = package.IteneraryDetail; packageViewModel.PhotoPath = package.PhotoPath; packageViewModel.FilePath = package.FilePath; packageViewModel.TVR = package.TVR; packageViewModel.Amount = package.Amount; List <string> personAmount = new List <string>(); for (int i = 1; i <= 5; i++) { personAmount.Add(i.ToString()); } personAmount.Add("Other"); packageViewModel.PersonAmount = personAmount.ToList().Select(e => new SelectListItem() { Text = e.ToString(), Value = e.ToString(), }).ToList(); return(View(packageViewModel)); } }
public JsonResult RedeemPackage(ViewPackageViewModel model) { try { using (var db = new TourEntities()) { int amountHeadCount = 0; if (model.PersonAmountValue == "Other") { if (Convert.ToInt32(model.OtherPersonAmountValue) == 0 || model.OtherPersonAmountValue == null) { ModelState.AddModelError("OtherPersonAmountValue", "Other Headcount amount cannot be 0 or empty"); } else { amountHeadCount = Convert.ToInt32(model.OtherPersonAmountValue); } } else { amountHeadCount = Convert.ToInt32(model.PersonAmountValue); } var package = db.Package.FirstOrDefault(e => e.PackageId == model.PackageId); if (package == null) { ModelState.AddModelError("PackageId", "Package is not found."); } var user = MetadataServices.GetCurrentUser(); var customer = db.Customer.Where(x => x.UserId == user.UserId).FirstOrDefault(); decimal totalAmount = amountHeadCount * package.Amount; int totalTVR = amountHeadCount * package.TVR; if (customer.AvailableTVR < totalTVR) { ModelState.AddModelError("TVR", "Your current available is not enough. Please proceed to top up."); } if (ModelState.IsValid) { string imageName = ""; if (model.Image != null) { imageName = System.IO.Path.GetFileName(model.Image.FileName); imageName = MetadataServices.GetDateTimeWithoutSlash() + "-" + imageName; string physicalPath = Server.MapPath("~/Image/TransactionImage/" + imageName); model.Image.SaveAs(physicalPath); } int balanceTVR = (customer.AvailableTVR - totalTVR); var userTransaction = new UserTransaction() { AgentId = customer.AgentId, CustomerId = customer.CustomerId, IntroducerId = customer.IntroducerId, ReferenceNo = MetadataServices.GenerateCode(6), ActivityId = (int)Helpers.TransactionActivity.RedeemPackage, PathForProof = imageName, Amount = customer.AvailableAmount, CurrentTVR = customer.AvailableTVR, TopUpTVR = 0, RedeemTVR = totalTVR, BalanceTVR = balanceTVR, CurrentPoint = customer.AvailablePoint, PointAdd = 0, PointRedeem = 0, PointBalance = 0, ActionDate = null, Remarks = "", PackageId = model.PackageId, TravelHeadCount = amountHeadCount, CreatedAt = MetadataServices.GetCurrentDate(), CreatedBy = user.Username, UpdatedAt = MetadataServices.GetCurrentDate(), UpdatedBy = user.Username, IsDeleted = false, TransactionStatus = (int)TransactionStatus.Pending }; db.UserTransaction.Add(userTransaction); var customerTransaction = new IntroducerTransaction() { IntroducerId = customer.CustomerId, CreditTVR = 0, FinalTVR = balanceTVR, DebitTVR = totalTVR, CreditAmount = 0, FinalAmount = customer.AvailableAmount, DebitAmount = 0, CreditPoint = 0, FinalPoint = customer.AvailablePoint, DebitPoint = 0, CreatedBy = user.Username, CreatedAt = DateTime.Now }; db.IntroducerTransaction.Add(customerTransaction); customer.AvailableTVR = customer.AvailableTVR - totalTVR; db.SaveChanges(); return(Json(new { success = true }, JsonRequestBehavior.AllowGet)); } else { List <string> errors = new List <string>(); foreach (ModelState modelState in ViewData.ModelState.Values) { foreach (ModelError error in modelState.Errors) { errors.Add(string.IsNullOrEmpty(error.ErrorMessage) ? error.Exception.ToString() : error.ErrorMessage); } } return(Json(new { success = false, errors = errors }, JsonRequestBehavior.AllowGet)); } } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }