Example #1
0
        private dynamic Calculate(int id, string action)
        {
            PassBook passBook = db.PassBooks.SingleOrDefault(n => n.Id == id);
            double   rate = passBook.InterestRate, demandrate = passBook.DemandInterestRate;
            double   interest = 0, interestKKH = 0;
            int      checkDays = (DateTime.Now - passBook.ChangeDate).Days;

            if (checkDays < passBook.Term.MinDate)
            {
                //Tính lãi suất không kỳ hạn
                interestKKH = ((double)passBook.Balance * (demandrate / 100) / 360) * checkDays;
            }
            else if (checkDays == passBook.Term.MinDate)
            {
                //Tính lãi suất đúng kỳ hạn
                interest = ((double)passBook.Balance * (rate / 100) / 360) * passBook.Term.MinDate;
            }
            else
            {
                //Tính lãi suất đúng kỳ hạn + lãi suất không kỳ hạn
                int      checkTerms = checkDays / passBook.Term.MinDate;
                double   currentBal = (double)passBook.Balance;
                DateTime changeDate = passBook.ChangeDate;
                //Vòng lặp tính lãi suất mỗi lần đúng kỳ hạn
                for (int i = 0; i < checkTerms; i++)
                {
                    interest   = (currentBal * (rate / 100) / 360) * passBook.Term.MinDate;
                    changeDate = changeDate.AddDays(passBook.Term.MinDate);

                    if (action == "AutoTermEnd")
                    {
                        PassbookDetail pbDetail = new PassbookDetail();
                        pbDetail.PassbookId = id;
                        pbDetail.Action     = "Lãi kỳ hạn";
                        pbDetail.ActionDate = changeDate;
                        pbDetail.Balance    = decimal.Parse(currentBal.ToString("F"));
                        pbDetail.Amount     = decimal.Parse(interest.ToString("F"));
                        pbDetail.Surplus    = decimal.Parse((currentBal + interest).ToString("F"));
                        pbDetail.Status     = 1;

                        db.PassbookDetails.Add(pbDetail);
                    }

                    currentBal += interest;
                }
                passBook.ChangeDate = changeDate;
                passBook.Balance    = decimal.Parse(currentBal.ToString("F"));
                passBook.TermEnd    = 1;
                interestKKH         = (currentBal * (demandrate / 100) / 360) * (checkDays - (passBook.Term.MinDate * checkTerms));
            }
            interest    = double.Parse(interest.ToString("F"));
            interestKKH = double.Parse(interestKKH.ToString("F"));
            var res = new
            {
                interest,
                interestKKH
            };

            return(res);
        }
