UFN_PLEDGE_GETINSTALLMENTREVALUATIONGAINLOSS

Returns a table containing the gain/loss due to revaluation for the installments on the given pledge.

Return

Return Type
table

Parameters

Parameter Parameter Type Mode Description
@PLEDGEID uniqueidentifier IN

Definition

Copy


            CREATE function dbo.UFN_PLEDGE_GETINSTALLMENTREVALUATIONGAINLOSS
            (
                @PLEDGEID uniqueidentifier
            ) 
            returns @INSTALLMENTGAINLOSS table(
                INSTALLMENTID uniqueidentifier,
                BASEGAINLOSS money,
                ORGANIZATIONGAINLOSS money,
                ISGAIN bit
            )
            with execute as caller
            as begin

                if dbo.UFN_REVENUE_HASNEEDEDRATES(@PLEDGEID) = 1
                begin

                    declare @CURRENTDATE datetime = getdate();

                    --Compute gain/loss for each record.

                    declare @INSTALLMENTREVALUATIONGAINLOSSES table(
                        INSTALLMENTID uniqueidentifier,
                        BASEGAINLOSS money,
                        ORGANIZATIONGAINLOSS money
                    );
                    insert into @INSTALLMENTREVALUATIONGAINLOSSES
                    select
                        INSTALLMENTS.ID,
                        (INSTALLMENTS.BASEBALANCE - ORGINIALINSTALLMENTS.BASEBALANCE),
                        (INSTALLMENTS.ORGANIZATIONBALANCE - ORGINIALINSTALLMENTS.ORGANIZATIONBALANCE)
                    from dbo.UFN_PLEDGE_GETREVALUEDINSTALLMENTBALANCES(@PLEDGEID,@CURRENTDATE,0) INSTALLMENTS
                        cross apply dbo.UFN_PLEDGE_GETREVALUEDINSTALLMENTBALANCES(@PLEDGEID,@CURRENTDATE,1) ORGINIALINSTALLMENTS
                    where INSTALLMENTS.ID = ORGINIALINSTALLMENTS.ID;    

                    --Delete rows that don't have a gain/loss

                    delete @INSTALLMENTREVALUATIONGAINLOSSES
                    where BASEGAINLOSS = 0 and ORGANIZATIONGAINLOSS = 0;

                    --Add rows to be returned if base and org gain/loss are both positive, both negative, or either is zero

                    insert into @INSTALLMENTGAINLOSS
                    select 
                        INSTALLMENTID,
                        abs(BASEGAINLOSS),
                        abs(ORGANIZATIONGAINLOSS),
                        case
                            when BASEGAINLOSS > 0 or ORGANIZATIONGAINLOSS > 0
                                then 1
                            else 0
                        end
                    from @INSTALLMENTREVALUATIONGAINLOSSES
                    where (BASEGAINLOSS * ORGANIZATIONGAINLOSS) >= 0;

                    --Add base gain/loss rows to be returned if base and org are both not zero and one is positive while the other is negative

                    insert into @INSTALLMENTGAINLOSS
                    select 
                        INSTALLMENTID,
                        abs(BASEGAINLOSS),
                        0,
                        case
                            when BASEGAINLOSS > 0
                                then 1
                            else 0
                        end
                    from @INSTALLMENTREVALUATIONGAINLOSSES
                    where (BASEGAINLOSS * ORGANIZATIONGAINLOSS) < 0;

                    --Add org gain/loss rows to be returned if base and org are both not zero and one is positive while the other is negative

                    insert into @INSTALLMENTGAINLOSS
                    select 
                        INSTALLMENTID,
                        0,
                        abs(ORGANIZATIONGAINLOSS),
                        case
                            when ORGANIZATIONGAINLOSS > 0
                                then 1
                            else 0
                        end
                    from @INSTALLMENTREVALUATIONGAINLOSSES
                    where (BASEGAINLOSS * ORGANIZATIONGAINLOSS) < 0;

                end

                return;
            end