public static void PopulateComboBoxes(ManageFeesViewModel model)
        {
            var file = DocumentsServiceFacade.GetLatestFileByLoanIdAndDocumentClass(model.TitleAndEscrow.LoanId, DocumentClass.SmartGFEComplianceCertificate, model.TitleAndEscrow.UserAccountId);

            if (file != null && file.FileStoreItemId != Guid.Empty)
            {
                model.TitleAndEscrow.SmartGfeDocumentLink = HttpUtility.UrlEncode(EncryptionHelper.EncryptRijndael(file.FileStoreItemId.ToString(), EncriptionKeys.Default));
                if (!model.TitleAndEscrow.SmartGFEEnabled.HasValue)
                {
                    model.TitleAndEscrow.SmartGFEEnabled = model.TitleAndEscrow.SmartGFEEnabledOriginalValue = true;
                }
            }
            model.TitleAndEscrow.ListPaidBy = new List <string>();
            var ddlValues = LookupServiceFacade.Lookup(LookupKeywords.CostPaidBy, model.TitleAndEscrow.UserAccountId);

            foreach (var c in ddlValues)
            {
                model.TitleAndEscrow.ListPaidBy.Add(c.StringValue);
            }

            model.TitleAndEscrow.ListPaidTo = new List <string>();
            ddlValues = LookupServiceFacade.Lookup(LookupKeywords.CostPaidTo, model.TitleAndEscrow.UserAccountId);
            foreach (var c in ddlValues)
            {
                model.TitleAndEscrow.ListPaidTo.Add(c.StringValue);
            }
            model.TitleAndEscrow.ListWhen = new List <string>();
            ddlValues = LookupServiceFacade.Lookup(LookupKeywords.CostPaidWhen, model.TitleAndEscrow.UserAccountId);
            foreach (var c in ddlValues)
            {
                model.TitleAndEscrow.ListWhen.Add(c.StringValue);
            }
        }
        public static TitleAndEscrow GetCostByLoanId(Guid loanId, UserAccount user, HttpContextBase httpContext)
        {
            if (user == null)
            {
                return(new TitleAndEscrow());
            }

            int userAccountId = user.UserAccountId;

            List <Cost> costs = CostServiceFacade.GetManageFeesCosts(loanId, MortgageType.Conventional, LoanTransactionType.Refinance, Guid.Empty, Guid.Empty, -1);

            TitleAndEscrow titleAndEscrow = LoanServiceFacade.RetrieveTitleAndEscrowDetails(loanId, userAccountId);

            if (titleAndEscrow == null || titleAndEscrow.CostGroups == null || titleAndEscrow.CostGroups.Count == 0)
            {
                return(null);
            }

            if (costs != null && !titleAndEscrow.IncludeTaxesAndInsurances)
            {
                costs = costs.Where(c => c.HUDLineNumber != 1002 && c.HUDLineNumber != 1004).ToList();
            }

            var file = DocumentsServiceFacade.GetLatestFileByLoanIdAndDocumentClass(loanId, DocumentClass.SmartGFEComplianceCertificate, userAccountId);

            if (file != null && file.FileStoreItemId != Guid.Empty)
            {
                titleAndEscrow.SmartGfeDocumentLink = HttpUtility.UrlEncode(EncryptionHelper.EncryptRijndael(file.FileStoreItemId.ToString(), EncriptionKeys.Default));
                if (!titleAndEscrow.SmartGFEEnabled.HasValue)
                {
                    titleAndEscrow.SmartGFEEnabled = titleAndEscrow.SmartGFEEnabledOriginalValue = true;
                }
            }

            if ((user.Privileges != null && user.Privileges.Any(p => p.Category.Equals(( int )ActionCategory.LockFee))) ||
                (user.Roles != null && user.Roles.Any(r => r.Privileges.Any(p => p.Category.Equals(( int )ActionCategory.LockFee)))))
            {
                titleAndEscrow.IsAuthorizedToLockFee = true;
            }

            foreach (var costGroup in titleAndEscrow.CostGroups)
            {
                if (costGroup.Costs == null)
                {
                    continue;
                }

                foreach (var cost in costGroup.Costs)
                {
                    if (costs != null)
                    {
                        var loanCost = costs.FirstOrDefault(c => c.HUDLineNumber == cost.HUDLineNumber && (String.IsNullOrWhiteSpace(cost.SubHUDLineNumber) || (c.SubHUDLineNumber == cost.SubHUDLineNumber)));
                        if (loanCost != null)
                        {
                            cost.CostId            = loanCost.CostId;
                            cost.Amount            = loanCost.Amount;
                            cost.FromBorrowerFunds = loanCost.Amount;
                            if (loanCost.Amount != 0)
                            {
                                cost.IsAprCost = loanCost.IsAprCost;
                            }
                            cost.CostId              = loanCost.CostId;
                            cost.PaidBy              = loanCost.PaidBy;
                            cost.PaidTo              = loanCost.PaidTo;
                            cost.PocAmount           = loanCost.PocAmount;
                            cost.PtcAmount           = loanCost.PtcAmount;
                            cost.When                = loanCost.PocAmount > 0 ? "O" : "T";
                            cost.Name                = costGroup.CostGroupNumber != 12 ? loanCost.Name : RetrieveDisplayNameForGroup20(loanCost.Name);
                            cost.SmartGfeRequestType = loanCost.SmartGfeRequestType;
                            cost.Provider            = loanCost.Provider;
                            cost.IsLocked            = loanCost.IsLocked;
                            cost.Percent             = loanCost.Percent;
                            cost.Payee               = loanCost.Payee;

                            if (cost.HUDLineNumber == 901)
                            {
                                DateTime?closingDate = titleAndEscrow.ClosingDate;
                                if (closingDate.HasValue)
                                {
                                    int interestDays = Calculator.Calculator.CalculateInterestDays(closingDate.Value);

                                    cost.ClosingDate   = closingDate.Value.ToString("MM/dd/yyyy");
                                    cost.ClosingDateTo = closingDate.Value.AddDays(interestDays - 1).ToString("MM/dd/yyyy");
                                    cost.InterestDays  = interestDays;
                                    cost.InterestRate  = loanCost.Amount / interestDays;
                                }
                            }

                            // Set default MonthsToBePaid to 1, in case that stored DB value is -1
                            if (loanCost.MonthsToBePaid == -1 && loanCost.Amount > 0)
                            {
                                loanCost.MonthsToBePaid = 1;
                            }

                            if (loanCost.MonthsToBePaid > 0)
                            {
                                cost.MonthsToBePaid = loanCost.MonthsToBePaid;
                                cost.YearsToBePaid  = ( int )Math.Ceiling(( double )cost.MonthsToBePaid / 12);
                            }
                        }

                        cost.OldAmount = cost.Amount;
                    }
                }
            }


            if (httpContext.Session[SessionHelper.CurrentLoanIdForBusinessContact] is Guid && ( Guid )httpContext.Session[SessionHelper.CurrentLoanIdForBusinessContact] == loanId)
            {
                if (httpContext.Session[SessionHelper.CurrentBusinessContactBuyerAgent] != null)
                {
                    titleAndEscrow.BuyerAgent = httpContext.Session[SessionHelper.CurrentBusinessContactBuyerAgent].ToString();
                }

                if (httpContext.Session[SessionHelper.CurrentBusinessContactSellerAgent] != null)
                {
                    titleAndEscrow.SellerAgent = httpContext.Session[SessionHelper.CurrentBusinessContactSellerAgent].ToString();
                    // If the Agent information is the same for both only display a single instance on row 701
                    if (titleAndEscrow.SellerAgent.ToLower() == titleAndEscrow.BuyerAgent.ToLower())
                    {
                        titleAndEscrow.SellerAgent = String.Empty;
                    }
                }
            }
            else
            {
                var realtor = BusinessContactServiceFacade.RetrieveBusinessContactByTypeAndLoan(BusinessContactCategory.BuyerAgent, loanId);
                if (realtor != null && !String.IsNullOrEmpty(realtor.CompanyName))
                {
                    titleAndEscrow.BuyerAgent = realtor.CompanyName;
                }

                realtor = BusinessContactServiceFacade.RetrieveBusinessContactByTypeAndLoan(BusinessContactCategory.SellerAgent, loanId);
                if (realtor != null && !String.IsNullOrEmpty(realtor.CompanyName))
                {
                    titleAndEscrow.SellerAgent = realtor.CompanyName;
                }
            }

            return(titleAndEscrow);
        }