public ActionResult Edit(//[Bind(Include = "LoanRepaymentId,LoanId,RepaymentCode,PrincipalAmount,InterestAmount,InterestRate,RepaymentAmount,PendingPrincipalAmount,Status,PaymentMode,ChequeDetails,Notes,CreatedOn,ModifiedOn,CreatedBy,ModifiedBy,IsDeleted,DeletedBy,DeletedOn")]
            sdtoLoanRepayment sdtoLoanRepayment)
        {
            if (ModelState.IsValid)
            {
                var loandetails = db.sdtoLoanInfoes.Find(sdtoLoanRepayment.LoanId);
                var lastRepaymentDate = loandetails.RepaymentStartDate.Value;
                var pendingPrincipalAmount = loandetails.LoanAmount;
                decimal previousDue = 0;

                var previousLoanRepayment1 = db.sdtoLoanRepayments.Where(x => x.LoanId == sdtoLoanRepayment.LoanId && x.IsDeleted == false && x.Status != RepaymentStatus.Cancelled && x.LoanRepaymentId != sdtoLoanRepayment.LoanRepaymentId).OrderByDescending(x => x.LoanRepaymentId).FirstOrDefault();
                if (previousLoanRepayment1 != null && previousLoanRepayment1.LoanRepaymentId > 0)
                {
                    lastRepaymentDate = previousLoanRepayment1.RepaymentDate.Value;
                    pendingPrincipalAmount = previousLoanRepayment1.PendingPrincipalAmount;
                    previousDue = previousLoanRepayment1.PreviousPaymentDueAmount;
                }
                var loanRepayment1 = db.sdtoLoanRepayments.Where(x => x.LoanRepaymentId == sdtoLoanRepayment.LoanRepaymentId && x.IsDeleted == false && x.Status != RepaymentStatus.Cancelled).FirstOrDefault();

                if (loanRepayment1 != null && loanRepayment1.LoanRepaymentId > 0)
                {
                    var days = (sdtoLoanRepayment.RepaymentDate.Value - lastRepaymentDate).Days;
                    days = days == 0 ? 1 : days;
                    var repaymentInterestAmt = (pendingPrincipalAmount * Convert.ToDecimal(sdtoLoanRepayment.InterestRate / 100) * days) / 365;

                    sdtoLoanRepayment.InterestAmount = Math.Round(repaymentInterestAmt, 2);

                    decimal paymentBalance = sdtoLoanRepayment.RepaymentAmount - repaymentInterestAmt - loandetails.InstallmentAmount;
                    sdtoLoanRepayment.PreviousPaymentDueAmount = paymentBalance > 0 ? (previousDue - paymentBalance) : (previousDue + paymentBalance);

                    sdtoLoanRepayment.PendingPrincipalAmount = pendingPrincipalAmount - loandetails.InstallmentAmount + sdtoLoanRepayment.PreviousPaymentDueAmount; //(sdtoLoanRepayment.RepaymentAmount - repaymentInterestAmt);
                    sdtoLoanRepayment.PendingInstallments -= Convert.ToInt32(Math.Floor(sdtoLoanRepayment.PendingPrincipalAmount / loandetails.InstallmentAmount));
                    sdtoLoanRepayment.PrincipalAmount = loandetails.LoanAmount;
                }

                db.Entry(sdtoLoanRepayment).State = EntityState.Modified;
                db.SaveChanges();

                bfTransaction objTrans = new bfTransaction(db);
                objTrans.CancelPostedLoanRepayment(sdtoLoanRepayment);
                objTrans.PostLoanRepayment(sdtoLoanRepayment);

                return RedirectToAction("Index", new { LoanId = sdtoLoanRepayment.LoanId });
            }
            var itemsLoan = db.sdtoLoanInfoes.Include(x => x.Member).ToList();
            var itemsLoans = itemsLoan.Select(x => new SelectListItem() { Value = x.LoanId.ToString(), Text = x.LoanId + " - " + x.LoanAmount + "[" + x.Member.FirstName + " " + x.Member.LastName + "]" }).ToList();
            itemsLoans.Insert(0, new SelectListItem() { Value = "0", Text = "Select a loan" });
            ViewBag.LoanList = new SelectList(itemsLoans, "Value", "Text");

            return View(sdtoLoanRepayment);
        }
        public ActionResult Create(//[Bind(Include = "LoanRepaymentId,LoanId,RepaymentCode,PrincipalAmount,InterestAmount,InterestRate,RepaymentAmount,PendingPrincipalAmount,Status,PaymentMode,ChequeDetails,Notes,CreatedOn,ModifiedOn,CreatedBy,ModifiedBy,IsDeleted,DeletedBy,DeletedOn")] 
            sdtoLoanRepayment sdtoLoanRepayment)
        {
            if (ModelState.IsValid)
            {
                if (sdtoLoanRepayment.LoanId > 0)
                {
                    var loandetails = db.sdtoLoanInfoes.Find(sdtoLoanRepayment.LoanId);

                    var loanPendingAmt = loandetails.LoanAmount;
                    var loanInterest = loandetails.InteresRate;
                    var loanPendingInstallments = loandetails.TotalInstallments;

                    var loanRepayment = db.sdtoLoanRepayments.Where(x => x.LoanId == sdtoLoanRepayment.LoanId && x.IsDeleted == false && x.Status != RepaymentStatus.Cancelled).OrderByDescending(x => x.LoanRepaymentId).FirstOrDefault();
                    var repaymentInterest = loanInterest;
                    var repaymentInterestAmt = (loanPendingAmt * Convert.ToDecimal(repaymentInterest / 100)) / 365;
                    var lastRepaymentDate = loandetails.RepaymentStartDate.Value;
                    var days = (DateTime.Now.Date - loandetails.RepaymentStartDate.Value).Days;

                    bfReport objReport = new bfReport(db);

                    if (loanRepayment != null && loanRepayment.LoanRepaymentId > 0)
                    {
                        if (loanRepayment.RepaymentDate != null)
                            lastRepaymentDate = loanRepayment.RepaymentDate.Value;
                        days = (sdtoLoanRepayment.RepaymentDate.Value - lastRepaymentDate).Days;
                        days = days == 0 ? 1 : days;
                        if (days < 0)
                            days = 0;

                        loanPendingAmt = loanRepayment.PendingPrincipalAmount;
                        loanPendingInstallments = loanRepayment.PendingInstallments;
                        repaymentInterest = loanRepayment.InterestRate;

                        repaymentInterestAmt = (loanPendingAmt * Convert.ToDecimal(repaymentInterest / 100) * days) / 365;
                        sdtoLoanRepayment.RepaymentCode = loanRepayment.RepaymentCode;

                        decimal paymentBalance = sdtoLoanRepayment.RepaymentAmount - repaymentInterestAmt - (loandetails.InstallmentAmount * days);
                        decimal previousDue = paymentBalance > 0 ? (loanRepayment.PreviousPaymentDueAmount - paymentBalance) : (loanRepayment.PreviousPaymentDueAmount + paymentBalance);
                        sdtoLoanRepayment.PreviousPaymentDueAmount = previousDue;
                    }

                    sdtoLoanRepayment.InterestAmount = Math.Round(repaymentInterestAmt, 2);
                    sdtoLoanRepayment.PendingPrincipalAmount = loanPendingAmt - (loandetails.InstallmentAmount * days) + sdtoLoanRepayment.PreviousPaymentDueAmount; //(sdtoLoanRepayment.RepaymentAmount - repaymentInterestAmt);
                    sdtoLoanRepayment.PendingInstallments -= Convert.ToInt32(Math.Floor(sdtoLoanRepayment.PendingPrincipalAmount / loandetails.InstallmentAmount));
                    sdtoLoanRepayment.PrincipalAmount = loandetails.LoanAmount;

                    sdtoLoanRepayment.Status = Data.Models.Enumerations.RepaymentStatus.Paid;
                    sdtoLoanRepayment.CreatedOn = DateTime.Now;

                    sdtoLoanRepayment.RepaymentCode = objReport.GenerateCode("LoanRepayment");

                    db.sdtoLoanRepayments.Add(sdtoLoanRepayment);
                    db.SaveChanges();

                    bfTransaction objTrans = new bfTransaction(db);
                    objTrans.PostLoanRepayment(sdtoLoanRepayment);

                    return RedirectToAction("Index", new { LoanId = sdtoLoanRepayment.LoanId });
                }
            }

            var itemsLoan = db.sdtoLoanInfoes.Include(x => x.Member).ToList();
            var itemsLoans = itemsLoan.Select(x => new SelectListItem() { Value = x.LoanId.ToString(), Text = x.LoanId + " - " + x.LoanAmount + "[" + x.Member.FirstName + " " + x.Member.LastName + "]" }).ToList();
            itemsLoans.Insert(0, new SelectListItem() { Value = "0", Text = "Select a loan" });
            ViewBag.LoanList = new SelectList(itemsLoans, "Value", "Text");

            return View(sdtoLoanRepayment);
        }