public bool checkDuplicateRecord(ShiftRecordModels model) { List<ShiftRecord> records = db.ShiftRecords.Where(s => s.volunteerId == model.volunteerId && s.startOn != null && s.endOn != null && s.recordId != model.recordId).ToList(); if (records != null) { foreach (var item in records) { // 1. Case: // // TS-------TE // BS------BE // // TS is after BS but before BE if (model.startOn != null) { if (model.startOn >= item.startOn && model.startOn < item.endOn) { return true; } } // 2. Case // // TS-------TE // BS---------BE // // TE is before BE but after BS if (model.endOn != null) { if (model.endOn > item.startOn && model.endOn <= item.endOn) { return true; } } // 3. Case // // TS----------TE // BS----BE // // TS is before BS and TE is after BE if (model.startOn != null && model.endOn != null) { if (model.startOn <= item.startOn && model.endOn >= item.endOn) { return true; } } } } return false; }
private ShiftRecordModels convertToShiftRecordModels(ShiftRecord model) { ShiftRecordModels record = new ShiftRecordModels(); record.volunteerId = model.volunteerId; record.teamName = model.teamName; record.startOn = model.startOn; record.endOn = model.endOn; record.recordId = model.recordId; record.earnHours = model.earnHours; record.earnedBucks = model.earnedBucks; return record; }
private decimal CalculateExtraEarnHours(ShiftRecordModels model) { int maxHours = 5; int doublePayStartHours = 22; DateTime tStart = model.startOn; DateTime tEnd = DateTime.Now; TimeSpan ts = new TimeSpan(doublePayStartHours, 0, 0); if (model.endOn != null) { tEnd = (System.DateTime)model.endOn; } if ((tStart.Day == tEnd.Day) && (tStart.Hour < doublePayStartHours && tEnd.Hour < doublePayStartHours)) { // tStar and tEnd at the same day and both are before 10 pm return 0; } else if (tStart.Hour >= doublePayStartHours && tEnd >= tStart.AddHours(maxHours)) { // if tStart after 10 pm and tEnd - tStar > 4 hours, return max hours return maxHours; } else if ((tStart.Hour <= doublePayStartHours && tEnd.Hour >= doublePayStartHours) || (tEnd.Day > tStart.Day)) { // if tStart before 10 pm, but tEnd after 10 pm // or if tEnd Day end in another day // set start time become 10 pm tStart = tStart.Date + ts; TimeSpan difference = tEnd - tStart; decimal diffHours = (decimal)Math.Round(difference.TotalHours, 1, MidpointRounding.AwayFromZero); if (diffHours >= maxHours) { // if hours > max hours, retunr max hours return maxHours; } else { return diffHours; } } return 0; }
private decimal CalculateEarnHours(ShiftRecordModels model) { DateTime tStart = model.startOn; DateTime tEnd = DateTime.Now; if (model.endOn != null) { tEnd = (System.DateTime)model.endOn; } TimeSpan difference = tEnd - tStart; decimal diffHours = (decimal)Math.Round(difference.TotalHours, 1, MidpointRounding.AwayFromZero); return diffHours; }
public ActionResult VolunteerHistoryCreate(ShiftRecordModels model) { model.startOn = getSignInOutTime(model.startOn); bool isDuplicationRecord = checkDuplicateRecord(model); if (isDuplicationRecord == true) { int volunteerId = (int)model.volunteerId; ViewBag.Message = duplicate_time_frame_msg; decimal earnedTotalBucks = getTotalEarnedBucks(volunteerId); ViewBag.EarnedTotalBucks = earnedTotalBucks; decimal earnedTotalHours = getTotalEarnedHours(volunteerId); ViewBag.EarnedTotalHours = earnedTotalHours; ViewBag.LastRecord = GetLastShiftRecord(volunteerId); ViewBag.Volunteer = db.Volunteers.Find(volunteerId); return View(GetVolunteerShiftRecords(volunteerId)); } else { ShiftRecord target = new ShiftRecord(); target.teamName = model.teamName; target.volunteerId = model.volunteerId; target.startOn = model.startOn; target.createOn = DateTime.Now; target.createBy = WebSecurity.CurrentUserName; target.modifiedOn = DateTime.Now; target.modifiedBy = WebSecurity.CurrentUserName; target.earnedBucks = 0; target.earnHours = 0; db.ShiftRecords.Add(target); db.SaveChanges(); return RedirectToAction("VolunteerHistory/" + model.volunteerId, "Volunteer"); } }
public ActionResult VolunteerHistory(ShiftRecordModels model) { ViewBag.Message = ""; model.endOn = getSignInOutTime((DateTime)model.endOn); Definition def = db.Definitions.Where(d => d.keyName == "BucksPerShift").FirstOrDefault(); var calOrgHours = from r in db.ShiftRecords where r.volunteerId == model.volunteerId group r.earnHours by r.earnHours into data select new { earnedTotalHours = data.Sum() }; decimal oldHoursTotal = calOrgHours.AsEnumerable().Sum(r => r.earnedTotalHours); Definition defPlateauHours = db.Definitions.Where(d => d.keyName == "PlateauHours").FirstOrDefault(); int plateauHours = int.Parse(defPlateauHours.keyValue); bool isDuplicateRecord = checkDuplicateRecord(model); if ((model.startOn < model.endOn) && (isDuplicateRecord == false)) { if (model != null && ModelState.IsValid) { { var target = db.ShiftRecords.Find(model.recordId); target.volunteerId = model.volunteerId; target.teamName = model.teamName; target.endOn = model.endOn; target.modifiedOn = DateTime.Now; target.modifiedBy = WebSecurity.CurrentUserName; target.earnHours = CalculateEarnHours(model); target.earnedBucks = decimal.Parse(def.keyValue) * (target.earnHours + CalculateExtraEarnHours(model)); db.Entry(target).State = EntityState.Modified; db.SaveChanges(); } } } decimal earnedTotalBucks = getTotalEarnedBucks((int)model.volunteerId); ViewBag.EarnedTotalBucks = earnedTotalBucks; decimal earnedTotalHours = getTotalEarnedHours((int)model.volunteerId); ViewBag.EarnedTotalHours = earnedTotalHours; if (model.startOn >= model.endOn) { ViewBag.Message = startTime_endTime_error_msg; } else if (isDuplicateRecord == true) { ViewBag.Message = duplicate_time_frame_msg; } else if ((oldHoursTotal < plateauHours) && (earnedTotalHours >= plateauHours)) { Definition defHonorMessage = db.Definitions.Where(d => d.keyName == "HonorMessage").FirstOrDefault(); ViewBag.Message = defHonorMessage.keyValue; } ViewBag.LastRecord = GetLastShiftRecord((int)model.volunteerId); ViewBag.Volunteer = db.Volunteers.Find((int)model.volunteerId); return View(GetVolunteerShiftRecords((int)model.volunteerId)); }
public ActionResult ShiftRecord_Update([DataSourceRequest] DataSourceRequest request, ShiftRecordModels model) { Definition def = db.Definitions.Where(d => d.keyName == "BucksPerShift").FirstOrDefault(); if (model != null && ModelState.IsValid) { { var target = db.ShiftRecords.Find(model.recordId); target.volunteerId = model.volunteerId; target.teamName = model.teamName; target.startOn = getSignInOutTime((DateTime)model.startOn); target.endOn = getSignInOutTime((DateTime)model.endOn); target.modifiedOn = DateTime.Now; target.modifiedBy = WebSecurity.CurrentUserName; target.earnHours = CalculateEarnHours(model); target.earnedBucks = decimal.Parse(def.keyValue) * (target.earnHours + CalculateExtraEarnHours(model)); db.Entry(target).State = EntityState.Modified; db.SaveChanges(); } } return Json(ModelState.ToDataSourceResult()); }
public ActionResult ShiftRecord_Delete([DataSourceRequest] DataSourceRequest request, ShiftRecordModels model) { if (model != null && ModelState.IsValid) { ShiftRecord record = db.ShiftRecords.Find(model.recordId); if (record != null) { db.ShiftRecords.Remove(record); db.SaveChanges(); } } return Json(ModelState.ToDataSourceResult()); }
public ShiftRecordModels GetLastShiftRecord(int id = 0) { ShiftRecord model = db.ShiftRecords.Where(a => a.endOn == null && a.volunteerId == id).FirstOrDefault(); if (model == null) { return null; } else { ShiftRecordModels target = new ShiftRecordModels(); target.volunteerId = model.volunteerId; target.teamName = model.teamName; target.recordId = model.recordId; target.startOn = model.startOn; return target; } }