USP_DATAFORMTEMPLATE_EDIT_WRITEOFFGLDISTRIBUTION

The save procedure used by the edit dataform template "Write-off GL Distribution Edit Form".

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier IN The input ID parameter indicating the ID of the record being edited.
@GLDISTRIBUTION xml IN Revenue GL distribution
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the procedure.

Definition

Copy


                    CREATE procedure dbo.USP_DATAFORMTEMPLATE_EDIT_WRITEOFFGLDISTRIBUTION
                    (
                        @ID uniqueidentifier,
                        @GLDISTRIBUTION xml,
                        @CHANGEAGENTID uniqueidentifier = null
                    )
                    as
                    set nocount on;

                    begin try

                        --validate post status

                        if (select count(ID) from dbo.WRITEOFF
                            where ID = @ID and POSTSTATUSCODE = 0) > 0
                            raiserror('You cannot edit a posted write-off.', 13, 1)

                        -- If user hasn't entered anything in the grid we will assume that user 

                        -- wants to use system-generated values for Post to GL and delete any previous entries

                        --if (select count(*) FROM dbo.UFN_REVENUE_GETWRITEOFFGLDISTRIBUTION_FROMITEMLISTXML(@GLDISTRIBUTION)) = 0

                        --    begin

                        --        delete from dbo.WRITEOFFGLDISTRIBUTION where WRITEOFFID = @ID;

                        --        return 0;

                        --    end


                        if (dbo.UFN_GLDISTRIBUTION_DEBITSEQUALCREDITS(@GLDISTRIBUTION) = 0)
                            raiserror('The sum of the debit accounts must equal the sum of the credit accounts.', 13, 1)

                        --if (dbo.UFN_GLDISTRIBUTION_ACCOUNTEXISTS(@GLDISTRIBUTION) = 0)

                        --    raiserror('One or more of the edited accounts do not exist.', 13, 1)


                        declare @REVENUEID uniqueidentifier
                        select @REVENUEID = b.REVENUEID  from dbo.WRITEOFFGLDISTRIBUTION as a join WRITEOFF as b on  a.WRITEOFFID = b.ID where WRITEOFFID = @ID                            

                        if (dbo.UFN_GLDISTRIBUTION_ACCOUNTEXISTS_2(@GLDISTRIBUTION,@REVENUEID) = 0)
                            raiserror('One or more of the edited accounts do not exist.', 13, 1)

                        if @CHANGEAGENTID is null  
                            exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output;

                        declare @POSTDATE datetime;
                        select @POSTDATE = POSTDATE from dbo.WRITEOFF where ID = @ID;

                        declare @TRANSACTIONCURRENCYID uniqueidentifier;
                        declare @BASECURRENCYID uniqueidentifier;
                        declare @BASEEXCHANGERATEID uniqueidentifier;
                        declare @ORGANIZATIONEXCHANGERATEID uniqueidentifier;

                        select top 1
                            @TRANSACTIONCURRENCYID = WRITEOFFSPLIT.TRANSACTIONCURRENCYID,
                            @BASECURRENCYID = WRITEOFFSPLIT.BASECURRENCYID,
                            @BASEEXCHANGERATEID = WRITEOFFSPLIT.BASEEXCHANGERATEID,
                            @ORGANIZATIONEXCHANGERATEID = WRITEOFFSPLIT.ORGANIZATIONEXCHANGERATEID
                        from dbo.WRITEOFFSPLIT
                        where WRITEOFFSPLIT.WRITEOFFID = @ID;

                        set @GLDISTRIBUTION = dbo.UFN_GLDISTRIBUTION_CONVERTAMOUNTSINXML(@GLDISTRIBUTION, @BASECURRENCYID, @ORGANIZATIONEXCHANGERATEID, @TRANSACTIONCURRENCYID, @BASEEXCHANGERATEID, @ORGANIZATIONEXCHANGERATEID, @BASEEXCHANGERATEID);
                        exec dbo.USP_REVENUE_GETWRITEOFFGLDISTRIBUTION_CUSTOMUPDATEFROMXML_2 @ID, @GLDISTRIBUTION, @POSTDATE, @CHANGEAGENTID;
                    end try

                    begin catch
                        exec dbo.USP_RAISE_ERROR;
                        return 1;
                    end catch

                    return 0;