Exemple #1
0
        public ProjectHistoryModel CreateProjectHistoryReport(int id)
        {
            decimal?monthlyRate = TimeUnit.Projects.Get(id).Amount;


            var        project = TimeUnit.Projects.Get(id);
            List <int> years   = new List <int>
            {
                2016,
                2017,
                2018
            };
            List <ProjectHistoryEmployee> ReportEmployees = new List <ProjectHistoryEmployee>();
            List <ProjectHistoryTotal>    Total           = new List <ProjectHistoryTotal>();
            var employees = TimeUnit.Projects.Get().Where(x => x.Id == id).Select(y => y.Team).SelectMany(z => z.Engagements).Select(y => y.Employee).Where(d => d.Days.Count > 0).ToList();

            foreach (var employee in employees)
            {
                decimal?employeeTotalHours      = 0;
                ProjectHistoryEmployee empToAdd = new ProjectHistoryEmployee()
                {
                    Id   = employee.Id,
                    Name = employee.FullName
                };



                var totalHours = TimeUnit.Days.Get().Where(x => x.Employee.Id == employee.Id).SelectMany(y => y.Assignments).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum();
                foreach (var year in years)
                {
                    ProjectHistoryTotal empvar = new ProjectHistoryTotal()
                    {
                        Year       = year,
                        TotalHours = TimeUnit.Days.Get().Where(x => x.Employee.Id == employee.Id && x.Date.Value.Year == year).SelectMany(y => y.Assignments).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum()
                    };
                    for (int i = 0; i < 12; i++)
                    {
                        decimal?monthlyHours = 0;
                        monthlyHours = TimeUnit.Days.Get().Where(x => x.Employee.Id == employee.Id && x.Date.Value.Month == i + 1 && x.Date.Value.Year == year).SelectMany(y => y.Assignments).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum();
                        empvar.MonthlyHours[i].Hours = monthlyHours;
                    }
                    empToAdd.Sums.Add(empvar);

                    employeeTotalHours += empvar.TotalHours;
                }
                empToAdd.TotalHours = employeeTotalHours;
                if (empToAdd.TotalHours > 0)
                {
                    ReportEmployees.Add(empToAdd);
                }
            }



            var projectTotalHours = TimeUnit.Days.Get().SelectMany(y => y.Assignments).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum();

            foreach (var year in years)
            {
                ProjectHistoryTotal YearTotal = new ProjectHistoryTotal()
                {
                    Year       = year,
                    TotalHours = TimeUnit.Days.Get().Where(x => x.Date.Value.Year == year).SelectMany(y => y.Assignments).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum()
                };
                for (int i = 0; i < 12; i++)
                {
                    decimal?monthlyHours = 0;
                    monthlyHours = TimeUnit.Days.Get().Where(x => x.Date.Value.Year == year && x.Date.Value.Month == i + 1).SelectMany(y => y.Assignments).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum();
                    YearTotal.MonthlyHours[i].Hours = monthlyHours;
                }
                Total.Add(YearTotal);
            }


            return(new ProjectHistoryModel()
            {
                Id = id,
                Name = project.Name,
                TotalHours = projectTotalHours,
                Employees = ReportEmployees,
                Amount = project.Amount,
                BeginDate = project.BeginDate,
                EndDate = project.EndDate,
                Total = Total
            });

            /*var query = project.Assignments.GroupBy(x => new { year = x.Day.Date.Value.Year, empl = x.Day.Employee }).
             *  Select(x => new ProjectHistoryEmployee()
             *  {
             *      Id = x.Key.empl.Id,
             *      Name = x.Key.empl.FullName,
             *
             *      //Employee = new BaseModel { Id = x.Key.empl.Id, Name = x.Key.empl.FullName}
             *  }).ToList();*/

            //var report = TimeUnit.Projects.Get()
            //                          .Where(x => x.Id == id)
            //                          .Select(x => new ProjectHistoryModel()
            //                          {
            //                              ProjectName = x.Name,
            //                              Amount = x.Amount.Value,
            //                              BeginDate = x.BeginDate.Value,
            //                              EndDate = x.EndDate,
            //                              Hours = x.Assignments.GroupBy(m => m.Day.Date.Value.Year)
            //                                             .Select(d => d.Sum(h => h.Hours.Value)).DefaultIfEmpty(0)
            //                                             .ToList(),
            //                          }).FirstOrDefault();
            // report.Employees = query;
            //return report;
        }
        public ProjectHistoryModel ProjectHistory(int id)
        {
            decimal?monthlyRate = TimeUnit.Projects.Get(id).Amount;


            var        project = TimeUnit.Projects.Get(id);
            List <int> years   = new List <int>
            {
                2016,
                2017,
                2018
            };

            List <ProjectHistoryEmployee> ReportEmployees = new List <ProjectHistoryEmployee>();
            List <ProjectHistoryTotal>    Total           = new List <ProjectHistoryTotal>();
            var        employees   = TimeUnit.Projects.Get().Where(x => x.Id == id).Select(y => y.Team).SelectMany(z => z.Engagements).Select(y => y.Employee).ToList();
            List <int> ByYearTotal = new List <int>();

            foreach (var employee in employees)
            {
                decimal?employeeTotalHours      = 0;
                ProjectHistoryEmployee empToAdd = new ProjectHistoryEmployee()
                {
                    Id   = employee.Id,
                    Name = employee.FirstName + " " + employee.LastName
                };



                var totalHours = TimeUnit.Days.Get().Where(x => x.Employee.Id == employee.Id).SelectMany(y => y.Tasks).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum();
                foreach (var year in years)
                {
                    var dailyHours             = TimeUnit.Days.Get().Where(x => x.Employee.Id == employee.Id && x.Date.Year == year).GroupBy(x => new { x.Date.Month, x.Hours }).Select(x => x.Sum(w => w.Hours)).ToList();
                    ProjectHistoryTotal empvar = new ProjectHistoryTotal()
                    {
                        DailyHours = dailyHours,
                        Year       = year,
                        TotalHours = TimeUnit.Days.Get().Where(x => x.Employee.Id == employee.Id && x.Date.Year == year).SelectMany(y => y.Tasks).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum()
                    };

                    empToAdd.Sums.Add(empvar);

                    employeeTotalHours += empvar.TotalHours;
                }
                empToAdd.TotalHours = employeeTotalHours;
                ReportEmployees.Add(empToAdd);
            }



            var projectTotalHours = TimeUnit.Days.Get().SelectMany(y => y.Tasks).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum();

            foreach (var year in years)
            {
                ProjectHistoryTotal YearTotal = new ProjectHistoryTotal()
                {
                    Year       = year,
                    TotalHours = TimeUnit.Days.Get().Where(x => x.Date.Year == year).SelectMany(y => y.Tasks).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum()
                };
                Total.Add(YearTotal);
            }


            return(new ProjectHistoryModel()
            {
                Id = id,
                Name = project.Name,
                TotalHours = projectTotalHours,
                Employees = ReportEmployees,
                Amount = project.Amount,
                BeginDate = project.StartDate,
                EndDate = project.EndDate,
                Total = Total
            });

            /*
             * decimal? monthlyRate = TimeUnit.Projects.Get(id).Amount;
             * ProjectHistoryModel report = new ProjectHistoryModel();
             * int yearTotal = 0;
             * decimal? sumTotal = 0;
             *
             * var project = TimeUnit.Projects.Get(id);
             * report.BeginDate = project.StartDate;
             * report.EndDate = project.EndDate;
             * report.ProjectName = project.Name;
             *
             * List<int> years = new List<int>
             * {
             *  2015,
             *  2016,
             *  2017,
             *  2018
             * };
             * List<ProjectHistoryEmployee> ReportEmployees = new List<ProjectHistoryEmployee>();
             * var employees = TimeUnit.Projects.Get().Where(x => x.Id == id).Select(y => y.Team).SelectMany(z => z.Engagements).Select(y => y.Employee).ToList();
             * var engagements = TimeUnit.Projects.Get().Where(x => x.Id == id).Select(y => y.Team).SelectMany(z => z.Engagements);
             * ProjectHistoryEmployee total = new ProjectHistoryEmployee { Name = "TOTAL" };
             * total.TotalHours = 0;
             * foreach (var employee in employees)
             * {
             *  ProjectHistoryEmployee empToAdd = new ProjectHistoryEmployee();
             *
             *  empToAdd.Id = employee.Id;
             *  empToAdd.Name = employee.FirstName + " " + employee.LastName;
             *
             *  var totalHours = TimeUnit.Days.Get().Where(x => x.Employee.Id == employee.Id).SelectMany(y => y.Tasks).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum();
             *  empToAdd.TotalHours = totalHours;
             *  total.TotalHours += empToAdd.TotalHours;
             *  foreach (var year in years)
             *  {
             *      ProjectHistoryTotal empvar = new ProjectHistoryTotal()
             *      {
             *          Year = year,
             *          Sum = TimeUnit.Days.Get().Where(x => x.Employee.Id == employee.Id && x.Date.Year == year).SelectMany(y => y.Tasks).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum()
             *      };
             *
             *      ProjectHistoryTotal totalHis = new ProjectHistoryTotal()
             *      {
             *          Year = yearTotal,
             *          Sum = sumTotal
             *      };
             *
             *      yearTotal = year;
             *      sumTotal += empvar.Sum;
             *
             *      empToAdd.Sums.Add(empvar);
             *      total.Sums.Add(totalHis);
             *  }
             *
             *
             *  sumTotal = 0;
             *  ReportEmployees.Add(empToAdd);
             *
             * }
             * ReportEmployees.Add(total);
             * report.Employees = ReportEmployees;
             *
             *
             * var projectTotalHours = TimeUnit.Days.Get().SelectMany(y => y.Tasks).Where(x => x.Project.Id == id).Select(x => x.Hours).DefaultIfEmpty(0).Sum();
             * report.TotalHours = projectTotalHours;
             * return report;*/
        }