public async Task <ActionResult <dynamic> > Employee(SearchVM search) { List <EmployeeAttendanceVM> employeeAttendances = new List <EmployeeAttendanceVM>(); var employees = await _context.ProjectEmployee.Where(x => x.ProjectId == search.Project).Include(y => y.Employee).ThenInclude(y => y.CategoryNavigation).Include(y => y.EmployeeAttendance).ToListAsync(); DateTime[] last7Days = Enumerable.Range(1, 7).Select(i => DateTime.Now.Date.AddDays(-i)).ToArray(); foreach (var emp in employees) { EmployeeAttendanceVM _empAttendance = new EmployeeAttendanceVM() { EmployeeCode = emp.Employee.EmployeeNumber, EmployeeId = emp.Employee.Id, EmployeeName = emp.Employee.Name, StartDate = emp.Employee.StartDate, Phone = emp.Employee.Phone, Position = emp.Employee.CategoryNavigation.Category, ProjectEmployeeId = emp.Id, ProjectId = emp.ProjectId, //EmployeeAttendance = emp.EmployeeAttendance.Where(x => last7Days.Contains(x.Date)).Select(x => new EmployeeAttendance //{ // Attendance = x.Attendance , // Date = x.Date //}).ToArray(), TodayAttendance = emp.EmployeeAttendance.Where(x => x.Date.Date == DateTime.Now.Date).Select(p => p.Attendance).FirstOrDefault(), Month1Count = emp.EmployeeAttendance.Where(x => x.Date.Month == (DateTime.Now.Month - 1) && x.Date.Year == DateTime.Now.Year).Count(), Month2Count = emp.EmployeeAttendance.Where(x => x.Date.Month == (DateTime.Now.Month - 2) && x.Date.Year == DateTime.Now.Year).Count(), Month3Count = emp.EmployeeAttendance.Where(x => x.Date.Month == (DateTime.Now.Month - 3) && x.Date.Year == DateTime.Now.Year).Count() }; List <EmployeeAttendance> _attendanceList = new List <EmployeeAttendance>();; foreach (DateTime d in last7Days) { string _attendance = emp.EmployeeAttendance.Where(x => x.Date.Date == d.Date).Select(p => p.Attendance).FirstOrDefault(); _attendanceList.Add(new EmployeeAttendance() { Attendance = string.IsNullOrWhiteSpace(_attendance) ? "-" : _attendance }); } _attendanceList.Reverse(); _empAttendance.EmployeeAttendance = _attendanceList.ToArray(); employeeAttendances.Add(_empAttendance); } // check todays attendance done var todayAttendance = await _context.EmployeeAttendance.Where(p => p.Date.Date == DateTime.Now.Date && p.ProjectEmployee.ProjectId == search.Project).ToListAsync(); return(new { Attendance = employeeAttendances, AttendanceDone = todayAttendance.Count() }); }
public object CreateEmployeeAttendance([FromBody] EmployeeAttendanceVM model) { object result = null; string message = ""; string errorcode = ""; string excp = ""; if (model == null) { return(BadRequest()); } using (_context) { using (var _ctxTransaction = _context.Database.BeginTransaction()) { try { if (model.SelectedDate == null) { return(BadRequest()); } DateTime TillDate = GetTillDate(model.SelectedDate.Value.Month, model.SelectedDate.Value.Year); if (model.SelectedDate != null && model.SelectedShiftId != null) { PayrollShiftWiseAttendance objAttendance = new PayrollShiftWiseAttendance(); //DateTime AttendanceDate = Convert.ToDateTime(model.SelectedDate); DateTime?AttendanceDate = model.SelectedDate; Shift shift = _context.Shift.Where(y => y.Id == model.SelectedShiftId).FirstOrDefault(); var Result = DeleteEmployeeAttendance(model.SelectedShiftId, AttendanceDate); if (Result == 1) { return(BadRequest()); } foreach (var item in model.EmployeeAttendanceGridData) { EmployeeAttendance _EmployeeAttendance = new EmployeeAttendance(); if (item.IsAbsent == true) { int LookupId = 9; int leavetype = _context.LookupData.Where(z => z.LookupId == LookupId).FirstOrDefault() != null?_context.LookupData.Where(z => z.LookupId == LookupId).FirstOrDefault().RowId : 0; if (leavetype == 0) { LookupData objLookupData = new LookupData(); objLookupData.LookupId = 9; objLookupData.RowId = 1; objLookupData.FieldName = "LeaveTypeTitle"; objLookupData.Value = "casual leave"; _context.LookupData.Add(objLookupData); LookupData objLookupData1 = new LookupData(); objLookupData1.LookupId = 9; objLookupData1.RowId = 1; objLookupData1.FieldName = "LeavePeriod"; objLookupData1.Value = "Yearly"; _context.LookupData.Add(objLookupData1); LookupData objLookupData2 = new LookupData(); objLookupData2.LookupId = 9; objLookupData2.RowId = 1; objLookupData2.FieldName = "NumberOfLeave"; objLookupData2.Value = "12"; _context.LookupData.Add(objLookupData2); _context.SaveChanges(); leavetype = objLookupData.RowId; } _EmployeeAttendance.EmployeeId = item.EmployeeId; _EmployeeAttendance.DepartmentId = item.DepartmentId; _EmployeeAttendance.FromDate = model.SelectedDate; _EmployeeAttendance.Attendance = "Absent"; _EmployeeAttendance.LeaveReason = ""; _EmployeeAttendance.LeaveTypeId = leavetype; _EmployeeAttendance.IsOnHalfDay = 1; _EmployeeAttendance.IsChanged = false; _EmployeeAttendance.CreatedDate = DateTime.Now; _EmployeeAttendance.ShiftId = model.SelectedShiftId; _context.EmployeeAttendance.Add(_EmployeeAttendance); } } } //_context.CheckList.Add(model); //await _ctx.SaveChangesAsync(); _context.SaveChanges(); _ctxTransaction.Commit(); message = "Saved Successfully"; errorcode = "0"; } catch (Exception e) { _ctxTransaction.Rollback(); e.ToString(); message = "Saved Error"; errorcode = "1"; excp = e.ToString(); } result = new { error = errorcode, msg = message, excp = excp }; } } return(result); }