public static void AddAttendanceEvents(HomeGroupEventViewModel hgEvent, oikonomosEntities context, int eventValue, string eventName, Person currentPerson)
        {
            OldEvent groupEvent = new OldEvent();
            groupEvent.Created = DateTime.Now;
            groupEvent.CreatedByPersonId = currentPerson.PersonId;
            groupEvent.ChurchId = currentPerson.ChurchId;

            //Check to see if it is not already in the database
            var groupCheck = (from e in context.OldEvents
                              where e.TableId == (int)Tables.Group
                              && e.Reference == hgEvent.GroupId
                              && e.EventDate == hgEvent.EventDate
                              && e.Description == eventName
                              select e).FirstOrDefault();

            if (groupCheck != null)
            {
                groupEvent = groupCheck;
            }
            else
            {
                context.OldEvents.AddObject(groupEvent);
            }

            groupEvent.TableId = (int)Tables.Group;
            groupEvent.Reference = hgEvent.GroupId;
            groupEvent.Description = eventName;
            groupEvent.EventDate = hgEvent.EventDate;
            groupEvent.Value = eventValue.ToString();
            groupEvent.EventVisibilityId = (int)EventVisibilities.GroupAdministrators;
            groupEvent.Changed = DateTime.Now;
            groupEvent.ChangedByPersonId = currentPerson.PersonId;
        }
        public static void CheckToSeeIfEventAlreadyExists(PersonEventViewModel personEvents, oikonomosEntities context, EventViewModel personEvent, OldEvent pe)
        {
            string groupId = personEvent.GroupId.ToString();
            //Check to see if this event already exists
            OldEvent duplicateEvent = null;
            var check = (from e in context.OldEvents
                         where e.TableId == (int)Tables.Person
                         && e.Reference == personEvents.PersonId
                         && e.EventDate == personEvent.Date
                         && e.Description.StartsWith(personEvent.Name) //Caters for the attended homegroup (blah)
                         select e);

            if (personEvent.GroupId != 0)
            {
                duplicateEvent = (from e in check
                                  where e.Value == groupId
                                  select e).FirstOrDefault();
            }
            else
            {
                duplicateEvent = (from e in check select e).FirstOrDefault();
            }

            if (duplicateEvent == null)
            {
                context.OldEvents.AddObject(pe);
            }

            if (personEvent.Name == EventNames.AttendedGroup)
            {
                //Remove a "did not attend event on the same date"
                var atCheck = (from e in context.OldEvents
                               where e.TableId == (int)Tables.Person
                               && e.Reference == personEvents.PersonId
                               && e.EventDate == personEvent.Date
                               && e.Description.StartsWith(EventNames.DidNotAttendGroup)
                               select e);

                if (personEvent.GroupId != 0)
                {
                    duplicateEvent = (from e in atCheck
                                      where e.Value == groupId
                                      select e).FirstOrDefault();
                }
                else
                {
                    duplicateEvent = (from e in check select e).FirstOrDefault();
                }

                if (duplicateEvent != null)
                {
                    context.DeleteObject(duplicateEvent);
                }
            }

            if (personEvent.Name == EventNames.DidNotAttendGroup)
            {
                //Remove an "attended" event on the same date
                var atCheck = (from e in context.OldEvents
                               where e.TableId == (int)Tables.Person
                               && e.Reference == personEvents.PersonId
                               && e.EventDate == personEvent.Date
                               && e.Description.StartsWith(EventNames.AttendedGroup)
                               select e);

                if (personEvent.GroupId != 0)
                {
                    duplicateEvent = (from e in atCheck
                                      where e.Value == groupId
                                      select e).FirstOrDefault();
                }
                else
                {
                    duplicateEvent = (from e in check select e).FirstOrDefault();
                }

                if (duplicateEvent != null)
                {
                    context.DeleteObject(duplicateEvent);
                }
            }
        }
 public static OldEvent SavePersonEvent(oikonomosEntities context, PersonEventViewModel personEvents, Person currentPerson, EventViewModel personEvent)
 {
     var pe = new OldEvent
                  {
                      Created = DateTime.Now,
                      CreatedByPersonId = currentPerson.PersonId,
                      Changed = DateTime.Now,
                      ChangedByPersonId = currentPerson.PersonId,
                      Description = personEvent.Name,
                      TableId = (int) Tables.Person,
                      EventVisibilityId = (int) EventVisibilities.GroupAdministrators,
                      Reference = personEvents.PersonId,
                      EventDate = personEvent.Date,
                      ChurchId = currentPerson.ChurchId
                  };
     if (personEvent.GroupId != 0)
     {
         pe.Value = personEvent.GroupId.ToString();
         if (personEvent.Name == EventNames.AttendedGroup
             || personEvent.Name == EventNames.DidNotAttendGroup
             || personEvent.Name == EventNames.LeftTheGroup)
         {
             var hgName = (from g in context.Groups
                           where g.GroupId == personEvent.GroupId
                           select g.Name).FirstOrDefault();
             pe.Description += string.Format(" ({0})", hgName);
         }
     }
     return pe;
 }
        public static void SavePersonComment(int personId, string comment, Person currentPerson)
        {
            using (oikonomosEntities context = new oikonomosEntities(ConfigurationManager.ConnectionStrings["oikonomosEntities"].ConnectionString))
            {
                OldEvent commentEvent = new OldEvent();
                commentEvent.Description = EventNames.Comment;
                commentEvent.Comments = comment;
                commentEvent.EventVisibilityId = (int)EventVisibilities.GroupAdministrators;
                commentEvent.CreatedByPersonId = currentPerson.PersonId;
                commentEvent.ChangedByPersonId = currentPerson.PersonId;
                commentEvent.Created = DateTime.Now;
                commentEvent.Changed = DateTime.Now;
                commentEvent.EventDate = DateTime.Now;
                commentEvent.TableId = (int)Tables.Person;
                commentEvent.Reference = personId;
                commentEvent.ChurchId = currentPerson.ChurchId;

                context.AddToOldEvents(commentEvent);

                context.SaveChanges();
            }
        }