UFN_PLEDGE_GETSPLITSFORPAYMENT
Returns the splits for a pledge, pro-rated to a given amount.
Return
Return Type |
---|
table |
Parameters
Parameter | Parameter Type | Mode | Description |
---|---|---|---|
@PLEDGEID | uniqueidentifier | IN | |
@AMOUNT | money | IN |
Definition
Copy
CREATE function dbo.UFN_PLEDGE_GETSPLITSFORPAYMENT
(
@PLEDGEID uniqueidentifier,
@AMOUNT money
)
returns @PAYMENTSPLITS table
(
ID uniqueidentifier,
APPLICATIONCODE tinyint,
TYPECODE tinyint,
DESIGNATIONID uniqueidentifier,
AMOUNT money
)
as
begin
declare @WEIGHTSUM decimal(30, 5);
declare @IDEALAMOUNT decimal(30, 5);
declare @AMOUNTDISTRIBUTEDIDEAL decimal(30, 5);
declare @AMOUNTDISTRIBUTED decimal(30, 5);
declare @SOURCETYPE tinyint;
declare @WEIGHTAMOUNT decimal(30, 5);
declare @DESIGNATIONID uniqueidentifier;
declare @APPLICATIONID uniqueidentifier;
set @AMOUNTDISTRIBUTEDIDEAL = 0;
set @AMOUNTDISTRIBUTED = 0;
select @SOURCETYPE = TRANSACTIONTYPECODE from dbo.REVENUE where ID = @PLEDGEID
--Sanity check the inputs
--only allow larger amounts for recurring gifts
if @AMOUNT > (select AMOUNT from dbo.REVENUE where ID = @PLEDGEID) and coalesce(@SOURCETYPE, 0) <> 2
begin
insert into @PAYMENTSPLITS(ID, TYPECODE, APPLICATIONCODE, DESIGNATIONID, AMOUNT)
select ID, TYPECODE, APPLICATIONCODE, DESIGNATIONID, NULL
from dbo.REVENUESPLIT
where REVENUEID = @PLEDGEID
order by DATEADDED;
end
else
begin
insert into @PAYMENTSPLITS(ID, TYPECODE, APPLICATIONCODE, DESIGNATIONID, AMOUNT)
select ID, TYPECODE, APPLICATIONCODE, DESIGNATIONID, AMOUNT
from dbo.REVENUESPLIT
where REVENUEID = @PLEDGEID
order by DATEADDED;
select
@WEIGHTSUM = sum(AMOUNT)
from @PAYMENTSPLITS;
declare PAYMENTSPLITS cursor local fast_forward for
select ID, AMOUNT
from @PAYMENTSPLITS
open PAYMENTSPLITS;
fetch next from PAYMENTSPLITS into @APPLICATIONID, @WEIGHTAMOUNT;
while @@FETCH_STATUS = 0
begin
if @WEIGHTSUM <> 0
set @IDEALAMOUNT = (@WEIGHTAMOUNT / @WEIGHTSUM) * @AMOUNT;
else
set @IDEALAMOUNT = 0;
set @WEIGHTAMOUNT = ROUND(@IDEALAMOUNT + @AMOUNTDISTRIBUTEDIDEAL - @AMOUNTDISTRIBUTED, 2);
update @PAYMENTSPLITS
set AMOUNT = @WEIGHTAMOUNT
where ID = @APPLICATIONID;
set @AMOUNTDISTRIBUTEDIDEAL = @AMOUNTDISTRIBUTEDIDEAL + @IDEALAMOUNT;
set @AMOUNTDISTRIBUTED = @AMOUNTDISTRIBUTED + @WEIGHTAMOUNT;
fetch next from PAYMENTSPLITS into @APPLICATIONID, @WEIGHTAMOUNT;
end
--When a cursor is used, it should be explicitly closed/deallocated in case of blocking or USP running long
close PAYMENTSPLITS;
deallocate PAYMENTSPLITS;
end
return;
end