/// <summary> /// Executes an upsert/delete operation on a simulation's investment library data /// Throws a RowNotInTableException if no simulation is found /// </summary> /// <param name="model">InvestmentLibraryModel</param> /// <param name="db">BridgeCareContext</param> /// <returns>InvestmentLibraryModel</returns> public InvestmentLibraryModel SaveSimulationInvestmentLibrary(InvestmentLibraryModel model, BridgeCareContext db) { var id = int.Parse(model.Id); if (!db.Simulations.Any(s => s.SIMULATIONID == id)) { throw new RowNotInTableException($"No scenario found with id {id}"); } var simulation = db.Simulations .Include(s => s.INVESTMENTS) .Include(s => s.YEARLYINVESTMENTS) .Single(s => s.SIMULATIONID == id); if (simulation.INVESTMENTS != null) { model.UpdateInvestment(simulation.INVESTMENTS); } if (simulation.YEARLYINVESTMENTS.Any()) { simulation.YEARLYINVESTMENTS.ToList().ForEach(yearlyInvestment => { var yearlyInvestmentModel = model.BudgetYears.SingleOrDefault(m => m.Id == yearlyInvestment.YEARID.ToString()); if (yearlyInvestmentModel == null) { YearlyInvestmentEntity.DeleteEntry(yearlyInvestment, db); } else { yearlyInvestmentModel.matched = true; yearlyInvestmentModel.UpdateYearlyInvestment(yearlyInvestment); } }); } if (simulation.INVESTMENTS == null) { db.Investments.Add(new InvestmentsEntity(model)); } if (model.BudgetYears.Any(m => !m.matched)) { db.YearlyInvestments.AddRange(model.BudgetYears .Where(yearlyInvestmentModel => !yearlyInvestmentModel.matched) .Select(yearlyInvestmentModel => new YearlyInvestmentEntity(id, yearlyInvestmentModel)) .ToList() ); } db.SaveChanges(); return(new InvestmentLibraryModel(simulation)); }
/// <summary> /// Executes an upsert/delete operation on a simulation's investment library data regardless of ownership /// Throws a RowNotInTableException if no simulation is found for that user /// </summary> /// <param name="model">InvestmentLibraryModel</param> /// <param name="db">BridgeCareContext</param> /// <param name="username">Username</param> /// <returns>InvestmentLibraryModel</returns> public InvestmentLibraryModel SaveAnySimulationInvestmentLibrary(InvestmentLibraryModel model, BridgeCareContext db) { var id = int.Parse(model.Id); if (!db.Simulations.Any(s => s.SIMULATIONID == id)) { throw new RowNotInTableException($"No scenario found with id {id}"); } return(SaveSimulationInvestmentLibrary(model, db)); }
/// <summary> /// Executes an upsert/delete operation on a simulation's investment library data if it is owned by the provided user /// Throws a RowNotInTableException if no simulation is found for that user /// </summary> /// <param name="model">InvestmentLibraryModel</param> /// <param name="db">BridgeCareContext</param> /// <param name="username">Username</param> /// <returns>InvestmentLibraryModel</returns> public InvestmentLibraryModel SavePermittedSimulationInvestmentLibrary(InvestmentLibraryModel model, BridgeCareContext db, string username) { var id = int.Parse(model.Id); if (!db.Simulations.Any(s => s.SIMULATIONID == id)) { throw new RowNotInTableException($"No scenario found with id {id}"); } if (!db.Simulations.Include(s => s.USERS).First(s => s.SIMULATIONID == id).UserCanModify(username)) { throw new UnauthorizedAccessException("You are not authorized to modify this scenario's investments."); } return(SaveSimulationInvestmentLibrary(model, db)); }
public InvestmentsEntity(InvestmentLibraryModel model) { INFLATIONRATE = model.InflationRate; BUDGETORDER = model.BudgetOrder != null ? string.Join(",", model.BudgetOrder) : "Rehabilitation,Maintenance,Construction"; var years = model.BudgetYears.Any() ? model.BudgetYears.OrderBy(by => by.Year) .Select(by => by.Year).Distinct().ToList() : new List <int>(); FIRSTYEAR = years.Any() ? years[0] : DateTime.Now.Year; NUMBERYEARS = years.Any() ? years.Count() : 1; }
private void FillInvestmentAndBudgetCriteria(ExcelWorksheet worksheet, InvestmentLibraryModel inflationAndInvestments, List <Models.CriteriaDrivenBudgets.CriteriaDrivenBudgetModel> criteriaDrivenBudgets) { var currencyFormat = "_-$* #,##0.00_-;-$* #,##0.00_-;_-$* \"-\"??_-;_-@_-"; worksheet.Cells[38, 1].Value = "Years"; worksheet.Cells[38, 2].Value = "Total Funding"; excelHelper.ApplyColor(worksheet.Cells[38, 2], Color.DimGray); excelHelper.ApplyColor(worksheet.Cells[38, 1], Color.DimGray); var startingRowInvestment = 40; var startingBudgetHeaderColumn = 2; var nextBudget = 0; var investmentGrid = new Dictionary <int, List <(string BudgetName, double?BudgetAmount)> >(); foreach (var item in inflationAndInvestments.BudgetYears) { if (!investmentGrid.ContainsKey(item.Year)) { investmentGrid.Add(item.Year, new List <(string BudgetName, double?BudgetAmount)> { (item.BudgetName, item.BudgetAmount) }); } else { investmentGrid[item.Year].Add((item.BudgetName, item.BudgetAmount)); } } var firstRow = true; foreach (var item in investmentGrid) { worksheet.Cells[startingRowInvestment, 1].Value = item.Key; foreach (var budget in item.Value) { if (firstRow == true) { worksheet.Cells[39, startingBudgetHeaderColumn + nextBudget].Value = budget.BudgetName; worksheet.Cells[startingRowInvestment, startingBudgetHeaderColumn + nextBudget].Value = budget.BudgetAmount.Value; worksheet.Cells[startingRowInvestment, startingBudgetHeaderColumn + nextBudget].Style.Numberformat.Format = currencyFormat; nextBudget++; continue; } for (var column = startingBudgetHeaderColumn; column <= item.Value.Count + 1; column++) { if (worksheet.Cells[39, column].Value.ToString() == budget.BudgetName) { worksheet.Cells[startingRowInvestment, column].Style.Numberformat.Format = currencyFormat; worksheet.Cells[startingRowInvestment, column].Value = budget.BudgetAmount.Value; break; } } } startingRowInvestment++; firstRow = false; nextBudget = 0; } excelHelper.MergeCells(worksheet, 38, 1, 39, 1); excelHelper.MergeCells(worksheet, 38, 2, 38, inflationAndInvestments.BudgetOrder.Count + 1); excelHelper.ApplyBorder(worksheet.Cells[38, 1, startingRowInvestment - 1, inflationAndInvestments.BudgetOrder.Count + 1]); FillBudgetCriteria(worksheet, startingRowInvestment, criteriaDrivenBudgets); }
public IHttpActionResult SaveSimulationInvestmentLibrary([FromBody] InvestmentLibraryModel model) { UserInformationModel userInformation = ESECSecurity.GetUserInformation(Request); return(Ok(InvestmentLibrarySaveMethods[userInformation.Role](model, userInformation))); }
public IHttpActionResult SaveSimulationInvestmentLibrary([FromBody] InvestmentLibraryModel model) => Ok(repo.SaveSimulationInvestmentLibrary(model, db));
/// <summary> /// Executes an upsert/delete operation on a simulation's investment library data /// Throws a RowNotInTableException if no simulation is found /// </summary> /// <param name="model">InvestmentLibraryModel</param> /// <param name="db">BridgeCareContext</param> /// <returns>InvestmentLibraryModel</returns> private InvestmentLibraryModel SaveSimulationInvestmentLibrary(InvestmentLibraryModel model, BridgeCareContext db) { var id = int.Parse(model.Id); var simulation = db.Simulations .Include(s => s.INVESTMENTS) .Include(s => s.YEARLYINVESTMENTS) .Include(s => s.CriteriaDrivenBudgets) .Include(s => s.PRIORITIES).Include(s => s.PRIORITIES.Select(p => p.PRIORITYFUNDS)) .Single(s => s.SIMULATIONID == id); if (simulation.CriteriaDrivenBudgets.Any()) { simulation.CriteriaDrivenBudgets.ToList().ForEach(criteriaDrivenBudget => { var criteriaDrivenBudgetModel = model.CriteriaDrivenBudgets.SingleOrDefault(m => m.Id == criteriaDrivenBudget.BUDGET_CRITERIA_ID.ToString()); if (criteriaDrivenBudgetModel == null) { CriteriaDrivenBudgetEntity.DeleteEntry(criteriaDrivenBudget, db); } else { criteriaDrivenBudgetModel.matched = true; criteriaDrivenBudgetModel.UpdateCriteriaDrivenBudget(criteriaDrivenBudget); } }); } if (simulation.INVESTMENTS != null) { model.UpdateInvestment(simulation.INVESTMENTS); } if (simulation.YEARLYINVESTMENTS.Any()) { simulation.YEARLYINVESTMENTS.ToList().ForEach(yearlyInvestment => { var yearlyInvestmentModel = model.BudgetYears.SingleOrDefault(m => m.Id == yearlyInvestment.YEARID.ToString()); if (yearlyInvestmentModel == null) { YearlyInvestmentEntity.DeleteEntry(yearlyInvestment, db); } else { yearlyInvestmentModel.matched = true; yearlyInvestmentModel.UpdateYearlyInvestment(yearlyInvestment); } }); } simulation.PRIORITIES.ToList().ForEach(priorityEntity => { var budgetsForNewFunds = new List <string>(); if (priorityEntity.PRIORITYFUNDS.Any()) { model.BudgetOrder.ForEach(budget => { budgetsForNewFunds.Add(budget); }); } if (budgetsForNewFunds.Any()) { priorityEntity.PRIORITYFUNDS .ToList() .ForEach(priorityFundEntity => { PriorityFundEntity.DeleteEntry(priorityFundEntity, db); }); db.PriorityFunds.AddRange(budgetsForNewFunds .Select(budget => new PriorityFundEntity(priorityEntity.PRIORITYID, budget)) ); } }); if (simulation.INVESTMENTS == null) { db.Investments.Add(new InvestmentsEntity(model)); } if (model.BudgetYears.Any(m => !m.matched)) { db.YearlyInvestments.AddRange(model.BudgetYears .Where(yearlyInvestmentModel => !yearlyInvestmentModel.matched) .Select(yearlyInvestmentModel => new YearlyInvestmentEntity(id, yearlyInvestmentModel)) .ToList() ); } if (model.CriteriaDrivenBudgets.Any(m => !m.matched)) { db.CriteriaDrivenBudgets.AddRange(model.CriteriaDrivenBudgets .Where(criteriaDrivenBudgetModel => !criteriaDrivenBudgetModel.matched) .Select(criteriaDrivenBudgetModel => new CriteriaDrivenBudgetEntity(id, criteriaDrivenBudgetModel)) .ToList() ); } db.SaveChanges(); return(new InvestmentLibraryModel(simulation)); }