public void Start() { //current user var currentUser = _userManager.GetUserAsync(HttpContext.User).Result; //check if DayRecord exist with Date.Today var dayToday = _context.DayRecords .Where(c => c.Data.Date == DateTime.Now.Date).FirstOrDefault(c => c.UserId == currentUser.Id); try { //if not exist DayRecord for today - create new if (dayToday == null) { DayRecord todayRecord = new DayRecord() { Data = DateTime.Now, User = currentUser, UserId = currentUser.Id }; _context.DayRecords.Add(todayRecord); _context.SaveChanges(); //create TimeRecord TimeRecord timeRecordNew = new TimeRecord() { StartTime = todayRecord.Data, DayRecord = todayRecord, DayRecordId = todayRecord.Id }; _context.TimeRecords.Add(timeRecordNew); _context.SaveChanges(); } else { //if DayRecord already exist in db // check if already exist open TimeRecord (i.e. 24/12/2020 9:01 NULL) // for defend db from second registration of already registed TimeRecord // Double registration happens on telefone on double click, because system react very slow // if open TimeRecord exists - do nothing, redirect to sucess Response Page // If do not exists - create new TimeRecord and redirect to sucess Response Page var timeRecordsTodayLast = _context.TimeRecords.Where(c => c.DayRecordId == dayToday.Id) .OrderBy(c => c.StartTime).ToList().Last(); if (timeRecordsTodayLast.EndTime != null) { TimeRecord timeRecordNew = new TimeRecord() { StartTime = DateTime.Now, DayRecord = dayToday, DayRecordId = dayToday.Id }; _context.TimeRecords.Add(timeRecordNew); _context.SaveChanges(); //DayRecord and at least one TimeRecord already exist in db, //then on Start button click function create another TimeRecord, it means that we have an Interval //create Interval var timeRecordsTodayList = _context.TimeRecords.Where(c => c.DayRecordId == dayToday.Id) .OrderBy(c => c.StartTime).ToList(); //Take penultimate element var lastTimeRecordsToday = timeRecordsTodayList[timeRecordsTodayList.Count - 2]; var intervalNew = new IntervalRecord() { StartTime = (DateTime)lastTimeRecordsToday.EndTime, EndTime = (DateTime)timeRecordNew.StartTime, DayRecord = dayToday, DayRecordId = dayToday.Id }; _context.IntervalRecords.Add(intervalNew); _context.SaveChanges(); } } } catch (Exception) { TempData["Failure"] = "Algo correu errado, por favor, tente novamente ou contacte Administrador do Sistema."; } TempData["Success"] = "Obrigada. As Horas estão registados com sucesso."; }
//This DayRecord has one of TimeRecords (last) that is not closed //public bool RemoveAllUncloseTimeEntries(DayRecord record) //{ // var IsRemovedTotaly = false; // //get all TimeRecords with this DayRecord Id // var timeRecordsList = _context.TimeRecords.Where(c => c.DayRecordId == record.Id).ToList(); // //if length = 1, then remove TimeRecord and DayRecord // if(timeRecordsList.Count()==1) // { // var timeRecordToRemove = timeRecordsList.First(); // _context.TimeRecords.Remove(timeRecordToRemove); // _context.DayRecords.Remove(record); // _context.SaveChanges(); // IsRemovedTotaly = true; // } // //if length > 1, then remove just last unclose TimeRecord // if(timeRecordsList.Count()>1) // { // //remove last unclosed TimeRecord // var timeRecordToRemove = timeRecordsList.Last(); // _context.TimeRecords.Remove(timeRecordToRemove); // //remove last Interval // var intervalsList = _context.IntervalRecords.Where(c => c.DayRecordId == record.Id).ToList(); // var lastIntervalToRemove = intervalsList.Last(); // _context.IntervalRecords.Remove(lastIntervalToRemove); // _context.SaveChanges(); // } // return IsRemovedTotaly; //} public void SetNormalizeTimetableInTimerLost(DayRecord record) { var user = _userManager.FindByIdAsync(record.UserId).Result; var startWorkTime = (DateTime)user.StartWorkTime; var endWorkTime = (DateTime)user.EndWorkTime; var startLunchTime = user.StartLunchTime; var endLunchTime = user.EndLunchTime; //change db.DayRecord.Data value - i.e DateTime.Add(TimeSpan time) record.Data = record.Data.Date.Add(startWorkTime.TimeOfDay); record.StartDayDelayExplanation = null; record.EndDayDelayExplanation = null; //find all TimeRecords (with this record.Id) and remove it foreach (var timerecord in _context.TimeRecords) { if (timerecord.DayRecordId == record.Id) { _context.TimeRecords.Remove(timerecord); } } //remove Intervals with this Record.Id foreach (var intervalrecord in _context.IntervalRecords) { if (intervalrecord.DayRecordId == record.Id) { _context.IntervalRecords.Remove(intervalrecord); } } //Add two normalized entries to db.TimeRecords 9:00 - 13:00 and 14:00 - 18:00 //var timeSpan1 = new TimeSpan(13, 0, 0); //var startlunch = startLunchTime == null ? timeSpan1 : ((DateTime)startLunchTime).TimeOfDay; //var timeSpan2 = new TimeSpan(14, 0, 0); //var endLunch = endLunchTime == null ? timeSpan2 : ((DateTime)endLunchTime).TimeOfDay; //if startLunchTime and endLunchTime are not empty -> add two TimeRecords + Interval if (startLunchTime.HasValue && endLunchTime.HasValue) { var newTimeRecordBeforeLunch = new TimeRecord() { StartTime = record.Data.Date.Add(startWorkTime.TimeOfDay), EndTime = record.Data.Date.Add(((DateTime)startLunchTime).TimeOfDay), DayRecordId = record.Id }; var newTimeRecordAfterLunch = new TimeRecord() { StartTime = record.Data.Date.Add(((DateTime)endLunchTime).TimeOfDay), EndTime = record.Data.Date.Add(endWorkTime.TimeOfDay), DayRecordId = record.Id }; _context.TimeRecords.Add(newTimeRecordBeforeLunch); _context.TimeRecords.Add(newTimeRecordAfterLunch); //add new normalize Inerval Record - 13:00 -14:00 Lunch var normalizeInterval = new IntervalRecord() { StartTime = record.Data.Date.Add(((DateTime)startLunchTime).TimeOfDay), EndTime = record.Data.Date.Add(((DateTime)endLunchTime).TimeOfDay), DayRecordId = record.Id, IntervalType = "Almoço" }; _context.IntervalRecords.Add(normalizeInterval); _context.SaveChanges(); } //if startLunchTime and endLunchTime are empty->add TimeRecord without interval else { var newTimeRecordWithoutLunch = new TimeRecord() { StartTime = record.Data.Date.Add(startWorkTime.TimeOfDay), EndTime = record.Data.Date.Add(endWorkTime.TimeOfDay), DayRecordId = record.Id }; _context.TimeRecords.Add(newTimeRecordWithoutLunch); _context.SaveChanges(); } }