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; }
public ActionResult VolunteerHistoryEdit(ShiftRecord model) { bool isDuplicateRecord = false; model.startOn = getSignInOutTime((DateTime)model.startOn); model.endOn = getSignInOutTime((DateTime)model.endOn); isDuplicateRecord = checkDuplicateRecord(model); if ((model.startOn >= model.endOn) || (isDuplicateRecord == true)) { if (isDuplicateRecord == true) { ViewBag.Message = duplicate_time_frame_msg; } else { ViewBag.Message = startTime_endTime_error_msg; } int volunteerId = (int)model.volunteerId; decimal earnedTotalBucks = getTotalEarnedBucks(volunteerId); ViewBag.EarnedTotalBucks = earnedTotalBucks; decimal earnedTotalHours = getTotalEarnedHours(volunteerId); ViewBag.EarnedTotalHours = earnedTotalHours; ViewBag.LastRecord = model; ViewBag.Volunteer = db.Volunteers.Find(volunteerId); return View(GetVolunteerShiftRecords(volunteerId)); } else { Definition def = db.Definitions.Where(d => d.keyName == "BucksPerShift").FirstOrDefault(); ShiftRecord record = db.ShiftRecords.Find(model.recordId); if (record != null) { var target = record; 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(convertToShiftRecordModels(model)); target.earnedBucks = decimal.Parse(def.keyValue) * (target.earnHours + CalculateExtraEarnHours(convertToShiftRecordModels(model))); db.Entry(target).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("VolunteerHistory/" + model.volunteerId, "Volunteer"); } } return View(); }
public bool checkDuplicateRecord(ShiftRecord 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; }
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"); } }