示例#1
0
        public TimesheetsOverviewWeekViewModel(TimesheetsCalendarViewModel parent, DateTime date, List <TimesheetSearchResult> timesheets, List <SessionSwitch> sessionSwitches)
            : base(parent)
        {
            WeekNumber = date.ToWeekNumber();
            DateStart  = date.ToPreviousDay(DayOfWeek.Monday);
            DateEnd    = date.ToNextDay(DayOfWeek.Sunday);

            Days = new List <TimesheetsOverviewDayViewModel>();
            for (var i = 0; i < 7; i++)
            {
                var dayDate = DateStart.AddDays(i);

                var day = new TimesheetsOverviewDayViewModel(this, dayDate);
                Days.Add(day);

                day.Timesheets.AddRange(timesheets.Where(x => x.StartTime.Date == day.Date).OrderBy(x => x.StartTime));
            }
        }
        public void FlattenDay(TimesheetsOverviewDayViewModel dayItem)
        {
            //using(var db = new TimesheetsContext())
            //{
            //    var timesheets = db.GetTimesheets(Session.Username, dayItem.Date, dayItem.Date);

            //    //add break item of 30m if missing ONLY if it's 1 timesheet item around lunch
            //    if(timesheets.All(x => x.Type != (int)TimesheetType.Break))
            //    {
            //        //TODO check if only 1 timesheet is between 12am and 2 pm, if yes then add break so that no other timesheets get cut by accident
            //        var lunchStart = dayItem.Date.AddHours(12);
            //        var lunchEnd = dayItem.Date.AddHours(14);
            //        var timesheetsAroundLunch = timesheets.Where(x => x.IntersectsWithTimeRange(lunchStart, lunchEnd)).ToArray();
            //        if(timesheetsAroundLunch.Length == 1)
            //        {
            //            timesheets.Add(new Timesheet()
            //            {
            //                Username = Session.Username,
            //                Type = (int)TimesheetType.Break,
            //                StartTime = dayItem.Date.AddHours(12),
            //                EndTime = dayItem.Date.AddHours(12).AddMinutes(30)
            //            });
            //        }
            //    }

            //    //split long timesheets
            //    foreach(var timesheet in timesheets.OrderBy(x => x.StartTime).ToArray())
            //    {
            //        var activeItem = timesheets.OrderBy(x => x.StartTime).FirstOrDefault(x => x.StartTime < timesheet.StartTime && x.EndTime > timesheet.StartTime);
            //        if(activeItem != null)
            //        {
            //            if(activeItem.EndTime > timesheet.EndTime)
            //            {
            //                timesheets.Add(new Timesheet()
            //                {
            //                    Username = Session.Username,
            //                    StartTime = timesheet.EndTime,
            //                    EndTime = activeItem.EndTime,
            //                    Type = activeItem.Type,
            //                    //JobId = activeItem.JobId,
            //                    Description = activeItem.Description
            //                });
            //            }

            //            activeItem.EndTime = timesheet.StartTime;
            //        }
            //    }

            //    //cluster timesheets together
            //    var flatList = new List<Timesheet>();

            //    foreach(var timesheet in timesheets.OrderBy(x => x.StartTime))
            //    {
            //        var item = flatList.SingleOrDefault(x => x.Type == timesheet.Type && x.Description == timesheet.Description);
            //        if(item == null)
            //        {
            //            flatList.Add(timesheet);
            //        }
            //        else
            //        {
            //            var minutes = (timesheet.EndTime - timesheet.StartTime).TotalMinutes;
            //            item.EndTime = item.EndTime.AddMinutes(minutes);

            //            foreach(var item2 in flatList.Where(x => x.StartTime > item.StartTime))
            //            {
            //                item2.StartTime = item2.StartTime.AddMinutes(minutes);
            //                item2.EndTime = item2.EndTime.AddMinutes(minutes);
            //            }
            //        }
            //    }

            //    foreach(var item in flatList)
            //    {
            //        db.Save(item);
            //    }

            //    var deleteItems = timesheets.Where(x => !x.IsNew && flatList.All(y => y.Id != x.Id)).ToArray();
            //    foreach(var item in deleteItems)
            //    {
            //        db.Delete(item);
            //    }
            //}

            //RefreshDateRange(dayItem.Date, dayItem.Date);

            //if(dayItem.IsSelected)
            //{
            //    TimesheetDayViewModel.RefreshData();
            //}
        }