public override bool PerformAction(Assignment assignment, Person currentPerson)
        {
            try
            {
                if (assignment.Requester != null && assignment.Requester.Area != null)
                {
                    AssignmentTypeField field = assignment.AssignmentType.Fields.FindByTitle("Requester's Area");
                    if (field != null)
                    {
                        AssignmentFieldValue fieldValue = assignment.FieldValues.FindByID(field.CustomFieldId);
                        if (fieldValue == null)
                        {
                            fieldValue = new AssignmentFieldValue();
                            fieldValue.AssignmentId  = assignment.AssignmentId;
                            fieldValue.CustomFieldId = field.CustomFieldId;
                            assignment.FieldValues.Add(fieldValue);
                        }
                        fieldValue.SelectedValue = assignment.Requester.Area.AreaID.ToString();
                        assignment.Save("SetAreaFromRequester", null);
                    }
                }

                return(true);
            }
            catch (System.Exception ex)
            {
                assignment.AddNote(ex.Message, false, currentPerson, "SetAreaFromRequester");
                return(false);
            }
        }
        public WorkerResultStatus ProcessVisitors(out string message, out int state)
        {
            Trace.Write("Starting FirstTimeVisitAssignments Agent...\n");

            WorkerResultStatus workerResultStatus = WorkerResultStatus.Ok;

            message = string.Empty;
            state   = STATE_OK;

            try
            {
                //groupAssignmentType = new AssignmentType(GroupVisitAssignmentType);
                //if (groupAssignmentType.AssignmentTypeId == -1)
                //    throw new Exception("Invalid 'Neighborhood Group Assignment Type' setting");

                childAssignmentType = new AssignmentType(ChildVisitAssignmentType);
                if (childAssignmentType.AssignmentTypeId == -1)
                {
                    throw new Exception("Invalid 'Child Assignment Type' setting");
                }

                juniorHighAssignmentType = new AssignmentType(JuniorHighVisitAssignmentType);
                if (juniorHighAssignmentType.AssignmentTypeId == -1)
                {
                    throw new Exception("Invalid 'Junior High Assignment Type' setting");
                }

                highSchoolAssignmentType = new AssignmentType(HighSchoolVisitAssignmentType);
                if (highSchoolAssignmentType.AssignmentTypeId == -1)
                {
                    throw new Exception("Invalid 'High School Assignment Type' setting");
                }

                adultAssignmentType = new AssignmentType(AdultVisitAssignmentType);
                if (adultAssignmentType.AssignmentTypeId == -1)
                {
                    throw new Exception("Invalid 'Adult Assignment Type' setting");
                }

                //groupFamilyIDField = GetAssignmentTypeField(groupAssignmentType, "Family ID");
                //groupFirstVisitDateField = GetAssignmentTypeField(groupAssignmentType, "First Visit Date");

                childFamilyIDField       = GetAssignmentTypeField(childAssignmentType, "Family ID");
                childFirstVisitDateField = GetAssignmentTypeField(childAssignmentType, "First Visit Date");

                juniorHighFamilyIDField       = GetAssignmentTypeField(juniorHighAssignmentType, "Family ID");
                juniorHighFirstVisitDateField = GetAssignmentTypeField(juniorHighAssignmentType, "First Visit Date");

                highSchoolFamilyIDField       = GetAssignmentTypeField(highSchoolAssignmentType, "Family ID");
                highSchoolFirstVisitDateField = GetAssignmentTypeField(highSchoolAssignmentType, "First Visit Date");

                adultFamilyIDField       = GetAssignmentTypeField(adultAssignmentType, "Family ID");
                adultFirstVisitDateField = GetAssignmentTypeField(adultAssignmentType, "First Visit Date");

                Lookup child = new Lookup(SystemLookup.FamilyRole_Child);

                DateTime beginDate = DateTime.Today.Date.AddDays(-DaysBack);

                ArrayList lst = new ArrayList();
                lst.Add(new SqlParameter("@AttributeID", FirstTimeVisitAttributeID));
                lst.Add(new SqlParameter("@BeginDate", beginDate));
                //lst.Add(new SqlParameter("@GroupFamilyIDField", groupFamilyIDField.CustomFieldId));
                lst.Add(new SqlParameter("@ChildFamilyIDField", childFamilyIDField.CustomFieldId));
                lst.Add(new SqlParameter("@JuniorHighFamilyIDField", juniorHighFamilyIDField.CustomFieldId));
                lst.Add(new SqlParameter("@HighSchoolFamilyIDField", highSchoolFamilyIDField.CustomFieldId));
                lst.Add(new SqlParameter("@AdultFamilyIDField", adultFamilyIDField.CustomFieldId));

                Arena.DataLayer.Organization.OrganizationData oData = new Arena.DataLayer.Organization.OrganizationData();

                SqlDataReader rdr = oData.ExecuteReader("cust_ccv_recent_visitor_families", lst);

                while (rdr.Read())
                {
                    OccurrenceAttendanceCollection attendances = new OccurrenceAttendanceCollection();
                    //bool interestedInGroup = false;

                    Family family = new Family((int)rdr["family_id"]);

                    bool newFamily  = true;
                    int  childCount = 0;
                    int  adultCount = 0;
                    bool attendedHS = false;
                    bool attendedJH = false;

                    foreach (FamilyMember fm in family.FamilyMembers)
                    {
                        PersonAttribute pa = (PersonAttribute)fm.Attributes.FindByID(FirstTimeVisitAttributeID);
                        if (pa != null && !pa.DateValue.IsEmptyDate() && pa.DateValue < beginDate)
                        {
                            newFamily = false;
                        }

                        if (newFamily)
                        {
                            if (fm.FamilyRole.LookupID == child.LookupID)
                            {
                                childCount++;
                            }
                            else
                            {
                                adultCount++;
                            }

                            //pa = (PersonAttribute)fm.Attributes.FindByID(InterestedInGroupAttributeID);
                            //if (pa != null && pa.IntValue == 1)
                            //    interestedInGroup = true;

                            ArrayList lstOccurrence = new ArrayList();
                            lstOccurrence.Add(new SqlParameter("@PersonID", fm.PersonID));
                            SqlDataReader rdrOccurrence = oData.ExecuteReader("cust_ccv_recent_visitor_first_checkin", lstOccurrence);
                            if (rdrOccurrence.Read())
                            {
                                if (((DateTime)rdrOccurrence["first_attended"]).Date >= beginDate)
                                {
                                    OccurrenceAttendance oa = new OccurrenceAttendance((int)rdrOccurrence["occurrence_attendance_id"]);
                                    if (oa.Occurrence.OccurrenceType.OccurrenceTypeId == HighSchoolAttendanceTypeID)
                                    {
                                        attendedHS = true;
                                    }
                                    else if (oa.Occurrence.OccurrenceType.OccurrenceTypeId == JuniorHighAttendanceTypeID)
                                    {
                                        attendedJH = true;
                                    }

                                    attendances.Add(oa);
                                }
                            }
                            rdrOccurrence.Close();
                        }
                    }

                    if (newFamily)
                    {
                        _regionalPastorID = -1;

                        Assignment assignment = new Assignment();
                        assignment.Title             = family.FamilyName;
                        assignment.Description       = BuildDescription(family, attendances);
                        assignment.RequesterPersonId = family.FamilyHead.PersonID;

                        //if (interestedInGroup)
                        //{
                        //    assignment.AssignmentTypeId = groupAssignmentType.AssignmentTypeId;
                        //    assignment.PriorityId = groupAssignmentType.DefaultPriorityId;
                        //    assignment.StatusId = groupAssignmentType.DefaultStatusId;

                        //    AssignmentFieldValue familyIDField = new AssignmentFieldValue(groupFamilyIDField.CustomFieldId);
                        //    familyIDField.SelectedValue = family.FamilyID.ToString();
                        //    assignment.FieldValues.Add(familyIDField);

                        //    AssignmentFieldValue firstVisitDateField = new AssignmentFieldValue(groupFirstVisitDateField.CustomFieldId);
                        //    firstVisitDateField.SelectedValue = ((DateTime)rdr["first_visit"]).ToShortDateString();
                        //    assignment.FieldValues.Add(firstVisitDateField);

                        //    if (_regionalPastorID != -1)
                        //        assignment.SubmitAssignmentEntry(childAssignmentType.Owner != null ? adultAssignmentType.Owner : family.FamilyHead, "FirstTimeVisitAssignment Agent", _regionalPastorID);
                        //    else
                        //        assignment.SubmitAssignmentEntry(childAssignmentType.Owner != null ? adultAssignmentType.Owner : family.FamilyHead, "FirstTimeVisitAssignment Agent");
                        //}
                        //else
                        //{

                        if (attendedHS)
                        {
                            assignment.AssignmentTypeId = highSchoolAssignmentType.AssignmentTypeId;
                            assignment.PriorityId       = highSchoolAssignmentType.DefaultPriorityId;

                            AssignmentFieldValue familyIDField = new AssignmentFieldValue(highSchoolFamilyIDField.CustomFieldId);
                            familyIDField.SelectedValue = family.FamilyID.ToString();
                            assignment.FieldValues.Add(familyIDField);

                            AssignmentFieldValue firstVisitDateField = new AssignmentFieldValue(highSchoolFirstVisitDateField.CustomFieldId);
                            firstVisitDateField.SelectedValue = ((DateTime)rdr["first_visit"]).ToShortDateString();
                            assignment.FieldValues.Add(firstVisitDateField);

                            assignment.SubmitAssignmentEntry(highSchoolAssignmentType.Owner != null ? highSchoolAssignmentType.Owner : family.FamilyHead, "FirstTimeVisitAssignment Agent");
                        }
                        else if (attendedJH)
                        {
                            assignment.AssignmentTypeId = juniorHighAssignmentType.AssignmentTypeId;
                            assignment.PriorityId       = juniorHighAssignmentType.DefaultPriorityId;

                            AssignmentFieldValue familyIDField = new AssignmentFieldValue(juniorHighFamilyIDField.CustomFieldId);
                            familyIDField.SelectedValue = family.FamilyID.ToString();
                            assignment.FieldValues.Add(familyIDField);

                            AssignmentFieldValue firstVisitDateField = new AssignmentFieldValue(juniorHighFirstVisitDateField.CustomFieldId);
                            firstVisitDateField.SelectedValue = ((DateTime)rdr["first_visit"]).ToShortDateString();
                            assignment.FieldValues.Add(firstVisitDateField);

                            assignment.SubmitAssignmentEntry(highSchoolAssignmentType.Owner != null ? juniorHighAssignmentType.Owner : family.FamilyHead, "FirstTimeVisitAssignment Agent");
                        }
                        else if (childCount == 0)
                        {
                            assignment.AssignmentTypeId = adultAssignmentType.AssignmentTypeId;
                            assignment.PriorityId       = adultAssignmentType.DefaultPriorityId;

                            AssignmentFieldValue familyIDField = new AssignmentFieldValue(adultFamilyIDField.CustomFieldId);
                            familyIDField.SelectedValue = family.FamilyID.ToString();
                            assignment.FieldValues.Add(familyIDField);

                            AssignmentFieldValue firstVisitDateField = new AssignmentFieldValue(adultFirstVisitDateField.CustomFieldId);
                            firstVisitDateField.SelectedValue = ((DateTime)rdr["first_visit"]).ToShortDateString();
                            assignment.FieldValues.Add(firstVisitDateField);

                            assignment.SubmitAssignmentEntry(adultAssignmentType.Owner != null ? adultAssignmentType.Owner : family.FamilyHead, "FirstTimeVisitAssignment Agent");
                        }
                        else if (adultCount > 0)
                        {
                            assignment.AssignmentTypeId = childAssignmentType.AssignmentTypeId;
                            assignment.PriorityId       = childAssignmentType.DefaultPriorityId;

                            AssignmentFieldValue familyIDField = new AssignmentFieldValue(childFamilyIDField.CustomFieldId);
                            familyIDField.SelectedValue = family.FamilyID.ToString();
                            assignment.FieldValues.Add(familyIDField);

                            AssignmentFieldValue firstVisitDateField = new AssignmentFieldValue(childFirstVisitDateField.CustomFieldId);
                            firstVisitDateField.SelectedValue = ((DateTime)rdr["first_visit"]).ToShortDateString();
                            assignment.FieldValues.Add(firstVisitDateField);

                            assignment.SubmitAssignmentEntry(childAssignmentType.Owner != null ? childAssignmentType.Owner : family.FamilyHead, "FirstTimeVisitAssignment Agent");
                        }
                    }
                }
                rdr.Close();
            }
            catch (Exception ex)
            {
                workerResultStatus = WorkerResultStatus.Exception;
                message            = "An error occured while processing First Time Visitor Assignments.\n\nMessage\n------------------------\n" + ex.Message + "\n\nStack Trace\n------------------------\n" + ex.StackTrace;
            }
            finally
            {
            }

            return(workerResultStatus);
        }
