public bool PostLoanIssue(sdtoLoanInfo LoanInfo)
        {
            bool tranFlag = true;

            //      var member = AppDb.User.Join(AppDb.AccountHeads, // the source table of the inner join
            //post => post.AccountHead.AccountHeadId,        // Select the primary key (the first part of the "on" clause in an sql "join" statement)
            //meta => meta.AccountHeadId,   // Select the foreign key (the second part of the "on" clause)
            //(post, meta) => new { Post = post, Meta = meta }).Where(x => x.Post.UserID == LoanInfo.UserId).Select(x => new { x.Post.UserID, AccountHead = new { AccountHeadId = x.Meta.AccountHeadId, AccountCode = x.Meta.AccountCode, AccountName = x.Meta.AccountName, AccountType = x.Meta.AccountType, AccountTypeId = x.Meta.AccountTypeId } }).FirstOrDefault();

            //from f in AppDb.User
            //join b in AppDb.AccountHeads on f.AccountHeadId equals b.AccountHeadId
            //where f.UserID == LoanInfo.UserId
            //select new { f.UserID, AccountHead = new sdtoAccountHead() { AccountHeadId = b.AccountHeadId, AccountCode = b.AccountCode, AccountName = b.AccountName, AccountType = b.AccountType, AccountTypeId = b.AccountTypeId } };

            //var member = members.FirstOrDefault();

            var member = AppDb.User.Where(x => x.UserID == LoanInfo.UserId).FirstOrDefault();

            //var member = AppDb.User.Include(x => x.AccountHeadId).Where(x => x.UserID == LoanInfo.UserId).FirstOrDefault();
            if (member != null)
            {
                var accHeadMember = AppDb.AccountHeads.Find(member.AccountHeadId);
                if (accHeadMember != null)
                {
                    var settingCashBookId = AppDb.GeneralSettings.FirstOrDefault().CashBookId;
                    //Post for Bank book
                    var accCashBook = AppDb.AccountBooks.Where(x => x.AccountBookId == settingCashBookId).FirstOrDefault();
                    if (accCashBook != null)
                    {
                        var receipt = new sdtoReceiptHeader()
                        {
                            BookId = accCashBook.AccountBookId,
                            TransDate = LoanInfo.TransactionDate.Value,
                            VoucherTotal = LoanInfo.LoanAmount, //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.LoanEntry, //Doubt: //0 for Cash Receipt, 1 for Cash Payment, 2 for "Loan Entry", 3 for "Loan repayment"
                            TransId = LoanInfo.LoanId, //Doubt: Is transaction id loan id?
                            Cancelled = 0
                        };
                        AppDb.ReceiptHeader.Add(receipt);
                        AppDb.SaveChanges();

                        receipt.VoucherNo = accCashBook.ReceiptVoucherPrefix + receipt.Id + accCashBook.ReceiptVoucherSuffix;
                        AppDb.Entry(receipt).State = EntityState.Modified;
                        AppDb.SaveChanges();

                        // Member
                        var receiptDetailsDb = new sdtoReceiptDetails()
                        {
                            ReceiptsId = receipt.Id,
                            AccountId = accHeadMember.AccountHeadId,
                            Narration = "Loan issued",
                            Amount = LoanInfo.LoanAmount,
                            Display = 1
                        };

                        UpdateClosingBalance(accHeadMember.AccountHeadId, CurrentUser.UserSession.FinancialYearId.Value, LoanInfo.LoanAmount);
                        UpdateDayBookBalance(accHeadMember.AccountHeadId, CurrentUser.UserSession.FinancialYearId.Value, LoanInfo.TransactionDate.Value, LoanInfo.LoanAmount, TransactionType.LoanEntry);

                        // Cash Account
                        var receiptDetailsCr = new sdtoReceiptDetails()
                        {
                            ReceiptsId = receipt.Id,
                            AccountId = accCashBook.AccountHeadId.Value,
                            Narration = "Loan issued",
                            Amount = -1 * LoanInfo.LoanAmount,
                            Display = 1
                        };

                        UpdateClosingBalance(accCashBook.AccountHeadId.Value, CurrentUser.UserSession.FinancialYearId.Value, LoanInfo.LoanAmount);
                        UpdateDayBookBalance(accCashBook.AccountHeadId.Value, CurrentUser.UserSession.FinancialYearId.Value, LoanInfo.TransactionDate.Value, LoanInfo.LoanAmount, TransactionType.LoanEntry);

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

            return tranFlag;
        }
        public bool AddCashReceipt(sdtoViewAccCashReceiptPayment objCashReceiptPayment)
        {
            var accBankBook = AppDb.AccountBooks.Where(x => x.AccountBookId == objCashReceiptPayment.Book.AccountBookId).FirstOrDefault();
            if (accBankBook != null)
            {
                sdtoReceiptHeader hdrBankDeposit = new sdtoReceiptHeader()
                {
                    BookId = accBankBook.AccountBookId,
                    Cancelled = 0,
                    CreatedOn = DateTime.Now,
                    CreatedBy = CurrentUser.UserID,
                    TransDate = objCashReceiptPayment.Date,
                    FinancialYearId = CurrentUser.UserSession.FinancialYearId.Value,
                    FromModule = 0,
                    IsDeleted = false,
                    //VoucherNo = objCashReceiptPayment.Voucher,
                    VoucherTotal = objCashReceiptPayment.Details.Sum(x => x.Amount),
                    Transaction = TransactionType.CashReceipt,
                    TransType = ReceiptType.Receipt
                };

                AppDb.ReceiptHeader.Add(hdrBankDeposit);
                AppDb.SaveChanges();

                hdrBankDeposit.VoucherNo = accBankBook.ReceiptVoucherPrefix + hdrBankDeposit.Id + accBankBook.ReceiptVoucherSuffix;
                AppDb.Entry(hdrBankDeposit).State = EntityState.Modified;
                AppDb.SaveChanges();

                foreach (sdtoViewAccCashReceiptPaymentDetails dtl in objCashReceiptPayment.Details)
                {
                    sdtoReceiptDetails bankDepositDtlCr = new sdtoReceiptDetails()
                    {
                        ReceiptsId = hdrBankDeposit.Id,
                        AccountId = dtl.AccountHeadId,
                        Narration = dtl.Narration,
                        Amount = -1 * dtl.Amount,
                        CreatedBy = CurrentUser.UserID,
                        CreatedOn = DateTime.Now,
                        Display = 1,
                        IsDeleted = false,
                    };
                    UpdateClosingBalance(bankDepositDtlCr.AccountId, CurrentUser.UserSession.FinancialYearId.Value, bankDepositDtlCr.Amount);
                    UpdateDayBookBalance(bankDepositDtlCr.AccountId, CurrentUser.UserSession.FinancialYearId.Value, objCashReceiptPayment.Date, bankDepositDtlCr.Amount, TransactionType.CashReceipt);
                    AppDb.ReceiptDetails.Add(bankDepositDtlCr);
                }

                sdtoReceiptDetails bankDepositDtlDb = new sdtoReceiptDetails()
                {
                    ReceiptsId = hdrBankDeposit.Id,
                    AccountId = accBankBook.AccountHeadId.Value,
                    Narration = "Cash Receipt - " + hdrBankDeposit.VoucherNo,
                    Amount = objCashReceiptPayment.Details.Sum(x => x.Amount),
                    CreatedBy = CurrentUser.UserID,
                    CreatedOn = DateTime.Now,
                    Display = 0,
                    IsDeleted = false
                };

                UpdateClosingBalance(bankDepositDtlDb.AccountId, CurrentUser.UserSession.FinancialYearId.Value, bankDepositDtlDb.Amount);
                UpdateDayBookBalance(bankDepositDtlDb.AccountId, CurrentUser.UserSession.FinancialYearId.Value, objCashReceiptPayment.Date, bankDepositDtlDb.Amount, TransactionType.CashReceipt);
                AppDb.ReceiptDetails.Add(bankDepositDtlDb);
                AppDb.SaveChanges();
            }
            return true;
        }
        public bool PostDepositWithdrawal(sdtoWithdrawalInfo WithdrawalInfo)
        {
            bool tranFlag = true;

            var DepositInfo = AppDb.sdtoDepositInfoes.Where(x => x.DepositId == WithdrawalInfo.DepositId).FirstOrDefault();
            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();

                    var settingsInterestAccId = AppDb.GeneralSettings.FirstOrDefault().InterestAccountId;
                    var accInterest = AppDb.AccountHeads.Where(x => x.AccountHeadId == settingsInterestAccId).FirstOrDefault();
                    if (accCashBook != null && accBankBook != null)
                    {
                        var receipt = new sdtoReceiptHeader()
                        {
                            BookId = WithdrawalInfo.InstrumentMode == Instrument.Cash ? accCashBook.AccountBookId : accBankBook.AccountBookId,
                            TransDate = DateTime.Now,
                            VoucherTotal = WithdrawalInfo.WithdrawalAmount, //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.DepositWithdrawal, //Doubt: //0 for Cash Receipt, 1 for Cash Payment, 2 for "Loan Entry", 3 for "Loan repayment"
                            TransId = WithdrawalInfo.WithdrawalId, //Doubt: Is transaction id loan id?
                            Cancelled = 0
                        };
                        AppDb.ReceiptHeader.Add(receipt);
                        AppDb.SaveChanges();

                        if (WithdrawalInfo.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 receiptDetailsDb = new sdtoReceiptDetails()
                        {
                            ReceiptsId = receipt.Id,
                            AccountId = accHeadMember.AccountHeadId,
                            Narration = "Deposit Amount Withdrawn",
                            Amount = WithdrawalInfo.WithdrawalAmount,
                            Display = 1
                        };

                        // Cash Account
                        var receiptDetailsCr = new sdtoReceiptDetails()
                        {
                            ReceiptsId = receipt.Id,
                            AccountId = WithdrawalInfo.InstrumentMode == Instrument.Cash ? accCashBook.AccountHeadId.Value : accBankBook.AccountHeadId.Value,
                            Narration = "Deposit Amount Withdrawn",
                            Amount = -1 * (WithdrawalInfo.WithdrawalAmount - WithdrawalInfo.InterestAmount),
                            Display = 1
                        };

                        var receiptDetailsCrInt = new sdtoReceiptDetails()
                        {
                            ReceiptsId = receipt.Id,
                            AccountId = accInterest.AccountHeadId,
                            Narration = "Deposit Amount Withdrawn",
                            Amount = -1 * WithdrawalInfo.InterestAmount,
                            Display = 1
                        };

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

            return tranFlag;
        }
        public bool PostLoanRepayment(sdtoLoanRepayment LoanRepaymentInfo)
        {
            bool tranFlag = true;
            var LoanInfo = AppDb.sdtoLoanInfoes.Find(LoanRepaymentInfo.LoanId);

            var member = AppDb.User.Where(x => x.UserID == LoanInfo.UserId).FirstOrDefault();
            if (member != null)
            {
                var accHeadMember = AppDb.AccountHeads.Find(member.AccountHeadId);
                if (accHeadMember != null)
                {
                    var settingCashBookId = AppDb.GeneralSettings.FirstOrDefault().CashBookId;
                    var settingsInterestAccountId = AppDb.GeneralSettings.FirstOrDefault().InterestAccountId;
                    //Post for Bank book
                    var accCashBook = AppDb.AccountBooks.Where(x => x.AccountBookId == settingCashBookId).FirstOrDefault();
                    var accInterest = AppDb.AccountHeads.Where(x => x.AccountHeadId == settingsInterestAccountId).FirstOrDefault();
                    if (accCashBook != null)
                    {
                        var receipt = new sdtoReceiptHeader()
                        {
                            BookId = accCashBook.AccountBookId,
                            TransDate = DateTime.Now,
                            VoucherTotal = LoanInfo.LoanAmount, //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.LoanRepayment, //Doubt: //0 for Cash Receipt, 1 for Cash Payment, 2 for "Loan Entry", 3 for "Loan repayment"
                            TransId = LoanRepaymentInfo.LoanRepaymentId, //Doubt: Is transaction id loan id?
                            Cancelled = 0
                        };
                        AppDb.ReceiptHeader.Add(receipt);
                        AppDb.SaveChanges();

                        receipt.VoucherNo = accCashBook.ReceiptVoucherPrefix + receipt.Id + accCashBook.ReceiptVoucherSuffix;
                        AppDb.Entry(receipt).State = EntityState.Modified;
                        AppDb.SaveChanges();

                        // Member
                        var receiptDetailsCr = new sdtoReceiptDetails()
                        {
                            ReceiptsId = receipt.Id,
                            AccountId = accHeadMember.AccountHeadId,
                            Narration = "Loan Repayment received",
                            Amount = -1 * LoanRepaymentInfo.RepaymentAmount,
                            Display = 1
                        };

                        UpdateClosingBalance(accHeadMember.AccountHeadId, CurrentUser.UserSession.FinancialYearId.Value, -1 * LoanRepaymentInfo.RepaymentAmount);
                        UpdateDayBookBalance(accHeadMember.AccountHeadId, CurrentUser.UserSession.FinancialYearId.Value, LoanInfo.TransactionDate.Value, -1 * LoanRepaymentInfo.RepaymentAmount, TransactionType.LoanRepayment);

                        // Cash Account
                        var receiptDetailsDb = new sdtoReceiptDetails()
                        {
                            ReceiptsId = receipt.Id,
                            AccountId = accCashBook.AccountHeadId.Value,
                            Narration = "Loan Repayment received",
                            Amount = (LoanRepaymentInfo.RepaymentAmount - LoanRepaymentInfo.InterestAmount),
                            Display = 1
                        };

                        UpdateClosingBalance(accCashBook.AccountHeadId.Value, CurrentUser.UserSession.FinancialYearId.Value, (LoanRepaymentInfo.RepaymentAmount - LoanRepaymentInfo.InterestAmount));
                        UpdateDayBookBalance(accCashBook.AccountHeadId.Value, CurrentUser.UserSession.FinancialYearId.Value, LoanInfo.TransactionDate.Value, (LoanRepaymentInfo.RepaymentAmount - LoanRepaymentInfo.InterestAmount), TransactionType.LoanRepayment);

                        var receiptDetailsDbInt = new sdtoReceiptDetails()
                        {
                            ReceiptsId = receipt.Id,
                            AccountId = accInterest.AccountHeadId,
                            Narration = "Loan Repayment received",
                            Amount = LoanRepaymentInfo.InterestAmount,
                            Display = 1
                        };

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

            return tranFlag;
        }