public async Task <IActionResult> Create(Hour hour, IFormFile Document, int?Selectbilling, int?approval, int?description, int?clients, int?projects, int?employees, int?month, int?year)
        {
            GetSessions();

            if (ViewBag.Email == null)
            {
                return(ExpiredSession());
            }


            try
            {
                int empId       = ViewBag.Id;
                int accessLevel = ViewBag.AcessLevel;

                if (!ModelState.IsValid || _context.Hour.Count(hours => hours.Id_Project == hour.Id_Project && hours.Date == hour.Date && hours.Arrival_Time == hour.Arrival_Time && hours.Exit_Time == hour.Exit_Time) > 0)
                {
                    var clientes = await _clientService.FindAllAsync(accessLevel, empId);

                    var projetos = await _projectService.FindProjectAsync(empId, accessLevel);

                    var funcionarios = await _employeeService.FindEmployeesActivesAsync();

                    var localities = await _context.Locality.Where(x => x.Active == 1).ToListAsync();

                    var viewModel = new HourFormViewModel {
                        Projects = projetos, Employees = funcionarios, Clients = clientes, Locality = localities
                    };
                }

                hour.File = string.Empty;
                string nomeArquivo = string.Empty;

                await _hourService.InsertAsync(hour);

                await _context.SaveChangesAsync();

                if (Document != null)
                {
                    var lastId = (from result in _context.Hour
                                  where result.Employee_Id == empId
                                  orderby result.Id descending
                                  select result).FirstOrDefault();

                    if (lastId != null)
                    {
                        int hoursId = lastId.Id;
                        nomeArquivo = string.Concat(hoursId, "-", Document.FileName);
                        hour.File   = nomeArquivo;
                        _files.EnviarArquivo(Document, nomeArquivo, storage);
                    }

                    _context.Update(hour);
                    await _context.SaveChangesAsync();
                }

                return(RedirectToAction(nameof(Index), new { Selectbilling, approval, description, clients, projects, employees, month, year }));
            }
            catch (Exception e)
            {
                return(RedirectToAction(nameof(Error), new { message = e.Message }));
            }
        }
        public async Task <IActionResult> Create(Hour hour, IFormFile Document, IFormFile Attachment, int?approval, int?description, int?clients, int?projects, int?month, int?year)
        {
            GetSessions();

            if (ViewBag.Email == null)
            {
                return(ExpiredSession());
            }

            try
            {
                int    empId       = ViewBag.Id;
                int    accessLevel = ViewBag.AcessLevel;
                string email       = ViewBag.Email;

                if (_context.Employee.Count(elem => elem.TypeReleases != 10 && elem.Email == email) > 0)
                {
                    if (!ModelState.IsValid)
                    {
                        return(RedirectToAction(nameof(Error), new { message = "Erro no servidor, não foi possivel carregar todas as informações" }));
                    }

                    if (_context.Hour.Count(hours => hours.Id_Project == hour.Id_Project && hours.Date == hour.Date && hours.Arrival_Time == hour.Arrival_Time && hours.Exit_Time == hour.Exit_Time && hours.Employee_Id == empId) > 0)
                    {
                        return(RedirectToAction(nameof(Error), new { message = "As informações inseridas estão duplicadas, por favor verificar" }));
                    }
                }

                if (ModelState.IsValid)
                {
                    hour.File       = string.Empty;
                    hour.Attachment = string.Empty;

                    string nomeArquivo           = string.Empty;
                    string nomeArquivoAttachment = string.Empty;

                    await _hourService.InsertAsync(hour);

                    await _context.SaveChangesAsync();

                    if (Document != null)
                    {
                        var lastId = (from result in _context.Hour
                                      where result.Employee_Id == empId
                                      orderby result.Id descending
                                      select result).FirstOrDefault();

                        if (lastId != null)
                        {
                            int hoursId = lastId.Id;
                            nomeArquivo = string.Concat(hoursId, "-", Document.FileName);
                            hour.File   = nomeArquivo;
                            _files.EnviarArquivo(Document, nomeArquivo, storage);
                        }

                        _context.Update(hour);
                        await _context.SaveChangesAsync();
                    }

                    if (Attachment != null)
                    {
                        var lastId = (from result in _context.Hour
                                      where result.Employee_Id == empId
                                      orderby result.Id descending
                                      select result).FirstOrDefault();

                        if (lastId != null)
                        {
                            int hoursId = lastId.Id;
                            nomeArquivoAttachment = string.Concat(hoursId, "-", Attachment.FileName);
                            hour.Attachment       = nomeArquivoAttachment;
                            _files.EnviarArquivo(Attachment, nomeArquivoAttachment, storageAttachment);
                        }

                        _context.Update(hour);
                        await _context.SaveChangesAsync();
                    }
                }
                else
                {
                    return(RedirectToAction(nameof(Error), new { message = "Informações que foram inseridas não são válidas, por favor verificar" }));
                }

                return(RedirectToAction(nameof(Index), new { approval, description, clients, projects, month, year }));
            }
            catch (NotFoundException e)
            {
                return(RedirectToAction(nameof(Error), new { message = e.Message }));
            }
            catch (DbConcurrencyException e)
            {
                return(RedirectToAction(nameof(Error), new { message = e.Message }));
            }
            catch (Exception e)
            {
                return(RedirectToAction(nameof(Error), new { message = e.Message }));
            }


            //return View(hour);
        }