/*
        protected string UpdateCC()
        {
            int casecount = -1;

            DateTime d;
            DateTime? date = null;

            if(DateTime.TryParse(Request.Form["date"], out d))
                date = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, 00);

            int.TryParse(Request.Form["counts"], out casecount);

            if (casecount > -1 && date.HasValue)
            {

                using (DB db = new DB())
                {
                    List<ThroughputHistory> thList = DCSDashboardDemoHelper.getThroughPutHistories();

                    ThroughputHistory th = (from o in thList
                                            where (o.Date.DayOfYear == date.Value.DayOfYear && o.Date.Year == date.Value.Year && o.Date.Hour == date.Value.Hour)
                                            orderby o.Date descending
                                            select o).FirstOrDefault();

                    DateTime tmpDate = new DateTime(d.Year, d.Month, d.Day, d.Hour, 00, 00);

                    tmpDate = tmpDate.AddHours(1d);

                    if (th != null)
                    {
                        if (th.Date != date)
                            tmpDate = th.Date;
                    }

                    List<CaseCount> cList = DCSDashboardDemoHelper.GetCaseCounts(date, tmpDate, "lebelge");

                    foreach (CaseCount cc in cList)
                    {
                        int id = cc.Id;

                        if (cc.EventStop.Value >= d && cc.EventStop.Value.Hour == d.Hour)
                        {
                            CaseCount tmpCase = (from o in db.CaseCountSet
                                                 where o.Id == id
                                                 select o).FirstOrDefault();

                            db.DeleteObject(tmpCase);
                        }
                    }

                    int diff = (int)d.Subtract(tmpDate).TotalMinutes;

                    if (diff < 0)
                        diff = diff * -1;

                    decimal val = (decimal)casecount / ((decimal)diff / 5M);

                    decimal total = 0;
                    decimal lastCC = 0;
                    decimal counter = 0;

                    CaseCount lastCaseCount = (from o in db.CaseCountSet
                                                where o.EventStop.Value < date
                                                orderby o.EventStop.Value descending
                                                select o).FirstOrDefault();

                    if (lastCaseCount != null)
                        lastCC = lastCaseCount.CaseCount1;

                    total = lastCC;

                    DateTime end = date.Value.AddHours(1d);

                    diff = diff / 5;

                    List<CaseCount> tmpList = new List<CaseCount>();

                    if (val > 0)
                    {
                        for (int x = 0; x < diff; x++)
                        {
                            if (date.Value.Hour == end.Hour)
                                break;

                            total += val;
                            counter += val;

                            CaseCount cc = new CaseCount();
                            cc.CaseCount1 = Convert.ToInt32(total);
                            cc.Client = "lebelge";
                            cc.EventStart = date;
                            cc.EventStop = date;
                            cc.Line = "company-demo";

                            tmpList.Add(cc);

                            date = date.Value.AddMinutes(5d);

                        }
                    }

                    if (tmpList.Count > 0)
                    {
                        tmpList.ElementAt(tmpList.Count - 1).CaseCount1 = tmpList.ElementAt(0).CaseCount1 + casecount;

                        foreach (CaseCount cc in tmpList)
                        {
                            db.AddToCaseCountSet(cc);
                        }
                    }

                    if (db.SaveChanges() >= 0)
                        return "Success" + d.ToString("MM/dd/yyyy hh:mm:ss tt");
                    else
                        return "ERROR";

                }

            }

            return "ERROR";

        }
        */
        protected string UpdateCC()
        {
            int casecount = -1;

            DateTime d;
            DateTime? date = null;

            if (DateTime.TryParse(Request.Form["date"], out d))
                date = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, 00);

            int.TryParse(Request.Form["counts"], out casecount);

            if (casecount > -1 && date.HasValue)
            {

                using (DB db = new DB())
                {
                    List<ThroughputHistory> thList = DCSDashboardDemoHelper.getThroughPutHistories("company-demo");

                    ThroughputHistory th = (from o in thList
                                            where (o.Date.DayOfYear == date.Value.DayOfYear && o.Date.Year == date.Value.Year && o.Date.Hour == date.Value.Hour)
                                            orderby o.Date descending
                                            select o).FirstOrDefault();

                    DateTime tmpDate = new DateTime(d.Year, d.Month, d.Day, d.Hour, 00, 00);

                    tmpDate = tmpDate.AddHours(1d);

                    if (th != null)
                    {
                        if (th.Date != date)
                            tmpDate = th.Date;
                    }

                    List<CaseCount> cList = DCSDashboardDemoHelper.GetCaseCounts(date, tmpDate, "lebelge");

                    foreach (CaseCount cc in cList)
                    {
                        int id = cc.Id;

                        if (cc.EventStop.Value >= d && cc.EventStop.Value.Hour == d.Hour)
                        {
                            CaseCount tmpCase = (from o in db.CaseCountSet
                                                 where o.Id == id
                                                 select o).FirstOrDefault();

                            db.DeleteObject(tmpCase);
                        }
                    }

                    int diff = (int)d.Subtract(tmpDate).TotalMinutes;

                    if (diff < 0)
                        diff = diff * -1;

                    CaseCount lastCaseCount = (from o in db.CaseCountSet
                                               where o.EventStop.Value < date && o.Client == DCSDashboardDemoHelper.Filter_Client
                                               orderby o.EventStop.Value descending
                                               select o).FirstOrDefault();

                    DateTime lastHr = date.Value.AddHours(-1);

                    Dictionary<DateTime, ManualCaseCount> tmpCList = GetCalculatedCaseCounts(date.Value, "company-demo");

                    decimal lastCC = 0;

                    if (tmpCList.Count > 0)
                    {
                        casecount = casecount - tmpCList[lastHr].cases;
                    }

                    decimal val = (casecount) / ((decimal)diff / 5M);

                    if (lastCaseCount != null)
                        lastCC = lastCaseCount.CaseCount1;

                    decimal total = 0;
                    decimal counter = 0;

                    total = lastCC;

                    DateTime end = date.Value.AddHours(1d);

                    diff = diff / 5;

                    List<CaseCount> tmpList = new List<CaseCount>();

                    if (val > 0)
                    {
                        for (int x = 0; x < diff; x++)
                        {
                            if (date.Value.Hour == end.Hour)
                                break;

                            total += val;
                            counter += val;

                            CaseCount cc = new CaseCount();
                            cc.CaseCount1 = Convert.ToInt32(total);
                            cc.Client = "lebelge";
                            cc.EventStart = date;
                            cc.EventStop = date;
                            cc.Line = "company-demo";

                            tmpList.Add(cc);

                            date = date.Value.AddMinutes(5d);

                        }
                    }

                    if (tmpList.Count > 0)
                    {
                        tmpList.ElementAt(tmpList.Count - 1).CaseCount1 = tmpList.ElementAt(0).CaseCount1 + casecount;

                        foreach (CaseCount cc in tmpList)
                        {
                            db.AddToCaseCountSet(cc);
                        }
                    }

                    if (db.SaveChanges() >= 0)
                         return "Success" + d.ToString("MM/dd/yyyy hh:mm:ss tt");
                    else
                        return "ERROR";
                }

            }

            return "ERROR";
        }