Query:
{%- if PageParameter['ActivePledges'] -%}
{%- assign active = PageParameter['ActivePledges'] -%}
{%- endif -%}
{%- if PageParameter['FinancialAccount'] -%}
{%- assign account = PageParameter['FinancialAccount'] -%}
{%- endif -%}
{%- if PageParameter['AnalysisDate'] -%}
{%- assign analysisdate = PageParameter['AnalysisDate'] -%}
{%- endif -%}
{%- if account -%}
{%- if analysisdate -%}
Declare @endDate as Date = '{{ analysisdate | Date: 'yyyy-MM-dd' }}' -- Date Set
{% else %}
Declare @endDate as Date = getDate() -- get Date
{% endif %}
Declare @account as Int = (Select Id From FinancialAccount Where [Guid] = '{{account}}')
DECLARE @PledgeReport table (PledgeId int, AccountId int, StartDate date, EndDate date, PersonId int, PledgeAmount decimal, TotalGiven decimal,PercentPledgeGiven decimal, PledgeProgress decimal )
INSERT INTO @PledgeReport
Select
fp.Id as PledgeId
, fp.AccountId as AccountId
, fp.StartDate
, fp.EndDate
, p.Id
, fp.TotalAmount as PledgeAmount
, Case When [TotalGiven] is Null Then 0 Else [TotalGiven] End as TotalGiven
, Case When TotalGiven is Null Then 0 Else Floor(Cast(TotalGiven as Decimal) / Cast(fp.TotalAmount as Decimal) * 100) End As PercentPledgeGiven
, Case
When Cast(DATEDIFF(day, fp.StartDate, @endDate) as Decimal) / Cast(DateDiff(day, fp.StartDate, fp.EndDate) as Decimal) > 1 Then 100
When TotalGiven = 0 Then 0
Else Floor(Cast(DATEDIFF(day, fp.StartDate, @endDate) as Decimal) / Cast(DateDiff(day, fp.StartDate, fp.EndDate) as Decimal) * 100)
End as PledgeProgress
From FinancialPledge fp
Join PersonAlias pa
on pa.Id = fp.PersonAliasId
Inner Join Person p on pa.PersonId = p.Id
Left Outer Join PhoneNumber pn
On pn.PersonId = p.Id and NumberTypeValueId = 12
Outer Apply (
Select
Distinct
Sum(aft.Amount) Over (Partition By fp.Id) as TotalGiven
From AnalyticsFactFinancialTransaction aft
Where
aft.TransactionDateTime >= fp.StartDate
and aft.TransactionDateTime <= @endDate
and p.GivingGroupId = aft.GivingGroupId
and fp.AccountId = aft.AccountId
) as [Sum]
Where
fp.AccountId = @account
and fp.StartDate <= @endDate
and fp.EndDate >= @endDate
Select
PledgeId
,p.Id
, fa.[Name]
, p.FirstName + ' ' + p.LastName as FullName
, spouse.FirstName + ' ' + spouse.LastName as Spouse
, p.Email
, dbo.ufnCrm_GetAddress(p.Id,'Home','FormattedHtmlAddress') as [Address]
, pn.NumberFormatted as MobilePhone
, Concat(PledgeProgress,'%') as PledgeProgress
, Concat(PercentPledgeGiven,'%') as '% Pledge Given'
, Case
When tpt.PercentPledgeGiven > tpt.PledgeProgress then 'Over'
When tpt.PercentPledgeGiven = tpt.PledgeProgress then 'Meets'
ELse 'Under'
End as PledgeStatus
, FORMAT(PledgeAmount, 'C') as TotalPledge
, FORMAT([TotalGiven], 'C') as [TotalGiven]
, Format(TotalGiven - (PledgeAmount), 'C') as DifferenceFromTotalPledge
, Format(PledgeAmount * (PledgeProgress / 100),'C') as PledgeToDate
, Format(TotalGiven - (PledgeAmount * (PledgeProgress / 100)), 'C') as DifferenceToDate
, av.ValueAsDateTime as LastGave
From @PledgeReport tpt
Inner Join FinancialAccount fa
on fa.Id = tpt.AccountId
Inner Join Person p
on p.Id = tpt.PersonId
Left Outer Join PhoneNumber pn
On pn.PersonId = p.Id and NumberTypeValueId = 12
Left Outer Join AttributeValue av
on p.Id = av.EntityId and AttributeId = 1788
Left OUter Join Person spouse
on spouse.Id = dbo.ufnCrm_GetSpousePersonIDFromPersonId(p.Id)
{%- endif -%}