Beispiel #1
0
        public JsonResult SignUp(SignUpViewModel model)
        {
            int amount = 0;

            using (var db = new TourEntities())
            {
                var agent = db.Agent.FirstOrDefault(e => e.AgentCode == model.AgentCode);
                if (agent == null)
                {
                    ModelState.AddModelError("AgentCode", "Agent Code is not found.");
                }

                var introducer = new Customer();
                if (!string.IsNullOrEmpty(model.IntroducerCode))
                {
                    introducer = db.Customer.FirstOrDefault(e => e.CustomerCode == model.IntroducerCode);
                    if (introducer == null)
                    {
                        ModelState.AddModelError("IntroducerCode", "Introducer Code is not found.");
                    }
                }
                var systemSetting = db.SystemSetting.OrderByDescending(x => x.CreadtedDate).FirstOrDefault();

                if (model.TopUpAmountValue == "Other")
                {
                    if ((model.OtherAmount % systemSetting.MinAmount) != 0 || model.OtherAmount == 0)
                    {
                        ModelState.AddModelError("OtherAmount", "Other Amount not multiply of " + systemSetting.MinAmount);
                    }
                    else
                    {
                        amount = model.OtherAmount;
                    }
                }
                else
                {
                    amount = Convert.ToInt32(model.TopUpAmountValue);
                }

                var userWithNRIC = db.Customer.Where(x => x.NRIC == model.NRIC).FirstOrDefault();

                if (userWithNRIC != null)
                {
                    ModelState.AddModelError("NRIC", "NRIC Duplication");
                }

                var userWithUsername = db.User.Where(x => x.Username == model.Username).FirstOrDefault();

                if (userWithUsername != null)
                {
                    ModelState.AddModelError("Username", "Username Duplication");
                }

                if (ModelState.IsValid)
                {
                    var user = new User()
                    {
                        Username     = model.Username,
                        EmailAddress = model.EmailAddress,
                        Password     = PasswordHash.CreateHash(model.Password),
                        RoleId       = TourRoleId.Customer,
                        CreatedAt    = MetadataServices.GetCurrentDate(),
                        CreatedBy    = model.Username,
                        UpdatedAt    = MetadataServices.GetCurrentDate(),
                        UpdatedBy    = model.Username,
                        IsActive     = false,
                        IsDeleted    = false,
                    };
                    db.User.Add(user);
                    db.SaveChanges();

                    string customerCode = model.NRIC.GetLast(6);
                    var    customer     = new Customer()
                    {
                        CustomerName      = model.CustomerName,
                        NRIC              = model.NRIC,
                        CustomerCode      = customerCode,
                        BankAccountNumber = model.BankAccountNumber,
                        PhoneNumber       = model.PhoneNumber,
                        Address           = model.Address,
                        AvailableTVR      = 0,
                        AvailableAmount   = 0,
                        AvailablePoint    = 0,
                        AgentId           = agent.AgentId,
                        IntroducerId      = introducer.CustomerId,
                        JoinDate          = MetadataServices.GetCurrentDate(),
                        UserId            = user.UserId,
                        CreatedAt         = MetadataServices.GetCurrentDate(),
                        CreatedBy         = model.Username,
                        UpdatedAt         = MetadataServices.GetCurrentDate(),
                        UpdatedBy         = model.Username,
                        DateOfBirth       = model.DateOfBirth,
                        RenewDate         = MetadataServices.GetCurrentDate(),
                        ExpiredDate       = MetadataServices.GetCurrentDate(),
                        BankName          = model.BankName,
                        IsDeleted         = false
                    };
                    db.Customer.Add(customer);
                    db.SaveChanges();

                    var customerNew = db.Customer.FirstOrDefault(e => e.CustomerCode == customerCode);


                    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);
                    }

                    var userTransaction = new UserTransaction()
                    {
                        AgentId           = agent.AgentId,
                        CustomerId        = customerNew.CustomerId,
                        IntroducerId      = customerNew.IntroducerId,
                        ReferenceNo       = MetadataServices.GenerateCode(6),
                        ActivityId        = (int)Helpers.TransactionActivity.SignUp,
                        PathForProof      = imageName,
                        Amount            = 0,
                        CurrentTVR        = 0,
                        TopUpTVR          = systemSetting.TVRMutiply * amount,
                        RedeemTVR         = 0,
                        BalanceTVR        = 0,
                        CurrentPoint      = 0,
                        PointAdd          = ((amount / systemSetting.MinAmount) * systemSetting.PointCalculation),
                        PointRedeem       = 0,
                        PointBalance      = 0,
                        ActionDate        = null,
                        Remarks           = "",
                        CreatedAt         = MetadataServices.GetCurrentDate(),
                        CreatedBy         = model.Username,
                        UpdatedAt         = MetadataServices.GetCurrentDate(),
                        UpdatedBy         = model.Username,
                        IsDeleted         = false,
                        TransactionStatus = (int)TransactionStatus.Pending
                    };
                    db.UserTransaction.Add(userTransaction);
                    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));
                }
            }
        }
