/// <summary> /// Shows the giving stats for last12 months. /// </summary> private void ShowGivingStatsForLast12Months() { var givingId = Person.GivingId; var rockContext = new RockContext(); var financialTransactionService = new FinancialTransactionService(rockContext); var oneYearAgo = RockDateTime.Now.AddMonths(-12); var twelveMonthsTransactionsQry = financialTransactionService .GetGivingAutomationSourceTransactionQueryByGivingId(givingId) .Where(t => t.TransactionDateTime >= oneYearAgo); var twelveMonthTransactions = twelveMonthsTransactionsQry .Select(a => new { TransactionDateTime = a.TransactionDateTime, TotalAmount = a.TransactionDetails.Sum(d => d.Amount) }) .ToList(); var last12MonthTotal = twelveMonthTransactions.Sum(t => t.TotalAmount); var last12MonthCount = twelveMonthTransactions.Count; // Last 12 Months KPI string kpiLast12Months; var last12MonthCountText = $"{last12MonthCount} {"gift".PluralizeIf( last12MonthCount != 1 )}"; kpiLast12Months = GetKpiShortCode( "Last 12 Months", $"<span class=\"currency-span\">{FormatAsCurrency( last12MonthTotal )}</span>", subValue: $"<div class=\"small\">{last12MonthCountText}</div>"); // Last 90 Days KPI var oneHundredEightyDaysAgo = RockDateTime.Now.AddDays(-180); var ninetyDaysAgo = RockDateTime.Now.AddDays(-90); var transactionPriorNinetyDayTotal = twelveMonthTransactions.Where(t => t.TransactionDateTime >= oneHundredEightyDaysAgo && t.TransactionDateTime < ninetyDaysAgo).Sum(t => t.TotalAmount); var baseGrowthContribution = transactionPriorNinetyDayTotal; var last90DaysContribution = twelveMonthTransactions.Where(t => t.TransactionDateTime >= ninetyDaysAgo).Sum(t => t.TotalAmount); decimal growthPercent = 0; if (baseGrowthContribution == 0) { growthPercent = 100; } else { growthPercent = (last90DaysContribution - baseGrowthContribution) / baseGrowthContribution * 100; } var isGrowthPositive = growthPercent >= 0; var growthPercentText = Math.Abs(growthPercent).ToString("N1") + "%"; string growthPercentDisplay; // Show growth Percent // If more than 1000% show HIGH or LOW if (growthPercent > 1000) { growthPercentDisplay = "HIGH"; } else if (growthPercent < -1000) { growthPercentDisplay = "LOW"; } else { growthPercentDisplay = growthPercentText; } var last90DayCount = twelveMonthTransactions.Count(t => t.TransactionDateTime >= ninetyDaysAgo); var last90DayCountText = $"{last90DayCount} {"gift".PluralizeIf( last90DayCount != 1 )}"; var last90DaysSubValue = $@"<span title=""{growthPercentText}"" class=""small text-{ ( isGrowthPositive ? "success" : "danger" )}""> <i class=""fa {( isGrowthPositive ? "fa-arrow-up" : "fa-arrow-down" )}""></i> {growthPercentDisplay} </span> <div class=""small"">{last90DayCountText}</div>"; var kpiLast90Days = GetKpiShortCode( "Last 90 Days", $"<span class=\"currency-span\">{FormatAsCurrency( last90DaysContribution )}</span>", subValue: last90DaysSubValue); // Gives as family / individual KPI var givesAs = Person.GivingGroupId.HasValue ? "Family" : "Individual"; var givesAsIcon = Person.GivingGroupId.HasValue ? "fa-fw fa-users" : "fa-fw fa-user"; var kpiGivesAs = GetKpiShortCode("Gives As", givesAs, icon: givesAsIcon); // Giving Journey var journeyStage = ( GivingJourneyStage )Person.GetAttributeValue(Rock.SystemGuid.Attribute.PERSON_GIVING_CURRENT_GIVING_JOURNEY_STAGE.AsGuid()).AsInteger(); var journeyStageName = journeyStage.GetDescription() ?? journeyStage.ConvertToString(); var kpiGivingJourney = GetKpiShortCode("Giving Journey", journeyStageName, icon: "fa fa-fw fa-hiking"); // Combined KPIs var kpi = kpiLast12Months + kpiLast90Days + kpiGivesAs + kpiGivingJourney; var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(null); lLastGiving.Text = string.Format(@"{{[kpis style:'edgeless' iconbackground:'false' columnmin:'200px' columncount:'4' columncountmd:'4' columncountsm:'2']}}{0}{{[endkpis]}}", kpi).ResolveMergeFields(mergeFields); }