public AllocationDto CreateAllocation(AllocationDto allocationDto) { var plan = _planDataStore.GetPlan(allocationDto.PlanId); if (plan == null) { throw new RecordNotFoundException($"Unable to find plan with id {allocationDto.PlanId}."); } if (allocationDto.Person == null && allocationDto.Project == null) { throw new BusinessLogicException("Person and Project can not both be null"); } allocationDto.Person ??= "Any Person"; allocationDto.Project ??= "Any Project"; var currentAllocationSum = GetCurrentTotalAllocation(allocationDto.PlanId); var maxAllowedAllocation = plan.Currency - currentAllocationSum; if (maxAllowedAllocation < allocationDto.Currency) { throw new BusinessLogicException($"{allocationDto.Currency} is more than the remaining allotment of {maxAllowedAllocation}."); } var newAllocation = _planDataStore.CreateAllocation(_mapper.Map <Allocation>(allocationDto)); return(_mapper.Map <AllocationDto>(newAllocation)); }
public async Task StartAllocation_WhenStartNewAllocationWithWrongLicensePlate_ThenReturnBadRequest() { var server = new TestServer(new WebHostBuilder() .UseStartup <TestStartup>()); using (server) { var client = server .CreateClient(); var context = server.Host.Services.GetService <ParkSharkContext>(); FillingInMemoryDatabase(context); AllocationDto allocationDto = new AllocationDto() { MemberPeronId = 1, ParkinglotId = 1, MemberLicensePlate = new LicensePlate("OtherPlate", "BE") }; var content = JsonConvert.SerializeObject(allocationDto); var stringContent = new StringContent(content, Encoding.UTF8, "application/json"); var response = await client.PostAsync("api/allocations", stringContent); Assert.False(response.IsSuccessStatusCode); } }
/// <summary> /// Calculate the actual for assets. Add into the dictionary /// </summary> /// <param name="assets">All assets over the years</param> /// <returns>The max year in the assets </returns> private void GetActualYears(Asset[] assets) { // calculate the actual allocations var years = assets.Select(x => x.AssetDate.Year).Distinct().ToArray(); var minYr = years.Min(); var maxYr = years.Max(); for (var year = minYr; year <= maxYr; year++) { var curAssets = assets.ForYear(year); var curTotal = curAssets.Sum(x => x.AssetAmount); var preDto = AllocationDict.ContainsKey(year - 1) ? AllocationDict[year - 1] : null; var preTotal = preDto?.AssetTotal ?? 0m; var dto = new AllocationDto(year, AllocationStatusTypes.Actual, _settings.RetirementIncome) { AgeYear = new RetirementAge(year - BirthYear, year).ToString(), AssetTotal = curTotal, Assets = curAssets, PreAssets = preDto?.Assets ?? new Asset[0], AssetTotalChanged = AssetsHelper.GetTotalWithChange(curTotal, preTotal) }; AllocationDict.Add(year, dto); } }
private void AllocateRetiredEarlyBeforeSocialAndPension(int r401KAge, int pensionAge, int maxAge) { for (var year = r401KAge; year < pensionAge; year++) { var preDto = AllocationDict[year - 1]; var preAssets = preDto.Assets; var assetDate = new DateTime(year, 12, 31); var transformer = new SimpleTransformer(_settings.InvestmentReturnRate); var assets = transformer.Transform(assetDate, preAssets); // 401K var r401Amount = SimpleR401KAllocator.Allocate(assets, maxAge, year); // Cash var target = GetTarget(preDto); var cashAmount = r401Amount >= target ? 0m : target - r401Amount; var cashWithdrawal = SimpleAllocator.Allocate(assets, assets.ForCash(), cashAmount); var preTotal = preDto.AssetTotal; var curTotal = assets.Sum(x => x.AssetAmount); var dto = new AllocationDto(year, AllocationStatusTypes.RetiredEstimated, target) { AgeYear = new RetirementAge(year - BirthYear, year).ToString(), CashAmount = cashWithdrawal, R401KAmount = r401Amount, Assets = assets.ToArray(), PreAssets = preDto.Assets, AssetTotalChanged = AssetsHelper.GetTotalWithChange(curTotal, preTotal) }; AllocationDict.Add(year, dto); } }
/// <summary> /// Calculate transition years. Assume 4% increase and self-sustained. /// </summary> private void GetTransitionYears() { var minYr = AllocationDict.Keys.Max() + 1; var maxYr = BirthYear + RetirementAge; for (var year = minYr; year <= maxYr; year++) { var preDto = AllocationDict[year - 1]; var preAssets = preDto.Assets; var assetDate = new DateTime(year, 12, 31); var transformer = new SimpleTransformer(_settings.InvestmentReturnRate, _settings.TransitionYear401KSaving); var assets = transformer.Transform(assetDate, preAssets); var preTotal = preDto.AssetTotal; var curTotal = assets.Sum(x => x.AssetAmount); var dto = new AllocationDto(year, AllocationStatusTypes.Estimated, _settings.RetirementIncome) { AgeYear = new RetirementAge(year - BirthYear, year).ToString(), Assets = assets.ToArray(), PreAssets = preDto.Assets, AssetTotalChanged = AssetsHelper.GetTotalWithChange(curTotal, preTotal) }; AllocationDict.Add(year, dto); } }
public async Task StartAllocation_WhenStartNewAllocation_ThenReturnStatusCode201WithAllocationGuid() { var server = new TestServer(new WebHostBuilder() .UseStartup <TestStartup>()); using (server) { var client = server .CreateClient(); var context = server.Host.Services.GetService <ParkSharkContext>(); FillingInMemoryDatabase(context); AllocationDto allocationDto = new AllocationDto() { MemberPeronId = 1, ParkinglotId = 1, MemberLicensePlate = new LicensePlate("123", "BE") }; var content = JsonConvert.SerializeObject(allocationDto); var stringContent = new StringContent(content, Encoding.UTF8, "application/json"); var response = await client.PostAsync("api/allocations", stringContent); var allocationGuid = await response.Content.ReadAsStringAsync(); var pl_response = await client.GetAsync("api/parkinglots/1"); var responseString = await pl_response.Content.ReadAsStringAsync(); ParkinglotDto parkinglotDto = JsonConvert.DeserializeObject <ParkinglotDto>(responseString); //context.Dispose(); //context = server.Host.Services.GetService<ParkSharkContext>(); //var pl = context.Parkinglots.FirstOrDefault(p => p.Id == 1); //Assert.Equal(4, pl.Capacity); Assert.Equal(4, parkinglotDto.AvailablePlaces); } }
private void AllocateRetiredFully(int pensionAge, int maxAge) { for (var year = pensionAge; year < maxAge; year++) { var preDto = AllocationDict[year - 1]; var preAssets = preDto.Assets; var assetDate = new DateTime(year, 12, 31); var transformer = new SimpleTransformer(_settings.InvestmentReturnRate); var assets = transformer.Transform(assetDate, preAssets); // 401K var r401Amount = SimpleR401KAllocator.Allocate(assets, maxAge, year); // Cash var target = GetTarget(preDto); var cashPortion = (target - r401Amount - PensionIncome - SocialSecurityIncome); var cashAmount = cashPortion >= 0 ? cashPortion : 0m; var cashWithdrawal = SimpleAllocator.Allocate(assets, assets.ForCash(), cashAmount); // Still missing target var shortAmount = target - cashWithdrawal - r401Amount - SocialSecurityIncome - PensionIncome; if (shortAmount > 0) { r401Amount += SimpleAllocator.Allocate(assets, assets.For401K(), shortAmount); } var preTotal = preDto.AssetTotal; var curTotal = assets.Sum(x => x.AssetAmount); var dto = new AllocationDto(year, AllocationStatusTypes.RetiredEstimated, target) { AgeYear = new RetirementAge(year - BirthYear, year).ToString(), CashAmount = cashWithdrawal, R401KAmount = r401Amount, SocialSecurityAmount = SocialSecurityIncome, PensionAmount = PensionIncome, Assets = assets.ToArray(), PreAssets = preDto.Assets, AssetTotalChanged = AssetsHelper.GetTotalWithChange(curTotal, preTotal) }; AllocationDict.Add(year, dto); } }
public ActionResult <AllocationDto> UpdateAllocation(long planId, long allocationId, AllocationDto allocationDto) { allocationDto.PlanId = planId; allocationDto.Id = allocationId; return(Ok(_planService.UpdateAllocation(allocationDto))); }
public ActionResult <AllocationDto> CreateAllocation(long planId, AllocationDto allocationDto) { allocationDto.PlanId = planId; return(Ok(_planService.CreateAllocation(allocationDto))); }
private decimal GetTarget(AllocationDto preDto) { return(preDto.Target == 0 ? _settings.RetirementIncome : preDto.Target *(1 + _settings.RetirementIncomeAdjustmentRate)); }