public bool CreateTimeslipsByCustomDay(CustomDateVM customDateVM)
        {
            CustomDay_WBIRepo customDay_WBIRepo = new CustomDay_WBIRepo(_context);
            CustomDayRepo     customDayRepo     = new CustomDayRepo(_context);

            var       timeslipTemplateList = customDay_WBIRepo.GetAllTimeslipTemplateByCustomDay(customDateVM.CustomdayId);
            CustomDay customDay            = customDayRepo.GetOneCustomDay(customDateVM.CustomdayId);

            foreach (CustomDay_WBI tt in timeslipTemplateList)
            {
                DateTime newStartTime = new DateTime(DateTime.Parse(customDateVM.Date).Year, DateTime.Parse(customDateVM.Date).Month, DateTime.Parse(customDateVM.Date).Day, tt.StartTime.Hour, tt.StartTime.Minute, tt.StartTime.Second);
                DateTime newEndTime   = new DateTime(DateTime.Parse(customDateVM.Date).Year, DateTime.Parse(customDateVM.Date).Month, DateTime.Parse(customDateVM.Date).Day, tt.EndTime.Hour, tt.EndTime.Minute, tt.EndTime.Second);

                TimeslipVM newTimeslip = new TimeslipVM()
                {
                    TimeslipId = null,
                    StartTime  = Convert.ToString(newStartTime),
                    EndTime    = Convert.ToString(newEndTime),
                    Remarks    = tt.Remarks,
                    DayId      = customDateVM.CustomdayId,
                    WBI_Id     = Convert.ToString(tt.NewChangeRequestId),
                    UserId     = Convert.ToString(customDay.UserId)
                };
                CreateTimeslip(newTimeslip);
            }
            return(true);
        }
        public NewTimesheetEntryExtensionBase CreateTimeslip(TimeslipVM timeslipVM)
        {
            Guid userGuid = Guid.Parse(timeslipVM.UserId);
            Guid wbiGuid  = Guid.Parse(timeslipVM.WBI_Id);
            NewTimesheetEntryExtensionBase timeslip = new NewTimesheetEntryExtensionBase()
            {
                NewTimesheetEntryId = Guid.NewGuid(),
                NewRemarks          = timeslipVM.Remarks,
                CustomDayId         = timeslipVM.DayId,
                NewChangeRequestId  = wbiGuid,
                OwningUser          = userGuid,
                NewStartTask        = DateTime.Parse(timeslipVM.StartTime),
                NewEndTask          = DateTime.Parse(timeslipVM.EndTime)
            };

            TimeSpan?duration = timeslip.NewEndTask - timeslip.NewStartTask;

            int durationInHours = (int)duration?.TotalHours;

            NewChangeRequestExtensionBase wbi = _context.NewChangeRequestExtensionBase
                                                .Where(w => w.NewChangeRequestId == timeslip.NewChangeRequestId)
                                                .FirstOrDefault();

            wbi.NewActualHours += durationInHours;

            if (wbi.NewActualHours > wbi.NewEstimatedHours)
            {
                throw new ArgumentException("Alloted hours for this WBI has been maxed out.");
            }
            // a.start <= b.end && a.end >= b.start

            var date     = Convert.ToDateTime(timeslip.NewStartTask);
            var sameDate = date.Date;

            foreach (var item in _context.NewTimesheetEntryExtensionBase
                     .Where(u => Convert.ToDateTime(u.NewStartTask).Date == sameDate &&
                            u.OwningUser == userGuid))
            {
                if (item.NewTimesheetEntryId != timeslip.NewTimesheetEntryId)
                {
                    if (item.NewStartTask < timeslip.NewEndTask && item.NewEndTask > timeslip.NewStartTask)
                    {
                        throw new ArgumentException("Times cannot overlap");
                    }
                }
            }
            _context.NewTimesheetEntryExtensionBase.Add(timeslip);
            _context.SaveChanges();

            return(timeslip);
        }
        public NewTimesheetEntryExtensionBase EditTimeslip(TimeslipVM timeslipVM)
        {
            var timeslip = GetOneTimeslip(timeslipVM.TimeslipId);

            if (timeslip == null)
            {
                return(timeslip);
            }
            else
            {
                NewChangeRequestExtensionBase wbi = _context.NewChangeRequestExtensionBase
                                                    .Where(w => w.NewChangeRequestId == timeslip.NewChangeRequestId)
                                                    .FirstOrDefault();
                TimeSpan?oldDuration        = timeslip.NewEndTask - timeslip.NewStartTask; //old timeslip's difference
                int      oldDurationInHours = (int)oldDuration?.TotalHours;

                TimeSpan?newDuration        = DateTime.Parse(timeslipVM.EndTime) - DateTime.Parse(timeslipVM.StartTime);//new timeslip's difference
                int      newDurationInHours = (int)newDuration?.TotalHours;

                if (newDurationInHours > oldDurationInHours)
                {
                    //add
                    int difference = newDurationInHours - oldDurationInHours;
                    wbi.NewActualHours = wbi.NewActualHours + difference;
                }
                else if (newDurationInHours < oldDurationInHours)
                {
                    //minus
                    int difference = oldDurationInHours - newDurationInHours;
                    wbi.NewActualHours = wbi.NewActualHours - difference;
                }

                timeslip.NewStartTask = DateTime.Parse(timeslipVM.StartTime);
                timeslip.NewEndTask   = DateTime.Parse(timeslipVM.EndTime);
                timeslip.NewRemarks   = timeslipVM.Remarks;

                _context.SaveChanges();
            }
            return(timeslip);
        }
