UFN_CHECKDETAIL_BENEFITSCHANGED

Determines if the benefits for a revenue detail record have changed.

Return

Return Type
bit

Parameters

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

Definition

Copy


            create function dbo.UFN_CHECKDETAIL_BENEFITSCHANGED
            (
                @REVENUEID uniqueidentifier,
                @BENEFITS xml
            )
            returns bit
            with execute as caller
            as
            begin
                declare @BENEFITSTABLE table
                (
                    ID uniqueidentifier,
                    BENEFITID uniqueidentifier,
                    QUANTITY int,
                    UNITVALUE money,
                    DETAILS nvarchar(255),
                    SEQUENCE int
                );

                declare @NEWBENEFITSTABLE table
                (
                    ID uniqueidentifier,
                    BENEFITID uniqueidentifier,
                    QUANTITY int,
                    UNITVALUE money,
                    DETAILS nvarchar(255),
                    SEQUENCE int
                );

                declare @BENEFITSCOUNT int;
                declare @CHANGED bit;
                set @CHANGED = 0;

                if @CHANGED = 0
                begin
                    insert into @NEWBENEFITSTABLE(ID, BENEFITID, QUANTITY, UNITVALUE, DETAILS, SEQUENCE
                        select ID, BENEFITID, QUANTITY, UNITVALUE, DETAILS, SEQUENCE from dbo.UFN_REVENUE_GETBENEFITS_FROMITEMLISTXML(@BENEFITS);

                    insert into @BENEFITSTABLE(ID, BENEFITID, QUANTITY, UNITVALUE, DETAILS, SEQUENCE
                            select ID, BENEFITID, QUANTITY, UNITVALUE, DETAILS, SEQUENCE from dbo.UFN_REVENUE_GETBENEFITS(@REVENUEID);

                    select @BENEFITSCOUNT = count(ID) from @BENEFITSTABLE;
                    if @BENEFITSCOUNT <> (select count(ID) from @NEWBENEFITSTABLE)
                        set @CHANGED = 1;

                    if @CHANGED = 0
                    begin
                        select @BENEFITSCOUNT = count([NEW].ID) 
                        from @NEWBENEFITSTABLE as [NEW]
                        inner join @BENEFITSTABLE as [OLD]
                            on [OLD].BENEFITID = [NEW].BENEFITID 
                            and [OLD].QUANTITY = [NEW].QUANTITY
                            and [OLD].UNITVALUE = [NEW].UNITVALUE
                            and [OLD].DETAILS = [NEW].DETAILS
                            and [OLD].SEQUENCE = [NEW].SEQUENCE;

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

                return @CHANGED;
            end