private RosterModel CalculateRosterEditEntries(RosterApp rosterApp, List<Models.RosterDetail> rosterDetails)
        {
            Shift shift = db.Shifts.First(aa => aa.ShiftID == rosterApp.ShiftID);
            RosterModel _objmodel = new RosterModel();
            int i = 1;
            try
            {
                _objmodel._RosterAttributes = new List<RosterAttributes>();
                DateTime _StartDate = (DateTime)rosterApp.DateStarted;
                _objmodel.RotaAppID = rosterApp.RotaApplD;
                _objmodel.ShiftName = shift.ShiftName;
                switch (rosterApp.RosterCriteria)
                {
                    case "S":

                        break;
                    case "C":
                        _objmodel.CriteriaValueName = db.Crews.Where(aa => aa.CrewID == rosterApp.CriteriaData).First().CrewName;
                        break;
                    case "T":

                        break;
                    case "employee":

                        break;
                }
                while (_StartDate <= rosterApp.DateEnded)
                {
                    string _day = _StartDate.Date.ToString("dddd");
                    string _date = _StartDate.Date.ToString("dd-MMM-yyyy");
                    string _DTime="";
                    TimeSpan _DutyTime = new TimeSpan();
                    int _WorkMin = 0;
                    if (rosterDetails.Where(aa => aa.RosterDate == _StartDate).Count() > 0)
                    {
                        // from roster details
                        RosterDetail rotaDetail = rosterDetails.First(aa=>aa.RosterDate == _StartDate);
                        _DTime = rotaDetail.DutyTime.Value.Hours.ToString("00")+rotaDetail.DutyTime.Value.Minutes.ToString("00");
                        _WorkMin = (int)rotaDetail.WorkMin;
                        _DutyTime = (TimeSpan)rotaDetail.DutyTime;

                    }
                    else
                    {
                        //from shift

                        _DTime = shift.StartTime.Hours.ToString("00") + shift.StartTime.Minutes.ToString("00");
                        _WorkMin = CalculateShiftWorkMins(_StartDate,shift);
                        _DutyTime = shift.StartTime;
                    }
                    _objmodel._RosterAttributes.Add(new RosterAttributes { ID = i, DateString = _date, Day = _day, DutyDate = _StartDate.Date, DutyTimeString = _DTime, DutyTime = _DutyTime, WorkMin = _WorkMin });
                    _StartDate = _StartDate.AddDays(1);
                    i++;
                }
                _objmodel.NoOfDays = i;
                return _objmodel;
            }
            catch (Exception ex)
            {
                return _objmodel;
            }
        }
        public bool DeleteRoster(int RosterAppID, int LoggedUserID)
        {
            RosterApp RApp = new RosterApp();
            //User uid = new User();
            RApp = db.RosterApps.First(rr => rr.RotaApplD == RosterAppID);
            if (RApp.UserID == LoggedUserID)
            {
                try
                {
                    List<RosterDetail> RAppDetail = new List<Models.RosterDetail>();
                    RAppDetail = db.RosterDetails.Where(aa => aa.RosterAppID == RosterAppID).ToList();
                    foreach (var item in RAppDetail)
                    {
                        db.RosterDetails.Remove(item);
                    }
                }
                catch (Exception)
                {

                    throw;
                }
                db.RosterApps.Remove(RApp);
                db.SaveChanges();
            }

            return true;
        }
 private List<RosterDetailAttributes> CalculateRosterDetails(List<Models.RosterDetail> rosterdetails, RosterApp rosterApp)
 {
     //List<RosterDetailModel> rdm = new List<RosterDetailModel>();
     List<RosterDetailAttributes> rda = new List<RosterDetailAttributes>();
     Shift shift = new Shift();
     shift = db.Shifts.First(aa => aa.ShiftID == rosterApp.ShiftID);
     DateTime currentDate = rosterApp.DateStarted.Value;
     List<RosterDetail> tempRotaDetails = new List<RosterDetail>();
     while (currentDate <= rosterApp.DateEnded)
     {
         RosterDetailAttributes rdaS = new RosterDetailAttributes();
         tempRotaDetails = rosterdetails.Where(aa => aa.RosterDate == currentDate).ToList();
         if (tempRotaDetails.Count > 0)
         {
             rdaS.Changed = true;
             rdaS.Day = tempRotaDetails.FirstOrDefault().RosterDate.Value.ToString("dddd");
             rdaS.DutyCode = tempRotaDetails.FirstOrDefault().DutyCode;
             rdaS.DutyDate = tempRotaDetails.FirstOrDefault().RosterDate.Value;
             rdaS.DutyTime = (TimeSpan)tempRotaDetails.FirstOrDefault().DutyTime;
             rdaS.WorkMin = (short)tempRotaDetails.FirstOrDefault().WorkMin;
         }
         else
         {
             rdaS.Changed = false;
             rdaS.Day = currentDate.ToString("dddd");
             int wrkMin = CalculateDutyCode(shift, currentDate);
             if (wrkMin == 0)
                 rdaS.DutyCode = "R";
             else
                 rdaS.DutyCode = "D";
             rdaS.DutyDate = currentDate;
             rdaS.DutyTime = shift.StartTime;
             rdaS.WorkMin = wrkMin;
         }
         rda.Add(rdaS);
         currentDate = currentDate.AddDays(1);
     }
     return rda;
 }
        public ActionResult Create(FormCollection form)
        {
            int _Shift = Convert.ToInt32(Request.Form["ShiftList"].ToString());
            int _RosterType = Convert.ToInt32(Request.Form["RosterType"].ToString());
            DateTime _StartDate = Convert.ToDateTime(Request.Form["dateStart"]);
            DateTime _EndDate = Convert.ToDateTime(Request.Form["dateEndHidden"]);
            TimeSpan _DutyTime = MyHelper.ConvertTime(Request.Form["dutyTime"]);
            int _WorkMin = Convert.ToInt16(Request.Form["mints"]);
            string Criteria = "";
            bool check = false;
            int RosterCriteriaValue = 0;
            switch (Request.Form["cars"].ToString())
            {
                case "shift":
                    RosterCriteriaValue = Convert.ToInt32(Request.Form["ShiftList"].ToString());
                    Criteria = "S";
                    break;
                case "crew":
                    RosterCriteriaValue = Convert.ToInt32(Request.Form["CrewList"].ToString());
                    Criteria = "C";
                    break;
                case "section":
                    RosterCriteriaValue = Convert.ToInt32(Request.Form["SectionList"].ToString());
                    Criteria = "T";
                    break;
                case "employee":

                    break;
            }
            if (check == false)
            {
                RosterApp ra = new RosterApp()
                {
                    DateStarted = _StartDate,
                    DateEnded = _EndDate.AddDays(-1),
                    DateCreated = DateTime.Now,
                    RosterCriteria = Criteria,
                    CriteriaData = RosterCriteriaValue,
                    DutyTime = _DutyTime,
                    RotaTypeID = (byte)_RosterType,
                    WorkMin = (short)_WorkMin,
                    Status = true,
                    ShiftID = (byte)_Shift,
                    UserID = Convert.ToInt32(Session["LogedUserID"].ToString())
                };
                db.RosterApps.Add(ra);
                db.SaveChanges();

                return View(CalculateRosterFields(_RosterType, _StartDate, _WorkMin, _DutyTime, Criteria, RosterCriteriaValue, _Shift, ra.RotaApplD));
            }
            else
            {
                ViewBag.RosterType = new SelectList(db.RosterTypes.OrderBy(s => s.Name), "ID", "Name");
                ViewBag.ShiftList = new SelectList(db.Shifts.OrderBy(s => s.ShiftName), "ShiftID", "ShiftName");
                ViewBag.CrewList = new SelectList(db.Crews.OrderBy(s => s.CrewName), "CrewID", "CrewName");
                ViewBag.SectionList = new SelectList(db.Sections.OrderBy(s => s.SectionName), "SectionID", "SectionName");
                return View("Index");
            }
        }