USP_MATCHINGGIFTPLEDGE_DELETE

Executes the "Matching Gift Claim: 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_MATCHINGGIFTPLEDGE_DELETE
                    (
                        @ID uniqueidentifier,
                        @CHANGEAGENTID uniqueidentifier
                    )
                    as
                        --Note: This procedure accepts a REVENUE ID as the @ID parameter

                        set NOCOUNT on;

                        if (select ID from dbo.REVENUEPOSTED where ID = @ID) is not null
                        begin
                            raiserror('This matching gift claim has been posted and cannot be deleted.', 13, 1);
                            return 0;
                        end

                        if (select count(INSTALLMENTPAYMENT.INSTALLMENTID) from dbo.INSTALLMENTPAYMENT where PLEDGEID = @ID) > 0
                        begin
                            raiserror('This matching gift claim has payments made against it. The payments must be deleted before this matching gift claim can be deleted.', 13, 1);
                            return 0;
                        end

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

                        --Cache CONTEXT INFO

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

                        if not @CHANGEAGENTID is null
                            set CONTEXT_INFO @CHANGEAGENTID;

                        delete from dbo.INSTALLMENT where REVENUEID = @ID;

                        --Mark the matching gift as deleted if all payments towards the matching gift have also been deleted,

                        --otherwise removing the matching gift from the FT table will fail due to referential integrity checks.

                        --Because of deleted matching gift payments have id of MG in SOURCELINITEMID  

                        if 
                          exists
                          (
                              select 1 
                              from dbo.FINANCIALTRANSACTIONLINEITEM as FTLI_MG
                              inner join FINANCIALTRANSACTIONLINEITEM FTLI_PAYMENT on FTLI_PAYMENT.SOURCELINEITEMID = FTLI_MG.ID
                              where
                                  FTLI_MG.FINANCIALTRANSACTIONID = @ID
                                  and FTLI_PAYMENT.TYPECODE = 0
                                  and FTLI_PAYMENT.DELETEDON is not null
                                  and not exists( select 1 from FINANCIALTRANSACTIONLINEITEM FTLI
                                            where FTLI.SOURCELINEITEMID = FTLI_MG.ID
                                            and FTLI.TYPECODE = 0
                                            and FTLI.DELETEDON is null
                                              )
                          )
                        begin
                            declare @CURRENTDATE datetime;
                            set @CURRENTDATE = getdate();
                            update dbo.FINANCIALTRANSACTIONLINEITEM
                               set 
                                 DELETEDON = @CURRENTDATE,
                                 CHANGEDBYID = @CHANGEAGENTID,
                                 DATECHANGED = @CURRENTDATE
                                 where FINANCIALTRANSACTIONID = @ID;

                            update dbo.FINANCIALTRANSACTION
                               set 
                                 DELETEDON = @CURRENTDATE,
                                 CHANGEDBYID = @CHANGEAGENTID,
                                 DATECHANGED = @CURRENTDATE
                                 where ID = @ID;
                        end
                        else
                        begin
                            delete from dbo.REVENUE where ID = @ID;
                        end
                        --Restore CONTEXT INFO 

                        if not @contextCache is null
                            set CONTEXT_INFO @contextCache

                        return 0;