Example #2
0
        public ActionResult Withdraw([Bind(Include = "Id,PassBookId,Acronym,WithdrawDate,Amount,Status")] Withdraw withdraw)
        {
            withdraw.Acronym      = "W_TESTING";
            withdraw.WithdrawDate = DateTime.Now;
            withdraw.Status       = 1;

            string   msgamount = null, msg = null;
            PassBook passBook  = db.PassBooks.SingleOrDefault(n => n.Id == withdraw.PassBookId);
            int      checkDays = (DateTime.Now - passBook.ChangeDate).Days;

            if (withdraw.Amount == 0)
            {
                msgamount = "Nhập số tiền rút !";
            }
            else if (withdraw.Amount > passBook.Balance)
            {
                msgamount = "Số tiền rút không thể lớn hơn số dư hiện tại !";
            }
            else if (withdraw.Amount < 100000)
            {
                //Error message
                msgamount = "Số tiền rút không thể nhỏ hơn 100.000 !";
            }
            else if (withdraw.Amount % 50000 > 0)
            {
                msgamount = "Đơn vị tiền nhỏ nhất là 50.000 !";
            }
            else
            {
                msg = "completed";
                db.Withdraws.Add(withdraw);

                decimal interest = 0;

                if (checkDays == passBook.Term.MinDate && passBook.TermEnd == 1)
                {
                    interest = decimal.Parse(Calculate(withdraw.PassBookId, "withdraw").interest.ToString("F"));

                    PassbookDetail pbDetail2 = new PassbookDetail();
                    pbDetail2.PassbookId = passBook.Id;
                    pbDetail2.ActionDate = passBook.ChangeDate.AddDays(passBook.Term.MinDate);
                    pbDetail2.Action     = "Lãi kỳ hạn";
                    pbDetail2.Balance    = passBook.Balance;
                    pbDetail2.Amount     = interest;
                    pbDetail2.Surplus    = pbDetail2.Balance + interest;
                    pbDetail2.Status     = 1;
                    db.PassbookDetails.Add(pbDetail2);

                    passBook.ChangeDate = passBook.ChangeDate.AddDays(passBook.Term.MinDate);
                }

                PassbookDetail pbDetail = new PassbookDetail();
                pbDetail.PassbookId = passBook.Id;
                pbDetail.ActionDate = DateTime.Now;
                pbDetail.Action     = "Rút một phần";
                pbDetail.Balance    = passBook.Balance + interest;
                pbDetail.Amount     = -withdraw.Amount;
                pbDetail.Surplus    = pbDetail.Balance - withdraw.Amount;
                pbDetail.Status     = 2;
                db.PassbookDetails.Add(pbDetail);

                decimal demandinterest = decimal.Parse((((double)withdraw.Amount * (passBook.DemandInterestRate / 100) / 360) * checkDays).ToString("F"));

                PassbookDetail pbDetail3 = new PassbookDetail();
                pbDetail3.PassbookId = passBook.Id;
                pbDetail3.ActionDate = DateTime.Now;
                pbDetail3.Action     = "Lãi khi rút";
                pbDetail3.Balance    = passBook.Balance + interest - withdraw.Amount;
                pbDetail3.Amount     = demandinterest;
                pbDetail3.Surplus    = pbDetail3.Balance + demandinterest;
                pbDetail3.Status     = 2;
                db.PassbookDetails.Add(pbDetail3);

                passBook.Balance += (interest - withdraw.Amount + demandinterest);
                db.SaveChanges();
                return(Json(new { msg }, JsonRequestBehavior.AllowGet));
            }

            return(Json(new { msgamount }, JsonRequestBehavior.AllowGet));
        }
Example #3
0
        public ActionResult Close([Bind(Include = "Id,PassBookId,Acronym,WithdrawDate,Amount,Status")] Withdraw withdraw)
        {
            withdraw.Acronym      = "C_TESTING";
            withdraw.WithdrawDate = DateTime.Now;
            withdraw.Status       = 1;

            string   msg      = null;
            PassBook passBook = db.PassBooks.SingleOrDefault(n => n.Id == withdraw.PassBookId);

            if (ModelState.IsValid)
            {
                int checkDays = (DateTime.Now - passBook.ChangeDate).Days;
                //Tính lãi suất
                var res = Calculate(withdraw.PassBookId, "close");

                decimal interest = 0, demandinterest = 0;
                interest       = decimal.Parse(res.interest.ToString("F"));
                demandinterest = decimal.Parse(res.interestKKH.ToString("F"));

                if (checkDays == passBook.Term.MinDate)
                {
                    PassbookDetail pbDetail2 = new PassbookDetail();
                    pbDetail2.PassbookId = passBook.Id;
                    pbDetail2.ActionDate = passBook.ChangeDate.AddDays(passBook.Term.MinDate);
                    pbDetail2.Action     = "Lãi kỳ hạn";
                    pbDetail2.Balance    = passBook.Balance;
                    pbDetail2.Amount     = interest;
                    pbDetail2.Surplus    = pbDetail2.Balance + interest;
                    pbDetail2.Status     = 1;
                    db.PassbookDetails.Add(pbDetail2);
                }
                else if (checkDays < passBook.Term.MinDate)
                {
                    PassbookDetail pbDetail3 = new PassbookDetail();
                    pbDetail3.PassbookId = passBook.Id;
                    pbDetail3.ActionDate = DateTime.Now;
                    pbDetail3.Action     = "Lãi KKH";
                    pbDetail3.Balance    = passBook.Balance;
                    pbDetail3.Amount     = demandinterest;
                    pbDetail3.Surplus    = pbDetail3.Balance + demandinterest;
                    pbDetail3.Status     = 1;
                    db.PassbookDetails.Add(pbDetail3);
                }

                //Add PassbookDetail
                PassbookDetail pbDetail = new PassbookDetail();
                pbDetail.PassbookId = passBook.Id;
                pbDetail.ActionDate = DateTime.Now;
                pbDetail.Action     = "Tất toán";
                pbDetail.Balance    = passBook.Balance + interest + demandinterest;
                pbDetail.Amount     = -pbDetail.Balance;
                pbDetail.Surplus    = 0;
                pbDetail.Status     = 2;
                db.PassbookDetails.Add(pbDetail);

                withdraw.Amount = pbDetail.Balance;

                //Edit Passbook details
                passBook.Balance    = 0;
                passBook.ChangeDate = DateTime.Now;
                passBook.TermEnd    = 3;
                passBook.Status     = 2;

                msg = "completed";

                db.Withdraws.Add(withdraw);
                db.SaveChanges();
                return(Json(new { msg }, JsonRequestBehavior.AllowGet));
            }

            return(Json(new { msg }, JsonRequestBehavior.AllowGet));
        }
