Ejemplo n.º 1
0
        private async Task GetProjectsAsync()
        {
            List <TimesheetEntry> timesheetEntries;

            ShownProjectsCollection.Clear();
            AllProjects.Clear();

            //Get all entries with a project ID
            using (var ctx = new DatabaseDir.Database())
                //Get entries
                timesheetEntries = ctx.TimesheetEntries.Include(ts => ts.vismaEntries.Select(ve => ve.LinkedRate)).ToList();

            //Convert to projects
            foreach (TimesheetEntry timesheetEntry in timesheetEntries)
            {
                //Filter empty project IDs
                if (string.IsNullOrWhiteSpace(timesheetEntry.ProjectID))
                {
                    continue;
                }

                //Filter timesheets with no "Arbejde" types
                if (timesheetEntry.vismaEntries.Where(x => x.LinkedRate.Type == "Arbejde").ToList().Count() == 0)
                {
                    continue;
                }

                //Filter if a period is specified
                if (SelectedWeek > 0 && SelectedYear > 0)
                {
                    DateTime from = DateHelper.WeekNumToDateTime(SelectedWeek, SelectedYear, 0);
                    DateTime to   = DateHelper.WeekNumToDateTime(SelectedWeek, SelectedYear, 6);

                    if (timesheetEntry.Date < from || timesheetEntry.Date > to)
                    {
                        continue; //Then skip
                    }
                }

                //Initalize
                double normalHours   = 0;
                double overtimeHours = 0;

                //Sum up hours from the entry
                foreach (VismaEntry vismaEntry in timesheetEntry.vismaEntries)
                {
                    //Filter non "Arbejde" (work) types
                    if (vismaEntry.LinkedRate.Type != "Arbejde")
                    {
                        continue;
                    }

                    //Sum overtime hours
                    if (vismaEntry.LinkedRate.Name == "Normal")
                    {
                        if (vismaEntry.Value > 0)
                        {
                            normalHours += vismaEntry.Value;
                        }
                    }
                    else
                    {
                        overtimeHours += vismaEntry.Value;
                    }
                }

                //Add time to project
                if (!AllProjects.Where(p => p.ProjectID == timesheetEntry.ProjectID).ToList().Any())
                {
                    //If project is not already listed
                    AllProjects.Add(new Project(timesheetEntry.ProjectID, normalHours, overtimeHours));
                }
                else
                {
                    //Else add hours to existing
                    Project SelectedProject = AllProjects.Where(p => p.ProjectID == timesheetEntry.ProjectID).FirstOrDefault();
                    SelectedProject.TotalNormalHours   += normalHours;
                    SelectedProject.TotalOverTimeHours += overtimeHours;
                }
            }
            AllProjects.Sort((x, y) => string.Compare(x.ProjectID, y.ProjectID));
            FiltherProjects();
        }