public List<ShiftLog> LogWorkingHours(string employeeID) { connection.Open(); var getTotalRecords = connection.CreateCommand(); getTotalRecords.CommandText = "select guid guid, dtcreated DTCreated, type, comment from Log where resource='" + employeeID + "' and type in (0,1) order by dtcreated ASC ;"; getTotalRecords.CommandType = CommandType.Text; SqliteDataReader sqlite_datareader = getTotalRecords.ExecuteReader(); List<Log> logList = sqlite_datareader.Cast<IDataRecord>() .Select(dr => new Log { OID = dr["guid"].ToString(), Type = (LogType)int.Parse(dr["Type"].ToString()), //Resource = (Employee)dr["Resource"], Comment = (string)dr["Comment"], DTCreated = new DateTime(long.Parse(dr["DTCreated"].ToString())) }).ToList(); connection.Close(); IShiftService _shiftService = new ShiftService(); List<Shift> shiftList = _shiftService.GetShifts(); List<ShiftLog> shiftLogList = new List<ShiftLog>(); for (int a=0; a< logList.Count;a++) { ShiftLog sh = new ShiftLog(); sh.TimeFrom = logList[a].DTCreated; // Fetch rate for this shift from comments if (!string.IsNullOrEmpty(logList[a].Comment)) sh.Rate = double.Parse(logList[a].Comment); if (a!= logList.Count-1 && logList[a + 1].Type == LogType.Logout) { // in case worker logs in and out we skip second iteration. sh.TimeTill = logList[a + 1].DTCreated; TimeSpan durationTmsp = sh.TimeTill.TimeOfDay - sh.TimeFrom.TimeOfDay; sh.Hours = Math.Round(durationTmsp.TotalHours,2); sh.DisplayHours = string.Format("{0}:{1}", durationTmsp.Hours, durationTmsp.Minutes); a++; } else { // If worker didnt log out set default date span Shift ss = shiftList.FirstOrDefault(shft => shft.Category == EventCategory.WorkingTime); TimeSpan durationTmsp = ss.EndTime - sh.TimeFrom.TimeOfDay; sh.Hours = Math.Round(durationTmsp.TotalHours,2); sh.DisplayHours = string.Format("{0}:{1}", durationTmsp.Hours, durationTmsp.Minutes); sh.TimeTill = sh.TimeFrom.AddMinutes(durationTmsp.TotalMinutes);// Temporary; TODO: add shift dictionary to get default working hours } shiftLogList.Add(sh); } return shiftLogList; }
/// <summary> /// Log in or log out Employee /// </summary> /// <param name="workerID"></param> /// <param name="toLogIn"></param> /// <returns></returns> public DateTime LogTime(string employeeID, bool toLogIn) { double EmployeeRate = 0; if (IsLoggedIn(employeeID) && toLogIn) throw new Exception("employee already logged in"); if (IsOnLeave(employeeID)) throw new Exception("Employee is on leave"); IShiftService _shiftService = new ShiftService(); List<Shift> shiftList = _shiftService.GetShifts(); Shift ss = shiftList.FirstOrDefault(shft => shft.Category == EventCategory.WorkingTime); if (ss.StartTime > DateTime.Now.TimeOfDay && ss.EndTime < DateTime.Now.TimeOfDay) throw new Exception("loggin outside working hours"); connection.Open(); //var getRecords = connection.CreateCommand(); var getTotalRecords = connection.CreateCommand(); getTotalRecords.CommandText = "select rate from Employee where guid='"+ employeeID + "'"; getTotalRecords.CommandType = CommandType.Text; EmployeeRate = Convert.ToDouble(getTotalRecords.ExecuteScalar()); if (EmployeeRate > 0) { using (var transaction = connection.BeginTransaction()) { var insertCmd = connection.CreateCommand(); insertCmd.CommandText = "INSERT INTO Log VALUES('"+Guid.NewGuid().ToString()+"',"+ DateTime.Now.Ticks + ",'"+ (toLogIn?(int)LogType.Loggin: (int)LogType.Logout)+"','"+ employeeID + "','"+ ((toLogIn)?EmployeeRate.ToString():"") + "')"; insertCmd.ExecuteNonQuery(); insertCmd.CommandText = "Update Employee set LastLoggedIn='"+DateTime.Now.Ticks + "' where guid='"+ employeeID + "'"; insertCmd.ExecuteNonQuery(); transaction.Commit(); } } connection.Close(); return DateTime.Now; }