Пример #1
0
        public IEnumerable <ProjectAssignmentDto> Get(int year, int month, int day)
        {
            List <TimesheetDto>          timesheetsDtos = new List <TimesheetDto>();
            IEnumerable <Timesheet>      timesheets     = new List <Timesheet>();
            IList <ProjectAssignmentDto> assignments    = new List <ProjectAssignmentDto>();

            //If no date provide, then bring back all timesheets
            if (year == 0 || year == 1970)
            {
                timesheets = _timeSheetRepository.GetTimesheets();
            }
            else
            {
                DateTime weekStarting = new DateTime(year, month, day);
                timesheets = _timeSheetRepository.GetTimesheetsByDate(weekStarting);
            }

            foreach (Timesheet ts in timesheets)
            {
                //We only are concerned with approved timesheets
                if (ts.Status != TimesheetStatus.Approved)
                {
                    continue;
                }

                foreach (TimesheetEntry tse in ts.TimesheetEntries)
                {
                    if (!projectCodeAlreadyRecorded(assignments, tse.Code))
                    {
                        //For none chargeable codes we ignore.
                        if (!isProjectCode(tse.Code))
                        {
                            continue;
                        }
                        ProjectAssignmentDto adto = new ProjectAssignmentDto();
                        adto.Code = tse.Code;
                        if (adto.Users == null)
                        {
                            adto.Users = new List <UserAssignmentDetails>();
                        }
                        bool addUser = true;
                        foreach (UserAssignmentDetails user in adto.Users)
                        {
                            if (user.UserName.Equals(ts.Username))
                            {
                                addUser = false;
                            }
                        }
                        if (addUser)
                        {
                            UserAssignmentDetails newUserDetails = new UserAssignmentDetails();
                            newUserDetails.UserName = ts.Username;
                            DateTime now       = DateTime.UtcNow;
                            string[] startTime = tse.StartTime.Split(":");
                            string[] endTime   = tse.EndTime.Split(":");

                            DateTime start = new DateTime(now.Year, now.Month, now.Day, Int32.Parse(startTime[0]), Int32.Parse(startTime[1]), 0);
                            DateTime end   = new DateTime(now.Year, now.Month, now.Day, Int32.Parse(endTime[0]), Int32.Parse(endTime[1]), 0);

                            TimeSpan varTime = (DateTime)end - (DateTime)start;

                            newUserDetails.TotalMinutes = (int)varTime.TotalMinutes;
                            adto.Users.Add(newUserDetails);
                        }
                        assignments.Add(adto);
                    }
                    else
                    {
                        foreach (ProjectAssignmentDto dto in assignments)
                        {
                            if (dto.Code.Equals(tse.Code))
                            {
                                if (dto.Users == null)
                                {
                                    dto.Users = new List <UserAssignmentDetails>();
                                }
                                bool addUser = true;
                                foreach (UserAssignmentDetails user in dto.Users)
                                {
                                    if (user.UserName.Equals(ts.Username))
                                    {
                                        addUser = false;
                                    }
                                }
                                if (addUser)
                                {
                                    UserAssignmentDetails newUserDetails = new UserAssignmentDetails();
                                    newUserDetails.UserName = ts.Username;
                                    DateTime now       = DateTime.UtcNow;
                                    string[] startTime = tse.StartTime.Split(":");
                                    string[] endTime   = tse.EndTime.Split(":");

                                    DateTime start = new DateTime(now.Year, now.Month, now.Day, Int32.Parse(startTime[0]), Int32.Parse(startTime[1]), 0);
                                    DateTime end   = new DateTime(now.Year, now.Month, now.Day, Int32.Parse(endTime[0]), Int32.Parse(endTime[1]), 0);

                                    TimeSpan varTime = (DateTime)end - (DateTime)start;

                                    newUserDetails.TotalMinutes = (int)varTime.TotalMinutes;
                                    dto.Users.Add(newUserDetails);
                                }
                                else
                                {
                                    foreach (UserAssignmentDetails user in dto.Users)
                                    {
                                        if (user.UserName.Equals(ts.Username))
                                        {
                                            DateTime now       = DateTime.UtcNow;
                                            string[] startTime = tse.StartTime.Split(":");
                                            string[] endTime   = tse.EndTime.Split(":");

                                            DateTime start = new DateTime(now.Year, now.Month, now.Day, Int32.Parse(startTime[0]), Int32.Parse(startTime[1]), 0);
                                            DateTime end   = new DateTime(now.Year, now.Month, now.Day, Int32.Parse(endTime[0]), Int32.Parse(endTime[1]), 0);

                                            TimeSpan varTime = (DateTime)end - (DateTime)start;

                                            user.TotalMinutes += (int)varTime.TotalMinutes;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(assignments);
        }