public ActionResult Create(//[Bind(Include = "DepositId,UserId,Duration,DepositType,MaturityDate,TotalInstallments,DepositAmount,MatureAmount,InstallmentAmount,ClosedDate,RecurringDepositDate,Status,ChequeDetails,InteresRate,ApprovedDate,ApprovedBy,Notes,CreatedOn,ModifiedOn,CreatedBy,ModifiedBy,IsDeleted,DeletedBy,DeletedOn")] 
            sdtoDepositInfo depositInfo)
        {
            depositInfo.DepositType = Data.Models.Enumerations.DepositType.Fixed;
            sdtoUser sessionUser = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser) as sdtoUser;
            depositInfo.IsDeleted = false;
            depositInfo.CreatedBy = sessionUser != null ? sessionUser.UserID : 0;
            depositInfo.CreatedOn = DateTime.Now;
            depositInfo.MatureAmount = depositInfo.DepositAmount + ((depositInfo.DepositAmount * Convert.ToDecimal(depositInfo.InteresRate / 100) * depositInfo.Duration) / 365);
            depositInfo.MaturityDate = depositInfo.CreatedOn.Value.AddDays(depositInfo.Duration);
            if (ModelState.IsValid)
            {
                db.sdtoDepositInfoes.Add(depositInfo);
                db.SaveChanges();

                bfTransaction objTrans = new bfTransaction(db);
                objTrans.PostDepositIssue(depositInfo);

                return RedirectToAction("Index");
            }

            var listUsers = db.User.Where(x => x.UserType == UserType.Member);
            ViewBag.UserList = new SelectList(listUsers.Select(x => new { UserID = x.UserID, Name = x.FirstName + " " + x.LastName }), "UserID", "Name");
            return View(depositInfo);
        }
        // GET: sdtoDepositInfoes/Create
        public ActionResult Create(long? UserId)
        {
            var bankInterest = db.GeneralSettings.FirstOrDefault().BankInterest;
            var deposit = new sdtoDepositInfo();
            deposit.DepositType = Data.Models.Enumerations.DepositType.Fixed;
            deposit.Status = Data.Models.Enumerations.DepositStatus.Active;
            deposit.InteresRate = Convert.ToSingle(bankInterest == null ? 0 : bankInterest.Value);
            deposit.Duration = 30;
            deposit.IsDeleted = false;
            sdtoUser sessionUser = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser) as sdtoUser;
            deposit.CreatedBy = sessionUser != null ? sessionUser.UserID : 0;
            deposit.CreatedOn = DateTime.Now;

            var listUsers = db.User.Where(x => x.UserType == UserType.Member && (UserId == null || UserId.Value == 0 || x.UserID == UserId)).OrderBy(x=>x.FirstName);

            if (listUsers == null || listUsers.Count(x => x.UserID > 0) == 0)
                return RedirectToAction("Create", "Member");

            ViewBag.UserList = new SelectList(listUsers.Select(x => new { UserID = x.UserID, Name = x.FirstName + " " + x.LastName }), "UserID", "Name");
            return View(deposit);
        }
        public bool PostDepositIssue(sdtoDepositInfo DepositInfo)
        {
            bool tranFlag = true;

            var member = AppDb.User.Where(x => x.UserID == DepositInfo.UserId).FirstOrDefault();
            if (member != null)
            {
                var accHeadMember = AppDb.AccountHeads.Find(member.AccountHeadId);
                if (accHeadMember != null)
                {
                    var settingCashBookId = AppDb.GeneralSettings.FirstOrDefault().CashBookId;
                    var settingBankBookId = AppDb.GeneralSettings.FirstOrDefault().BankBookId;
                    //Post for Bank book
                    var accCashBook = AppDb.AccountBooks.Where(x => x.AccountBookId == settingCashBookId).FirstOrDefault();
                    var accBankBook = AppDb.AccountBooks.Where(x => x.AccountBookId == settingBankBookId).FirstOrDefault();
                    if (accCashBook != null && accBankBook != null)
                    {
                        var receipt = new sdtoReceiptHeader()
                        {
                            BookId = accCashBook.AccountBookId,
                            TransDate = DateTime.Now,
                            VoucherTotal = DepositInfo.DepositAmount, //Doubt: Voucher total should be loan amount or loan amount + additional value from user
                            TransType = ReceiptType.Receipt,
                            FinancialYearId = CurrentUser.UserSession.FinancialYearId.Value,
                            FromModule = 1,  //Doubt: 0 for "From Accounts", 1 for "From Posting"
                            Transaction = TransactionType.DepositEntry, //Doubt: //0 for Cash Receipt, 1 for Cash Payment, 2 for "Loan Entry", 3 for "Loan repayment"
                            TransId = DepositInfo.DepositId, //Doubt: Is transaction id loan id?
                            Cancelled = 0
                        };
                        AppDb.ReceiptHeader.Add(receipt);
                        AppDb.SaveChanges();

                        if (DepositInfo.InstrumentMode == Instrument.Cash)
                            receipt.VoucherNo = accCashBook.ReceiptVoucherPrefix + receipt.Id + accCashBook.ReceiptVoucherSuffix;
                        else
                            receipt.VoucherNo = accBankBook.ReceiptVoucherPrefix + receipt.Id + accBankBook.ReceiptVoucherSuffix;
                        AppDb.Entry(receipt).State = EntityState.Modified;
                        AppDb.SaveChanges();

                        // Member
                        var receiptDetailsCr = new sdtoReceiptDetails()
                        {
                            ReceiptsId = receipt.Id,
                            AccountId = accHeadMember.AccountHeadId,
                            Narration = "Deposit Account Opened",
                            Amount = -1 * DepositInfo.DepositAmount,
                            Display = 1
                        };

                        // Cash Account
                        var receiptDetailsDb = new sdtoReceiptDetails()
                        {
                            ReceiptsId = receipt.Id,
                            AccountId = DepositInfo.InstrumentMode == Instrument.Cash ? accCashBook.AccountHeadId.Value : accBankBook.AccountHeadId.Value,
                            Narration = "Loan Repayment received",
                            Amount = DepositInfo.DepositAmount,
                            Display = 1
                        };

                        AppDb.ReceiptDetails.Add(receiptDetailsCr);
                        AppDb.ReceiptDetails.Add(receiptDetailsDb);
                        AppDb.SaveChanges();
                    }
                }
            }

            return tranFlag;
        }
        public bool CancelPostedDepositIssue(sdtoDepositInfo DepositInfo)
        {
            bool bFlag = true;
            var member = AppDb.User.Where(x => x.UserID == DepositInfo.UserId).FirstOrDefault();
            if (member != null)
            {
                var accHeadMember = AppDb.AccountHeads.Find(member.AccountHeadId);
                if (accHeadMember != null)
                {
                    var settingCashBookId = AppDb.GeneralSettings.FirstOrDefault().CashBookId;
                    var settingBankBookId = AppDb.GeneralSettings.FirstOrDefault().BankBookId;
                    //Post for Bank book
                    var accCashBook = AppDb.AccountBooks.Where(x => x.AccountBookId == settingCashBookId).FirstOrDefault();
                    var accBankBook = AppDb.AccountBooks.Where(x => x.AccountBookId == settingBankBookId).FirstOrDefault();
                    if (accCashBook != null && accBankBook != null)
                    {
                        var header = AppDb.ReceiptHeader.Where(x => x.IsDeleted == false && x.Cancelled == 0 && (x.BookId == accCashBook.AccountBookId || x.BookId == accBankBook.AccountBookId) && x.TransId == DepositInfo.DepositId && x.Transaction == TransactionType.DepositEntry).FirstOrDefault();
                        if (header != null)
                        {
                            header.Cancelled = 1;
                            AppDb.Entry(header).State = EntityState.Modified;
                            AppDb.SaveChanges();

                            var dtls = AppDb.ReceiptDetails.Where(x => x.ReceiptsId == header.Id && x.IsDeleted == false).ToList();
                            //dtls.ForEach(x => x.IsDeleted = true);

                            foreach (var dtlItem in dtls)
                            {
                                dtlItem.IsDeleted = true;
                                AppDb.Entry(dtlItem).State = EntityState.Modified;
                            }

                            AppDb.SaveChanges();
                        }
                    }
                }
            }
            return bFlag;
        }