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 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); } } } }