public void SaveBalanceWithoutBookings(DateTime dateTime, int refUserId) { var lastBalance = TimeBalances.GetLastByDateAndRefUserId(dateTime, refUserId); var obligatoryHours = TimeObligatoryHours.GetByRefUserId(refUserId); if (lastBalance != null) { if (obligatoryHours != null) { var obligatoryHoursDay = obligatoryHours.Single(x => x.DayOfWeek == dateTime.DayOfWeek).HoursPerDay; var existingItem = TimeBalances.GetByDateAndRefUserId(dateTime, refUserId); if (existingItem != null) { existingItem.Balance = lastBalance.Balance - obligatoryHoursDay; TimeBalances.Update(existingItem); } else { TimeBalances.Insert(new TimeBalance(refUserId, dateTime, lastBalance.Balance - obligatoryHoursDay)); } } } else { if (obligatoryHours != null) { var obligatoryHoursDay = obligatoryHours.Single(x => x.DayOfWeek == dateTime.DayOfWeek).HoursPerDay; TimeBalances.Insert(new TimeBalance(refUserId, dateTime, (obligatoryHoursDay * (-1)))); } } }
private void GetTimeBookings() { TimeBookingList = timeBookingManager.GetBookingItemsForMonth(DateTime.Now, SelectedUserId).ToSvenTechCollection(); var balanceItem = TimeBalances.GetLastByDateAndRefUserId(DateTime.Now, SelectedUserId); if (balanceItem != null) { BalanceInfoBoxViewModel.Value = (decimal)balanceItem.Balance; } }
public IEnumerable <TimeBookingDayItem> GetBookingItemsForMonth(DateTime dateTime, int refUserId) { List <TimeBooking> bookingsForMonth = TimeBookings.GetDataForMonth(dateTime, refUserId); var timeBookingDayItems = CreateDayItems(bookingsForMonth).ToList(); var startDate = new DateTime(dateTime.Year, dateTime.Month, 1); while (startDate.Month == dateTime.Month && startDate.Date <= DateTime.Now.Date) { if (!timeBookingDayItems.Any(x => x.BookingDate.Date == startDate.Date)) { TimeBookingDayItem timeBookingDayItem = new TimeBookingDayItem(); timeBookingDayItem.BookingDate = startDate; timeBookingDayItem.WorkingHours = new TimeSpan(0); timeBookingDayItem.BreaktimeHours = new TimeSpan(0); timeBookingDayItem.ObligatoryHours = GetObgligaryHoursForDay(startDate, refUserId); var balanceForDay = TimeBalances.GetByDateAndRefUserId(startDate, refUserId); if (balanceForDay != null) { timeBookingDayItem.Balance = balanceForDay.Balance; } else { var lastBalance = TimeBalances.GetLastByDateAndRefUserId(startDate, refUserId); if (lastBalance != null) { timeBookingDayItem.Balance = lastBalance.Balance; } else { timeBookingDayItem.Balance = 0; } } timeBookingDayItems.Add(timeBookingDayItem); } startDate = startDate.AddDays(1); } return(timeBookingDayItems.OrderBy(x => x.BookingDate)); }
private void SaveBalance(TimeBooking timeBooking) { var bookings = TimeBookings.GetDataForDay(timeBooking.BookingTime, timeBooking.RefUserId); var hours = timeBooking.BookingTime - bookings.First().BookingTime; var obligatoryHours = TimeObligatoryHours.GetByRefUserId(timeBooking.RefUserId); if (obligatoryHours.Count > 0) { var day = obligatoryHours.SingleOrDefault(x => x.DayOfWeek == timeBooking.BookingTime.DayOfWeek); if (day != null) { var dayItem = CreateDayItem(bookings, day); var balanceValue = dayItem.WorkingHours.TotalHours - day.HoursPerDay; var lastItem = TimeBalances.GetLastByDateAndRefUserId(timeBooking.BookingTime, timeBooking.RefUserId); if (lastItem != null) { balanceValue += lastItem.Balance; } var existingItem = TimeBalances.GetByDateAndRefUserId(timeBooking.BookingTime, timeBooking.RefUserId); if (existingItem != null) { existingItem.Balance = balanceValue; TimeBalances.Update(existingItem); } else { TimeBalance timeBalance = new TimeBalance(timeBooking.RefUserId, timeBooking.BookingTime, balanceValue); TimeBalances.Insert(timeBalance); } } } }
private TimeBookingDayItem CreateDayItem(IEnumerable <TimeBooking> timeBookings, TimeObligatoryHour timeObligatoryHour) { if (timeBookings == null || timeObligatoryHour == null) { return(null); } List <TimeBooking> timeBookingList = timeBookings.ToList(); if (!timeBookings.Any(x => x.TimeBookingType == TimeBookingType.Logout)) { return(null); } DateTime login = DateTime.MinValue; DateTime logout = DateTime.MinValue; DateTime startBreak = DateTime.MinValue; DateTime endBreak = DateTime.MinValue; TimeSpan sumWorkingTime = new TimeSpan(0); TimeSpan sumBreakTime = new TimeSpan(0); for (int i = 0; i < timeBookingList.Count; i++) { switch (timeBookingList[i].TimeBookingType) { case TimeBookingType.Login: login = timeBookingList[i].BookingTime; break; case TimeBookingType.Logout: logout = timeBookingList[i].BookingTime; if (logout > login) { sumWorkingTime += logout.Subtract(login); } break; case TimeBookingType.StartBreak: startBreak = timeBookingList[i].BookingTime; break; case TimeBookingType.EndBreak: endBreak = timeBookingList[i].BookingTime; if (logout > login) { sumBreakTime += logout.Subtract(login); } break; } } TimeBookingDayItem timeBookingDayItem = CorrectWorkingTime(sumWorkingTime, sumBreakTime); timeBookingDayItem.BookingDate = timeBookingList[0].BookingTime.Date; timeBookingDayItem.ObligatoryHours = timeObligatoryHour.HoursPerDay; var timeBalance = TimeBalances.GetByDateAndRefUserId(timeBookingList[0].BookingTime, timeBookingList[0].RefUserId); if (timeBalance != null) { timeBookingDayItem.Balance = timeBalance.Balance; } return(timeBookingDayItem); }