Beispiel #2
0
        public JsonResult TopUpCustomer(TopUpViewModel topUpVieModel)
        {
            using (var db = new TourEntities())
            {
                int amount = 0;

                var systemSetting = db.SystemSetting.OrderByDescending(x => x.CreadtedDate).FirstOrDefault();

                if (topUpVieModel.TopUpAmountValue == "Other")
                {
                    if ((topUpVieModel.OtherAmount % systemSetting.MinAmount) != 0 || topUpVieModel.OtherAmount == 0)
                    {
                        ModelState.AddModelError("OtherAmount", "Other Amount not multiply of " + systemSetting.MinAmount);
                    }
                    else
                    {
                        amount = topUpVieModel.OtherAmount;
                    }
                }
                else
                {
                    amount = Convert.ToInt32(topUpVieModel.TopUpAmountValue);
                }

                if (ModelState.IsValid)
                {
                    var user     = MetadataServices.GetCurrentUser();
                    var customer = db.Customer.Where(x => x.UserId == user.UserId).FirstOrDefault();

                    string imageName = "";

                    if (topUpVieModel.Image != null)
                    {
                        imageName = System.IO.Path.GetFileName(topUpVieModel.Image.FileName);
                        imageName = MetadataServices.GetDateTimeWithoutSlash() + "-" + imageName;
                        string physicalPath = Server.MapPath("~/Image/TransactionImage/" + imageName);
                        topUpVieModel.Image.SaveAs(physicalPath);
                    }

                    var userTransaction = new UserTransaction()
                    {
                        AgentId           = customer.AgentId,
                        CustomerId        = customer.CustomerId,
                        IntroducerId      = customer.IntroducerId,
                        ReferenceNo       = MetadataServices.GenerateCode(6),
                        ActivityId        = (int)Helpers.TransactionActivity.TopUp,
                        PathForProof      = imageName,
                        Amount            = amount,
                        CurrentTVR        = customer.AvailableTVR,
                        TopUpTVR          = systemSetting.TVRMutiply * amount,
                        RedeemTVR         = 0,
                        BalanceTVR        = 0,
                        CurrentPoint      = customer.AvailablePoint,
                        PointAdd          = ((amount / systemSetting.MinAmount) * systemSetting.PointCalculation),
                        PointRedeem       = 0,
                        PointBalance      = 0,
                        ActionDate        = null,
                        Remarks           = "",
                        CreatedAt         = MetadataServices.GetCurrentDate(),
                        CreatedBy         = MetadataServices.GetCurrentUser().Username,
                        UpdatedAt         = MetadataServices.GetCurrentDate(),
                        UpdatedBy         = MetadataServices.GetCurrentUser().Username,
                        IsDeleted         = false,
                        TransactionStatus = (int)TransactionStatus.Pending
                    };


                    db.UserTransaction.Add(userTransaction);
                    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));
                }
            }
        }
Beispiel #3
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;
            }
        }
Beispiel #4
0
        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;
            }
        }