public JsonResult RedeemPoint(ViewPointRedemption model) { try { using (var db = new TourEntities()) { var product = db.ProductRedeem.Where(x => x.ProductRedeemId == model.ProductRedeemId).FirstOrDefault(); if (product == null) { ModelState.AddModelError("ProductRedeemId", "Product Redeem is not found."); } var user = MetadataServices.GetCurrentUser(); var customer = db.Customer.Where(x => x.UserId == user.UserId).FirstOrDefault(); if (customer.AvailablePoint < model.RedeemPoint) { ModelState.AddModelError("RedeemPoint", "Not Enough Redeem Point."); } if (ModelState.IsValid) { int balancePoint = (customer.AvailablePoint - product.RedeemPoint); var userTransaction = new UserTransaction() { AgentId = customer.AgentId, CustomerId = customer.CustomerId, IntroducerId = customer.IntroducerId, ReferenceNo = MetadataServices.GenerateCode(6), ActivityId = (int)Helpers.TransactionActivity.RedeemPoint, Amount = customer.AvailableAmount, PathForProof = string.Empty, CurrentTVR = customer.AvailableTVR, TopUpTVR = 0, RedeemTVR = 0, BalanceTVR = 0, CurrentPoint = customer.AvailablePoint, PointAdd = 0, PointRedeem = product.RedeemPoint, PointBalance = balancePoint, ActionDate = null, Remarks = "", ProductId = model.ProductRedeemId, 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 = customer.AvailableTVR, DebitTVR = 0, CreditAmount = 0, FinalAmount = customer.AvailableAmount, DebitAmount = 0, CreditPoint = 0, FinalPoint = balancePoint, DebitPoint = product.RedeemPoint, CreatedBy = user.Username, CreatedAt = DateTime.Now }; db.IntroducerTransaction.Add(customerTransaction); customer.AvailablePoint = balancePoint; 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; } }
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; } }