private bool AddJCAbsentToAttData(string _empDate, short _WorkCardID)
        {
            bool check = false;
            try
            {
                //Absent
                using (var context = new TAS2013Entities())
                {
                    AttData _attdata = context.AttDatas.FirstOrDefault(aa => aa.EmpDate == _empDate);
                    if (_attdata != null)
                    {
                        _attdata.DutyCode = "D";
                        _attdata.StatusAB = true;
                        _attdata.StatusDO = false;
                        _attdata.StatusLeave = false;
                        _attdata.Remarks = "[Absent][Manual]";
                        _attdata.TimeIn = null;
                        _attdata.TimeOut = null;
                        _attdata.WorkMin = null;
                        _attdata.EarlyIn = null;
                        _attdata.EarlyOut = null;
                        _attdata.LateIn = null;
                        _attdata.LateOut = null;
                        _attdata.OTMin = null;
                        _attdata.StatusEI = null;
                        _attdata.StatusEO = null;
                        _attdata.StatusLI = null;
                        _attdata.StatusLO = null;
                        _attdata.StatusP = null;
                    }
                    context.SaveChanges();
                    if (context.SaveChanges() > 0)
                        check = true;
                    context.Dispose();
                }
            }
            catch (Exception ex)
            {

            }
            return check;
        }
        public void CreateAttendance(DateTime dateTime)
        {
            using (var ctx = new TAS2013Entities())
            {
                List<Emp> _emp = new List<Emp>();
                _emp = ctx.Emps.Where(aa => aa.Status == true).ToList();
                List<Roster> _Roster = new List<Roster>();
                _Roster = context.Rosters.Where(aa => aa.RosterDate == dateTime).ToList();
                List<RosterDetail> _NewRoster = new List<RosterDetail>();
                _NewRoster = context.RosterDetails.Where(aa => aa.RosterDate == dateTime).ToList();
                List<LvData> _LvData = new List<LvData>();
                _LvData = context.LvDatas.Where(aa => aa.AttDate == dateTime).ToList();
                List<LvShort> _lvShort = new List<LvShort>();
                _lvShort = context.LvShorts.Where(aa => aa.DutyDate == dateTime).ToList();
                List<AttData> _AttData = context.AttDatas.Where(aa => aa.AttDate == dateTime).ToList();
                _myHelperClass.WriteToLogFile("**********************Attendance Creating Started: Total Employees are:" + _emp.Count + "*********************");
                foreach (var emp in _emp)
                {
                    string empDate = emp.EmpID + dateTime.ToString("yyMMdd");
                    if (_AttData.Where(aa => aa.EmpDate == empDate).Count() == 0)
                    {
                        try
                        {
                            /////////////////////////////////////////////////////
                            //  Mark Everyone Absent while creating Attendance //
                            /////////////////////////////////////////////////////
                            //Set DUTYCODE = D, StatusAB = true, and Remarks = [Absent]
                            AttData att = new AttData();
                            att.AttDate = dateTime.Date;
                            att.DutyCode = "D";
                            att.StatusAB = true;
                            att.Remarks = "[Absent]";
                            if (emp.Shift != null)
                                att.DutyTime = emp.Shift.StartTime;
                            else
                                att.DutyTime = new TimeSpan(07, 45, 00);
                            att.EmpID = emp.EmpID;
                            att.EmpNo = emp.EmpNo;
                            att.EmpDate = emp.EmpID + dateTime.ToString("yyMMdd");
                            att.ShifMin = CalculateShiftMinutes(emp.Shift, dateTime.DayOfWeek);
                            //////////////////////////
                            //  Check for Rest Day //
                            ////////////////////////
                            //Set DutyCode = R, StatusAB=false, StatusDO = true, and Remarks=[DO]
                            //Check for 1st Day Off of Shift
                            if (emp.Shift.DaysName.Name == ReturnDayOfWeek(dateTime.DayOfWeek))
                            {
                                att.DutyCode = "R";
                                att.StatusAB = false;
                                att.StatusDO = true;
                                att.Remarks = "[DO]";
                            }
                            //Check for 2nd Day Off of shift
                            if (emp.Shift.DaysName1.Name == ReturnDayOfWeek(dateTime.DayOfWeek))
                            {
                                att.DutyCode = "R";
                                att.StatusAB = false;
                                att.StatusDO = true;
                                att.Remarks = "[DO]";
                            }

                            //////////////////////////
                            //  Check for GZ Day //
                            ////////////////////////
                            //Set DutyCode = R, StatusAB=false, StatusGZ = true, and Remarks=[GZ]
                            if (emp.Shift.GZDays == true)
                            {
                                foreach (var holiday in context.Holidays)
                                {
                                    if (context.Holidays.Where(hol => hol.HolDate.Month == att.AttDate.Value.Month && hol.HolDate.Day == att.AttDate.Value.Day).Count() > 0)
                                    {
                                        att.DutyCode = "G";
                                        att.StatusAB = false;
                                        att.StatusGZ = true;
                                        att.Remarks = "[GZ]";
                                        att.ShifMin = 0;
                                    }
                                }
                            }

                            //////////////////////////
                            //  Check for Roster   //
                            ////////////////////////
                            //If Roster DutyCode is Rest then change the StatusAB and StatusDO
                            foreach (var roster in _Roster.Where(aa => aa.EmpDate == att.EmpDate))
                            {
                                att.DutyCode = roster.DutyCode.Trim();
                                if (att.DutyCode == "R")
                                {
                                    att.StatusAB = false;
                                    att.StatusDO = true;
                                    att.DutyCode = "R";
                                    att.Remarks = "[DO]";
                                }
                                att.ShifMin = roster.WorkMin;
                                att.DutyTime = roster.DutyTime;
                            }

                            ////New Roster
                            string empCdate = "Emp" + emp.EmpID.ToString() + dateTime.ToString("yyMMdd");
                            string sectionCdate = "Section" + emp.SecID.ToString() + dateTime.ToString("yyMMdd");
                            string crewCdate = "Crew" + emp.CrewID.ToString() + dateTime.ToString("yyMMdd");
                            string shiftCdate = "Shift" + emp.ShiftID.ToString() + dateTime.ToString("yyMMdd");
                            if (_NewRoster.Where(aa => aa.CriteriaValueDate == empCdate).Count() > 0)
                            {
                                var roster = _NewRoster.FirstOrDefault(aa => aa.CriteriaValueDate == empCdate);
                                if (roster.WorkMin == 0)
                                {
                                    att.StatusAB = false;
                                    att.StatusDO = true;
                                    att.Remarks = "[DO]";
                                    att.DutyCode = "R";
                                    att.ShifMin = 0;
                                }
                                else
                                {
                                    att.ShifMin = roster.WorkMin;
                                    att.DutyCode = "D";
                                    att.DutyTime = roster.DutyTime;
                                }
                            }
                            else if (_NewRoster.Where(aa => aa.CriteriaValueDate == sectionCdate).Count() > 0)
                            {
                                var roster = _NewRoster.FirstOrDefault(aa => aa.CriteriaValueDate == sectionCdate);
                                if (roster.WorkMin == 0)
                                {
                                    att.StatusAB = false;
                                    att.StatusDO = true;
                                    att.Remarks = "[DO]";
                                    att.DutyCode = "R";
                                    att.ShifMin = 0;
                                }
                                else
                                {
                                    att.ShifMin = roster.WorkMin;
                                    att.DutyCode = "D";
                                    att.DutyTime = roster.DutyTime;
                                }
                            }
                            else if (_NewRoster.Where(aa => aa.CriteriaValueDate == crewCdate).Count() > 0)
                            {
                                var roster = _NewRoster.FirstOrDefault(aa => aa.CriteriaValueDate == crewCdate);
                                if (roster.WorkMin == 0)
                                {
                                    att.StatusAB = false;
                                    att.StatusDO = true;
                                    att.Remarks = "[DO]";
                                    att.DutyCode = "R";
                                    att.ShifMin = 0;
                                }
                                else
                                {
                                    att.ShifMin = roster.WorkMin;
                                    att.DutyCode = "D";
                                    att.DutyTime = roster.DutyTime;
                                }
                            }
                            else if (_NewRoster.Where(aa => aa.CriteriaValueDate == shiftCdate).Count() > 0)
                            {
                                var roster = _NewRoster.FirstOrDefault(aa => aa.CriteriaValueDate == shiftCdate);
                                if (roster.WorkMin == 0)
                                {
                                    att.StatusAB = false;
                                    att.StatusDO = true;
                                    att.Remarks = "[DO]";
                                    att.DutyCode = "R";
                                    att.ShifMin = 0;
                                }
                                else
                                {
                                    att.ShifMin = roster.WorkMin;
                                    att.DutyCode = "D";
                                    att.DutyTime = roster.DutyTime;
                                }
                            }
                            ////////////////////////////
                            //TODO Check for Job Card//
                            //////////////////////////

                            ////////////////////////////
                            //  Check for Short Leave//
                            //////////////////////////
                            foreach (var sLeave in _lvShort.Where(aa => aa.EmpDate == att.EmpDate))
                            {
                                if (_lvShort.Where(lv => lv.EmpDate == att.EmpDate).Count() > 0)
                                {
                                    att.StatusSL = true;
                                    att.StatusAB = null;
                                    att.DutyCode = "L";
                                    att.Remarks = "[Short Leave]";
                                }
                            }

                            //////////////////////////
                            //   Check for Leave   //
                            ////////////////////////
                            //Set DutyCode = R, StatusAB=false, StatusGZ = true, and Remarks=[GZ]
                            foreach (var Leave in _LvData)
                            {
                                var _Leave = _LvData.Where(lv => lv.EmpDate == att.EmpDate && lv.HalfLeave != true);
                                if (_Leave.Count() > 0)
                                {
                                    att.StatusLeave = true;
                                    att.StatusAB = false;
                                    att.DutyCode = "L";
                                    att.StatusDO = false;
                                    if (Leave.LvCode == "A")
                                        att.Remarks = "[Casual Leave]";
                                    else if (Leave.LvCode == "B")
                                        att.Remarks = "[Annual Leave]";
                                    else if (Leave.LvCode == "C")
                                        att.Remarks = "[Sick Leave]";
                                    else
                                        att.Remarks = "[" + _Leave.FirstOrDefault().LvType.LvDesc + "]";
                                }
                                else
                                {
                                    att.StatusLeave = false;
                                }
                            }

                            /////////////////////////
                            //Check for Half Leave///
                            ////////////////////////
                            var _HalfLeave = _LvData.Where(lv => lv.EmpDate == att.EmpDate && lv.HalfLeave == true);
                            if (_HalfLeave.Count() > 0)
                            {
                                att.StatusLeave = true;
                                att.StatusAB = false;
                                att.DutyCode = "L";
                                att.StatusHL = true;
                                att.StatusDO = false;
                                if (_HalfLeave.FirstOrDefault().LvCode == "A")
                                    att.Remarks = "[H-CL]";
                                else if (_HalfLeave.FirstOrDefault().LvCode == "B")
                                    att.Remarks = "[S-AL]";
                                else if (_HalfLeave.FirstOrDefault().LvCode == "C")
                                    att.Remarks = "[H-SL]";
                                else
                                    att.Remarks = "[Half Leave]";
                            }
                            else
                            {
                                att.StatusLeave = false;
                            }
                            ctx.AttDatas.AddObject(att);
                            ctx.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            _myHelperClass.WriteToLogFile("-------Error In Creating Attendance of Employee: " + emp.EmpNo + " ------" + ex.InnerException.Message);
                        }
                    }
                }
                _myHelperClass.WriteToLogFile("****************Creating Attendance Completed*****************");
                AttProcess attp = new AttProcess();
                attp.ProcessDate = dateTime;
                ctx.AttProcesses.AddObject(attp);
                ctx.SaveChanges();
                ctx.Dispose();
            }
            // reprocess attendance from last 5 days
            CreateMissingAttendance ca = new CreateMissingAttendance();
            ca.CreatemissingAttendance(dateTime.AddDays(-7), dateTime);
            _myHelperClass.WriteToLogFile("Creating Attendance of Date: " + dateTime.ToString());
        }
        public void BootstrapAttendance(DateTime dateTime, List<Emp> emps, List<AttData> _AttData)
        {
            using (var ctx = new TAS2013Entities())
            {
                List<Roster> _Roster = new List<Roster>();
                _Roster = context.Rosters.Where(aa => aa.RosterDate == dateTime).ToList();
                List<RosterDetail> _NewRoster = new List<RosterDetail>();
                _NewRoster = context.RosterDetails.Where(aa => aa.RosterDate == dateTime).ToList();
                List<LvData> _LvData = new List<LvData>();
                _LvData = context.LvDatas.Where(aa => aa.AttDate == dateTime).ToList();
                List<LvShort> _lvShort = new List<LvShort>();
                _lvShort = context.LvShorts.Where(aa => aa.DutyDate == dateTime).ToList();
                //List<AttData> _AttData = context.AttDatas.Where(aa => aa.AttDate == dateTime).ToList();
                foreach (var emp in emps)
                {
                    string empDate = emp.EmpID + dateTime.ToString("yyMMdd");
                    if (_AttData.Where(aa => aa.EmpDate == empDate).Count() >0)
                    {
                        try
                        {

                            /////////////////////////////////////////////////////
                            //  Mark Everyone Absent while creating Attendance //
                            /////////////////////////////////////////////////////
                            //Set DUTYCODE = D, StatusAB = true, and Remarks = [Absent]
                            AttData att = _AttData.First(aa => aa.EmpDate == empDate);
                            //Reset Flags
                            att.TimeIn = null;
                            att.TimeOut = null;
                            att.Tin0 = null;
                            att.Tout0 = null;
                            att.Tin1 = null;
                            att.Tout1 = null;
                            att.Tin2 = null;
                            att.Tout2 = null;
                            att.Tin3 = null;
                            att.Tout3 = null;
                            att.Tin4 = null;
                            att.Tout4 = null;
                            att.Tin5 = null;
                            att.Tout5 = null;
                            att.Tin6 = null;
                            att.Tout6 = null;
                            att.Tin7 = null;
                            att.Tout7 = null;
                            att.Tin8 = null;
                            att.Tout8 = null;
                            att.StatusP = null;
                            att.StatusDO = null;
                            att.StatusEO = null;
                            att.StatusGZ = null;
                            att.StatusGZOT = null;
                            att.StatusHD = null;
                            att.StatusHL = null;
                            att.StatusIN = null;
                            att.StatusLeave = null;
                            att.StatusLI = null;
                            att.StatusLO = null;
                            att.StatusMN = null;
                            att.StatusOD = null;
                            att.StatusOT = null;
                            att.StatusSL = null;
                            att.WorkMin = null;
                            att.LateIn = null;
                            att.LateOut = null;
                            att.OTMin = null;
                            att.EarlyIn = null;
                            att.EarlyOut = null;
                            att.Remarks = null;
                            att.ShifMin = null;
                            att.SLMin = null;

                            att.AttDate = dateTime.Date;
                            att.DutyCode = "D";
                            att.StatusAB = true;
                            att.Remarks = "[Absent]";
                            if (emp.Shift != null)
                                att.DutyTime = emp.Shift.StartTime;
                            else
                                att.DutyTime = new TimeSpan(07, 45, 00);
                            att.EmpID = emp.EmpID;
                            att.EmpNo = emp.EmpNo;
                            att.EmpDate = emp.EmpID + dateTime.ToString("yyMMdd");
                            att.ShifMin = CalculateShiftMinutes(emp.Shift, dateTime.DayOfWeek);
                            //////////////////////////
                            //  Check for Rest Day //
                            ////////////////////////
                            //Set DutyCode = R, StatusAB=false, StatusDO = true, and Remarks=[DO]
                            //Check for 1st Day Off of Shift
                            if (emp.Shift.DaysName.Name == ReturnDayOfWeek(dateTime.DayOfWeek))
                            {
                                att.DutyCode = "R";
                                att.StatusAB = false;
                                att.StatusDO = true;
                                att.Remarks = "[DO]";
                            }
                            //Check for 2nd Day Off of shift
                            if (emp.Shift.DaysName1.Name == ReturnDayOfWeek(dateTime.DayOfWeek))
                            {
                                att.DutyCode = "R";
                                att.StatusAB = false;
                                att.StatusDO = true;
                                att.Remarks = "[DO]";
                            }
                            //////////////////////////
                            //  Check for Roster   //
                            ////////////////////////
                            //If Roster DutyCode is Rest then change the StatusAB and StatusDO
                            foreach (var roster in _Roster.Where(aa => aa.EmpDate == att.EmpDate))
                            {
                                att.DutyCode = roster.DutyCode.Trim();
                                if (att.DutyCode == "R")
                                {
                                    att.StatusAB = false;
                                    att.StatusDO = true;
                                    att.DutyCode = "R";
                                    att.Remarks = "[DO]";
                                }
                                att.ShifMin = roster.WorkMin;
                                att.DutyTime = roster.DutyTime;
                            }

                            ////New Roster
                            string empCdate = "Emp" + emp.EmpID.ToString() + dateTime.ToString("yyMMdd");
                            string sectionCdate = "Section" + emp.SecID.ToString() + dateTime.ToString("yyMMdd");
                            string crewCdate = "Crew" + emp.CrewID.ToString() + dateTime.ToString("yyMMdd");
                            string shiftCdate = "Shift" + emp.ShiftID.ToString() + dateTime.ToString("yyMMdd");
                            if (_NewRoster.Where(aa => aa.CriteriaValueDate == empCdate).Count() > 0)
                            {
                                var roster = _NewRoster.FirstOrDefault(aa => aa.CriteriaValueDate == empCdate);
                                if (roster.WorkMin == 0)
                                {
                                    att.StatusAB = false;
                                    att.StatusDO = true;
                                    att.Remarks = "[DO]";
                                    att.DutyCode = "R";
                                    att.ShifMin = 0;
                                }
                                else
                                {
                                    att.ShifMin = roster.WorkMin;
                                    att.DutyCode = "D";
                                    att.DutyTime = roster.DutyTime;
                                }
                            }
                            else if (_NewRoster.Where(aa => aa.CriteriaValueDate == sectionCdate).Count() > 0)
                            {
                                var roster = _NewRoster.FirstOrDefault(aa => aa.CriteriaValueDate == sectionCdate);
                                if (roster.WorkMin == 0)
                                {
                                    att.StatusAB = false;
                                    att.StatusDO = true;
                                    att.Remarks = "[DO]";
                                    att.DutyCode = "R";
                                    att.ShifMin = 0;
                                }
                                else
                                {
                                    att.ShifMin = roster.WorkMin;
                                    att.DutyCode = "D";
                                    att.DutyTime = roster.DutyTime;
                                }
                            }
                            else if (_NewRoster.Where(aa => aa.CriteriaValueDate == crewCdate).Count() > 0)
                            {
                                var roster = _NewRoster.FirstOrDefault(aa => aa.CriteriaValueDate == crewCdate);
                                if (roster.WorkMin == 0)
                                {
                                    att.StatusAB = false;
                                    att.StatusDO = true;
                                    att.Remarks = "[DO]";
                                    att.DutyCode = "R";
                                    att.ShifMin = 0;
                                }
                                else
                                {
                                    att.ShifMin = roster.WorkMin;
                                    att.DutyCode = "D";
                                    att.DutyTime = roster.DutyTime;
                                }
                            }
                            else if (_NewRoster.Where(aa => aa.CriteriaValueDate == shiftCdate).Count() > 0)
                            {
                                var roster = _NewRoster.FirstOrDefault(aa => aa.CriteriaValueDate == shiftCdate);
                                if (roster.WorkMin == 0)
                                {
                                    att.StatusAB = false;
                                    att.StatusDO = true;
                                    att.Remarks = "[DO]";
                                    att.DutyCode = "R";
                                    att.ShifMin = 0;
                                }
                                else
                                {
                                    att.ShifMin = roster.WorkMin;
                                    att.DutyCode = "D";
                                    att.DutyTime = roster.DutyTime;
                                }
                            }

                            //////////////////////////
                            //  Check for GZ Day //
                            ////////////////////////
                            //Set DutyCode = R, StatusAB=false, StatusGZ = true, and Remarks=[GZ]
                            if (emp.Shift.GZDays == true)
                            {
                                foreach (var holiday in context.Holidays)
                                {
                                    if (context.Holidays.Where(hol => hol.HolDate.Month == att.AttDate.Value.Month && hol.HolDate.Day == att.AttDate.Value.Day).Count() > 0)
                                    {
                                        att.DutyCode = "G";
                                        att.StatusAB = false;
                                        att.StatusGZ = true;
                                        att.Remarks = "[GZ]";
                                        att.ShifMin = 0;
                                    }
                                }
                            }
                            ////////////////////////////
                            //TODO Check for Job Card//
                            //////////////////////////

                            ////////////////////////////
                            //  Check for Short Leave//
                            //////////////////////////
                            foreach (var sLeave in _lvShort.Where(aa => aa.EmpDate == att.EmpDate))
                            {
                                if (_lvShort.Where(lv => lv.EmpDate == att.EmpDate).Count() > 0)
                                {
                                    att.StatusSL = true;
                                    att.StatusAB = null;
                                    att.DutyCode = "L";
                                    att.Remarks = "[Short Leave]";
                                }
                            }

                            //////////////////////////
                            //   Check for Leave   //
                            ////////////////////////
                            //Set DutyCode = R, StatusAB=false, StatusGZ = true, and Remarks=[GZ]
                            foreach (var Leave in _LvData)
                            {
                                var _Leave = _LvData.Where(lv => lv.EmpDate == att.EmpDate && lv.HalfLeave != true);
                                if (_Leave.Count() > 0)
                                {
                                    att.StatusLeave = true;
                                    att.StatusAB = false;
                                    att.DutyCode = "L";
                                    att.StatusDO = false;
                                    if (Leave.LvCode == "A")
                                        att.Remarks = "[CL]";
                                    else if (Leave.LvCode == "B")
                                        att.Remarks = "[AL]";
                                    else if (Leave.LvCode == "C")
                                        att.Remarks = "[SL]";
                                    else
                                        att.Remarks = "[" + _Leave.FirstOrDefault().LvType.LvDesc + "]";
                                }
                                else
                                {
                                    att.StatusLeave = false;
                                }
                            }

                             //////////////////////////
                            //Check for Half Leave///
                           /////////////////////////
                            var _HalfLeave = _LvData.Where(lv => lv.EmpDate == att.EmpDate && lv.HalfLeave == true);
                            if (_HalfLeave.Count() > 0)
                            {
                                att.StatusLeave = true;
                                att.StatusAB = false;
                                att.DutyCode = "L";
                                att.StatusHL = true;
                                att.StatusDO = false;
                                if (_HalfLeave.FirstOrDefault().LvCode == "A")
                                    att.Remarks = "[H-CL]";
                                else if (_HalfLeave.FirstOrDefault().LvCode == "B")
                                    att.Remarks = "[S-AL]";
                                else if (_HalfLeave.FirstOrDefault().LvCode == "C")
                                    att.Remarks = "[H-SL]";
                                else
                                    att.Remarks = "[Half Leave]";
                            }
                            else
                            {
                                att.StatusLeave = false;
                            }
                            ctx.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            _myHelperClass.WriteToLogFile("-------Error In Creating Attendance of Employee: " + emp.EmpNo + " ------" + ex.InnerException.Message);
                        }
                    }
                }
                ctx.Dispose();
            }
        }
        //public void DownloadData(List<Reader> reader)
        //{
        //    try
        //    {
        //        List<ReadersCommLibrary.Poll> records = new List<ReadersCommLibrary.Poll>();
        //        using (var context = new TAS2013Entities())
        //        {
        //            // Ping to Device and recieve true if device exists
        //            if (IsConnectedToInternet(reader.FirstOrDefault().IpAdd))
        //            {
        //                ReadersCommLibrary.IReader readerHelper = new ReadersCommLibrary.ZKReader();
        //                // Connect function and recieve true if connection established
        //                if (readerHelper.Connect(reader.FirstOrDefault().IpAdd, reader.FirstOrDefault().IpPort))
        //                {
        //                    try
        //                    {
        //                        // Download Records from Device and store it to records
        //                        SaveServiceLog(reader.FirstOrDefault().RdrID, "Data Downloading Start", 3);
        //                        records = readerHelper.DownloadData(5);
        //                        readerHelper.Disconnect();
        //                        // Save Device Attendance Data to Poll Data
        //                        try
        //                        {
        //                            SaveAttDataToPollData(records, reader.FirstOrDefault().RdrID);
        //                            // Enter Service log -- ErrorCode = 5 -- Data Downloaded Complete
        //                            SaveServiceLog(reader.FirstOrDefault().RdrID, "Data Downloaded Complete-Total Records are:" + records.Count.ToString(), 5);
        //                            //// Enter Reader Event log of performed operation
        //                            //SaveRdrEventLog(reader.FirstOrDefault().RdrID, "Download", 5);
        //                        }
        //                        catch (Exception ex)
        //                        {
        //                            SaveServiceLog(reader.FirstOrDefault().RdrID, "PollData Save Error with exception:" + ex.InnerException.Message, 6); //ErrorCode = 6 -- PollData Save Error
        //                        }
        //                    }
        //                    catch (Exception e)
        //                    {
        //                        // Enter Service log if downloading not performed or recieve error
        //                        SaveServiceLog(reader.FirstOrDefault().RdrID, "Data Downloaded Failed with exception:" + e.InnerException.Message, 4); //ErrorCode = 4 -- Data Download Failed
        //                    }
        //                }
        //                else
        //                {
        //                    // enter Service log for Reader failed to Connect -- //ErrorCode = 2 -- notconnected
        //                    SaveServiceLog(reader.FirstOrDefault().RdrID, "Reader failed to Connect", 2);
        //                }
        //            }
        //            else
        //            {
        //                //Service Log for reader not pinged
        //                //ErrorCode = 1 -- notpinged
        //                SaveServiceLog(reader.FirstOrDefault().RdrID, "Reader Not Pinged", 1);
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //    }
        //}
        // Save Service Log
        private void SaveServiceLog(short _RdrID,string _Description,byte _ErrorCode)
        {
            try
            {
                using (var context = new TAS2013Entities())
                {
                    ServiceLog _ServiceLog = new ServiceLog();
                    _ServiceLog.Description = _Description;
                    _ServiceLog.ErrorCode = _ErrorCode;
                    _ServiceLog.DateTime = DateTime.Now;
                    _ServiceLog.ReaderID = _RdrID;
                    _ServiceLog.Processed = false;
                    context.ServiceLogs.AddObject(_ServiceLog);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {

            }
        }
 // Save Rdr Event log
 private void SaveRdrEventLog(short _RdrID, string _Description, byte _ErrorCode)
 {
     using (var context = new TAS2013Entities())
     {
         RdrEventLog _rdrEventLog = new RdrEventLog();
         _rdrEventLog.ReaderID = _RdrID;
         _rdrEventLog.Description = _Description;
         _rdrEventLog.ErrorCode = _ErrorCode;
         _rdrEventLog.EventDate = DateTime.Now;
         context.RdrEventLogs.AddObject(_rdrEventLog);
         context.SaveChanges();
     }
 }
        private void SaveAttDataToPollData(List<ReadersCommLibrary.Poll> records, Int16 _RdrID)
        {
            using (var context = new TAS2013Entities())
            {
                List<Emp> emps = context.Emps.Where(aa => aa.Status == true).ToList();
                foreach (var entry in records)
                {
                    try
                    {
                        var emp = emps.Where(aa => aa.FpID == entry.ID);
                        var reader = context.Readers.Where(aa => aa.RdrID == _RdrID);
                        if (emp.Count() > 0)
                        {
                            PollData p = new PollData();
                            p.EmpID = emp.FirstOrDefault().EmpID;
                            p.EntDate = entry.EntryDateTime.Date;
                            p.EntTime = entry.EntryDateTime;
                            p.EmpDate = emp.FirstOrDefault().EmpID.ToString() + entry.EntryDateTime.Date.ToString("yyMMdd");
                            p.CardNo = emp.FirstOrDefault().CardNo;
                            p.RdrID = reader.FirstOrDefault().RdrID;
                            p.FpID = entry.ID;
                            p.RdrDuty = reader.FirstOrDefault().RdrDutyCode.RdrDutyID;
                            p.Split = false;
                            p.Process = false;
                            p.AddedDate = DateTime.Today;
                            context.PollDatas.AddObject(p);
                            context.SaveChanges();
                        }
                        else
                        {
                            PollDataError pde = new PollDataError();
                            pde.DeviceRegID = entry.ID;
                            pde.EntryDate = entry.EntryDateTime.Date;
                            pde.EntryTime = entry.EntryDateTime;
                            pde.AddedDate = DateTime.Today;
                            context.PollDataErrors.AddObject(pde);
                            context.SaveChanges();
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }
        public static void CalculateSummary(DateTime dateStart, String criteria, int criteriaValue)
        {
            bool ProcssedDS = false;
            DateTime makeMe = new DateTime(dateStart.Year, dateStart.Month, dateStart.Day, 0, 0, 0);
            SummaryEntity summary = new SummaryEntity();

            TAS2013Entities context = new TAS2013Entities();
            ViewMultipleInOut vmio = new ViewMultipleInOut();
            summary.SummaryDateCriteria = dateStart.ToString("yyMMdd") + summary.criterianame + criteriaValue;
            String day = DateTime.Today.DayOfWeek.ToString();
            day = day.Substring(0, 3);
            day = day + "Min";

            List<ViewMultipleInOut> attList = new List<ViewMultipleInOut>();
            switch (criteria)
            {
                case "C": attList = context.ViewMultipleInOuts.Where(aa => aa.AttDate == dateStart).ToList();
                    //Change the below line for NHA please
                    summary.criterianame = context.Options.FirstOrDefault().CompanyName;
                    summary.SummaryDateCriteria = summary.SummaryDateCriteria + criteria + criteriaValue;

                    break;

                case "R": attList = context.ViewMultipleInOuts.Where(aa => aa.AttDate == dateStart).ToList();
                    attList = attList.Where(aa => aa.RegionID == criteriaValue).ToList();
                    if (attList.Count > 0)
                        summary.criterianame = attList[0].RegionName;
                    break;
                case "D": attList = context.ViewMultipleInOuts.Where(aa => aa.AttDate == dateStart && aa.DeptID == criteriaValue).ToList();
                    if (attList.Count > 0)
                        summary.criterianame = attList[0].DeptName;
                    break;
                case "E": attList = context.ViewMultipleInOuts.Where(aa => aa.AttDate == dateStart && aa.SecID == criteriaValue).ToList();
                    if (attList.Count > 0)
                        summary.criterianame = attList[0].SectionName;
                    break;
                case "L": attList = context.ViewMultipleInOuts.Where(aa => aa.AttDate == dateStart && aa.LocID == criteriaValue).ToList();
                    if (attList.Count > 0)
                        summary.criterianame = attList[0].LocName;
                    break;
                case "S": attList = context.ViewMultipleInOuts.Where(aa => aa.AttDate == dateStart && aa.ShiftID == criteriaValue).ToList();
                    if (attList.Count > 0)
                        summary.criterianame = attList[0].ShiftName;
                    break;
                default: attList = context.ViewMultipleInOuts.Where(aa => aa.AttDate >= dateStart).ToList();
                    if (attList.Count > 0)
                        summary.criterianame = context.Options.FirstOrDefault().CompanyName;
                    break;

            }
            if (attList.Count > 0)
            {
                DailySummary dailysumm = new DailySummary();
                if (context.DailySummaries.Where(aa => aa.SummaryDateCriteria == summary.SummaryDateCriteria).Count() > 0)
                {
                    dailysumm = context.DailySummaries.First(aa => aa.SummaryDateCriteria == summary.SummaryDateCriteria);
                    ProcssedDS = true;
                }
                dailysumm.SummaryDateCriteria = summary.SummaryDateCriteria;
                dailysumm.Criteria = criteria;
                dailysumm.CriteriaValue = (short)criteriaValue;
                dailysumm.CriteriaName = summary.criterianame;
                dailysumm.TotalEmps = (short)attList.Count;
                dailysumm.PresentEmps = (short)attList.Where(aa => aa.StatusP == true).Count<ViewMultipleInOut>();
                dailysumm.AbsentEmps = (short)(dailysumm.TotalEmps - dailysumm.PresentEmps);
                dailysumm.Date = makeMe;
                dailysumm.EIEmps = (short)attList.Where(aa => aa.StatusEI == true).Count<ViewMultipleInOut>();
                dailysumm.EOEmps = (short)attList.Where(aa => aa.StatusEO == true).Count<ViewMultipleInOut>();
                dailysumm.LIEmps = (short)attList.Where(aa => aa.StatusLI == true).Count<ViewMultipleInOut>();
                dailysumm.LOEmps = (short)attList.Where(aa => aa.StatusLO == true).Count<ViewMultipleInOut>();
                dailysumm.LvEmps = 0;
                dailysumm.HalfLvEmps = 0;
                dailysumm.ShortLvEmps = 0;
                dailysumm.ExpectedWorkMins = 0;
                dailysumm.ActualWorkMins = 0;
                dailysumm.LossWorkMins = 0;
                dailysumm.OTMins = 0;
                dailysumm.LIMins = 0;
                dailysumm.LOMins = 0;
                dailysumm.EIMins = 0;
                dailysumm.EOMins = 0;
                dailysumm.AOTMins = 0;
                dailysumm.OTEmps = 0;
                dailysumm.OTMins = 0;
                long averageTimeIn = 0;
                long averageTimeOut = 0;
                //LV ,short half
                foreach (var emp in attList)
                {
                    if (emp.TimeIn != null)
                        averageTimeIn = averageTimeIn + emp.TimeIn.Value.Ticks;
                    if (emp.TimeOut != null)
                        averageTimeOut = averageTimeOut + emp.TimeOut.Value.Ticks;

                    if (emp.LateIn != null)
                        dailysumm.LIMins = dailysumm.LIMins + emp.LateIn;
                    if (emp.LateOut != null)
                        dailysumm.LOMins = dailysumm.LOMins + emp.LateOut;
                    if (emp.EarlyIn != null)
                        dailysumm.EIMins = dailysumm.EIMins + emp.EarlyIn;
                    if (emp.EarlyOut != null)
                        dailysumm.EOMins = dailysumm.EOMins + emp.EarlyOut;
                    if (emp.WorkMin != null)
                        dailysumm.ActualWorkMins = dailysumm.ActualWorkMins + emp.WorkMin;
                    //code leave bundle
                    if (emp.StatusLeave == true)
                        dailysumm.LvEmps = dailysumm.LvEmps++;
                    if (emp.StatusHL == true)
                        dailysumm.HalfLvEmps = dailysumm.HalfLvEmps++;
                    if (emp.StatusSL == true)
                        dailysumm.ShortLvEmps = dailysumm.ShortLvEmps++;

                    //code for over time emps
                    if (emp.StatusOT == true)
                    {
                        dailysumm.OTEmps = (short)(dailysumm.OTEmps + 1);
                        dailysumm.OTMins = dailysumm.OTMins + emp.OTMin;
                    }

                    //code for day off
                    if (emp.StatusDO == true)
                        dailysumm.DayOffEmps++;

                    //code for expected work mins
                    if (emp.ShifMin != null)
                        dailysumm.ExpectedWorkMins = emp.ShifMin + dailysumm.ExpectedWorkMins;

                }

                // dailysumm.OnTimeEmps = (short)(dailysumm.TotalEmps - dailysumm.OTEmps);

                dailysumm.DayOffEmps = (short)(dailysumm.ShortLvEmps + dailysumm.LvEmps);
                dailysumm.LossWorkMins = dailysumm.ExpectedWorkMins - dailysumm.ActualWorkMins;

                try
                {
                    if (dailysumm.PresentEmps != 0)
                        dailysumm.AvgTimeIn = new DateTime((long)(averageTimeIn / dailysumm.PresentEmps)).TimeOfDay;
                    if (dailysumm.PresentEmps != 0)
                        dailysumm.AvgTimeOut = new DateTime((long)(averageTimeOut / dailysumm.PresentEmps)).TimeOfDay;
                    if (dailysumm.TotalEmps != 0)
                        dailysumm.AActualMins = (short)(dailysumm.ActualWorkMins / dailysumm.TotalEmps);
                    if (dailysumm.TotalEmps != 0)
                        dailysumm.ALossMins = (short)(dailysumm.LossWorkMins / dailysumm.TotalEmps);
                    if (dailysumm.TotalEmps != 0)
                        dailysumm.AExpectedMins = (short)(dailysumm.ExpectedWorkMins / dailysumm.TotalEmps);
                    if (dailysumm.LIEmps != 0)
                        dailysumm.ALIMins = (short)(dailysumm.LIMins / dailysumm.LIEmps);
                    if (dailysumm.LOEmps != 0)
                        dailysumm.ALOMins = (short)(dailysumm.LOMins / dailysumm.LOEmps);
                    if (dailysumm.EIEmps != 0)
                        dailysumm.AEIMins = (short)(dailysumm.EIMins / dailysumm.EIEmps);
                    if (dailysumm.EOEmps != 0)
                        dailysumm.AEOMins = (short)(dailysumm.EOMins / dailysumm.EOEmps);
                    if (dailysumm.OTEmps != 0)
                        dailysumm.AOTMins = (short)(dailysumm.OTMins / dailysumm.OTEmps);
                }
                catch (Exception e)
                { }
                if (ProcssedDS == false)
                    context.DailySummaries.AddObject(dailysumm);
                context.SaveChanges();

            }
        }
 private void AdjustPollData()
 {
     int currentYear = DateTime.Today.Date.Year;
     int StartYear = currentYear - 3;
     DateTime endDate = DateTime.Today.AddDays(2);
     DateTime startDate = new DateTime(StartYear,1,1);
     using (var ctx = new TAS2013Entities())
     {
         List<PollData> polls = ctx.PollDatas.Where(aa=>aa.EntDate <= startDate &&aa.Process == false).ToList();
         foreach (var item in polls)
         {
             item.Process = true;
         }
         ctx.SaveChanges();
         polls.Clear();
         polls = ctx.PollDatas.Where(aa => aa.EntDate >= endDate && aa.Process == false).ToList();
         foreach (var item in polls)
         {
             item.Process = true;
         }
         ctx.SaveChanges();
         ctx.Dispose();
     }
 }
        private void CorrectAttEntriesWithWrongFlags(DateTime startdate, DateTime endDate)
        {
            using (var ctx = new TAS2013Entities())
            {
                // where StatusGZ ==1 and DutyCode != G
                List<AttData> _attDataForGZ = ctx.AttDatas.Where(aa => aa.AttDate >= startdate && aa.AttDate<= endDate && aa.StatusGZ==true && aa.DutyCode != "G").ToList();
                foreach (var item in _attDataForGZ)
                {
                    item.DutyCode  ="G";
                }
                ctx.SaveChanges();

                // where StatusDO ==1 and DutyCode != R
                List<AttData> _attDataForDO = ctx.AttDatas.Where(aa => aa.AttDate >= startdate && aa.AttDate <= endDate && aa.StatusDO == true && aa.DutyCode != "R").ToList();
                foreach (var item in _attDataForGZ)
                {
                    item.DutyCode = "R";
                }
                ctx.SaveChanges();
                ctx.Dispose();
            }
        }
        public void ProcessManualEditAttendance(DateTime _dateStart, DateTime _dateEnd)
        {
            List<AttDataManEdit> _attEdit = new List<AttDataManEdit>();
            List<AttData> _AttData = new List<AttData>();
            AttData _TempAttData = new AttData();
            using (var ctx = new TAS2013Entities())
            {

                if (_dateStart == _dateEnd)
                {
                    _attEdit = ctx.AttDataManEdits.Where(aa => aa.NewTimeIn == _dateStart).OrderBy(aa => aa.EditDateTime).ToList();
                    _dateEnd = _dateEnd + new TimeSpan(23, 59, 59);
                    //_attEdit = ctx.AttDataManEdits.Where(aa => aa.NewTimeIn >= _dateStart && aa.NewTimeIn <= _dateEnd && (aa.EmpID == 472)).OrderBy(aa => aa.EditDateTime).ToList();
                    _AttData = ctx.AttDatas.Where(aa => aa.AttDate == _dateStart).ToList();
                }
                else
                {
                    _attEdit = ctx.AttDataManEdits.Where(aa => aa.NewTimeIn >= _dateStart && aa.NewTimeOut <= _dateEnd).OrderBy(aa => aa.EditDateTime).ToList();
                    //_attEdit = ctx.AttDataManEdits.Where(aa => aa.NewTimeIn >= _dateStart && (aa.NewTimeOut <= _dateEnd && aa.EmpID == 472)).OrderBy(aa => aa.EditDateTime).ToList();
                    _AttData = ctx.AttDatas.Where(aa => aa.AttDate >= _dateStart && aa.AttDate <= _dateEnd).ToList();
                }

                foreach (var item in _attEdit)
                {
                    _TempAttData = _AttData.First(aa => aa.EmpDate == item.EmpDate);
                    _TempAttData.TimeIn = item.NewTimeIn;
                    _TempAttData.TimeOut = item.NewTimeOut;
                    _TempAttData.DutyCode = item.NewDutyCode;
                    _TempAttData.DutyTime = item.NewDutyTime;
                    switch (_TempAttData.DutyCode)
                    {
                        case "D":
                            _TempAttData.StatusAB = true;
                            _TempAttData.StatusP = false;
                            _TempAttData.StatusMN = true;
                            _TempAttData.StatusDO = false;
                            _TempAttData.StatusGZ = false;
                            _TempAttData.StatusLeave = false;
                            _TempAttData.StatusOT = false;
                            _TempAttData.OTMin = null;
                            _TempAttData.EarlyIn = null;
                            _TempAttData.EarlyOut = null;
                            _TempAttData.LateIn = null;
                            _TempAttData.LateOut = null;
                            _TempAttData.WorkMin = null;
                            _TempAttData.GZOTMin = null;
                            break;
                        case "G":
                            _TempAttData.StatusAB = false;
                            _TempAttData.StatusP = false;
                            _TempAttData.StatusMN = true;
                            _TempAttData.StatusDO = false;
                            _TempAttData.StatusGZ = true;
                            _TempAttData.StatusLeave = false;
                            _TempAttData.StatusOT = false;
                            _TempAttData.OTMin = null;
                            _TempAttData.EarlyIn = null;
                            _TempAttData.EarlyOut = null;
                            _TempAttData.LateIn = null;
                            _TempAttData.LateOut = null;
                            _TempAttData.WorkMin = null;
                            _TempAttData.GZOTMin = null;
                            break;
                        case "R":
                            _TempAttData.StatusAB = false;
                            _TempAttData.StatusP = false;
                            _TempAttData.StatusMN = true;
                            _TempAttData.StatusDO = true;
                            _TempAttData.StatusGZ = false;
                            _TempAttData.StatusLeave = false;
                            _TempAttData.StatusOT = false;
                            _TempAttData.OTMin = null;
                            _TempAttData.EarlyIn = null;
                            _TempAttData.EarlyOut = null;
                            _TempAttData.LateIn = null;
                            _TempAttData.LateOut = null;
                            _TempAttData.WorkMin = null;
                            _TempAttData.GZOTMin = null;
                            break;
                    }
                    if (_TempAttData.TimeIn != null && _TempAttData.TimeOut != null)
                    {
                        //If TimeIn = TimeOut then calculate according to DutyCode
                        if (_TempAttData.TimeIn == _TempAttData.TimeOut)
                        {
                            CalculateInEqualToOut(_TempAttData);
                        }
                        else
                        {
                            if (_TempAttData.DutyTime == new TimeSpan(0, 0, 0))
                            {
                                CalculateOpenShiftTimes(_TempAttData, _TempAttData.Emp.Shift);
                            }
                            else
                            {
                                //if (attendanceRecord.TimeIn.Value.Date.Day == attendanceRecord.TimeOut.Value.Date.Day)
                                //{
                                CalculateShiftTimes(_TempAttData, _TempAttData.Emp.Shift);
                                //}
                                //else
                                //{
                                //    CalculateOpenShiftTimes(attendanceRecord, shift);
                                //}
                            }
                        }

                        //If TimeIn = TimeOut then calculate according to DutyCode
                    }
                    ctx.SaveChanges();
                }
                ctx.Dispose();

            }
            _myHelperClass.WriteToLogFile("ProcessManual Attendance Completed: ");
        }