UFN_CHECKDETAIL_DISTRIBUTIONCHANGED

Determines is a 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_DISTRIBUTIONCHANGED(@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),
                    PROJECT nvarchar(100)
                );

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

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

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

                    insert into @DISTRIBUTIONTABLE(ID, AMOUNT, TRANSACTIONTYPECODE, ACCOUNT, PROJECT)
                        select ID, TRANSACTIONAMOUNT, TRANSACTIONTYPECODE, ACCOUNT, PROJECT from dbo.REVENUEGLDISTRIBUTION 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
                        if exists(
                            select 1
                            from @NEWDISTRIBUTIONTABLE
                            where ID is null
                        ) 
                            set @CHANGED = 1;

                        if @CHANGED = 0
                        begin

                            select @DISTCOUNT = count([NEW].ID)
                            from @NEWDISTRIBUTIONTABLE as [NEW
                            inner join @DISTRIBUTIONTABLE as [OLD
                                on [NEW].ID = [OLD].ID 
                                    and [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))
                                    and ([NEW].PROJECT = [OLD].PROJECT or ([NEW].PROJECT is null and [OLD].PROJECT is null));

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

                return @CHANGED
            end