public ActionResult CreateSchedule(M_Schedule data)
        {
            try
            {
                string startTime = data.Timein.ToString();
                string endTime   = data.TimeOut.ToString();

                DateTime start = DateTime.Parse(startTime);
                DateTime end   = DateTime.Parse(endTime);

                TimeSpan duration = end.Subtract(start);
                if (duration.TotalHours < 0)
                {
                    end      = end.AddDays(1);
                    duration = end.Subtract(start);
                }

                //if (duration.TotalHours != 0)
                if (duration.TotalHours > 0) //change to military time
                {
                    data.CreateID   = user.UserName;
                    data.CreateDate = DateTime.Now;
                    data.UpdateID   = user.UserName;
                    data.UpdateDate = DateTime.Now;

                    M_Schedule checker = (from c in db.M_Schedule
                                          where c.Timein == data.Timein &&
                                          c.TimeOut == data.TimeOut &&
                                          c.Status == data.Status &&
                                          c.IsDeleted == false
                                          select c).FirstOrDefault();
                    if (checker == null)
                    {
                        db.M_Schedule.Add(data);
                        db.SaveChanges();
                        return(Json(new { msg = "Success" }, JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        return(Json(new { msg = "Failed" }, JsonRequestBehavior.AllowGet));
                    }
                }
                else
                {
                    return(Json(new { msg = "zero" }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception err)
            {
                Error_Logs error = new Error_Logs();
                error.PageModule = "Master - Schedule";
                error.ErrorLog   = err.Message;
                error.DateLog    = DateTime.Now;
                error.Username   = user.UserName;
                db.Error_Logs.Add(error);
                db.SaveChanges();
                return(Json(new { msg = err.Message }, JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult DeleteSchedule(int ID)
        {
            M_Schedule schedule = new M_Schedule();

            schedule = (from u in db.M_Schedule.ToList()
                        where u.ID == ID
                        select u).FirstOrDefault();
            schedule.IsDeleted       = true;
            schedule.UpdateDate      = DateTime.Now;
            schedule.UpdateID        = user.UserName;
            db.Entry(schedule).State = EntityState.Modified;
            db.SaveChanges();
            return(Json(new { msg = "Success" }, JsonRequestBehavior.AllowGet));
        }
        public ActionResult EditSchedule(M_Schedule data)
        {
            try
            {
                M_Schedule schedule = new M_Schedule();
                schedule = (from u in db.M_Schedule.ToList()
                            where u.ID == data.ID
                            select u).FirstOrDefault();
                schedule.Type    = data.Type;
                schedule.Timein  = data.Timein;
                schedule.TimeOut = data.TimeOut;
                schedule.Status  = data.Status;

                schedule.UpdateID   = user.UserName;
                schedule.UpdateDate = DateTime.Now;

                M_Schedule checker = (from c in db.M_Schedule
                                      where c.Timein == data.Timein &&
                                      c.Type == data.Type &&
                                      c.TimeOut == data.TimeOut &&
                                      c.Status == data.Status &&
                                      c.IsDeleted == false
                                      select c).FirstOrDefault();
                if (checker == null)
                {
                    db.Entry(schedule).State = EntityState.Modified;
                    db.SaveChanges();
                }
                else
                {
                    return(Json(new { msg = "Failed" }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception err)
            {
                Error_Logs error = new Error_Logs();
                error.PageModule = "Master - Schedule";
                error.ErrorLog   = err.Message;
                error.DateLog    = DateTime.Now;
                error.Username   = user.UserName;
                db.Error_Logs.Add(error);
                db.SaveChanges();
            }

            return(Json(new { msg = "Success" }, JsonRequestBehavior.AllowGet));
        }
        public ActionResult TimeValidate(List <string> list, DateTime DateFrom, string OTin, string OTOut, string Type)
        {
            bool          Allow       = true;
            List <string> EmpConflict = new List <string>();
            List <string> EmpAlready  = new List <string>();
            long?         GetSchedID;

            if (Type != "Regular")
            {
                foreach (string emp in list)
                {
                    string      currentRefno = helper.GenerateOTRef();
                    DateTime    a            = DateFrom.Date;
                    AF_OTfiling otnow        = (from c in db.AF_OTfiling where c.DateFrom == a && c.EmployeeNo == emp select c).FirstOrDefault();
                    //AF_OTfiling otnow2 = (from c in db.AF_OTfiling where c.OT_RefNo == currentRefno && c.OTout == OTOut && c.Status >= 0 && c.EmployeeNo == emp select c).FirstOrDefault();

                    if (otnow != null)// || otnow2 != null)
                    {
                        EmpAlready.Add(list[0]);
                        Allow = false;
                    }
                    else
                    {
                        Allow = true;
                    }
                }
            }
            else
            {
                foreach (string emp in list)
                {
                    M_Employee_Master_List Employee = (from c in db.M_Employee_Master_List where c.EmpNo == emp select c).FirstOrDefault();
                    GetSchedID = (from c in db.M_Employee_Master_List_Schedule where c.EmployeeNo == emp orderby c.ID descending select c.ScheduleID).FirstOrDefault();

                    string Schedule = (from c in db.M_Schedule where c.ID == GetSchedID select c.Type).FirstOrDefault();

                    M_Schedule schedcheck   = (from c in db.M_Schedule where c.Type == Schedule select c).FirstOrDefault();
                    DateTime   ShiftStart   = DateTime.Parse(schedcheck.Timein.ToString());
                    DateTime   ShiftEnd     = DateTime.Parse(schedcheck.TimeOut.ToString());
                    DateTime   TimecheckIN  = DateTime.Parse(OTin);
                    DateTime   TimecheckOUT = DateTime.Parse(OTOut);
                    if (ShiftStart > ShiftEnd)
                    {
                        ShiftEnd = ShiftEnd.AddDays(1);
                    }
                    //if ((ShiftStart > TimecheckIN
                    //    && TimecheckIN < ShiftEnd)
                    //    ||( ShiftStart > TimecheckOUT
                    //    && TimecheckOUT < ShiftEnd))
                    //{
                    if (TimecheckIN.ToString("HH:mm") == ShiftEnd.ToString("HH:mm") || TimecheckOUT.ToString("HH:mm") == ShiftStart.ToString("HH:mm"))
                    {
                        Allow = (Allow == false) ? false : true;
                    }
                    else
                    {
                        EmpConflict.Add(emp);
                        Allow = false;
                        //break;
                    }
                    string      currentRefno = helper.GenerateOTRef();
                    DateTime    a            = DateFrom.Date;
                    AF_OTfiling otnow        = (from c in db.AF_OTfiling where c.OT_RefNo == currentRefno && c.OTin == OTin && c.Status >= 0 && c.DateFrom == a && c.EmployeeNo == emp select c).FirstOrDefault();
                    //AF_OTfiling otnow2 = (from c in db.AF_OTfiling where c.OT_RefNo == currentRefno && c.OTout == OTOut && c.Status >= 0 && c.EmployeeNo == emp select c).FirstOrDefault();

                    if (otnow != null)// || otnow2 != null)
                    {
                        EmpAlready.Add(list[0]);
                        Allow = false;
                    }
                }
            }
            return(Json(new { Allow = Allow, EmpConflict = EmpConflict, EmpAlready = EmpAlready }, JsonRequestBehavior.AllowGet));
        }