public JsonResult VisitorNotify(DAL.db.Appointment appointment)
 {
     appointment.ReachTime        = DateTime.Now;
     emailUtility                 = new EmailUtility();
     unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
     result = unScheduleAppointmentFactory.SaveAppointment(appointment);
     if (result.isSucess)
     {
         //if (emailUtility.IsInternetConnected())
         //{
         //    var email = db.Employees.Where(x => x.EmployeeID == appointment.EmployeeID).FirstOrDefault();
         //    if (email != null && email.Email != null)
         //    {
         //        bool isMail = emailUtility.SentMail(email.Email, appointment.CompanyName, appointment.VisitorName, "", "");
         //        if (isMail)
         //        {
         //            result.message = "Notify And Email Successful.";
         //        }
         //        return Json(result, JsonRequestBehavior.AllowGet);
         //    }
         //}
         result.message = "Notify Successful.";
     }
     return(Json(result, JsonRequestBehavior.AllowGet));
 }
        public JsonResult VisitorOut(string cardNO)
        {
            //string cardID;
            if (cardNO != null)
            {
                cardFactory = new CardFactorys();
                utility     = new Utility();

                var appData = db.Appointments.Where(x => (x.CardNO == cardNO) && (x.Status == "I")).FirstOrDefault();

                if (appData != null)
                {
                    db.Entry(appData).State = EntityState.Detached;
                    appData.CheckedOutTime  = DateTime.Now;
                    appData.Status          = "O";

                    var cardInfo = db.Cards.Where(x => x.CardNO == cardNO).ToList();

                    if (cardInfo.Count > 0)
                    {
                        foreach (var card in cardInfo)
                        {
                            var deviceInfo = db.DeviceInfoes.Where(x => x.DeviceNO == card.DeviceNO).FirstOrDefault();

                            if (deviceInfo != null)
                            {
                                unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
                                result = unScheduleAppointmentFactory.SaveAppointment(appData);
                                if (result.isSucess)
                                {
                                    card.CardNO    = null;
                                    result         = cardFactory.SaveCard(card);
                                    result.message = "Checked OUT Successful.";
                                }
                            }
                            else
                            {
                                result.message = card.DeviceNO + " Number Device Not Found!";
                                return(Json(result, JsonRequestBehavior.AllowGet));
                            }
                        }
                    }
                }
                else
                {
                    result.isSucess = false;
                    result.message  = "No Checked IN Visitor Found Against This Card";
                }
            }
            else
            {
                result.isSucess = false;
                result.message  = "Please Punch Visitor Card";
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public JsonResult SaveCancelAppointment(DAL.db.Appointment appointment, List <VM_Employee> empList)
        {
            int empID = Convert.ToInt32(dictionary[1].Id == "" ? 0 : Convert.ToInt32(dictionary[1].Id));

            if (empID == 0)
            {
                result.sessionOut = true;
                return(Json(result, JsonRequestBehavior.AllowGet));
            }

            // Testing
            if (empList == null)
            {
                unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
                appointment.CreatedBy        = empID;
                appointment.CreatedDate      = toDayTime;
                appointment.AppointmentTime  = appointment.AppointmentTime;
                appointment.ReachTime        = toDayTime;
                appointment.AppointmentDate  = appointment.AppointmentDate;
                result = unScheduleAppointmentFactory.SaveAppointment(appointment);
                if (result.isSucess)
                {
                    emailUtility = new EmailUtility();
                    unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
                    appointment.Status           = "C";
                    result = unScheduleAppointmentFactory.SaveAppointment(appointment);
                }
            }
            else
            {
                foreach (var floor in empList)
                {
                    unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
                    if (floor.AppointmentID < 1)
                    {
                        appointment.AppointmentID = 0;
                    }
                    appointment.CreatedBy       = empID;
                    appointment.CreatedDate     = toDayTime;
                    appointment.AppointmentTime = appointment.AppointmentTime;
                    appointment.AppointmentDate = appointment.AppointmentDate;
                    appointment.ReachTime       = toDayTime;
                    appointment.EmployeeID      = floor.EmployeeID;
                    result = unScheduleAppointmentFactory.SaveAppointment(appointment);
                }
            }

            result.lastInsertedID = appointment.EmployeeID;
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public JsonResult VisitorBreakIn(DAL.db.Appointment appointment)
        {
            //Card card = new Card();
            //string cardID = string.Empty;
            if (appointment.CardNO != string.Empty)
            {
                int deviceNo = 1;
                cardFactory = new CardFactorys();
                utility     = new Utility();

                // Getting device Information
                //var empInfo = db.Employees.Where(x => x.EmployeeID == appointment.EmployeeID).FirstOrDefault();

                var appInfo = db.Appointments.Where(x => x.AppointmentID == appointment.AppointmentID).FirstOrDefault();
                db.Entry(appInfo).State = EntityState.Detached;

                if (appointment.Status == "I")
                {
                    var cardInfo = cardFactory.GetFreeCard(deviceNo);

                    if (cardInfo != null)
                    {
                        //cardID = cardInfo.CardID.ToString();
                        cardInfo.CardNO = appointment.CardNO;
                        result          = cardFactory.SaveCard(cardInfo);

                        appInfo.CardNO      = appointment.CardNO;
                        appInfo.BreakInTime = DateTime.Now;
                        appInfo.Status      = appointment.Status;

                        if (result.isSucess)
                        {
                            unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
                            result = unScheduleAppointmentFactory.SaveAppointment(appInfo);
                            if (result.isSucess)
                            {
                                result.message = "Visitor Checked IN From Break Successful.";
                            }
                        }
                    }
                }
            }
            else
            {
                result.message = "Please Punch Visitor Card";
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public JsonResult VisitorBreakOut(MV_Appointment appointment)
        {
            //Card card = new Card();
            string cardID = string.Empty;

            if (appointment.CardNO != string.Empty)
            {
                int deviceNo = 1;
                cardFactory = new CardFactorys();
                utility     = new Utility();

                // Getting device Information
                //var empInfo = db.Employees.Where(x => x.EmployeeID == appointment.EmployeeID).FirstOrDefault();

                //DAL.db.Appointment appInfo = new DAL.db.Appointment();

                var appInfo = db.Appointments.Where(x => x.AppointmentID == appointment.AppointmentID).FirstOrDefault();
                db.Entry(appInfo).State = EntityState.Detached;


                if (appointment.Status == "B")
                {
                    //var cardInfo = cardFactory.GetFreeCard(deviceNo);
                    appInfo.Status = appointment.Status;
                    result         = cardFactory.UnassignCard(deviceNo, appInfo.CardNO);
                    if (result.isSucess)
                    {
                        unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
                        appInfo.BreakOutTime         = DateTime.Now;
                        result = unScheduleAppointmentFactory.SaveAppointment(appInfo);
                        if (result.isSucess)
                        {
                            result.message = "Visitor Checked Out For Break Successful.";
                        }
                    }
                    else
                    {
                        result.message = appInfo.CardNO + " No card not found for break!!!";
                    }
                }
            }
            else
            {
                result.message = "Please Punch Visitor Card";
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public JsonResult VisitorIn(DAL.db.Appointment appointment)
        {
            int    deviceNo = 1;
            string cardID   = string.Empty;

            if (appointment.CardNO != string.Empty)
            {
                cardFactory = new CardFactorys();
                utility     = new Utility();

                // Getting device Information
                //var empInfo = db.Employees.Where(x => x.EmployeeID == appointment.EmployeeID).FirstOrDefault();
                if (appointment.Status == "I")
                {
                    var cardInfo = cardFactory.GetFreeCard(deviceNo);
                    if (cardInfo != null)
                    {
                        appointment.CheckedInTime = DateTime.Now;
                        cardID = cardInfo.CardID.ToString();

                        //result = utility.AssignToDevice(deviceNo, cardID, appointment.CardNO);
                        cardInfo.CardNO = appointment.CardNO;
                        result          = cardFactory.SaveCard(cardInfo);

                        if (result.isSucess)
                        {
                            unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
                            result = unScheduleAppointmentFactory.SaveAppointment(appointment);
                            if (result.isSucess)
                            {
                                result.message = "Checked IN Successful.";
                            }
                        }
                    }
                    else
                    {
                        result.isSucess = false;
                        result.message  = deviceNo + " NO. Free Card Found.";
                    }
                }
            }
            else
            {
                result.message = "Please Punch Visitor Card";
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public JsonResult VisitorInWithDevice(DAL.db.Appointment appointment)
        {
            //Card card = new Card();
            string cardID = string.Empty;

            if (appointment.CardNO != string.Empty)
            {
                string ip       = "";
                int    deviceNo = 0;
                int    portNO   = 0;

                cardFactory = new CardFactorys();
                utility     = new Utility();

                // Getting device Information
                var empInfo    = db.Employees.Where(x => x.EmployeeID == appointment.EmployeeID).FirstOrDefault();
                var deviceInfo = db.DeviceInfoes.Where(x => x.FloorNO == empInfo.Floor).FirstOrDefault();

                //var cardInfo = db.Cards.Where(x => x.CardNO == appointment.CardNO).ToList();

                if (deviceInfo != null)
                {
                    ip       = deviceInfo.IPAddress;
                    deviceNo = deviceInfo.DeviceNO;
                    portNO   = deviceInfo.PortNO;

                    if (appointment.Status == "I")
                    {
                        result = utility.Connect(ip, deviceNo, portNO);

                        if (result.isSucess)
                        {
                            var cardInfo = cardFactory.GetFreeCard(deviceNo);

                            if (cardInfo != null)
                            {
                                appointment.CheckedInTime = DateTime.Now;
                                cardID = cardInfo.CardID.ToString();

                                result = utility.AssignToDevice(deviceNo, cardID, appointment.CardNO);

                                //var card = new Card { CardID = Convert.ToInt16(cardID), CardNO = appointment.CardNO, DeviceNO = deviceNo };
                                cardInfo.CardNO = appointment.CardNO;
                                result          = cardFactory.SaveCard(cardInfo);

                                if (result.isSucess)
                                {
                                    unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
                                    result = unScheduleAppointmentFactory.SaveAppointment(appointment);
                                    if (result.isSucess)
                                    {
                                        result.message = "Checked IN Successful.";
                                    }
                                }
                            }
                            else
                            {
                                result.isSucess = false;
                                result.message  = deviceNo + " NO. Device Not Found.";
                            }
                        }
                        else
                        {
                            result.message = deviceNo + " Device Not Connected.";
                            return(Json(result, JsonRequestBehavior.AllowGet));
                        }
                    }
                }
            }
            else
            {
                result.message = "Please Punch Visitor Card";
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public JsonResult SaveUnScheduleAppointment(DAL.db.Appointment appointment)
        {
            int empID = Convert.ToInt32(dictionary[1].Id == "" ? 0 : Convert.ToInt32(dictionary[1].Id));

            if (empID == 0)
            {
                result.sessionOut = true;
                return(Json(result, JsonRequestBehavior.AllowGet));
            }

            unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
            appointment.CreatedBy        = empID;
            appointment.CreatedDate      = toDayTime;
            appointment.AppointmentTime  = appointment.AppointmentTime;
            appointment.ReachTime        = toDayTime;
            appointment.AppointmentDate  = appointment.AppointmentDate;

            appointment.ReachTime     = DateTime.Now;
            appointment.Status        = "N";
            appointment.NotifyMessage = "Mr." + " " + appointment.VisitorName + " " + "wants to meet with you.";


            result = unScheduleAppointmentFactory.SaveAppointment(appointment);
            if (result.isSucess)
            {
                result.message = "Notify Successful.";

                //appointment.ReachTime = DateTime.Now;
                //emailUtility = new EmailUtility();
                //unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
                //appointment.Status = "N";
                //appointment.NotifyMessage = "Mr." + " " + appointment.VisitorName + " " + "wants to meet with you.";
                //result = unScheduleAppointmentFactory.SaveAppointment(appointment);
                //if (result.isSucess)
                //{
                //    result.message = "Notify Successful.";
                //}
            }

            //else
            //{
            //    foreach (var floor in empList)
            //    {
            //        unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
            //        if (floor.AppointmentID < 1)
            //        {
            //            appointment.AppointmentID = 0;
            //        }
            //        appointment.CreatedBy = empID;
            //        appointment.CreatedDate = toDayTime;
            //        appointment.AppointmentTime = appointment.AppointmentTime;
            //        appointment.AppointmentDate = appointment.AppointmentDate;
            //        appointment.ReachTime = toDayTime;
            //        appointment.EmployeeID = floor.EmployeeID;
            //        result = unScheduleAppointmentFactory.SaveAppointment(appointment);
            //        if (result.isSucess)
            //        {
            //            appointment.ReachTime = DateTime.Now;
            //            emailUtility = new EmailUtility();
            //            unScheduleAppointmentFactory = new UnScheduleAppointmentFactorys();
            //            result = unScheduleAppointmentFactory.SaveAppointment(appointment);
            //            if (result.isSucess)
            //            {
            //                //if (emailUtility.IsInternetConnected())
            //                //{
            //                //    var email = db.Employees.Where(x => x.EmployeeID == appointment.EmployeeID).FirstOrDefault();
            //                //    if (email != null && email.Email != null)
            //                //    {
            //                //        bool isMail = emailUtility.SentMail(email.Email, appointment.CompanyName, appointment.VisitorName, "", "");
            //                //        if (isMail)
            //                //        {
            //                //            result.message = "Notify And Email Successful.";
            //                //        }
            //                //        return Json(result, JsonRequestBehavior.AllowGet);
            //                //    }
            //                //}
            //                result.message = "Notify Successful.";
            //            }

            //        }
            //    }
            //}

            result.lastInsertedID = appointment.EmployeeID;
            return(Json(result, JsonRequestBehavior.AllowGet));
        }