Exemplo n.º 1
0
        // GET: Checkin
        public ActionResult Index(int?month, int?year)
        {
            if (month == null)
            {
                month = DateTime.Now.Month;
                year  = DateTime.Now.Year;
            }

            var checkinsViewModel = new CheckinsViewModel();

            checkinsViewModel.UserName = Session["UserName"].ToString();
            checkinsViewModel.Month    = month.Value;
            checkinsViewModel.Year     = year.Value;

            var employeeId = (int)Session["UserId"];

            var employee = _db.Employees.Find(employeeId);

            var records = _db
                          .DailyCheckins
                          .Where(x => x.Employee.Id == employeeId &&
                                 x.Date.Value.Year == year && x.Date.Value.Month == month)
                          .ToList();

            if (records.Count == 0)
            {
                var daysInMonth = DateTime.DaysInMonth(year.Value, month.Value);

                var checkins = new List <DailyCheckin>();
                for (int day = 1; day <= daysInMonth; day++)
                {
                    var checkin = new DailyCheckin();
                    checkin.Date         = new DateTime(year.Value, month.Value, day);
                    checkin.EmployeeId   = employee.Id;
                    checkin.StatusId     = (int)StatusEnum.Init;
                    checkin.WorkingHours = 0;
                    checkin.Note         = "";
                    checkin.ModifiedDate = DateTime.Now;

                    checkins.Add(checkin);
                }
                checkinsViewModel.Checkins.AddRange(checkins);
                _db.DailyCheckins.AddRange(checkins);
                _db.SaveChanges();
                return(View(checkinsViewModel));
            }
            checkinsViewModel.Checkins.AddRange(records);
            return(View(checkinsViewModel));
        }
        public ActionResult CreateOrUpdateEmployee(Employee emp)
        {
            if (emp.Id == 0) // Create new
            {
                emp.IsActive = true;
                _timeKeepingContext.Employees.Add(emp);
            }
            else
            {
                var record = _timeKeepingContext.Employees.Find(emp.Id);
                record.Name     = emp.Name;
                record.PhoneNo  = emp.PhoneNo;
                record.Password = emp.Password;
                record.IsAdmin  = emp.IsAdmin;
            }

            _timeKeepingContext.SaveChanges();

            return(RedirectToAction("Index"));
        }