UFN_STOCKDETAIL_TRANSACTIONDISTRIBUTIONCHANGED

Determines is a distribution has changed for a transaction.

Return

Return Type
bit

Parameters

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

Definition

Copy


            CREATE function dbo.UFN_STOCKDETAIL_TRANSACTIONDISTRIBUTIONCHANGED(@TRANSACTIONID as uniqueidentifier, @DISTRIBUTION as xml) 
            returns bit
            with execute as caller
            as
            begin
                declare @DISTRIBUTIONTABLE table
                (
                    ID uniqueidentifier,
                    STOCKDETAILID uniqueidentifier,
                    AMOUNT money,
                    DEBITACCOUNT nvarchar(100),
                    CREDITACCOUNT nvarchar(100),
                    PROJECT nvarchar(100)
                );

                declare @NEWDISTRIBUTIONTABLE table
                (
                    ID uniqueidentifier,
                    STOCKDETAILID uniqueidentifier,
                    AMOUNT money,
                    DEBITACCOUNT nvarchar(100),
                    CREDITACCOUNT nvarchar(100),
                    PROJECT nvarchar(100)
                );

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

                if @CHANGED = 0
                begin
                    insert into @NEWDISTRIBUTIONTABLE(ID, STOCKDETAILID, AMOUNT, DEBITACCOUNT, CREDITACCOUNT, PROJECT) 
                        select ID, STOCKDETAILID, AMOUNT, DEBITACCOUNT, CREDITACCOUNT, PROJECT from dbo.UFN_REVENUETRANSACTION_GETSTOCKDETAILGLDISTRIBUTION_FROMITEMLISTXML(@DISTRIBUTION);

                    insert into @DISTRIBUTIONTABLE(ID, STOCKDETAILID, AMOUNT, DEBITACCOUNT, CREDITACCOUNT, PROJECT)
                        select ID, STOCKDETAILID, AMOUNT, DEBITACCOUNT, CREDITACCOUNT, PROJECT from dbo.STOCKDETAILGLDISTRIBUTION where STOCKDETAILID in (select REVENUEID from dbo.REVENUEPAYMENTMETHOD where REVENUEID = @TRANSACTIONID);

                    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].ID = [OLD].ID or ([NEW].ID is null and [OLD].ID is null)) 
                                and ([NEW].STOCKDETAILID = [OLD].STOCKDETAILID or ([NEW].STOCKDETAILID is null and [OLD].STOCKDETAILID is null)) 
                                and ([NEW].DEBITACCOUNT = [OLD].DEBITACCOUNT or ([NEW].DEBITACCOUNT is null and [OLD].DEBITACCOUNT is null)) 
                                and ([NEW].CREDITACCOUNT = [OLD].CREDITACCOUNT or ([NEW].CREDITACCOUNT is null and [OLD].CREDITACCOUNT 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

                return @CHANGED
            end