public ActionResult ShowCostHistoryPopup(String loanId)
        {
            Guid loanIdParsed = Guid.Parse(loanId);
            List <CostHistory> costHistory = new List <CostHistory>();

            costHistory = CostServiceFacade.RetrieveCostHistory(loanIdParsed, AccountHelper.GetUserAccountId());


            return(PartialView("Commands/ManageFees/_costHistoryPopUp", costHistory));
        }
        public JsonResult UpdateCostClosingDate(String loanId, String closingDate)
        {
            try
            {
                if (string.IsNullOrEmpty(loanId) || string.IsNullOrEmpty(closingDate))
                {
                    throw new ArgumentNullException("Loan Id and Closing Date are required");
                }

                int      userAccountId      = IdentityManager.GetUserAccountId();
                Guid     loanIdGuid         = new Guid(loanId);
                DateTime enteredClosingDate = DateTime.Parse(closingDate);

                UserAccount loggedUser         = ( UserAccount )System.Web.HttpContext.Current.Session[SessionHelper.UserData];
                var         closingDateHistory = new ClosingDateHistory
                {
                    ClosingDate  = enteredClosingDate,
                    DateOfChange = DateTime.Now,
                    LoanId       = loanIdGuid,
                    UserName     = loggedUser.Party.FirstName + " " + loggedUser.Party.LastName
                };

                LoanServiceFacade.UpdateManuallyEnteredCloseDate(loanIdGuid, true, userAccountId);
                LoanServiceFacade.UpdateLoanClosingDate(loanIdGuid, enteredClosingDate, userAccountId);
                CostServiceFacade.UpdateCostsAndClosingDate(loanIdGuid, enteredClosingDate, userAccountId);
                LoanServiceFacade.CreateClosingDateHistory(closingDateHistory, userAccountId);


                var model = ManageFeesHelper.GetCostByLoanId(loanIdGuid, loggedUser, HttpContext);

                var group901  = model.CostGroups.FirstOrDefault(x => x.CostGroupNumber == 90).Costs.FirstOrDefault(x => x.HUDLineNumber == 901);
                var group1002 = model.CostGroups.FirstOrDefault(x => x.CostGroupNumber == 10).Costs.FirstOrDefault(x => x.HUDLineNumber == 1002);
                var group1004 = model.CostGroups.FirstOrDefault(x => x.CostGroupNumber == 10).Costs.FirstOrDefault(x => x.HUDLineNumber == 1004);

                int isClosingDateChanged = model.HasClosingDateHistory ? 1 : 0;
                return(Json(new { success = true, group901 = group901, group1002 = group1002, group1004 = group1004, isClosingDateChanged = isClosingDateChanged }));
            }
            catch (Exception exception)
            {
                TraceHelper.Error(TraceCategory.LoanCenter, "_closingcosts.cshtml::UpdateCostClosingDate", exception, Guid.Empty, IdentityManager.GetUserAccountId());
                return(Json(new { success = false }));
            }
        }
        public ActionResult RetrieveSmartGfeData(ManageFeesViewModel model)
        {
            ModelState.Clear();
            var loan = LoanServiceFacade.RetrieveLoan(model.TitleAndEscrow.LoanId, false, model.TitleAndEscrow.UserAccountId);

            var costGroupNumber11      = model.TitleAndEscrow.CostGroups.First(c => c.CostGroupNumber == 11);
            var costGroupNumber12      = model.TitleAndEscrow.CostGroups.First(c => c.CostGroupNumber == 12);
            var closingCostRequestId   = IntegrationLogServiceFacade.GetClosingCorpXmlForLoanId(model.TitleAndEscrow.LoanId, model.TitleAndEscrow.UserAccountId);
            var defaultCostCalculation = new DefaultCostCalculation()
            {
                LoanId = model.TitleAndEscrow.LoanId,
                ClosingCostRequestId  = closingCostRequestId,
                FeeProvider           = model.TitleAndEscrow.FeeProvider,
                MortgageType          = loan.MortgageType,
                PropertyValue         = loan.SubjectProperty.CurrentEstimatedValue.HasValue ? ( decimal )loan.SubjectProperty.CurrentEstimatedValue : 0,
                LoanAmount            = ( decimal )loan.BaseLoanAmount,
                StateName             = loan.SubjectProperty.Address == null ? null : loan.SubjectProperty.Address.StateName,
                PropertyType          = loan.SubjectProperty.PropertyType,
                InvestorName          = loan.InvestorName,
                CompanyProfile        = CompanyProfileServiceFacade.RetrieveCompanyProfile(),
                LoanTransactionTypeId = loan.LoanType
            };

            costGroupNumber11 = CostServiceFacade.RetrieveCostFromSmartGfe(defaultCostCalculation, model.TitleAndEscrow.UserAccountId, costGroupNumber11);
            costGroupNumber12 = CostServiceFacade.RetrieveCostFromSmartGfe(defaultCostCalculation, model.TitleAndEscrow.UserAccountId, costGroupNumber12);
            ManageFeesHelper.PreselectDefaultCosts(costGroupNumber11);
            ManageFeesHelper.PreselectDefaultCosts(costGroupNumber12);
            var group1100Index = model.TitleAndEscrow.CostGroups.FindIndex(g => g.CostGroupNumber == 11);

            model.TitleAndEscrow.CostGroups.RemoveAt(group1100Index);
            model.TitleAndEscrow.CostGroups.Insert(group1100Index, costGroupNumber11);
            var group1200Index = model.TitleAndEscrow.CostGroups.FindIndex(g => g.CostGroupNumber == 12);

            model.TitleAndEscrow.CostGroups.RemoveAt(group1200Index);
            model.TitleAndEscrow.CostGroups.Insert(group1200Index, costGroupNumber12);

            var itemize = GeneralSettingsServiceFacade.RetrieveStatusByGeneralSettingsName("Itemize Lender Credit", Guid.Empty, 0);

            LoanHelper.PerformPTCCalculationsForTitleEscrow(model.TitleAndEscrow.CostGroups, itemize);
            ManageFeesHelper.PopulateComboBoxes(model);
            ManageFeesHelper.SetTitleAndEscrowModel(model);
            return(PartialView("Commands/ManageFees/_manageFeeSmartGfeSection", model));
        }
        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);
        }
        public ActionResult SaveCosts(ManageFeesViewModel model)
        {
            try
            {
                var group1200Index = model.TitleAndEscrow.CostGroups.FindIndex(g => g.CostGroupNumber == 12);
                foreach (var cost in model.TitleAndEscrow.CostGroups[group1200Index].Costs)
                {
                    if (!String.IsNullOrEmpty(cost.Name))
                    {
                        cost.TotalRow = cost.Name.Contains("1203") ? "1203" : cost.Name.Contains("1201") ? "1201" : null;
                        cost.Name     = cost.Name.Contains("1203") ? cost.Name.Replace(" - 1203", String.Empty) :
                                        cost.Name.Contains("1201") ? cost.Name.Replace(" - 1201", String.Empty) : cost.Name;
                    }
                }
                if (model.TitleAndEscrow.SmartGFEEnabled.HasValue)
                {
                    var group1100Index = model.TitleAndEscrow.CostGroups.FindIndex(g => g.CostGroupNumber == 11);


                    if (!model.TitleAndEscrow.SmartGFEEnabled.Value && model.TitleAndEscrow.SmartGFEEnabledOriginalValue)
                    {
                        var costs1100Exist = false;
                        foreach (var cost in model.TitleAndEscrow.CostGroups[group1100Index].Costs)
                        {
                            if (cost.Amount > 0 || cost.FromBorrowerFunds > 0)
                            {
                                costs1100Exist = true;
                                break;
                            }
                        }

                        var costs1200Exist = false;
                        foreach (var cost in model.TitleAndEscrow.CostGroups[group1200Index].Costs)
                        {
                            if (cost.Amount > 0 || cost.FromBorrowerFunds > 0)
                            {
                                costs1200Exist = true;
                                break;
                            }
                        }

                        // If user disabled SmartGFE, but removed all fees -> re-enable to SmartGFE
                        if (!costs1100Exist && !costs1200Exist)
                        {
                            model.TitleAndEscrow.SmartGFEEnabled = true;
                        }
                    }

                    if (model.TitleAndEscrow.SmartGFEEnabled.Value)
                    {
                        if (model.TitleAndEscrow.SmartGFEEnabledOriginalValue)
                        {
                            //If SmartGFE is turned on, do not update fees in section 1100 and 1200 as they are locked
                            model.TitleAndEscrow.CostGroups.RemoveAt(group1100Index);

                            group1200Index = model.TitleAndEscrow.CostGroups.FindIndex(g => g.CostGroupNumber == 12);
                            model.TitleAndEscrow.CostGroups.RemoveAt(group1200Index);
                        }
                        else
                        {
                            // If SmartGFE was manually turned back on, remove locked flags for all fees in sections 1100 and 1200
                            foreach (var cost in model.TitleAndEscrow.CostGroups[group1100Index].Costs)
                            {
                                cost.IsLocked = false;
                            }

                            foreach (var cost in model.TitleAndEscrow.CostGroups[group1200Index].Costs)
                            {
                                cost.IsLocked = false;
                            }
                        }
                    }
                }

                CostServiceFacade.UpdateCostsByTitleAndEscrow(model.TitleAndEscrow, IdentityManager.GetUserAccountId());
            }
            catch (Exception ex)
            {
                TraceHelper.Error(TraceCategory.LoanCenterController,
                                  "An error occurred in MangeFeesController! SaveCosts(ManageFeesViewModel model)", ex);
            }

            return(RedirectToAction("Execute", "Command",
                                    new { command = "ManageFees,LoanId=" + model.TitleAndEscrow.LoanId }));
        }