public void GetDateCountByForecastType_CanExtract() { // Arrange var user = new GenericRepository <User>(Session).SaveOrUpdate(DataGenerator.GetUser()); var client = new GenericRepository <Company>(Session).SaveOrUpdate(DataGenerator.GetCustomer(user)); var project = new GenericRepository <Project>(Session).SaveOrUpdate(DataGenerator.GetProject(user, client)); var forecastType = new GenericRepository <ForecastType>(Session).SaveOrUpdate(new ForecastType("Client", "", true, false)); var forecastTypeRepo = new ForecastTypeRepository(Session); forecastTypeRepo.SaveOrUpdate(forecastType); var clientForecastTypeId = forecastType.Id; var month = new ForecastMonth(1, 2013, 3, user, user); var repo = new ForecastRepository(Session); CreateForecastWithSingleProjectRegistration(month, new DateTime(2013, 1, 1), forecastType, project, 3); forecastType = new GenericRepository <ForecastType>(Session).SaveOrUpdate(new ForecastType("Vacation", "", false, false)); forecastTypeRepo.SaveOrUpdate(forecastType); var vacationForecastTypeId = forecastType.Id; CreateForecastWithSingleProjectRegistration(month, new DateTime(2013, 1, 2), forecastType, null, 0); CreateForecastWithSingleProjectRegistration(month, new DateTime(2013, 1, 3), forecastType, null, 0); var monthRepo = new ForecastMonthRepository(Session); monthRepo.SaveOrUpdate(month); // Act var clientForecastCount = repo.GetForecastCountByForecastType(1, clientForecastTypeId, DateSpan.YearDateSpan(2013)); var vacationForecastcount = repo.GetForecastCountByForecastType(1, vacationForecastTypeId, DateSpan.YearDateSpan(2013)); // Assert Assert.That(clientForecastCount, Is.EqualTo(1)); Assert.That(vacationForecastcount, Is.EqualTo(2)); }
private void EnsureNotLocked(ForecastMonth forecastMonth) { if (forecastMonth.IsLocked) { throw new Exception(string.Format("Cannot update/create workplan for month: {0} year: {1}, since it is locked", forecastMonth.Month, forecastMonth.Year)); } }
private void CreateForecastWithSingleProjectRegistration(ForecastMonth month, DateTime date, ForecastType forecastType, Project project, decimal hours, decimal?dedicatedHours = null) { var forecast = month.AddForecast(date, forecastType, dedicatedHours); if (forecastType.SupportsProjectHours) { forecast.AddProjectRegistration(project, hours); } }
/// <summary> /// Determines if month is locked. /// Business rule: /// If currentdate exceeds currentlockdate the previous month /// is by default locked unless stated explicitly that it is unlocked by UnLocked property /// </summary> /// <param name="dto"></param> /// <param name="forecastMonth"></param> public virtual void SetDtoIsLocked(ForecastMonthDto dto, ForecastMonth forecastMonth) { var now = Now; // Is explicitly set to allow update if (forecastMonth != null) { dto.IsLocked = forecastMonth.IsLocked; return; } // Forecast is null. var lockDate = ForecastMonth.CreateLockedFrom(dto.Month, dto.Year, _domainSettings.PastMonthsDayLock); dto.IsLocked = now > lockDate; }
public void IsLocked_TestCase(int month, int year, string nowDateString, int lockDay, bool expectedResult) { // Arrange var user = CreateMock <User>(); var sut = new ForecastMonth(month, year, lockDay, user.Object, user.Object) { Now = ParseDkDateString(nowDateString) }; // Act var result = sut.IsLocked; // Assert Assert.That(result, Is.EqualTo(expectedResult)); }
/// <summary> /// Gets forecasts supporting project hours /// for the current year /// </summary> /// <param name="userId"></param> /// <param name="queryInternalCustomers"></param> /// <param name="dateSpan"></param> /// <returns></returns> public decimal GetHourSumByCriteria(int userId, bool queryInternalCustomers, DateSpan dateSpan) { Forecast forecastAlias = null; ForecastType forecastTypeAlias = null; User userAlias = null; ForecastProjectHours regAlias = null; Company companyAlias = null; Project projectAlias = null; ForecastMonth monthAlias = null; var query = Session.QueryOver(() => regAlias) .JoinAlias(() => regAlias.Parent, () => forecastAlias) .JoinAlias(() => forecastAlias.ForecastMonth, () => monthAlias) .JoinAlias(() => monthAlias.User, () => userAlias) .JoinAlias(() => forecastAlias.ForecastType, () => forecastTypeAlias) .JoinAlias(() => regAlias.Project, () => projectAlias) .JoinAlias(() => projectAlias.Company, () => companyAlias) .Where(() => userAlias.UserID == userId) .And(() => forecastAlias.Date >= dateSpan.From) .And(() => forecastAlias.Date <= dateSpan.To) .And(() => forecastTypeAlias.SupportsProjectHours) .And(() => companyAlias.Internal == queryInternalCustomers) .Select(Projections.Sum <ForecastProjectHours>(x => x.Hours)); var result = query .UnderlyingCriteria .UniqueResult <decimal>(); // Dedicatedhours are accounted for internally if (queryInternalCustomers) { var typeDedicatedHours = Session.QueryOver(() => forecastAlias) .JoinAlias(() => forecastAlias.ForecastMonth, () => monthAlias) .JoinAlias(() => monthAlias.User, () => userAlias) .JoinAlias(() => forecastAlias.ForecastType, () => forecastTypeAlias) .Where(() => forecastAlias.Date >= dateSpan.From) .And(() => forecastAlias.Date <= dateSpan.To) .And(() => forecastTypeAlias.StatisticsInclusion) .And(() => userAlias.UserID == userId) .Select(Projections.Sum <Forecast>(x => x.DedicatedForecastTypeHours)) .UnderlyingCriteria .UniqueResult <decimal>(); result = result + typeDedicatedHours; } return(result); }
/// <summary> /// Returns the forecast count constrained by user, forecastype /// and datespan /// </summary> /// <param name="userId">User's id</param> /// <param name="forecastTypeId">ForecastType's id</param> /// <param name="dateSpan">Datespan to search in</param> /// <returns></returns> public int GetForecastCountByForecastType(int userId, int forecastTypeId, DateSpan dateSpan) { ForecastMonth monthAlias = null; var result = Session .QueryOver <Forecast>() .JoinAlias(x => x.ForecastMonth, () => monthAlias) .Where(() => monthAlias.User.UserID == userId) .And(x => x.ForecastType.Id == forecastTypeId) .And(x => x.Date >= dateSpan.From) .And(x => x.Date <= dateSpan.To) .Select(Projections.RowCount()) .UnderlyingCriteria .UniqueResult <int>(); return(result); }
public void GetRestOfYear_CanExtract() { // Arrange var user = new GenericRepository <User>(Session).SaveOrUpdate(DataGenerator.GetUser()); var client = DataGenerator.GetCustomer(user); client.Internal = true; client = new GenericRepository <Company>(Session).SaveOrUpdate(client); var project = new GenericRepository <Project>(Session).SaveOrUpdate(DataGenerator.GetProject(user, client)); var forecastType = new GenericRepository <ForecastType>(Session).SaveOrUpdate(new ForecastType("Client", "", true, true)); var illnessForecastType = new GenericRepository <ForecastType>(Session).SaveOrUpdate(new ForecastType("Illness", "", false, true) { StatisticsInclusion = false }); var forecastTypeRepo = new ForecastTypeRepository(Session); forecastTypeRepo.SaveOrUpdate(forecastType); var repo = new ForecastRepository(Session); var month = new ForecastMonth(1, 2013, 3, user, user); CreateForecastWithSingleProjectRegistration(month, new DateTime(2013, 1, 1), forecastType, project, 7.5m, 1); CreateForecastWithSingleProjectRegistration(month, new DateTime(2013, 1, 2), forecastType, project, 6, 1); CreateForecastWithSingleProjectRegistration(month, new DateTime(2013, 1, 3), forecastType, project, 5, 1); CreateForecastWithSingleProjectRegistration(month, new DateTime(2013, 1, 4), illnessForecastType, null, 0, 7.5m); var monthRepo = new ForecastMonthRepository(Session); monthRepo.SaveOrUpdate(month); // Act var all = repo.GetHourSumByCriteria(1, true, DateSpan.YearDateSpan(2013)); var allExcluded = repo.GetHourSumByCriteria(1, false, DateSpan.YearDateSpan(2013)); var twoByDateSpan = repo.GetHourSumByCriteria(1, true, new DateSpan { From = new DateTime(2013, 1, 1), To = new DateTime(2013, 1, 2) }); // Assert Assert.That(all, Is.EqualTo(21.5m)); // 7.5 + 6 + 5 + 1 + 1 + 1 (projecthours and dedicated hours (1's)) Assert.That(allExcluded, Is.EqualTo(0)); Assert.That(twoByDateSpan, Is.EqualTo(15.5m)); }
private void UpdateForecasts(bool getAll = false) { getAll = (bool)cbshowAll.IsChecked; Forecasts.Clear(); Months SelectedMonth; Enum.TryParse(cbMonth.SelectedValue.ToString(), out SelectedMonth); //string date = "01/01/2013"; //DateTime dt = Convert.ToDateTime(DateTime.); DateTime month = new DateTime(DateTime.Now.Year, (int)SelectedMonth, 1); // Adda en month för att kunna låsa (addas bara om den inte finns) ForecastMonth = ForecastingManagement.Instance.AddForecastMonth(month); ForecastingManagement.Instance.GetForecastsFromMonth(month, getAll); UpdateLabels(); if (Forecasts.Any()) { if (ForecastingManagement.Instance.CheckIfLocked(month)) { dgForecasts.IsEnabled = false; btnSave.IsEnabled = false; lblInfo.Content = "Uppföljning för denna månad är låst"; btnLock.IsEnabled = false; } else { dgForecasts.IsEnabled = true; btnSave.IsEnabled = true; btnLock.IsEnabled = true; lblInfo.Content = ""; } } }
public void AddForecast_ForecastDateIsNotPartOfForecastMonth_ThrowsException(string dateString, string message) { // Arrange Fixture.Behaviors.Add(new OmitOnRecursionBehavior()); var usr = Fixture .Build <User>() .Without(x => x.ForecastMonths) .Create(); var forecastType = Fixture.Create <ForecastType>(); var month = new ForecastMonth(1, 2013, 3, usr, usr); // Act var exp = Assert.Throws <Exception>(() => month.AddForecast(ParseDkDateString(dateString), forecastType, null)); // Assert Assert.That(exp.Message, Is.EqualTo(message)); }
/// <summary> /// Add forecast to database or save changes made to a forecast that is already added to the database /// </summary> public ForecastMonth AddForecastMonth(DateTime month) { string monthID = month.ToString("yyyyMM"); //Check if forecastmonth exists ForecastMonth fcm = db.ForecastMonth.SingleOrDefault(f => f.ForecastMonitorMonthID == monthID); if (fcm == null) { // Create forecastmonth object fcm = new ForecastMonth { ForecastLock = false, ForecastMonitorMonthID = monthID }; // Add object to db db.ForecastMonth.Add(fcm); db.SaveChanges(); } return(fcm); }
public void AddForecast_AddsNewForecastToCollection() { // Arrange Fixture.Behaviors.Add(new OmitOnRecursionBehavior()); var usr = Fixture .Build <User>() .Without(x => x.ForecastMonths) .Create(); var forecastType = Fixture.Create <ForecastType>(); var month = new ForecastMonth(1, 2013, 3, usr, usr); // Act month.AddForecast(new DateTime(2013, 1, 1), forecastType, null); // Assert var forecast = month.Forecasts.Single(); Assert.That(forecast.Date, Is.EqualTo(new DateTime(2013, 1, 1))); Assert.That(forecast.ForecastType, Is.EqualTo(forecastType)); Assert.That(forecast.DedicatedForecastTypeHours, Is.EqualTo(null)); }
private ForecastMonthDto ToForecastDtoObject(ForecastMonth forecastMonth, ForecastsByUserAndMonthRequest request) { ForecastMonthDto result = null; if (forecastMonth == null) { // Can be null, if not entered earlier. Resolution add to db and set UnLocked to true result = new ForecastMonthDto { Id = 0, Month = request.ForecastMonth, Year = request.ForecastYear, ForecastDtos = new List <ForecastDto>() }; } else { result = Mapper.Map <ForecastMonth, ForecastMonthDto>(forecastMonth); } SetDtoIsLocked(result, forecastMonth); return(result); }
public bool ForecastMonitorExists(ForecastMonth forecastMonth) { return(db.ForecastMonitor.Any(f => f.ForecastMonitorMonthID == forecastMonth.ForecastMonitorMonthID)); }
public void PostValue_SavesAsExpected() { // Arrange var fixture = InitializeFixture(); var forecastType = fixture.Create <Mock <ForecastType> >(); forecastType.SetupGet(x => x.Id).Returns(22); var forecastTypeRepoMock = FreezeMock <IForecastTypeRepository>(); forecastTypeRepoMock.Setup(x => x.GetAll()).Returns(new List <ForecastType> { forecastType.Object }); var userMock = fixture.Create <Mock <User> >(); userMock.SetupGet(x => x.EntityId).Returns(10); var userRepoMock = FreezeMock <IUserRepository>(); userRepoMock.Setup(x => x.GetByUserID(10)).Returns(userMock.Object); var forecastMonthMock = fixture.Create <Mock <ForecastMonth> >(); forecastMonthMock.CallBase = false; forecastMonthMock.SetupGet(x => x.Id).Returns(66); ForecastMonth buildtMonth = null; var monthRepoMock = FreezeMock <IForecastMonthRepository>(); monthRepoMock.Setup(x => x.GetById(0)).Returns((ForecastMonth)null); monthRepoMock.Setup(x => x.SaveOrUpdate(It.IsAny <ForecastMonth>())).Callback <ForecastMonth>(x => buildtMonth = x); var monthFactoryMock = FreezeMock <IForecastMonthFactory>(); monthFactoryMock.Setup(x => x.CreateForecastMonth(1, 2013, It.IsAny <User>(), It.IsAny <User>())).Returns(forecastMonthMock.Object); var request = new SaveForecastsRequest { ForecastMonthDto = new ForecastMonthDto { Id = 0, UserId = 10, Month = 1, Year = 2013, CreatedById = 10, ForecastDtos = new List <ForecastDto> { new ForecastDto { Date = new DateTime(2013, 1, 1), ForecastType = new ForecastTypeDto { Id = 22 }, ForecastProjectHoursDtos = new Collection <ForecastProjectHoursDto>() } } } }; var sut = fixture.Create <SaveForecastsService>(); // Act var result = sut.Post(request); // Assert monthRepoMock.VerifyAll(); monthFactoryMock.VerifyAll(); forecastType.VerifyAll(); userRepoMock.VerifyAll(); forecastMonthMock.Verify(x => x.AddForecast(new DateTime(2013, 1, 1), forecastType.Object, null)); Assert.That(result, Is.TypeOf <SaveForecastsResponse>()); Assert.That(((SaveForecastsResponse)result).ForecastMonthId, Is.EqualTo(buildtMonth.Id)); Assert.That(buildtMonth.Id, Is.EqualTo(66)); }
public IQueryOver <ForecastMonth, ForecastMonth> Apply(IQueryOver <ForecastMonth, ForecastMonth> queryOver, ForecastMonth monthAlias, Trex.Server.Core.Model.Forecast.Forecast forecastAlias) { Company companyAlias = null; Project projectAlias = null; ForecastProjectHours projectRegAlias = null; var projectCrit = QueryOver .Of <Company>() .Where(x => x.CustomerID == _companyId) .And(x => x.CustomerID == companyAlias.CustomerID) .Select(x => x.CustomerID) .Take(1); return(queryOver .JoinAlias(() => forecastAlias.ProjectRegistrations, () => projectRegAlias) .JoinAlias(() => projectRegAlias.Project, () => projectAlias) .JoinAlias(() => projectAlias.Company, () => companyAlias) .Where(Subqueries.WhereExists(projectCrit))); }
public IQueryOver <ForecastMonth, ForecastMonth> Apply(IQueryOver <ForecastMonth, ForecastMonth> queryOver, ForecastMonth monthAlias, Trex.Server.Core.Model.Forecast.Forecast forecastAlias) { var forecastTypeCrit = QueryOver .Of <Trex.Server.Core.Model.Forecast.Forecast>() .Where(x => x.ForecastType.Id == _forecastTypeId) .And(x => x.ForecastMonth.Id == monthAlias.Id) .Select(x => x.Id) .Take(1); return(queryOver.Where(Subqueries.WhereExists(forecastTypeCrit))); }
private static ForecastMonthDto ForecastMonthToDto(ForecastMonth x) { return(Mapper.Map <ForecastMonth, ForecastMonthDto>(x)); }
public IQueryOver <ForecastMonth, ForecastMonth> Apply(IQueryOver <ForecastMonth, ForecastMonth> queryOver, ForecastMonth monthAlias, Trex.Server.Core.Model.Forecast.Forecast forecastAlias) { ForecastProjectHours projectRegAlias = null; var projectCrit = QueryOver .Of <ForecastProjectHours>() .Where(x => x.Project.ProjectID == _projectId) .And(x => x.Parent.Id == forecastAlias.Id) .Select(x => x.Id) .Take(1); return(queryOver .JoinAlias(() => forecastAlias.ProjectRegistrations, () => projectRegAlias) .Where(Subqueries.WhereExists(projectCrit))); }