private string Save(int id,HttpContext context, bool changeMinutes = false) { int reasonId; string reasonCode, comment, line, client; DateTime startdatetime, enddatetime; decimal minutes; int throughputId, option1Id, option2Id = -1; reasonCode = request.Form["reasonCode"]; comment = request.Form["comment"]; line = request.Form["line"]; client = _currentClient; line = line.Replace('#', ' '); int.TryParse(request.Form["throughput"], out throughputId); int.TryParse(request.Form["option1"], out option1Id); int.TryParse(request.Form["option2"], out option2Id); if (!DateTime.TryParse(request.Form["startdatetime"], out startdatetime)) { context.Response.Write("start date time is empty."); return "false"; } if (!int.TryParse(request.Form["reasonId"], out reasonId)) { context.Response.Write("reason id is empty."); return "false"; } if (request.Form["minutes"] != "tv") { if (!decimal.TryParse(request.Form["minutes"], out minutes)) { context.Response.Write("minutes id is empty."); return "false"; } } else { minutes = -1; } if (string.IsNullOrEmpty(line)) { context.Response.Write("line is empty."); return "false"; } if (string.IsNullOrEmpty(reasonCode)) { context.Response.Write("reasonCode is empty."); return "false"; } startdatetime.AddMinutes(Convert.ToDouble(minutes)); bool result; using (DB db = new DB(DBHelper.GetConnectionString(_currentClient))) { var q = from o in db.DowntimeDataSet where o.ID == id select o; var dd = q.FirstOrDefault(); if (dd == null) return false.ToString().ToLower(); DowntimeReason reason = DCSDashboardDemoHelper.getReason(reasonId); if (dd.EventStop.HasValue == false) { if (!DateTime.TryParse(request.Form["enddatetime"], out enddatetime)) { context.Response.Write("end date time is empty."); return "false"; } dd.EventStop = enddatetime; if (dd.EventStart != null) dd.Minutes = Convert.ToDecimal(dd.EventStop.Value.Subtract(dd.EventStart.Value).TotalMinutes); changeMinutes = false; SwitchLineStatus(line, true); } dd.ReasonCodeID = reasonId; dd.ReasonCode = reasonCode; dd.Line = line; dd.Comment = comment; if(changeMinutes) dd.Minutes = minutes; dd.Client = client; if (reason.HideReasonInReports && dd.Minutes > reason.Duration && reason.Duration > 0)//If Planned && duration is greater than 0 { DowntimeChild child1 = (from o in db.DowntimeChildren where o.ParentDowntimeId == dd.ID select o).FirstOrDefault(); if (child1 == null) { if (dd.EventStart != null) { dd.EventStop = dd.EventStart.Value.AddMinutes(reason.Duration); // Tim mark DowntimeData dd2 = new DowntimeData { Client = dd.Client, ReasonCode = null, ReasonCodeID = null, Line = dd.Line, Minutes = dd.Minutes - reason.Duration, Comment = string.Empty, EventStart = dd.EventStop }; dd2.EventStop = dd.EventStop.Value.AddMinutes(Convert.ToDouble(dd2.Minutes)); child1 = new DowntimeChild {ParentDowntimeId = dd.ID}; dd.Minutes = reason.Duration;//Change after setting dd2's minutes db.AddToDowntimeDataSet(dd2); db.SaveChanges(); child1.DowntimeId = dd2.ID; child1.ReasonCodeId = reason.ID; db.AddToDowntimeChildren(child1); } db.SaveChanges(); } } else { db.SaveChanges(); } result = true; if (reason.IsChangeOver && throughputId > 0) { DCSDashboardDemoHelper.CreateThroughPutHistory(throughputId, option1Id, option2Id, dd.EventStop.Value, line); } else if (option1Id > 0 || option2Id > 0) { if (option1Id > 0) DCSDashboardDemoHelper.CreateNOS(dd.ID, option1Id); if (option2Id > 0) DCSDashboardDemoHelper.CreateNOS(dd.ID, option2Id); } } return result.ToString().ToLower(); }