Пример #3
0
        public override bool PerformAction(Assignment assignment, Person currentPerson)
        {
            try
            {
                if (assignment.WorkerPersonId == -1)
                {
                    AssignmentTypeField field = assignment.AssignmentType.Fields.FindByTitle("Requester's Area");
                    if (field != null)
                    {
                        AssignmentFieldValue fieldValue = assignment.FieldValues.FindByID(field.CustomFieldId);
                        if (fieldValue != null)
                        {
                            Area area = new Area(Convert.ToInt32(fieldValue.SelectedValue));
                            if (area != null)
                            {
                                foreach (AreaOutreachCoordinator leader in area.OutreachCoordinators)
                                {
                                    if (leader.AreaRole.LookupID == Convert.ToInt32(AreaRoleSetting))
                                    {
                                        assignment.WorkerPersonId = leader.PersonId;
                                        assignment.Save("AssignAreaLeader", null);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                if (assignment.WorkerPersonId != -1)
                {
                    if (assignment.RequesterPersonId != -1 && Boolean.Parse(NotifyRequesterSetting))
                    {
                        AssignmentEntryRequesterEmail email = new AssignmentEntryRequesterEmail();
                        if (currentPerson != null && currentPerson.Emails.FirstActive != string.Empty)
                        {
                            email.Template.Sender      = currentPerson.FullName;
                            email.Template.SenderEmail = currentPerson.Emails.FirstActive;
                        }
                        email.Send(assignment);
                    }

                    if (assignment.WorkerPersonId != -1 && Boolean.Parse(NotifyWorkerSetting))
                    {
                        AssignmentEntryWorkerEmail email = new AssignmentEntryWorkerEmail();
                        if (currentPerson != null && currentPerson.Emails.FirstActive != string.Empty)
                        {
                            email.Template.Sender      = currentPerson.FullName;
                            email.Template.SenderEmail = currentPerson.Emails.FirstActive;
                        }
                        email.Send(assignment);
                    }
                }

                return(true);
            }
            catch (System.Exception ex)
            {
                assignment.AddNote(ex.Message, false, currentPerson, "AssignAreaLeader");
                return(false);
            }
        }