Example #4
0
        public ActionResult Deposit([Bind(Include = "Id,PassBookId,Acronym,DepositDate,Amount,Status")] Deposit deposit)
        {
            deposit.PassBookId  = int.Parse(Session["passbookid"].ToString());
            deposit.Acronym     = "D_TESTING";
            deposit.DepositDate = DateTime.Now;
            deposit.Status      = 1;
            string msgamount = null, msg = null;

            if (deposit.Amount == 0)
            {
                msgamount = "Nhập số tiền gửi !";
            }
            else if (deposit.Amount < 100000)
            {
                //Error message
                msgamount = "Số tiền gửi không thể nhỏ hơn 100.000 !";
            }
            else if (deposit.Amount % 50000 > 0)
            {
                msgamount = "Đơn vị tiền nhỏ nhất là 50.000 !";
            }
            else
            {
                msg = "completed";
                db.Deposits.Add(deposit);

                PassBook passBook  = db.PassBooks.SingleOrDefault(n => n.Id == deposit.PassBookId);
                decimal  interest  = 0;
                int      checkDays = (DateTime.Now - passBook.ChangeDate).Days;

                if (checkDays == passBook.Term.MinDate && passBook.TermEnd == 1)
                {
                    interest = decimal.Parse(Calculate(deposit.PassBookId, "deposit").interest.ToString("F"));

                    PassbookDetail pbDetail2 = new PassbookDetail();
                    pbDetail2.PassbookId = passBook.Id;
                    pbDetail2.ActionDate = passBook.ChangeDate.AddDays(passBook.Term.MinDate);
                    pbDetail2.Action     = "Lãi kỳ hạn";
                    pbDetail2.Balance    = passBook.Balance;
                    pbDetail2.Amount     = interest;
                    pbDetail2.Surplus    = pbDetail2.Balance + interest;
                    pbDetail2.Status     = 1;
                    db.PassbookDetails.Add(pbDetail2);

                    passBook.ChangeDate = passBook.ChangeDate.AddDays(passBook.Term.MinDate);
                }

                PassbookDetail pbDetail = new PassbookDetail();
                pbDetail.PassbookId = passBook.Id;
                pbDetail.ActionDate = DateTime.Now;
                pbDetail.Action     = "Gửi thêm";
                pbDetail.Balance    = passBook.Balance + interest;
                pbDetail.Amount     = deposit.Amount;
                pbDetail.Surplus    = pbDetail.Balance + deposit.Amount;
                pbDetail.Status     = 1;
                db.PassbookDetails.Add(pbDetail);

                passBook.Balance += (interest + deposit.Amount);
                db.SaveChanges();
                return(Json(new { msg }, JsonRequestBehavior.AllowGet));
            }

            return(Json(new { msgamount }, JsonRequestBehavior.AllowGet));
        }