public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            var mergeFields = GetMergeFields(action);

            errorMessages = new List <string>();

            // Get the startdate/enddate
            DateTime?startDate = GetAttributeValue(action, "StartDate", true).ResolveMergeFields(mergeFields).AsDateTime();
            DateTime?endDate   = GetAttributeValue(action, "EndDate", true).ResolveMergeFields(mergeFields).AsDateTime();

            // Now set the person
            PersonAliasService personAliasService = new PersonAliasService(rockContext);
            PersonAlias        targetPersonAlias  = personAliasService.Get(GetAttributeValue(action, "Person", true).AsGuid());


            // get excluded currency types setting
            List <Guid> excludedCurrencyTypes = new List <Guid>();

            if (GetAttributeValue(action, "ExcludedCurrencyTypes").IsNotNullOrWhiteSpace())
            {
                excludedCurrencyTypes = GetAttributeValue(action, "ExcludedCurrencyTypes").Split(',').Select(Guid.Parse).ToList();
            }

            List <Guid> accountGuids = null;

            if (!string.IsNullOrWhiteSpace(GetAttributeValue(action, "Accounts")))
            {
                accountGuids = GetAttributeValue(action, "Accounts").Split(',').Select(Guid.Parse).ToList();
            }

            // Add all the merge fields from the Statement Utility
            Statement.AddMergeFields(mergeFields, targetPersonAlias.Person, new DateRange(startDate, endDate), excludedCurrencyTypes, accountGuids);

            var template = GetAttributeValue(action, "LavaTemplate");

            string output = template.ResolveMergeFields(mergeFields);


            // Now store the target attribute
            var targetAttribute = AttributeCache.Get(GetActionAttributeValue(action, "StatementHTML").AsGuid(), rockContext);

            if (targetAttribute.EntityTypeId == new Rock.Model.Workflow().TypeId)
            {
                action.Activity.Workflow.SetAttributeValue(targetAttribute.Key, output);
            }
            else if (targetAttribute.EntityTypeId == new WorkflowActivity().TypeId)
            {
                action.Activity.SetAttributeValue(targetAttribute.Key, output);
            }
            return(true);
        }
Example #2
0
        private void DisplayResults()
        {
            RockContext rockContext = new RockContext();

            var       statementYear = RockDateTime.Now.Year;
            DateRange dateRange     = new DateRange();

            if (Request["StatementYear"] != null)
            {
                Int32.TryParse(Request["StatementYear"].ToString(), out statementYear);
            }
            DateTime startDate = new DateTime(statementYear, 1, 1);
            DateTime endDate   = new DateTime(statementYear, 12, 31);

            if (Request["StatementStartDate"] != null)
            {
                DateTime.TryParse(Request["StatementStartDate"].ToString(), out startDate);
            }
            if (Request["StatementEndDate"] != null)
            {
                DateTime.TryParse(Request["StatementEndDate"].ToString(), out endDate);
            }
            dateRange.Start = startDate;
            dateRange.End   = endDate;

            Person targetPerson = CurrentPerson;

            // get excluded currency types setting
            List <Guid> excludedCurrencyTypes = new List <Guid>();

            if (GetAttributeValue("ExcludedCurrencyTypes").IsNotNullOrWhiteSpace())
            {
                excludedCurrencyTypes = GetAttributeValue("ExcludedCurrencyTypes").Split(',').Select(Guid.Parse).ToList();
            }

            var personGuid = Request["PersonGuid"].AsGuidOrNull();

            if (personGuid.HasValue)
            {
                // if "AllowPersonQueryString is False", only use the PersonGuid if it is a Guid of one of the current person's businesses
                var isCurrentPersonsBusiness = targetPerson != null && targetPerson.GetBusinesses().Any(b => b.Guid == personGuid.Value);
                if (GetAttributeValue("AllowPersonQuerystring").AsBoolean() || isCurrentPersonsBusiness)
                {
                    var person = new PersonService(rockContext).Get(personGuid.Value);
                    if (person != null)
                    {
                        targetPerson = person;
                    }
                }
            }

            List <Guid> accountGuids = null;

            if (!string.IsNullOrWhiteSpace(GetAttributeValue("Accounts")))
            {
                accountGuids = GetAttributeValue("Accounts").Split(',').Select(Guid.Parse).ToList();
            }

            var template = GetAttributeValue("LavaTemplate");

            var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, targetPerson);

            Statement.AddMergeFields(mergeFields, targetPerson, dateRange, excludedCurrencyTypes, accountGuids);
            lResults.Text = template.ResolveMergeFields(mergeFields);
        }