USP_WRITEOFF_DELETE

Executes the "Pledge Write-off: Delete" record operation.

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier IN Input parameter indicating the ID of the record being deleted.
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the delete.

Definition

Copy


                    CREATE procedure dbo.USP_WRITEOFF_DELETE
                    (
                        @ID uniqueidentifier,
                        @CHANGEAGENTID uniqueidentifier
                    )
                    as

                    set nocount on;

                    --Cache CONTEXT INFO

                    declare @contextCache varbinary(128);
                    set @contextCache = CONTEXT_INFO();

                    --declare @PLEDGEID uniqueidentifier = (select REVENUEID from dbo.WRITEOFF where ID = @ID)

                    declare @PLEDGEID uniqueidentifier;
                    declare @POSTSTATUSCODE tinyint;

                    select @PLEDGEID = parentid, @POSTSTATUSCODE = POSTSTATUSCODE from dbo.FINANCIALTRANSACTION where ID = @ID


                    if not @CHANGEAGENTID is null
                        set CONTEXT_INFO @CHANGEAGENTID;


                    --if (select POSTSTATUSCODE from dbo.WRITEOFF where ID = @ID) = 0

                    if @POSTSTATUSCODE = 2
                    begin
                        raiserror('This write-off has been posted and cannot be deleted.', 13, 1);
                        return 0;
                    end

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

                    -- Delete unposted write-off distributions.

                    /*
                    delete from dbo.WRITEOFFGLDISTRIBUTION where ID in (
                        select WRITEOFFGLDISTRIBUTION.ID
                        from WRITEOFFGLDISTRIBUTION
                        inner join dbo.GLTRANSACTION on WRITEOFFGLDISTRIBUTION.GLTRANSACTIONID = GLTRANSACTION.ID
                        where WRITEOFFGLDISTRIBUTION.WRITEOFFID = @ID and GLTRANSACTION.POSTSTATUSCODE <> 0
                    );
                    */

                    delete T1 from dbo.JOURNALENTRY T1
                    join dbo.FINANCIALTRANSACTIONLINEITEM T2 on T1.FINANCIALTRANSACTIONLINEITEMID = T2.ID
                    where T2.FINANCIALTRANSACTIONID  = @ID
                    and T2.POSTSTATUSCODE != 2;


                    --Restore CONTEXT_INFO

                    if not @contextCache is null
                        set CONTEXT_INFO @contextCache;

                    exec dbo.USP_WRITEOFF_DELETEBYID_WITHCHANGEAGENTID @ID, @CHANGEAGENTID;

                    -- Redefault installment receipt amounts

                    exec dbo.USP_INSTALLMENTS_DEFAULTRECEIPTAMOUNTS @PLEDGEID, @CHANGEAGENTID

                    return 0;