USP_CONSTITUENTMEMBERSHIP_DELETE

Executes the "Constituent Membership: 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_CONSTITUENTMEMBERSHIP_DELETE
                (
                    @ID uniqueidentifier,
                    @CHANGEAGENTID uniqueidentifier
                )
                as 

                    set nocount on;

                    begin
                        begin try
                            --don't delete if attached to revenue

                            if exists (select 
                                                    1 
                                                from dbo.MEMBERSHIPTRANSACTION 
                                                    inner join dbo.FINANCIALTRANSACTIONLINEITEM on MEMBERSHIPTRANSACTION.REVENUESPLITID = FINANCIALTRANSACTIONLINEITEM.ID 
                                                where MEMBERSHIPTRANSACTION.MEMBERSHIPID = @ID 
                                                    and FINANCIALTRANSACTIONLINEITEM.DELETEDON is null)
                            begin
                                raiserror('ERR_MEMBERSHIPDELETE_HASREVENUE', 13, 1);
                            end

                            --don't delete if there is a pending transaction in a batch

                            if exists 
                            (
                                select 1 
                                from 
                                    dbo.BATCHMEMBERSHIPDUES 
                                    inner join dbo.BATCH on BATCHMEMBERSHIPDUES.BATCHID = BATCH.ID 
                                where 
                                    EXISTINGMEMBERSHIPID = @ID 
                                    and BATCH.STATUSCODE in (0,3)
                            )
                            begin
                                raiserror('ERR_MEMBERSHIPDELETE_HASPENDINGBATCHTRANSACTION', 13, 1);
                            end

                            -- Delete links to any addons before deleting the membership

                            delete from dbo.MEMBERSHIPADDON where MEMBERSHIPID = @ID

                            exec dbo.USP_MEMBERSHIP_DELETEBYID_WITHCHANGEAGENTID @ID, @CHANGEAGENTID
                            return 0;
                        end try
                        begin catch
                            exec dbo.USP_RAISE_ERROR
                            return 1;
                        end catch                        
                    end