/// <summary>
        /// Insert Seller with Seller Name, Type and BusinessCategory only for specific loan
        /// </summary>
        /// <param name="loanId"></param>
        /// <param name="sellerData"></param>
        /// <returns></returns>
        public BusinessContact InsertSimpleSeller(Guid loanId, FormSeller sellerData)
        {
            BusinessContact contact = new BusinessContact();

            contact.BusinessContactId       = Guid.Empty;
            contact.SellerType              = sellerData.SellerType;
            contact.BusinessContactCategory = BusinessContactCategory.SellerAgent;

            switch (contact.SellerType)
            {
            case SellerType.Individual:
                contact.FirstName = sellerData.SellerName;
                break;

            case SellerType.Bank:
            case SellerType.LLC:
                contact.CompanyName = sellerData.SellerName;
                break;

            default:
                contact.FirstName = sellerData.SellerName;
                break;
            }
            return(BusinessContactServiceFacade.CreateBusinessContactByLoan(loanId, contact));
        }
        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);
        }
        private LoanDetailsViewModel GetLoanDetails(Guid loanId, int prospectId, UserAccount user, bool collapseSection)
        {
            var tempDetails = LoanServiceFacade.RetrieveWorkQueueItemDetails(loanId, prospectId, -1);

            LoanDetailsViewModel loanDetails = new LoanDetailsViewModel();

            string emptyField = "-";

            if (tempDetails != null)
            {
                CommonHelper.RetreiveContactDetailsFromWorkQueueItemDetails(tempDetails, loanDetails, user, emptyField);
                CommonHelper.RetreiveLoanDetailsFromWorkQueueItemDetails(tempDetails, loanDetails, user, emptyField);
            }

            loanDetails.TitleInformation = ConciergeWorkQueueServiceFacade.ExecuteSPGetBorrowerData("GetBorrowerData", loanId, user.UserAccountId);

            // var leadSourceInformation = ContactServiceFacade.RetrieveLeadSourceByContactIdAndLoanId( prospectId, loanId, user.UserAccountId );
            //if ( leadSourceInformation != null )
            //    loanDetails.LeadSourceInformation = leadSourceInformation.LeadSourceId + " " + leadSourceInformation.Description;

            LeadSource hearAboutUs = LoanServiceFacade.RetrieveHearAboutUs(loanId);

            if (hearAboutUs != null)
            {
                if (hearAboutUs.AffinityGroup == Contracts.Affiliate.AffinityGroup.PartnersProfiles)
                {
                    if (hearAboutUs.HBMId != null && hearAboutUs.HBMId != Guid.Empty)
                    {
                        loanDetails.HearAboutUs = hearAboutUs.LeadSourceId + " Realtor-HBM";
                    }
                    else
                    {
                        loanDetails.HearAboutUs = hearAboutUs.LeadSourceId + " Realtor";
                    }
                }
                else
                {
                    loanDetails.HearAboutUs = hearAboutUs.LeadSourceId + " " + hearAboutUs.Description;
                }
            }

            //List<BusinessContact> contacts = BusinessContactServiceFacade.RetrieveBusinessContacts( loanId );
            List <BusinessContact> contacts = BusinessContactServiceFacade.RetrieveBusinessContactsAppraisal(loanId);

            loanDetails.Contacts        = GetDisplayInformation(contacts);
            loanDetails.LoanId          = loanId;
            loanDetails.CollapseDetails = collapseSection;

            _httpContext.Session[SessionHelper.CurrentLoanIdForBusinessContact] = loanId;

            if (loanDetails.Contacts.FirstOrDefault(x => x.BusinessContactCategory == BusinessContactCategory.BuyerAgent) != null)
            {
                _httpContext.Session[SessionHelper.CurrentBusinessContactBuyerAgent] = loanDetails.Contacts.FirstOrDefault(x => x.BusinessContactCategory == BusinessContactCategory.BuyerAgent).CompanyName;
            }
            else
            {
                _httpContext.Session[SessionHelper.CurrentBusinessContactBuyerAgent] = String.Empty;
            }

            if (loanDetails.Contacts.FirstOrDefault(x => x.BusinessContactCategory == BusinessContactCategory.SellerAgent) != null)
            {
                _httpContext.Session[SessionHelper.CurrentBusinessContactSellerAgent] = loanDetails.Contacts.FirstOrDefault(x => x.BusinessContactCategory == BusinessContactCategory.SellerAgent).CompanyName;
            }
            else
            {
                _httpContext.Session[SessionHelper.CurrentBusinessContactSellerAgent] = String.Empty;
            }
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            parameters.Add("contactType", "-1");
            parameters.Add("activeInactive", "null");
            parameters.Add("searchString", "Search");
            parameters.Add("currentPage", "1");
            parameters.Add("pageSize", "10");
            parameters.Add("requestMultiplePages", "null");
            parameters.Add("getNextPages", "null");
            parameters.Add("hasChildren", "null");
            parameters.Add("loanId", loanId.ToString());

            loanDetails.LoanCompaniesAndContactsModel = ContactHelper.GetLoanCompaniesAndContacts(HttpContext, parameters);

            return(loanDetails);
        }