Reputation: 105
So i have a database that is connected with a device at the door that employees check when they come in and go out now i can get the history of statuses but i wanted to know how can I calculate the working hours for a day a week and a month for each employee. I got a this class for employee .
public class Employee : BaseEntity
{
public Employee()
{
this.HistoryOfStatuses = new List<Checkinout>();
this.TodayCheckedStatus = new List<Checkinout>();
}
public string Name { get; set; }
public string Department { get; set; }
public string CardNumber { get; set; }
public string Status { get; set; }
public byte[] Picture { get; set; }
public Checkinout ActualCheckinStatuse { get; set; }
public List<Checkinout> HistoryOfStatuses { get; set; }
public List<Checkinout> TodayCheckedStatus { get; set; }
public int UserId { get; internal set; }
And this is the checking status class
public class Checkinout : BaseEntity
{
public Checkinout()
{
}
public int EmployeeId { get; set; }
public string CheckStatus { get; set; }
public DateTime CheckTime { get; set; }
public Employee EmployeeObject { get; set; }
}
My controller looks something like this
public IActionResult Index()
{
using (var context = new RSAT.Api.Data.Proxy.ATT2018NOVUSContext())
{
var baseViewModel = base.GetLayoutViewModel();
var viewModel = new HomeViewModel()
{
User = baseViewModel.User,
RoleCollection = baseViewModel.RoleCollection,
TableCollection = baseViewModel.TableCollection,
//Olap = baseViewModel.Olap,
//Localization = baseViewModel.Localization,
EmployeeCollection = (from userinfo in context.Userinfo
join department in context.Dept on userinfo.Deptid equals department.Deptid
select new Employee()
{
Id = userinfo.Userid,
Name = userinfo.Name,
Picture = userinfo.Picture,
Department = department.DeptName,
CardNumber = userinfo.CardNum,
Status = userinfo.UserFlag.ToString(),
ActualCheckinStatuse = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).FirstOrDefault()
}).ToList()
};
return View(viewModel);
}
}
public IActionResult WorkingHours()
{
var inTime = "10:00";
var outTime = DateTime.Now.TimeOfDay;
var totalHours = Convert.ToDateTime(inTime).TimeOfDay.Subtract(outTime);
}
I wanted someones help to clear me out how to do this and how can i connect the last code in my controller .
Upvotes: 0
Views: 1242
Reputation: 699
Does your model have a property somewhere determining whether an entry is an in or out check? You will need this.
What you can do now is create a method that takes two dates that form a range. You can return the total work hours inside this range. You can then easily create a method for a week, month, year, where you specify the date ranges to this method.
Here's some pseudocode to get you started.
decimal GetRangeWorkHours(Employee employee, DateTime startOfRange, DateTime endOfRange){
//1. Ask the database for all the entries from this employee within the range of dates.
//2. Sort them by date, figuring out what entry the start/end is. You might get some edgecases where employees clock out midday and then clock back in later on the day. You can work this out using TimeSpans.
//3. Calculate the difference between all the now paired in/out entries.
//4. Sum the results of the previous step, return the result.
}
Consuming for week/month/year becomes easy
Employee someEmployee = new Employee(/*wetherever you use to identify employees*/);
//Replace these 2 values with whatever you need to provide your user with.
DateTime startOfRange = new DateTime(2019, 1, 1);
DateTime endOfRange = startOfRange.AddDays(7);
decimal workHours = GetRangeWorkHours(someEmployee, startOfRange, endOfRange);
Upvotes: 1