public JsonResult updateTimeTrack(CTimeTrack model) { string message = ""; if (Session["userId"] == null) { message = "Ej inloggad"; } if (message == "") { int userId = Convert.ToInt32(Session["userId"]); CTimeTrack ct = new CTimeTrack(); pdsTidRedLiveEntities db = new pdsTidRedLiveEntities(); try { if (model.timeTrackRowID == 0) { ct.addTimeTrack(model, userId); } else { ct.editTimeTrack(model, userId); } } catch (Exception ex) { message = ex.Message; } } return(Json(message, JsonRequestBehavior.AllowGet)); }
public JsonResult updateTimeTrack3(CTimeTrack model) { string message = ""; // 2018-04-03 KJBO int userId = Convert.ToInt32(Session["userId"]); CTimeTrack ct = new CTimeTrack(); pdsTidRedLiveEntities db = new pdsTidRedLiveEntities(); try { if (model.timeTrackRowID == 0) { ct.addTimeTrack(model, userId); /* timeTrackHead th = db.timeTrackHead.FirstOrDefault(x => x.customerID == model.customerID * && x.userId == userId && x.tDate == model.tDate); * if (th == null) * { * int ttHeadId = db.timeTrackHead.Max(x => x.timeTrackHeadID); * ttHeadId++; * th = new timeTrackHead(); * th.timeTrackHeadID = ttHeadId; * th.customerID = model.customerID; * th.userId = userId; * th.tDate = model.tDate; * db.timeTrackHead.Add(th); * db.SaveChanges(); * } * model.timeTrackHeadID = th.timeTrackHeadID; * int ttRowId = db.timeTrackRow.Max(x => x.timeTrackRowID); * ttRowId++; * timeTrackRow tr = new timeTrackRow(); * tr.timeTrackHeadID = model.timeTrackHeadID; * tr.timeTrackRowID = ttRowId; * tr.hours = model.hours; * tr.note = model.note; * tr.regDate = System.DateTime.Now; * tr.subProjectID = model.subProjectID; * db.timeTrackRow.Add(tr); * db.SaveChanges(); */ } else { ct.editTimeTrack(model, userId); /* * // First get the timeTrackHead to check if the date is the same as before * timeTrackHead th = db.timeTrackHead.FirstOrDefault(x => x.timeTrackHeadID == model.timeTrackHeadID); * if (th.tDate != model.tDate) * { * int customerId = th.customerID; * int ttHeadId = db.timeTrackHead.Max(x => x.timeTrackHeadID); * ttHeadId++; * th = new timeTrackHead(); * th.timeTrackHeadID = ttHeadId; * th.customerID = customerId; * th.userId = userId; * th.tDate = model.tDate; * db.timeTrackHead.Add(th); * db.SaveChanges(); * model.timeTrackHeadID = th.timeTrackHeadID; * * // Here we have work to do. First check if there are other timeTracks that uses the same timeTrackRow * int cntTTh = db.timeTrackRow.Where(x => x.timeTrackHeadID == model.timeTrackHeadID).Count(); * if (cntTTh == 1) * { * // In this case, only the current row points to this head and we can easily change the date without any problems * th.tDate = model.tDate; * db.SaveChanges(); * } * else * { * // Check if there exists one timeTrackHead with the same values and in that case use that * th = db.timeTrackHead.FirstOrDefault(x => x.customerID == model.customerID * && x.userId == userId && x.tDate == model.tDate); * if (th != null) * model.timeTrackHeadID = th.timeTrackHeadID; * else * { * int ttHeadId = db.timeTrackHead.Max(x => x.timeTrackHeadID); * ttHeadId++; * th = new timeTrackHead(); * th.timeTrackHeadID = ttHeadId; * th.customerID = model.customerID; * th.userId = userId; * th.tDate = model.tDate; * db.timeTrackHead.Add(th); * db.SaveChanges(); * model.timeTrackHeadID = th.timeTrackHeadID; * } * * } * * } * timeTrackRow tr = db.timeTrackRow.FirstOrDefault(x => x.timeTrackRowID == model.timeTrackRowID); * tr.timeTrackHeadID = model.timeTrackHeadID; * tr.subProjectID = model.subProjectID; * tr.hours = model.hours; * tr.note = model.note; * db.SaveChanges(); * * } * * subProject2 p = null; * if (model.subProjectID == 0) * { * p = new subProject2(); * p.projectID = model.projectID; * } * else * p = db.subProject2.SingleOrDefault(x => x.subProjectID == model.subProjectID); * p.subProjectName = model.subProjectName; * if (model.subProjectID == 0) * db.subProject2.Add(p); * db.SaveChanges(); */ } } catch (Exception ex) { message = ex.Message; } return(Json(message, JsonRequestBehavior.AllowGet)); }