UFN_CHECKDETAIL_GIFTFEEDISTRIBUTIONCHANGED

Determines is gift fees distribution has changed.

Return

Return Type
bit

Parameters

Parameter Parameter Type Mode Description
@REVENUEID uniqueidentifier IN
@DISTRIBUTION xml IN

Definition

Copy


            CREATE function dbo.UFN_CHECKDETAIL_GIFTFEEDISTRIBUTIONCHANGED
            (
                @REVENUEID as uniqueidentifier,
                @DISTRIBUTION as xml
            ) 
            returns bit
            with execute as caller
            as
            begin
                declare @DISTRIBUTIONTABLE table
                (
                    ID uniqueidentifier,
                    AMOUNT money,
                    TRANSACTIONTYPECODE tinyint,
                    ACCOUNT nvarchar(100)
                );

                declare @NEWDISTRIBUTIONTABLE table
                (
                    ID uniqueidentifier,
                    AMOUNT money,
                    TRANSACTIONTYPECODE tinyint,
                    ACCOUNT nvarchar(100)
                );

                declare @DISTCOUNT int
                declare @CHANGED as bit
                set @CHANGED = 0;

                if @CHANGED = 0
                begin
                    insert into @NEWDISTRIBUTIONTABLE(ID, AMOUNT, TRANSACTIONTYPECODE, ACCOUNT) 
                        select ID, AMOUNT, TRANSACTIONTYPECODE, ACCOUNT from dbo.UFN_REVENUE_GETGIFTFEEGLDISTRIBUTION_FROMITEMLISTXML(@DISTRIBUTION);

                    insert into @DISTRIBUTIONTABLE(ID, AMOUNT, TRANSACTIONTYPECODE, ACCOUNT)
                        select ID, AMOUNT, TRANSACTIONTYPECODE, ACCOUNT from dbo.GIFTFEEGLDISTRIBUTION
                        where REVENUEID = @REVENUEID and OUTDATED = 0;

                    select @DISTCOUNT = count(ID) from @DISTRIBUTIONTABLE;

                    if @DISTCOUNT <> (select count(ID) from @NEWDISTRIBUTIONTABLE
                        set @CHANGED = 1;

                    if @CHANGED = 0
                    begin
                        select @DISTCOUNT = count([NEW].ID)
                        from @NEWDISTRIBUTIONTABLE as [NEW
                        inner join @DISTRIBUTIONTABLE as [OLD
                            on [NEW].AMOUNT = [OLD].AMOUNT 
                                and ([NEW].TRANSACTIONTYPECODE = [OLD].TRANSACTIONTYPECODE or ([NEW].TRANSACTIONTYPECODE is null and [OLD].TRANSACTIONTYPECODE is null)) 
                                and ([NEW].ACCOUNT = [OLD].ACCOUNT or ([NEW].ACCOUNT is null and [OLD].ACCOUNT is null));

                        if @DISTCOUNT <> (select count(ID) from @NEWDISTRIBUTIONTABLE)
                            set @CHANGED = 1;
                    end

                end

                return @CHANGED
            end