Example #1
0
        public async Task <IActionResult> GetDailyReport([FromQuery] FilterViewModel filter)
        {
            try
            {
                if (filter.DateStart == null || filter.DateEnd == null)
                {
                    return(BadRequest("Не указан период"));
                }

                filter.DateStart = filter.DateStart.ToDateWithoutTime();
                filter.DateEnd   = filter.DateEnd.ToDateWithoutTime();

                var items = new List <ProjectTaskDailyReport>();

                var tasks = await _projectTaskRepository.ProjectTasksAsync();

                tasks = tasks.Where(e => e.ProjectTaskStatusId == 2 || e.ProjectTaskStatusId == 4).ToList();

                foreach (var task in tasks)
                {
                    var dateStart = task.StartFact ?? task.StartPlan;
                    var dateEnd   = task.EndFact ?? task.EndPlan;

                    if (dateStart == null || dateEnd == null)
                    {
                        continue;
                    }

                    dateStart = dateStart.ToDateWithoutTime();
                    dateEnd   = dateEnd.ToDateWithoutTime();

                    var isReport = false;

                    if (filter.DateStart <= dateStart && filter.DateEnd >= dateStart ||
                        filter.DateStart >= dateStart && filter.DateEnd <= dateEnd ||
                        filter.DateStart > dateStart && filter.DateEnd > dateEnd && filter.DateStart <= dateEnd ||
                        filter.DateStart < dateStart && filter.DateEnd > dateEnd)
                    {
                        isReport = true;

                        if (dateStart < filter.DateStart)
                        {
                            dateStart = filter.DateStart.Value.Date;
                        }

                        if (dateEnd > filter.DateEnd)
                        {
                            dateEnd = filter.DateEnd.Value.Date;
                        }
                    }

                    if (isReport)
                    {
                        var currentDay = dateStart.Value.Date;
                        while (currentDay <= dateEnd.Value)
                        {
                            foreach (var person in task.ProjectTaskPerformers.Where(e => !e.Deleted))
                            {
                                items.Add(new ProjectTaskDailyReport()
                                {
                                    Name       = person.User.ShortName,
                                    GroupName  = person.User.GroupName,
                                    DateWork   = currentDay.Date,
                                    TaskStatus = task.TaskStatus.StatusName,
                                    Comment    = task.Details,
                                    Project    = task.Project.ProjectName,
                                    TaskRange  = $"{(task.StartFact ?? task.StartPlan):dd.MM.yy} - {(task.EndFact ?? task.EndPlan):dd.MM.yy}"
                                });
                            }

                            currentDay = currentDay.AddDays(1);
                        }
                    }
                }

                items = items.OrderBy(e => e.DateWork)
                        .ThenBy(e => e.Name)
                        .ThenBy(e => e.Project)
                        .ToList();

                var filePath = _excelService.DailyReport(items.Cast <object>().ToList());

                if (System.IO.File.Exists(filePath))
                {
                    var result = PhysicalFile(filePath,
                                              "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                    string nameFile = $"Ежедневный_отчет_выполнения_задач_от_{DateTime.Now:yyyy.MM.dd HH.mm.ss}.xlsx";

                    Response.Headers["Content-Disposition"] = new ContentDispositionHeaderValue("attachment")
                    {
                        FileName = WebUtility.UrlEncode(nameFile)
                    }.ToString();

                    return(result);
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new { error = ex.Message }));
            }

            return(BadRequest(new { error = "Отчет не сформирован" }));
        }