Ejemplo n.º 1
0
        public ActionResult ViewPointRedemption(int productId)
        {
            using (var db = new TourEntities())
            {
                var product = db.ProductRedeem.Where(x => x.ProductRedeemId == productId).FirstOrDefault();

                ViewPointRedemption pointRedemptionViewModel = new ViewPointRedemption();

                pointRedemptionViewModel.ProductRedeemId   = product.ProductRedeemId;
                pointRedemptionViewModel.RedeemPoint       = product.RedeemPoint;
                pointRedemptionViewModel.ProductRedeemName = product.ProductRedeemName;
                pointRedemptionViewModel.RedeemPoint       = product.RedeemPoint;
                pointRedemptionViewModel.ImagePath         = product.ImagePath;

                return(View(pointRedemptionViewModel));
            }
        }
Ejemplo n.º 2
0
        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;
            }
        }