Esempio n. 4
0
        public IActionResult Create([FromBody] TimeslipVM timeslipVM)
        {
            //check if the view model being passed is null
            if (timeslipVM == null)
            {
                return(new BadRequestObjectResult(new { message = "Invalid timeslip view model" }));
            }

            DateTime newStartTime;
            DateTime newEndTime;

            //check if the dates are null
            if ((timeslipVM.StartTime == null || timeslipVM.StartTime == "") ||
                (timeslipVM.EndTime == null || timeslipVM.EndTime == ""))
            {
                return(new BadRequestObjectResult(new { message = "Invalid time input. Please enter a valid time." }));
            }
            //check that start time is a valid datetime
            bool success1 = DateTime.TryParse(timeslipVM.StartTime, out DateTime result1);

            if (success1)
            {
                newStartTime = result1;
            }
            else
            {
                return(new BadRequestObjectResult(new { message = "Please enter a valid start time" }));
            }
            //check that end time is a valid datetime
            bool success2 = DateTime.TryParse(timeslipVM.EndTime, out DateTime result2);

            if (success2)
            {
                newEndTime = result2;
            }
            else
            {
                return(new BadRequestObjectResult(new { message = "Please enter a valid end time" }));
            }
            //check if the user id is null
            if (timeslipVM.UserId == null || timeslipVM.UserId == "")
            {
                return(new BadRequestObjectResult(new { message = "Invalid user. Please log in." }));
            }
            //check if the wbi id is null
            if (timeslipVM.WBI_Id == null || timeslipVM.WBI_Id == "")
            {
                return(new BadRequestObjectResult(new { message = "Please enter a Work Breakdown Item." }));
            }
            //check if the end time is earlier than start time
            if (newStartTime >= newEndTime)
            {
                return(new BadRequestObjectResult(new { message = "Invalid time input. End time should be later that start time." }));
            }
            //check to make sure the actual hours do not exceed the estimated hours
            TimeSpan?duration         = Convert.ToDateTime(timeslipVM.EndTime) - Convert.ToDateTime(timeslipVM.StartTime);
            int      durationInHours  = (int)duration?.TotalHours;
            Guid     wbiGuid          = Guid.Parse(timeslipVM.WBI_Id);
            var      wbi              = wbiRepo.GetOneWBI(wbiGuid);
            int?     localActualHours = wbi.NewActualHours;

            localActualHours += durationInHours;
            if (localActualHours > wbi.NewEstimatedHours)
            {
                return(new BadRequestObjectResult(new { message = "Alloted hours for this WBI has been maxed out." }));
            }
            //check to make sure two timeslips do not overlap
            Guid userGuid             = Guid.Parse(timeslipVM.UserId);
            var  timeslipListByUserId = timeslipRepo.GetAllTimeslipsByUserId(userGuid);
            var  date     = Convert.ToDateTime(timeslipVM.StartTime);
            var  sameDate = date.Date;
            var  timeslipListByUserIdOnTheSameDay = timeslipListByUserId.Where(u => Convert.ToDateTime(u.NewStartTask).Date == sameDate);

            foreach (var item in timeslipListByUserIdOnTheSameDay)
            {
                if (item.NewStartTask < Convert.ToDateTime(timeslipVM.EndTime) &&
                    item.NewEndTask > Convert.ToDateTime(timeslipVM.StartTime))
                {
                    return(new BadRequestObjectResult(new { message = "Times cannot overlap" }));
                }
            }

            return(new ObjectResult(timeslipRepo.CreateTimeslip(timeslipVM)));
        }