Example #1
0
        public string AddMessageToSMSQueue(BO.SMS message)
        {
            try
            {
                //Set AccessToken to client header
                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("Authorization", AccessToken);
                client.BaseAddress = new Uri(NotificationServiceBaseURL);

                HttpResponseMessage response = client.PostAsJsonAsync("SMS/AddMessageToQueue", message).Result;

                response.EnsureSuccessStatusCode();
                var status = response.Content.ReadAsStringAsync().Result;

                return(status);
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }
Example #2
0
        public override object Save <T>(T entity)
        {
            BO.AttorneyVisit AttorneyVisitBO = (BO.AttorneyVisit)(object) entity;
            BO.CalendarEvent CalendarEventBO = AttorneyVisitBO.CalendarEvent;

            AttorneyVisit AttorneyVisitDB = new AttorneyVisit();
            bool          sendMessage     = false;

            using (var dbContextTransaction = _context.Database.BeginTransaction())
            {
                bool IsEditMode             = false;
                bool IsAddModeCalendarEvent = false;
                IsEditMode = (AttorneyVisitBO != null && AttorneyVisitBO.ID > 0) ? true : false;

                if (AttorneyVisitBO.ID <= 0 && AttorneyVisitBO.PatientId.HasValue == false && AttorneyVisitBO.CompanyId.HasValue == false)
                {
                    IsEditMode             = (CalendarEventBO != null && CalendarEventBO.ID > 0) ? true : false;
                    IsAddModeCalendarEvent = (CalendarEventBO != null && CalendarEventBO.ID > 0) ? false : true;
                }

                CalendarEvent CalendarEventDB = new CalendarEvent();
                #region Calendar Event
                if (CalendarEventBO != null)
                {
                    bool Add_CalendarEventDB = false;
                    CalendarEventDB = _context.CalendarEvents.Where(p => p.Id == CalendarEventBO.ID &&
                                                                    (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                      .FirstOrDefault();

                    if (CalendarEventDB == null && CalendarEventBO.ID <= 0)
                    {
                        CalendarEventDB     = new CalendarEvent();
                        Add_CalendarEventDB = true;
                    }
                    else if (CalendarEventDB == null && CalendarEventBO.ID > 0)
                    {
                        dbContextTransaction.Rollback();
                        return(new BO.ErrorObject {
                            errorObject = "", ErrorMessage = "Calendar Event details dosent exists.", ErrorLevel = ErrorLevel.Error
                        });
                    }

                    CalendarEventDB.Name                = IsEditMode == true && CalendarEventBO.Name == null ? CalendarEventDB.Name : CalendarEventBO.Name;
                    CalendarEventDB.EventStart          = IsEditMode == true && CalendarEventBO.EventStart.HasValue == false ? CalendarEventDB.EventStart : CalendarEventBO.EventStart.Value;
                    CalendarEventDB.EventEnd            = IsEditMode == true && CalendarEventBO.EventEnd.HasValue == false ? CalendarEventDB.EventEnd : CalendarEventBO.EventEnd.Value;
                    CalendarEventDB.TimeZone            = CalendarEventBO.TimeZone;
                    CalendarEventDB.Description         = CalendarEventBO.Description;
                    CalendarEventDB.RecurrenceId        = CalendarEventBO.RecurrenceId;
                    CalendarEventDB.RecurrenceRule      = IsEditMode == true && CalendarEventBO.RecurrenceRule == null ? CalendarEventDB.RecurrenceRule : CalendarEventBO.RecurrenceRule;
                    CalendarEventDB.RecurrenceException = IsEditMode == true && CalendarEventBO.RecurrenceException == null ? CalendarEventDB.RecurrenceException : CalendarEventBO.RecurrenceException;
                    CalendarEventDB.IsAllDay            = CalendarEventBO.IsAllDay;

                    if (IsEditMode == false)
                    {
                        CalendarEventDB.CreateByUserID = CalendarEventBO.CreateByUserID;
                        CalendarEventDB.CreateDate     = DateTime.UtcNow;
                    }
                    else
                    {
                        CalendarEventDB.UpdateByUserID = CalendarEventBO.UpdateByUserID;
                        CalendarEventDB.UpdateDate     = DateTime.UtcNow;
                    }

                    if (Add_CalendarEventDB == true)
                    {
                        CalendarEventDB = _context.CalendarEvents.Add(CalendarEventDB);
                    }
                    _context.SaveChanges();
                }
                else
                {
                    if (IsEditMode == false && AttorneyVisitBO.CalendarEventId <= 0)
                    {
                        dbContextTransaction.Rollback();
                        return(new BO.ErrorObject {
                            errorObject = "", ErrorMessage = "Please pass valid Calendar Event details.", ErrorLevel = ErrorLevel.Error
                        });
                    }
                    CalendarEventDB = null;
                }
                #endregion

                #region Patient Visit
                if (AttorneyVisitBO != null &&
                    ((AttorneyVisitBO.ID <= 0 && AttorneyVisitBO.PatientId.HasValue == true && AttorneyVisitBO.CompanyId.HasValue == true) ||
                     (AttorneyVisitBO.ID > 0)))
                {
                    bool Add_patientVisitDB = false;
                    AttorneyVisitDB = _context.AttorneyVisits.Where(p => p.Id == AttorneyVisitBO.ID &&
                                                                    (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                      .FirstOrDefault();

                    if (AttorneyVisitDB == null && AttorneyVisitBO.ID <= 0)
                    {
                        AttorneyVisitDB    = new AttorneyVisit();
                        Add_patientVisitDB = true;
                        sendMessage        = true;
                    }
                    else if (AttorneyVisitDB == null && AttorneyVisitBO.ID > 0)
                    {
                        dbContextTransaction.Rollback();
                        return(new BO.ErrorObject {
                            errorObject = "", ErrorMessage = "Attorney Visit doesn't exists.", ErrorLevel = ErrorLevel.Error
                        });
                    }
                    AttorneyVisitDB.CalendarEventId = (CalendarEventDB != null && CalendarEventDB.Id > 0) ? CalendarEventDB.Id : ((AttorneyVisitBO.CalendarEventId.HasValue == true) ? AttorneyVisitBO.CalendarEventId.Value : AttorneyVisitDB.CalendarEventId);

                    if (IsEditMode == false && AttorneyVisitBO.CaseId.HasValue == false)
                    {
                        int CaseId = _context.Cases.Where(p => p.PatientId == AttorneyVisitBO.PatientId.Value && p.CaseStatusId == 1 &&
                                                          (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                     .Select(p => p.Id)
                                     .FirstOrDefault <int>();

                        if (CaseId == 0)
                        {
                            return(new BO.ErrorObject {
                                errorObject = "", ErrorMessage = "No open case exists for given patient.", ErrorLevel = ErrorLevel.Error
                            });
                        }
                        else if (AttorneyVisitBO.CaseId.HasValue == true && AttorneyVisitBO.CaseId.Value != CaseId)
                        {
                            return(new BO.ErrorObject {
                                errorObject = "", ErrorMessage = "Case id dosent match with open case is for the given patient.", ErrorLevel = ErrorLevel.Error
                            });
                        }
                        else
                        {
                            AttorneyVisitDB.CaseId = CaseId;
                        }
                    }
                    else
                    {
                        AttorneyVisitDB.CaseId = AttorneyVisitBO.CaseId.HasValue == false ? AttorneyVisitDB.CaseId : AttorneyVisitBO.CaseId.Value;
                    }

                    AttorneyVisitDB.PatientId  = IsEditMode == true && AttorneyVisitBO.PatientId.HasValue == false ? AttorneyVisitDB.PatientId : (AttorneyVisitBO.PatientId.HasValue == false ? AttorneyVisitDB.PatientId : AttorneyVisitBO.PatientId.Value);
                    AttorneyVisitDB.AttorneyId = IsEditMode == true && AttorneyVisitBO.AttorneyId.HasValue == false ? AttorneyVisitDB.AttorneyId : (AttorneyVisitBO.AttorneyId.HasValue == false ? AttorneyVisitDB.AttorneyId : AttorneyVisitBO.AttorneyId.Value);
                    AttorneyVisitDB.CompanyId  = IsEditMode == true && AttorneyVisitBO.CompanyId.HasValue == false ? AttorneyVisitDB.CompanyId : (AttorneyVisitBO.CompanyId.HasValue == false ? AttorneyVisitDB.CompanyId : AttorneyVisitBO.CompanyId.Value);

                    AttorneyVisitDB.EventStart = AttorneyVisitBO.EventStart;
                    AttorneyVisitDB.EventEnd   = AttorneyVisitBO.EventEnd;

                    AttorneyVisitDB.Subject       = AttorneyVisitBO.Subject;
                    AttorneyVisitDB.VisitStatusId = AttorneyVisitBO.VisitStatusId;
                    AttorneyVisitDB.ContactPerson = AttorneyVisitBO.ContactPerson;
                    AttorneyVisitDB.Agenda        = AttorneyVisitBO.Agenda;

                    if (IsEditMode == false)
                    {
                        AttorneyVisitDB.CreateByUserID = AttorneyVisitBO.CreateByUserID;
                        AttorneyVisitDB.CreateDate     = DateTime.UtcNow;
                    }
                    else
                    {
                        AttorneyVisitDB.UpdateByUserID = AttorneyVisitBO.UpdateByUserID;
                        AttorneyVisitDB.UpdateDate     = DateTime.UtcNow;
                    }

                    if (Add_patientVisitDB == true)
                    {
                        AttorneyVisitDB = _context.AttorneyVisits.Add(AttorneyVisitDB);
                    }
                    _context.SaveChanges();

                    //if (AttorneyVisitDB.PatientId.HasValue == true && AttorneyVisitDB.CaseId.HasValue == true && AttorneyVisitDB.AncillaryProviderId.HasValue == true)
                    //{
                    //    using (PatientRepository patientRepo = new PatientRepository(_context))
                    //    {
                    //        patientRepo.AssociatePatientWithAncillaryCompany(AttorneyVisitDB.PatientId.Value, AttorneyVisitDB.CaseId.Value, AttorneyVisitBO.AncillaryProviderId.Value, AttorneyVisitBO.AddedByCompanyId);
                    //    }
                    //}
                }
                else
                {
                    if (IsEditMode == false && IsAddModeCalendarEvent == false)
                    {
                        dbContextTransaction.Rollback();
                        return(new BO.ErrorObject {
                            errorObject = "", ErrorMessage = "Please pass valid Attorney Visit details.", ErrorLevel = ErrorLevel.Error
                        });
                    }
                    AttorneyVisitDB = null;
                }

                _context.SaveChanges();
                #endregion

                dbContextTransaction.Commit();

                if (AttorneyVisitDB != null)
                {
                    AttorneyVisitDB = _context.AttorneyVisits.Include("CalendarEvent")
                                      .Include("Company")
                                      .Include("Patient").Include("Patient.User").Include("Patient.User.UserCompanies")
                                      .Where(p => p.Id == AttorneyVisitDB.Id &&
                                             (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                      .FirstOrDefault <AttorneyVisit>();
                }
                else if (CalendarEventDB != null)
                {
                    AttorneyVisitDB = _context.AttorneyVisits.Include("CalendarEvent")
                                      .Where(p => p.CalendarEvent.Id == CalendarEventDB.Id &&
                                             (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                      .FirstOrDefault <AttorneyVisit>();
                }
            }

            if (sendMessage == true)
            {
                try
                {
                    IdentityHelper identityHelper = new IdentityHelper();

                    User AdminUser = _context.Users.Include("ContactInfo").Include("UserCompanies").Include("UserCompanies.company")
                                     .Where(p => p.UserName == identityHelper.Email && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                     .FirstOrDefault();



                    //var AdminUser_CompanyId = AdminUser.UserCompanies.Select(p2 => p2.CompanyID).FirstOrDefault();

                    List <User> lstStaff = _context.Users.Include("ContactInfo").Include("UserCompanies")
                                           .Where(p => p.UserType == 2 &&
                                                  p.UserCompanies.Any(p1 => p1.CompanyID == AttorneyVisitBO.CompanyId &&
                                                                      (p1.IsDeleted.HasValue == false || (p1.IsDeleted.HasValue == true && p1.IsDeleted.Value == false))) == true &&
                                                  (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                           .ToList();

                    User patientInfo = _context.Users.Include("ContactInfo").Include("UserCompanies").Include("UserCompanies.company")
                                       .Where(p => p.id == AttorneyVisitBO.PatientId && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                       .FirstOrDefault();

                    User attorneyInfo = _context.Users.Include("ContactInfo").Include("UserCompanies").Include("UserCompanies.company")
                                        .Where(p => p.id == AttorneyVisitBO.AttorneyId && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                        .FirstOrDefault();



                    string MailMessageForPatient  = "<B> New Appointment Scheduled</B></ BR >Attorney provider has scheduled a client visit with Attorney: " + attorneyInfo.FirstName + " " + attorneyInfo.LastName + "<br><br>Thanks";
                    string NotificationForPatient = "Attorney provider has schedule a client visit with Attorney: " + attorneyInfo.FirstName + " " + attorneyInfo.LastName;
                    string SmsMessageForPatient   = "<B> New Appointment Scheduled</B></ BR >Attorney provider has scheduled a patient visit with Attorney: " + attorneyInfo.FirstName + " " + attorneyInfo.LastName + "<br><br>Thanks";


                    string MailMessageForStaff  = "<B> New Appointment Scheduled</B></BR>A new Appointment has been scheduled for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + "<br><br>Thanks";
                    string NotificationForStaff = "A new Appointment has been scheduled for patient :" + patientInfo.FirstName + " " + patientInfo.LastName;
                    string SmsMessageForStaff   = "<B> New Appointment Scheduled</B></BR>A new Appointment has been scheduled for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + "<br><br>Thanks";



                    #region  patient mail object

                    BO.EmailMessage emPatient = new BO.EmailMessage();
                    emPatient.ApplicationName = "Midas";
                    emPatient.ToEmail         = patientInfo.UserName;
                    emPatient.EMailSubject    = "MIDAS Notification";
                    emPatient.EMailBody       = MailMessageForPatient;
                    #endregion

                    #region patient sms object
                    BO.SMS smsPatient = new BO.SMS();
                    smsPatient.ApplicationName = "Midas";
                    smsPatient.ToNumber        = patientInfo.ContactInfo.CellPhone;
                    smsPatient.Message         = SmsMessageForPatient;
                    #endregion


                    NotificationHelper nh = new NotificationHelper();
                    MessagingHelper    mh = new MessagingHelper();

                    #region Patient
                    nh.PushNotification(patientInfo.UserName, AdminUser.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), NotificationForPatient, "New Appointment Schedule");      //patientInfo.UserName for Patient user email
                    mh.SendEmailAndSms(patientInfo.UserName, AdminUser.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), emPatient, smsPatient);
                    #endregion

                    foreach (var item in lstStaff)
                    {
                        #region  staff mail object
                        BO.EmailMessage emStaff = new BO.EmailMessage();
                        emStaff.ApplicationName = "Midas";
                        emStaff.ToEmail         = item.UserName;
                        emStaff.EMailSubject    = "MIDAS Notification";
                        emStaff.EMailBody       = MailMessageForStaff;
                        #endregion

                        #region admin sms object
                        BO.SMS smsStaff = new BO.SMS();
                        smsStaff.ApplicationName = "Midas";
                        smsStaff.ToNumber        = item.ContactInfo.CellPhone;
                        smsStaff.Message         = SmsMessageForStaff;
                        #endregion

                        nh.PushNotification(item.UserName, item.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), NotificationForStaff, "New Appointment Schedule");     //item.UserName
                        mh.SendEmailAndSms(item.UserName, item.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), emStaff, smsStaff);
                    }
                }
                catch (Exception ex)
                {
                }
            }

            var res = Convert <BO.AttorneyVisit, AttorneyVisit>(AttorneyVisitDB);
            return((object)res);
        }
        public override object SaveIMEVisit <T>(T entity)
        {
            BO.IMEVisit      IMEVisitBO       = (BO.IMEVisit)(object) entity;
            BO.CalendarEvent CalendarEventBO  = IMEVisitBO.CalendarEvent;
            string           patientUserName  = string.Empty;
            bool             sendNotification = false;
            bool             sendMessage      = false;

            //CalenderEventBO
            if (CalendarEventBO != null)
            {
                List <BO.FreeSlots>     currentEventSlots = new List <BO.FreeSlots>();
                CalendarEventRepository calEventRepo      = new CalendarEventRepository(_context);
                currentEventSlots = calEventRepo.GetBusySlotsByCalendarEvent(CalendarEventBO) as List <BO.FreeSlots>;

                if (currentEventSlots.Count > 0)
                {
                    DateTime dtStartDate = currentEventSlots.Min(p => p.ForDate);
                    DateTime dtEndDate   = currentEventSlots.Max(p => p.ForDate).AddDays(1);

                    List <BO.StartAndEndTime> busySlots = new List <BO.StartAndEndTime>();

                    if (IMEVisitBO.PatientId != null)
                    {
                        var result = calEventRepo.GetBusySlotsForPatients(IMEVisitBO.PatientId.Value, dtStartDate, dtEndDate);
                        if (result is BO.ErrorObject)
                        {
                            return(result);
                        }
                        else
                        {
                            busySlots = result as List <BO.StartAndEndTime>;
                        }
                    }
                    if (busySlots != null && busySlots.Count > 0)
                    {
                        foreach (var eachDayEventSlot in currentEventSlots)
                        {
                            DateTime ForDate = eachDayEventSlot.ForDate;
                            foreach (var eachEventSlot in eachDayEventSlot.StartAndEndTimes)
                            {
                                DateTime StartTime = eachEventSlot.StartTime;
                                DateTime EndTime   = eachEventSlot.EndTime;
                                var      StartAndEndTimesForDate = busySlots.Where(p => p.StartTime.Date == ForDate).ToList();
                                if (StartAndEndTimesForDate.Count > 0)
                                {
                                    var StartAndEndTimes = StartAndEndTimesForDate.Where(p => p.StartTime >= StartTime && p.StartTime < EndTime).ToList();

                                    if (StartAndEndTimes.Count > 0)
                                    {
                                        DateTime?checkContinuation = null;
                                        foreach (var eachSlot in StartAndEndTimes.Distinct().OrderBy(p => p.StartTime))
                                        {
                                            if (checkContinuation.HasValue == false)
                                            {
                                                checkContinuation = eachSlot.EndTime;
                                            }
                                            else
                                            {
                                                if (checkContinuation.Value != eachSlot.StartTime)
                                                {
                                                    return(new BO.ErrorObject {
                                                        errorObject = "", ErrorMessage = "The patient dosent have continued free slots on the planned visit time of " + checkContinuation.Value.ToString() + ".", ErrorLevel = ErrorLevel.Error
                                                    });
                                                }
                                                else
                                                {
                                                    checkContinuation = eachSlot.EndTime;
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        //return new BO.ErrorObject { errorObject = "", ErrorMessage = "The patient dosent have free slots on the planned visit time of " + ForDate.ToShortDateString() + " (" + StartTime.ToShortTimeString() + " - " + EndTime.ToShortTimeString() + ").", ErrorLevel = ErrorLevel.Error };
                                    }
                                }
                                else
                                {
                                    //return new BO.ErrorObject { errorObject = "", ErrorMessage = "The patient is not availabe on " + ForDate.ToShortDateString() + ".", ErrorLevel = ErrorLevel.Error };
                                }
                            }
                        }
                    }
                }
            }

            IMEVisit IMEVisitDB = new IMEVisit();

            using (var dbContextTransaction = _context.Database.BeginTransaction())
            {
                bool IsEditMode             = false;
                bool IsAddModeCalendarEvent = false;

                IsEditMode = (IMEVisitBO != null && IMEVisitBO.ID > 0) ? true : false;
                string patientContactNumber = null;
                User   patientuser          = null;

                if (IMEVisitBO.PatientId == null && IMEVisitBO.ID > 0)
                {
                    var IMEvisitData = _context.IMEVisits.Where(p => p.ID == IMEVisitBO.ID).Select(p => new { p.PatientId, p.CaseId }).FirstOrDefault();

                    patientuser = _context.Users.Where(usr => usr.id == IMEvisitData.PatientId).Include("ContactInfo").FirstOrDefault();
                }
                else if (IMEVisitBO.PatientId != null && IMEVisitBO.PatientId > 0)
                {
                    patientuser = _context.Users.Where(usr => usr.id == IMEVisitBO.PatientId).Include("ContactInfo").FirstOrDefault();
                }

                if (patientuser != null)
                {
                    patientUserName      = patientuser.UserName;
                    patientContactNumber = patientuser.ContactInfo.CellPhone;
                }

                CalendarEvent CalendarEventDB = new CalendarEvent();
                #region Calendar Event
                if (CalendarEventBO != null)
                {
                    bool Add_CalendarEventDB = false;
                    CalendarEventDB = _context.CalendarEvents.Where(p => p.Id == CalendarEventBO.ID &&
                                                                    (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                      .FirstOrDefault();

                    if (CalendarEventDB == null && CalendarEventBO.ID <= 0)
                    {
                        CalendarEventDB     = new CalendarEvent();
                        Add_CalendarEventDB = true;
                    }
                    else if (CalendarEventDB == null && CalendarEventBO.ID > 0)
                    {
                        dbContextTransaction.Rollback();
                        return(new BO.ErrorObject {
                            errorObject = "", ErrorMessage = "Calendar Event details dosent exists.", ErrorLevel = ErrorLevel.Error
                        });
                    }

                    //if (string.IsNullOrWhiteSpace(patientUserName) == false && dictionary.ContainsKey(patientUserName))
                    //{
                    //    if (CalendarEventDB.EventStart != CalendarEventBO.EventStart.Value) sendNotification = true;
                    //}

                    CalendarEventDB.Name                = IsEditMode == true && CalendarEventBO.Name == null ? CalendarEventDB.Name : CalendarEventBO.Name;
                    CalendarEventDB.EventStart          = IsEditMode == true && CalendarEventBO.EventStart.HasValue == false ? CalendarEventDB.EventStart : CalendarEventBO.EventStart.Value;
                    CalendarEventDB.EventEnd            = IsEditMode == true && CalendarEventBO.EventEnd.HasValue == false ? CalendarEventDB.EventEnd : CalendarEventBO.EventEnd.Value;
                    CalendarEventDB.TimeZone            = CalendarEventBO.TimeZone;
                    CalendarEventDB.Description         = CalendarEventBO.Description;
                    CalendarEventDB.RecurrenceId        = CalendarEventBO.RecurrenceId;
                    CalendarEventDB.RecurrenceRule      = IsEditMode == true && CalendarEventBO.RecurrenceRule == null ? CalendarEventDB.RecurrenceRule : CalendarEventBO.RecurrenceRule;
                    CalendarEventDB.RecurrenceException = IsEditMode == true && CalendarEventBO.RecurrenceException == null ? CalendarEventDB.RecurrenceException : CalendarEventBO.RecurrenceException;
                    CalendarEventDB.IsAllDay            = CalendarEventBO.IsAllDay;

                    if (IsEditMode == false)
                    {
                        CalendarEventDB.CreateByUserID = CalendarEventBO.CreateByUserID;
                        CalendarEventDB.CreateDate     = DateTime.UtcNow;
                    }
                    else
                    {
                        CalendarEventDB.UpdateByUserID = CalendarEventBO.UpdateByUserID;
                        CalendarEventDB.UpdateDate     = DateTime.UtcNow;
                    }

                    if (Add_CalendarEventDB == true)
                    {
                        CalendarEventDB = _context.CalendarEvents.Add(CalendarEventDB);
                    }
                    _context.SaveChanges();

                    //#region send SMS notification
                    //try
                    //{
                    //    if (sendNotification)
                    //    {
                    //        if (patientContactNumber != null && patientContactNumber != string.Empty)
                    //        {
                    //            string to = patientContactNumber;
                    //             //string body = "Your appointment has been scheduled at " + CalendarEventBO.EventStart.Value + " in " + _context.Locations.Where(loc => loc.id == patientVisitBO.LocationId).Select(lc => lc.Name).FirstOrDefault();
                    //            string body = "";
                    //            string msgid = SMSGateway.SendSMS(to, body);
                    //        }
                    //    }
                    //}
                    //catch (Exception) { }
                    //#endregion
                }
                else
                {
                    if (IsEditMode == false && IMEVisitBO.CalendarEventId <= 0)
                    {
                        dbContextTransaction.Rollback();
                        return(new BO.ErrorObject {
                            errorObject = "", ErrorMessage = "Please pass valid Calendar Event details.", ErrorLevel = ErrorLevel.Error
                        });
                    }
                    CalendarEventDB = null;
                }
                #endregion

                #region IME Visit
                if (IMEVisitBO != null && ((IMEVisitBO.ID <= 0 && IMEVisitBO.PatientId.HasValue == true) || (IMEVisitBO.ID > 0)))
                {
                    bool Add_IMEVisitDB = false;
                    IMEVisitDB = _context.IMEVisits.Where(p => p.ID == IMEVisitBO.ID &&
                                                          (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                 .FirstOrDefault();

                    if (IMEVisitDB == null && IMEVisitBO.ID <= 0)
                    {
                        IMEVisitDB     = new IMEVisit();
                        Add_IMEVisitDB = true;
                        sendMessage    = true;
                    }
                    else if (IMEVisitDB == null && IMEVisitBO.ID > 0)
                    {
                        dbContextTransaction.Rollback();
                        return(new BO.ErrorObject {
                            errorObject = "", ErrorMessage = "Patient Visit doesn't exists.", ErrorLevel = ErrorLevel.Error
                        });
                    }

                    IMEVisitDB.CalendarEventId = (CalendarEventDB != null && CalendarEventDB.Id > 0) ? CalendarEventDB.Id : ((IMEVisitBO.CalendarEventId.HasValue == true) ? IMEVisitBO.CalendarEventId.Value : IMEVisitDB.CalendarEventId);


                    if (IsEditMode == false && IMEVisitBO.CaseId.HasValue == false)
                    {
                        int CaseId = _context.Cases.Where(p => p.PatientId == IMEVisitBO.PatientId.Value && p.CaseStatusId == 1 &&
                                                          (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                     .Select(p => p.Id)
                                     .FirstOrDefault <int>();

                        if (CaseId == 0)
                        {
                            return(new BO.ErrorObject {
                                errorObject = "", ErrorMessage = "No open case exists for given patient.", ErrorLevel = ErrorLevel.Error
                            });
                        }
                        else if (IMEVisitBO.CaseId.HasValue == true && IMEVisitBO.CaseId.Value != CaseId)
                        {
                            return(new BO.ErrorObject {
                                errorObject = "", ErrorMessage = "Case id dosent match with open case is for the given patient.", ErrorLevel = ErrorLevel.Error
                            });
                        }
                        else
                        {
                            IMEVisitDB.CaseId = CaseId;
                        }
                    }
                    else
                    {
                        IMEVisitDB.CaseId = IMEVisitBO.CaseId.HasValue == false ? IMEVisitDB.CaseId : IMEVisitBO.CaseId.Value;
                    }

                    IMEVisitDB.PatientId  = IsEditMode == true && IMEVisitBO.PatientId.HasValue == false ? IMEVisitDB.PatientId : (IMEVisitBO.PatientId.HasValue == false ? IMEVisitDB.PatientId : IMEVisitBO.PatientId.Value);
                    IMEVisitDB.EventStart = IMEVisitBO.EventStart;
                    IMEVisitDB.EventEnd   = IMEVisitBO.EventEnd;

                    IMEVisitDB.Notes                   = IMEVisitBO.Notes;
                    IMEVisitDB.VisitStatusId           = IMEVisitBO.VisitStatusId;
                    IMEVisitDB.TransportProviderId     = IMEVisitBO.TransportProviderId;
                    IMEVisitDB.DoctorName              = IMEVisitBO.DoctorName;
                    IMEVisitDB.VisitCreatedByCompanyId = IsEditMode == true ? IMEVisitDB.VisitCreatedByCompanyId : IMEVisitBO.VisitCreatedByCompanyId.Value;

                    if (IsEditMode == false)
                    {
                        IMEVisitDB.CreateByUserID = IMEVisitBO.CreateByUserID;
                        IMEVisitDB.CreateDate     = DateTime.UtcNow;
                    }
                    else
                    {
                        IMEVisitDB.UpdateByUserID = IMEVisitBO.UpdateByUserID;
                        IMEVisitDB.UpdateDate     = DateTime.UtcNow;
                    }

                    if (Add_IMEVisitDB == true)
                    {
                        IMEVisitDB = _context.IMEVisits.Add(IMEVisitDB);
                    }
                    _context.SaveChanges();
                }
                else
                {
                    if (IsEditMode == false && IsAddModeCalendarEvent == false)
                    {
                        dbContextTransaction.Rollback();
                        return(new BO.ErrorObject {
                            errorObject = "", ErrorMessage = "Please pass valid Patient Visit details.", ErrorLevel = ErrorLevel.Error
                        });
                    }
                    IMEVisitDB = null;
                }

                _context.SaveChanges();
                #endregion

                dbContextTransaction.Commit();

                if (IMEVisitDB != null)
                {
                    IMEVisitDB = _context.IMEVisits.Include("CalendarEvent")
                                 .Include("Patient").Include("Patient.Cases")
                                 .Include("Patient.User").Include("Patient.User.UserCompanies")
                                 .Where(p => p.ID == IMEVisitDB.ID &&
                                        (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                 .FirstOrDefault <IMEVisit>();
                }
                else if (CalendarEventDB != null)
                {
                    IMEVisitDB = _context.IMEVisits.Include("CalendarEvent")
                                 .Where(p => p.CalendarEvent.Id == CalendarEventDB.Id &&
                                        (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                 .FirstOrDefault <IMEVisit>();
                }
            }

            if (sendMessage == true)
            {
                try
                {
                    IdentityHelper identityHelper = new IdentityHelper();
                    User           AdminUser      = _context.Users.Include("ContactInfo").Include("UserCompanies").Include("UserCompanies.company")
                                                    .Where(p => p.UserName == identityHelper.Email && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                                    .FirstOrDefault();

                    User patientInfo = _context.Users.Include("ContactInfo").Include("UserCompanies").Include("UserCompanies.company")
                                       .Where(p => p.id == IMEVisitBO.PatientId && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                       .FirstOrDefault();

                    User ancillaryInfo = _context.Users.Include("ContactInfo").Include("UserCompanies").Include("UserCompanies.company")
                                         .Where(p => p.UserType == 5 && p.UserCompanies.Where(p1 => p1.CompanyID == IMEVisitBO.TransportProviderId && (p1.IsDeleted.HasValue == false || (p1.IsDeleted.HasValue == true && p1.IsDeleted.Value == false))).Any() == true && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false))).FirstOrDefault();

                    List <User> lstStaff = _context.Users.Include("ContactInfo").Include("UserCompanies").Include("UserCompanies.company")
                                           .Where(p => p.UserType == 2 && p.UserCompanies.Where(p1 => p1.CompanyID == IMEVisitBO.VisitCreatedByCompanyId && (p1.IsDeleted.HasValue == false || (p1.IsDeleted.HasValue == true && p1.IsDeleted.Value == false))).Any() && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                           .ToList <User>();

                    //string VerificationLink = "<a href='" + Utility.GetConfigValue("PatientVerificationLink") + "/" + invitationDB_UniqueID + "' target='_blank'>" + Utility.GetConfigValue("PatientVerificationLink") + "/" + invitationDB_UniqueID + "</a>";
                    string MailMessageForPatient   = "<B> New Appointment Scheduled</B></ BR >Medical provider has schedule a patient visit with Doctor: " + IMEVisitBO.DoctorName + "<br><br>Thanks";
                    string MailMessageForAdmin     = "<B> New Appointment Scheduled</B></BR>A new Appointment schedule for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + " is schedule with doctor:" + IMEVisitBO.DoctorName + "<br><br>Thanks";
                    string MailMessageForAncillary = "<B> New Appointment Scheduled</B></BR>A new Appointment schedule for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + " is schedule with doctor:" + IMEVisitBO.DoctorName + "<br><br>Thanks";

                    string NotificationForPatient   = "Medical provider has schedule a patient visit with Doctor: " + IMEVisitBO.DoctorName;
                    string NotificationForAdmin     = "New Appointment schedule for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + " is schedule with doctor:" + IMEVisitBO.DoctorName;
                    string NotificationForAncillary = "New Appointment schedule for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + " is schedule with doctor:" + IMEVisitBO.DoctorName;

                    string SmsMessageForPatient   = "<B> New Appointment Scheduled</B></ BR >Medical provider has schedule a patient visit with Doctor: " + IMEVisitBO.DoctorName + "<br><br>Thanks";
                    string SmsMessageForAdmin     = "<B> New Appointment Scheduled</B></BR>A new Appointment schedule for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + " is schedule with doctor:" + IMEVisitBO.DoctorName + "<br><br>Thanks";
                    string SmsMessageForAncillary = "<B> New Appointment Scheduled</B></BR>A new Appointment schedule for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + " is schedule with doctor:" + IMEVisitBO.DoctorName + "<br><br>Thanks";


                    string MailMessageForStaff  = "<B> New Appointment Scheduled</B></BR>A new Appointment schedule for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + "<br><br>Thanks";
                    string NotificationForStaff = "New Appointment schedule for patient : " + patientInfo.FirstName + " " + patientInfo.LastName;
                    string SmsMessageForStaff   = "<B> New Appointment Scheduled</B></BR>A new Appointment schedule for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + "<br><br>Thanks";



                    #region  patient mail object

                    BO.EmailMessage emPatient = new BO.EmailMessage();
                    if (patientInfo != null)
                    {
                        emPatient.ApplicationName = "Midas";
                        emPatient.ToEmail         = patientInfo.UserName;;
                        emPatient.EMailSubject    = "MIDAS Notification";
                        emPatient.EMailBody       = MailMessageForPatient;
                    }
                    #endregion

                    #region patient sms object
                    BO.SMS smsPatient = new BO.SMS();
                    if (patientInfo != null)
                    {
                        smsPatient.ApplicationName = "Midas";
                        smsPatient.ToNumber        = patientInfo.ContactInfo.CellPhone;
                        smsPatient.Message         = SmsMessageForPatient;
                    }
                    #endregion


                    #region  admin mail object
                    BO.EmailMessage emAdmin = new BO.EmailMessage();
                    if (identityHelper != null)
                    {
                        emAdmin.ApplicationName = "Midas";
                        emAdmin.ToEmail         = identityHelper.Email;
                        emAdmin.EMailSubject    = "MIDAS Notification";
                        emAdmin.EMailBody       = MailMessageForAdmin;
                    }
                    #endregion

                    #region admin sms object
                    BO.SMS smsAdmin = new BO.SMS();
                    if (AdminUser != null)
                    {
                        smsAdmin.ApplicationName = "Midas";
                        smsAdmin.ToNumber        = AdminUser.ContactInfo.CellPhone;
                        smsAdmin.Message         = SmsMessageForAdmin;
                    }
                    #endregion

                    #region  Ancillary mail object
                    BO.EmailMessage emAncillary = new BO.EmailMessage();
                    if (ancillaryInfo != null)
                    {
                        emAdmin.ApplicationName = "Midas";
                        emAdmin.ToEmail         = ancillaryInfo.UserName;
                        emAdmin.EMailSubject    = "MIDAS Notification";
                        emAdmin.EMailBody       = MailMessageForAncillary;
                    }
                    #endregion

                    #region Ancillary sms object
                    BO.SMS smsAncillary = new BO.SMS();
                    if (ancillaryInfo != null)
                    {
                        smsAdmin.ApplicationName = "Midas";
                        smsAdmin.ToNumber        = ancillaryInfo.ContactInfo.CellPhone;
                        smsAdmin.Message         = SmsMessageForAncillary;
                    }
                    #endregion


                    NotificationHelper nh = new NotificationHelper();
                    MessagingHelper    mh = new MessagingHelper();

                    #region Patient
                    nh.PushNotification(patientInfo.UserName, patientInfo.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), NotificationForPatient, "New Appointment Schedule");
                    mh.SendEmailAndSms(patientInfo.UserName, patientInfo.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), emPatient, smsPatient);
                    #endregion


                    #region admin and staff
                    if (AdminUser.UserType == 4 || AdminUser.UserType == 3)
                    {
                        nh.PushNotification(AdminUser.UserName, AdminUser.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), NotificationForAdmin, "New Appointment Schedule");
                        mh.SendEmailAndSms(AdminUser.UserName, AdminUser.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), emAdmin, smsAdmin);
                        foreach (var item in lstStaff)
                        {
                            #region  staff mail object
                            BO.EmailMessage emStaff = new BO.EmailMessage();
                            emAdmin.ApplicationName = "Midas";
                            emAdmin.ToEmail         = item.UserName;
                            emAdmin.EMailSubject    = "MIDAS Notification";
                            emAdmin.EMailBody       = MailMessageForStaff;
                            #endregion

                            #region admin sms object
                            BO.SMS smsStaff = new BO.SMS();
                            smsAdmin.ApplicationName = "Midas";
                            smsAdmin.ToNumber        = item.ContactInfo.CellPhone;
                            smsAdmin.Message         = SmsMessageForStaff;
                            #endregion

                            nh.PushNotification(item.UserName, AdminUser.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), NotificationForStaff, "New Appointment Schedule");
                            mh.SendEmailAndSms(item.UserName, AdminUser.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), emStaff, smsStaff);
                        }
                    }
                    else
                    {
                        foreach (var item in lstStaff)
                        {
                            #region  staff mail object
                            BO.EmailMessage emStaff = new BO.EmailMessage();
                            emAdmin.ApplicationName = "Midas";
                            emAdmin.ToEmail         = item.UserName;
                            emAdmin.EMailSubject    = "MIDAS Notification";
                            emAdmin.EMailBody       = MailMessageForStaff;
                            #endregion

                            #region admin sms object
                            BO.SMS smsStaff = new BO.SMS();
                            smsAdmin.ApplicationName = "Midas";
                            smsAdmin.ToNumber        = item.ContactInfo.CellPhone;
                            smsAdmin.Message         = SmsMessageForStaff;
                            #endregion

                            nh.PushNotification(item.UserName, AdminUser.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), NotificationForStaff, "New Appointment Schedule");
                            mh.SendEmailAndSms(item.UserName, AdminUser.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), emStaff, smsStaff);
                        }
                    }

                    #endregion

                    #region Ancillary
                    if (IMEVisitBO.TransportProviderId != null)
                    {
                        nh.PushNotification(ancillaryInfo.UserName, ancillaryInfo.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), NotificationForAncillary, "New Appointment Schedule");
                        mh.SendEmailAndSms(ancillaryInfo.UserName, ancillaryInfo.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), emAncillary, smsAncillary);
                    }

                    #endregion
                }
                catch (Exception ex)
                {
                }
            }

            var res = ConvertIMEvisit <BO.IMEVisit, IMEVisit>(IMEVisitDB);
            return((object)res);
        }
Example #4
0
        public PreferedModeOfComunication SendEmailAndSms(string userName, int companyId, BO.EmailMessage emailData, BO.SMS smsData)
        {
            try
            {
                PreferedModeOfComunication predferredModewOfCommunication = GetModeOfComunication(userName, companyId);

                if (predferredModewOfCommunication == PreferedModeOfComunication.Email)
                {
                    AddMessageToEmailQueue(emailData);
                    return(PreferedModeOfComunication.Email);
                }
                else if (predferredModewOfCommunication == PreferedModeOfComunication.SMS)
                {
                    AddMessageToSMSQueue(smsData);
                    return(PreferedModeOfComunication.SMS);
                }
                else
                {
                    AddMessageToEmailQueue(emailData);
                    AddMessageToSMSQueue(smsData);
                    return(PreferedModeOfComunication.Both);
                }
            }
            catch (Exception e)
            {
                return(PreferedModeOfComunication.Both);
            }
        }
Example #5
0
        public override object SaveEOVisit <T>(T entity)
        {
            BO.EOVisit       EOVisitBO        = (BO.EOVisit)(object) entity;
            BO.CalendarEvent CalendarEventBO  = EOVisitBO.CalendarEvent;
            BO.Doctor        DoctorBO         = EOVisitBO.Doctor;
            string           doctorUserName   = string.Empty;
            bool             sendNotification = false;
            bool             sendMessage      = false;

            //CalenderEventBO
            if (CalendarEventBO != null)
            {
                List <BO.FreeSlots>     currentEventSlots = new List <BO.FreeSlots>();
                CalendarEventRepository calEventRepo      = new CalendarEventRepository(_context);
                currentEventSlots = calEventRepo.GetBusySlotsByCalendarEvent(CalendarEventBO) as List <BO.FreeSlots>;

                if (currentEventSlots.Count > 0)
                {
                    DateTime dtStartDate = currentEventSlots.Min(p => p.ForDate);
                    DateTime dtEndDate   = currentEventSlots.Max(p => p.ForDate).AddDays(1);

                    List <BO.StartAndEndTime> busySlots = new List <BO.StartAndEndTime>();

                    if (EOVisitBO.DoctorId != null)
                    {
                        var result = calEventRepo.GetBusySlotsForDoctors(EOVisitBO.DoctorId.Value, dtStartDate, dtEndDate);
                        if (result is BO.ErrorObject)
                        {
                            return(result);
                        }
                        else
                        {
                            busySlots = result as List <BO.StartAndEndTime>;
                        }
                    }

                    foreach (var eachDayEventSlot in currentEventSlots)
                    {
                        DateTime ForDate = eachDayEventSlot.ForDate;
                        foreach (var eachEventSlot in eachDayEventSlot.StartAndEndTimes)
                        {
                            DateTime StartTime = eachEventSlot.StartTime;
                            DateTime EndTime   = eachEventSlot.EndTime;
                            var      StartAndEndTimesForDate = busySlots.Where(p => p.StartTime.Date == ForDate).ToList();
                            if (StartAndEndTimesForDate.Count > 0)
                            {
                                var StartAndEndTimes = StartAndEndTimesForDate.Where(p => p.StartTime >= StartTime && p.StartTime < EndTime).ToList();

                                if (StartAndEndTimes.Count > 0)
                                {
                                    return(new BO.ErrorObject {
                                        errorObject = "", ErrorMessage = "The Doctor dosent have free slots for EO visit time on " + ForDate.ToShortDateString() + " (" + StartTime.ToShortTimeString() + " - " + EndTime.ToShortTimeString() + ").", ErrorLevel = ErrorLevel.Error
                                    });
                                }
                            }
                        }
                    }
                }
            }

            EOVisit EOVisitDB = new EOVisit();

            using (var dbContextTransaction = _context.Database.BeginTransaction())
            {
                bool IsEditMode             = false;
                bool IsAddModeCalendarEvent = false;

                IsEditMode = (EOVisitBO != null && EOVisitBO.ID > 0) ? true : false;
                string doctorContactNumber = null;
                User   doctoruser          = null;

                if (EOVisitBO.DoctorId == null && EOVisitBO.ID > 0)
                {
                    var EOvisitData = _context.EOVisits.Where(p => p.ID == EOVisitBO.ID).Select(p => new { p.DoctorId }).FirstOrDefault();

                    doctoruser = _context.Users.Where(usr => usr.id == EOvisitData.DoctorId).Include("ContactInfo").FirstOrDefault();
                }
                else if (EOVisitBO.DoctorId != null && EOVisitBO.DoctorId > 0)
                {
                    doctoruser = _context.Users.Where(usr => usr.id == EOVisitBO.DoctorId).Include("ContactInfo").FirstOrDefault();
                }

                if (doctoruser != null)
                {
                    doctorUserName      = doctoruser.UserName;
                    doctorContactNumber = doctoruser.ContactInfo.CellPhone;
                }

                CalendarEvent CalendarEventDB = new CalendarEvent();
                #region Calendar Event
                if (CalendarEventBO != null)
                {
                    bool Add_CalendarEventDB = false;
                    CalendarEventDB = _context.CalendarEvents.Where(p => p.Id == CalendarEventBO.ID &&
                                                                    (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                      .FirstOrDefault();

                    if (CalendarEventDB == null && CalendarEventBO.ID <= 0)
                    {
                        CalendarEventDB     = new CalendarEvent();
                        Add_CalendarEventDB = true;
                    }
                    else if (CalendarEventDB == null && CalendarEventBO.ID > 0)
                    {
                        dbContextTransaction.Rollback();
                        return(new BO.ErrorObject {
                            errorObject = "", ErrorMessage = "Calendar Event details dosent exists.", ErrorLevel = ErrorLevel.Error
                        });
                    }

                    //if (string.IsNullOrWhiteSpace(doctorUserName) == false && dictionary.ContainsKey(doctorUserName))
                    //{
                    //    if (CalendarEventDB.EventStart != CalendarEventBO.EventStart.Value) sendNotification = true;
                    //}

                    CalendarEventDB.Name                = IsEditMode == true && CalendarEventBO.Name == null ? CalendarEventDB.Name : CalendarEventBO.Name;
                    CalendarEventDB.EventStart          = IsEditMode == true && CalendarEventBO.EventStart.HasValue == false ? CalendarEventDB.EventStart : CalendarEventBO.EventStart.Value;
                    CalendarEventDB.EventEnd            = IsEditMode == true && CalendarEventBO.EventEnd.HasValue == false ? CalendarEventDB.EventEnd : CalendarEventBO.EventEnd.Value;
                    CalendarEventDB.TimeZone            = CalendarEventBO.TimeZone;
                    CalendarEventDB.Description         = CalendarEventBO.Description;
                    CalendarEventDB.RecurrenceId        = CalendarEventBO.RecurrenceId;
                    CalendarEventDB.RecurrenceRule      = IsEditMode == true && CalendarEventBO.RecurrenceRule == null ? CalendarEventDB.RecurrenceRule : CalendarEventBO.RecurrenceRule;
                    CalendarEventDB.RecurrenceException = IsEditMode == true && CalendarEventBO.RecurrenceException == null ? CalendarEventDB.RecurrenceException : CalendarEventBO.RecurrenceException;
                    CalendarEventDB.IsAllDay            = CalendarEventBO.IsAllDay;

                    if (IsEditMode == false)
                    {
                        CalendarEventDB.CreateByUserID = CalendarEventBO.CreateByUserID;
                        CalendarEventDB.CreateDate     = DateTime.UtcNow;
                    }
                    else
                    {
                        CalendarEventDB.UpdateByUserID = CalendarEventBO.UpdateByUserID;
                        CalendarEventDB.UpdateDate     = DateTime.UtcNow;
                    }

                    if (Add_CalendarEventDB == true)
                    {
                        CalendarEventDB = _context.CalendarEvents.Add(CalendarEventDB);
                    }
                    _context.SaveChanges();

                    #region send SMS notification
                    //try
                    //{
                    //    if (sendNotification)
                    //    {
                    //        if (doctorContactNumber != null && doctorContactNumber != string.Empty)
                    //        {
                    //            string to = doctorContactNumber;
                    //            string body = "Your appointment has been scheduled at. ";

                    //            string msgid = SMSGateway.SendSMS(to, body);
                    //        }
                    //    }
                    //}
                    //catch (Exception) { }
                    #endregion
                }
                else
                {
                    if (IsEditMode == false && EOVisitBO.CalendarEventId <= 0)
                    {
                        dbContextTransaction.Rollback();
                        return(new BO.ErrorObject {
                            errorObject = "", ErrorMessage = "Please pass valid Calendar Event details.", ErrorLevel = ErrorLevel.Error
                        });
                    }
                    CalendarEventDB = null;
                }
                #endregion

                #region EO Visit
                if (EOVisitBO != null && ((EOVisitBO.ID <= 0 && (EOVisitBO.DoctorId.HasValue == true || EOVisitBO.PatientId.HasValue == true)) || (EOVisitBO.ID > 0)))
                {
                    bool Add_EOVisitDB = false;
                    EOVisitDB = _context.EOVisits.Where(p => p.ID == EOVisitBO.ID &&
                                                        (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                .FirstOrDefault();

                    if (EOVisitDB == null && EOVisitBO.ID <= 0)
                    {
                        EOVisitDB     = new EOVisit();
                        Add_EOVisitDB = true;
                        sendMessage   = true;
                    }
                    else if (EOVisitDB == null && EOVisitBO.ID > 0)
                    {
                        dbContextTransaction.Rollback();
                        return(new BO.ErrorObject {
                            errorObject = "", ErrorMessage = "Patient Visit doesn't exists.", ErrorLevel = ErrorLevel.Error
                        });
                    }

                    EOVisitDB.CalendarEventId = (CalendarEventDB != null && CalendarEventDB.Id > 0) ? CalendarEventDB.Id : ((EOVisitBO.CalendarEventId.HasValue == true) ? EOVisitBO.CalendarEventId.Value : EOVisitDB.CalendarEventId);

                    EOVisitDB.DoctorId  = IsEditMode == true && EOVisitBO.DoctorId.HasValue == false ? EOVisitDB.DoctorId : (EOVisitBO.DoctorId.HasValue == false ? EOVisitDB.DoctorId : EOVisitBO.DoctorId.Value);
                    EOVisitDB.PatientId = IsEditMode == true && EOVisitBO.PatientId.HasValue == false ? EOVisitDB.PatientId : (EOVisitBO.PatientId.HasValue == false ? EOVisitDB.PatientId : EOVisitBO.PatientId.Value);

                    EOVisitDB.InsuranceProviderId = IsEditMode == true && EOVisitBO.InsuranceProviderId.HasValue == false ? EOVisitDB.InsuranceProviderId : (EOVisitBO.InsuranceProviderId.HasValue == false ? EOVisitDB.InsuranceProviderId : EOVisitBO.InsuranceProviderId.Value);
                    EOVisitDB.EventStart          = EOVisitBO.EventStart;
                    EOVisitDB.EventEnd            = EOVisitBO.EventEnd;

                    EOVisitDB.Notes         = EOVisitBO.Notes;
                    EOVisitDB.VisitStatusId = EOVisitBO.VisitStatusId;

                    EOVisitDB.VisitCreatedByCompanyId = IsEditMode == true ? EOVisitDB.VisitCreatedByCompanyId : EOVisitBO.VisitCreatedByCompanyId.Value;

                    if (IsEditMode == false)
                    {
                        EOVisitDB.CreateByUserID = EOVisitBO.CreateByUserID;
                        EOVisitDB.CreateDate     = DateTime.UtcNow;
                    }
                    else
                    {
                        EOVisitDB.UpdateByUserID = EOVisitBO.UpdateByUserID;
                        EOVisitDB.UpdateDate     = DateTime.UtcNow;
                    }

                    if (Add_EOVisitDB == true)
                    {
                        EOVisitDB = _context.EOVisits.Add(EOVisitDB);
                    }
                    _context.SaveChanges();
                }
                else
                {
                    if (IsEditMode == false && IsAddModeCalendarEvent == false)
                    {
                        dbContextTransaction.Rollback();
                        return(new BO.ErrorObject {
                            errorObject = "", ErrorMessage = "Please pass valid Patient Visit details.", ErrorLevel = ErrorLevel.Error
                        });
                    }
                    EOVisitDB = null;
                }

                _context.SaveChanges();
                #endregion

                dbContextTransaction.Commit();

                if (EOVisitDB != null)
                {
                    EOVisitDB = _context.EOVisits.Include("CalendarEvent")
                                .Include("Doctor")
                                .Include("Patient")
                                .Include("Company")
                                .Include("InsuranceMaster")
                                .Where(p => p.ID == EOVisitDB.ID &&
                                       (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                .FirstOrDefault <EOVisit>();
                }
                else if (CalendarEventDB != null)
                {
                    EOVisitDB = _context.EOVisits.Include("CalendarEvent")
                                .Where(p => p.CalendarEvent.Id == CalendarEventDB.Id &&
                                       (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                .FirstOrDefault <EOVisit>();
                }
            }

            if (sendMessage == true)
            {
                try
                {
                    IdentityHelper identityHelper = new IdentityHelper();

                    User AdminUser = _context.Users.Include("ContactInfo").Include("UserCompanies").Include("UserCompanies.company")
                                     .Where(p => p.UserName == identityHelper.Email && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                     .FirstOrDefault();

                    int currentCompanyType = _context.Companies.Where(p => p.id == EOVisitBO.VisitCreatedByCompanyId).Select(p1 => p1.CompanyType).FirstOrDefault();


                    List <User> lstStaff = _context.Users.Include("ContactInfo").Include("UserCompanies").Include("UserCompanies.company")
                                           .Where(p => p.UserType == 2 && p.UserCompanies.Where(p1 => p1.CompanyID == EOVisitBO.VisitCreatedByCompanyId && (p1.IsDeleted.HasValue == false || (p1.IsDeleted.HasValue == true && p1.IsDeleted.Value == false))).Any() && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                           .ToList <User>();
                    User patientInfo = _context.Users.Include("ContactInfo").Include("UserCompanies").Include("UserCompanies.company")
                                       .Where(p => p.id == EOVisitBO.PatientId && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false)))
                                       .FirstOrDefault();

                    User doctorInfo = _context.Users.Include("ContactInfo").Include("UserCompanies").Include("UserCompanies.company")
                                      .Where(p => p.id == EOVisitBO.DoctorId && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false))).FirstOrDefault();

                    InsuranceMaster insuranceInfo = _context.InsuranceMasters
                                                    .Where(p => p.Id == EOVisitBO.InsuranceProviderId && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false))).FirstOrDefault();


                    string MailMessageForPatient  = "<B> New Appointment Scheduled</B></ BR >Medical provider has scheduled a visit with Insurance Company: " + insuranceInfo.CompanyName + "<br><br>Thanks";
                    string NotificationForPatient = "Medical provider has scheduled a visit with Insurance Company: " + insuranceInfo.CompanyName;
                    string SmsMessageForPatient   = "<B> New Appointment Scheduled</B></ BR >Medical provider has scheduled a visit with Insurance Company: " + insuranceInfo.CompanyName + "<br><br>Thanks";

                    string MailMessageForDoctor  = "Appointment has been scheduled with Insurance Company: " + insuranceInfo.CompanyName + "<br><br>Thanks";
                    string NotificationForDoctor = "Appointment has been scheduled with Insurance Company: " + insuranceInfo.CompanyName;
                    string SmsMessageForDoctor   = "Appointment has been scheduled with Insurance Company: " + insuranceInfo.CompanyName + "<br><br>Thanks";

                    string MailMessageForStaff  = "";
                    string NotificationForStaff = "";
                    string SmsMessageForStaff   = "";
                    if (currentCompanyType == 2)
                    {
                        MailMessageForStaff  = "<B> New Appointment Scheduled</B></BR>A new Appointment schedule for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + "  with Insurance Company" + insuranceInfo.CompanyName + "<br><br>Thanks";
                        NotificationForStaff = "New Appointment schedule for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + "  with Insurance Company" + insuranceInfo.CompanyName;
                        SmsMessageForStaff   = "<B> New Appointment Scheduled</B></BR>A new Appointment schedule for patient : " + patientInfo.FirstName + " " + patientInfo.LastName + "  with Insurance Company" + insuranceInfo.CompanyName + "<br><br>Thanks";
                    }
                    else
                    {
                        MailMessageForStaff  = "<B> New Appointment Scheduled</B></BR>A new Appointment schedule for doctor : " + doctorInfo.FirstName + " " + doctorInfo.LastName + "  with Insurance Company" + insuranceInfo.CompanyName + "<br><br>Thanks";
                        NotificationForStaff = "New Appointment schedule for doctor : " + doctorInfo.FirstName + " " + doctorInfo.LastName + "  with Insurance Company" + insuranceInfo.CompanyName;
                        SmsMessageForStaff   = "<B> New Appointment Scheduled</B></BR>A new Appointment schedule for doctor : " + doctorInfo.FirstName + " " + doctorInfo.LastName + "  with Insurance Company" + insuranceInfo.CompanyName + "<br><br>Thanks";
                    }



                    #region  patient mail object
                    BO.EmailMessage emPatient = new BO.EmailMessage();
                    if (patientInfo != null)
                    {
                        emPatient.ApplicationName = "Midas";
                        emPatient.ToEmail         = patientInfo.UserName; //patientInfo.UserName;
                        emPatient.EMailSubject    = "MIDAS Notification";
                        emPatient.EMailBody       = MailMessageForPatient;
                    }
                    #endregion

                    #region patient sms object
                    BO.SMS smsPatient = new BO.SMS();
                    if (patientInfo != null)
                    {
                        smsPatient.ApplicationName = "Midas";
                        smsPatient.ToNumber        = patientInfo.ContactInfo.CellPhone;
                        smsPatient.Message         = SmsMessageForPatient;
                    }
                    #endregion



                    #region  doctor mail object
                    BO.EmailMessage emDoctor = new BO.EmailMessage();
                    if (doctorInfo != null)
                    {
                        emDoctor.ApplicationName = "Midas";
                        emDoctor.ToEmail         = doctorInfo.UserName; //doctorInfo.UserName;
                        emDoctor.EMailSubject    = "MIDAS Notification";
                        emDoctor.EMailBody       = MailMessageForDoctor;
                    }
                    #endregion

                    #region doctor sms object
                    BO.SMS smsDoctor = new BO.SMS();
                    if (doctorInfo != null)
                    {
                        smsDoctor.ApplicationName = "Midas";
                        smsDoctor.ToNumber        = doctorInfo.ContactInfo.CellPhone;
                        smsDoctor.Message         = SmsMessageForDoctor;
                    }
                    #endregion



                    NotificationHelper nh = new NotificationHelper();
                    MessagingHelper    mh = new MessagingHelper();

                    if (currentCompanyType == 2)   // 2 attorney
                    {
                        #region Patient
                        nh.PushNotification(patientInfo.UserName, AdminUser.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), NotificationForPatient, "New Patient Registration");  //patientInfo.UserName for Patient user email
                        mh.SendEmailAndSms(patientInfo.UserName, AdminUser.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), emPatient, smsPatient);
                        #endregion

                        foreach (var item in lstStaff)
                        {
                            #region  staff mail object
                            BO.EmailMessage emStaff = new BO.EmailMessage();
                            emStaff.ApplicationName = "Midas";
                            emStaff.ToEmail         = item.UserName;
                            emStaff.EMailSubject    = "MIDAS Notification";
                            emStaff.EMailBody       = MailMessageForStaff;
                            #endregion

                            #region admin sms object
                            BO.SMS smsStaff = new BO.SMS();
                            smsStaff.ApplicationName = "Midas";
                            smsStaff.ToNumber        = item.ContactInfo.CellPhone;
                            smsStaff.Message         = SmsMessageForStaff;
                            #endregion

                            nh.PushNotification(item.UserName, item.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), NotificationForStaff, "New Appointment Schedule"); //item.UserName
                            mh.SendEmailAndSms(item.UserName, item.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), emStaff, smsStaff);
                        }
                    }
                    else   // medical provider
                    {
                        #region Doctor
                        nh.PushNotification(doctorInfo.UserName, AdminUser.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), NotificationForDoctor, "New Appointment Schedule");  //doctorInfo.UserName
                        mh.SendEmailAndSms(doctorInfo.UserName, AdminUser.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), emDoctor, smsDoctor);
                        #endregion

                        foreach (var item in lstStaff)
                        {
                            #region  staff mail object
                            BO.EmailMessage emStaff = new BO.EmailMessage();
                            emStaff.ApplicationName = "Midas";
                            emStaff.ToEmail         = item.UserName;
                            emStaff.EMailSubject    = "MIDAS Notification";
                            emStaff.EMailBody       = MailMessageForStaff;
                            #endregion

                            #region staff sms object
                            BO.SMS smsStaff = new BO.SMS();
                            smsStaff.ApplicationName = "Midas";
                            smsStaff.ToNumber        = item.ContactInfo.CellPhone;
                            smsStaff.Message         = SmsMessageForStaff;
                            #endregion

                            nh.PushNotification(item.UserName, item.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), NotificationForStaff, "New Appointment Schedule");
                            mh.SendEmailAndSms(item.UserName, item.UserCompanies.Select(p => p.Company.id).FirstOrDefault(), emStaff, smsStaff);
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }

            var res = ConvertEOvisit <BO.EOVisit, EOVisit>(EOVisitDB);
            return((object)res);
        }