USP_DATAFORMTEMPLATE_EDIT_PLEDGE

The save procedure used by the edit dataform template "Pledge Edit Form".

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier IN The input ID parameter indicating the ID of the record being edited.
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the procedure.
@DATE datetime IN Date
@AMOUNT money IN Amount
@POSTSTATUSCODE tinyint IN Post status
@POSTDATE datetime IN Post date
@SENDPLEDGEREMINDER bit IN Send pledge reminders
@SPLITS xml IN Designations
@FREQUENCYCODE tinyint IN Frequency
@NUMBEROFINSTALLMENTS int IN No. installments
@STARTDATE datetime IN Starting on
@INSTALLMENTS xml IN
@FINDERNUMBER bigint IN Finder number
@SOURCECODE nvarchar(50) IN Source code
@APPEALID uniqueidentifier IN Appeal
@BENEFITS xml IN Benefits
@BENEFITSWAIVED bit IN Benefits waived
@GIVENANONYMOUSLY bit IN Pledge is anonymous
@MAILINGID uniqueidentifier IN Mailing
@CHANNELCODEID uniqueidentifier IN Channel
@DONOTACKNOWLEDGE bit IN Do not acknowledge
@PLEDGESUBTYPEID uniqueidentifier IN Subtype

Definition

Copy


                    CREATE procedure dbo.USP_DATAFORMTEMPLATE_EDIT_PLEDGE
                    (
                        @ID uniqueidentifier,
                        @CHANGEAGENTID uniqueidentifier = null,
                        @DATE datetime,
                        @AMOUNT money,
                        @POSTSTATUSCODE tinyint,
                        @POSTDATE datetime,
                        @SENDPLEDGEREMINDER bit,
                        @SPLITS xml,
                        @FREQUENCYCODE tinyint,
                        @NUMBEROFINSTALLMENTS int,
                        @STARTDATE datetime,
                        @INSTALLMENTS xml,
                        @FINDERNUMBER bigint,
                        @SOURCECODE nvarchar(50),
                        @APPEALID uniqueidentifier,
                        @BENEFITS xml,
                        @BENEFITSWAIVED bit,
                        @GIVENANONYMOUSLY bit,
                        @MAILINGID uniqueidentifier,
                        @CHANNELCODEID uniqueidentifier,
                        @DONOTACKNOWLEDGE bit,
                        @PLEDGESUBTYPEID uniqueidentifier
                    )
                    as
                    begin
                        set nocount on;

                        declare @CURRENTDATE datetime;
                        declare @SUM money;
                        declare @COUNT int;

                        begin try
                            if @FINDERNUMBER is null
                                set @FINDERNUMBER = 0;
                            else if @FINDERNUMBER <> 0
                                begin
                                    if dbo.[UFN_MKTFINDERNUMBER_VALIDATE_MOD10](@FINDERNUMBER) = 0
                                        raiserror('BBERR_FINDERNUMBER_FAILEDCHECKDIGIT', 13, 1);

                                    if dbo.[UFN_MKTSEGMENTATIONFINDERNUMBER_GETSEGMENTATION](@FINDERNUMBER) is null
                                        raiserror('BBERR_FINDERNUMBER_INVALID', 13, 1);
                                end

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

                            set @CURRENTDATE = GetDate();

                            exec dbo.USP_REVENUESPLIT_VALIDATESPLITS @SPLITS, @AMOUNT, 1;

                            select 
                                @SUM = sum(AMOUNT),
                                @COUNT = count(AMOUNT)
                            from 
                                dbo.UFN_INSTALLMENT_GETINSTALLMENTS_FROMITEMLISTXML(@INSTALLMENTS);

                            if @COUNT = 0 
                                raiserror('Please enter at least one installment.',13,1);

                            if @SUM <> @AMOUNT
                                raiserror('The sum of the installment amounts must equal the pledge amount.',13,1);

                            /* You can only edit unposted pledges here */
                            if (select count(REVENUE.ID) from dbo.REVENUE
                                inner join dbo.REVENUEPOSTED on REVENUEPOSTED.ID = REVENUE.ID
                                where REVENUE.ID = @ID) > 0
                                raiserror('You cannot edit a posted pledge.', 13, 1)

                            if dbo.UFN_INSTALLMENT_DATESOVERLAP(@INSTALLMENTS) = 1
                                raiserror('Installment dates are out of sequence.',13,1);

                            if (select count(INSTALLMENT.ID) from dbo.INSTALLMENT 
                                inner join dbo.UFN_INSTALLMENT_GETINSTALLMENTS_FROMITEMLISTXML(@INSTALLMENTS) XMLINST
                                on INSTALLMENT.ID = XMLINST.ID
                                where XMLINST.AMOUNT < (INSTALLMENT.AMOUNT - XMLINST.BALANCE)) > 0 
                                raiserror('The installment amount cannot be less than the sum of the amount paid and written off for that installment.', 13, 1)

                            declare @TRIBUTEAMOUNT money;
                            select @TRIBUTEAMOUNT = sum(AMOUNT) from dbo.REVENUETRIBUTE where REVENUEID = @ID;

                            -- do not allow the gift amount to be adjusted less than the applied tribute amount

                            if (@TRIBUTEAMOUNT is not null) and (@AMOUNT < @TRIBUTEAMOUNT)
                            begin
                                raiserror('The pledge amount cannot be less than the sum of the tribute amounts applied to this pledge.', 13, 1)
                            end


                            declare @DONOTPOST bit;
                            set @DONOTPOST = case @POSTSTATUSCODE when 2 then 1 else 0 end;

                            -- if the designations or amount has changed, clear any user-defined gl distributions for this revenue record

                            if exists (select ID from dbo.REVENUEGLDISTRIBUTION where REVENUEID = @ID
                                or exists (select GL.ID from dbo.WRITEOFFGLDISTRIBUTION GL inner join dbo.WRITEOFF on GL.WRITEOFFID = WRITEOFF.ID where WRITEOFF.REVENUEID = @ID
                            begin
                                declare @CLEARGLDISTRIBUTION bit;
                                declare @CLEARWRITEOFFGLDISTRIBUTION bit;
                                set @CLEARGLDISTRIBUTION = 0;
                                set @CLEARWRITEOFFGLDISTRIBUTION = 0;

                                -- check to see if designations have changed

                                if dbo.UFN_CHECKDETAIL_SPLITSCHANGED(@ID, @SPLITS) = 1
                                begin
                                    set @CLEARGLDISTRIBUTION = 1;
                                    set @CLEARWRITEOFFGLDISTRIBUTION = 1;
                                end    

                                -- check to see if amount has changed

                                if @CLEARGLDISTRIBUTION = 0
                                    if (select AMOUNT from dbo.REVENUE where ID = @ID) <> @AMOUNT
                                        set @CLEARGLDISTRIBUTION = 1;

                                -- clear the user-defined gl distributions

                                if @CLEARGLDISTRIBUTION = 1
                                begin
                                    --Clear GL

                                    --Cache CONTEXT INFO

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

                                    if not @CHANGEAGENTID is null
                                        set CONTEXT_INFO @CHANGEAGENTID;

                                    delete from dbo.REVENUEGLDISTRIBUTION where REVENUEID = @ID;    

                                    if @CLEARWRITEOFFGLDISTRIBUTION = 1 
                                        delete from dbo.WRITEOFFGLDISTRIBUTION where WRITEOFFID in (select WO.ID from dbo.WRITEOFF WO where WO.REVENUEID = @ID);

                                    --Restore CONTEXT_INFO

                                    if not @contextCache is null
                                        set CONTEXT_INFO @contextCache;
                                end
                            end


                            update
                                dbo.REVENUE
                            set
                                DATE = @DATE,
                                DONOTPOST = @DONOTPOST,
                                POSTDATE = @POSTDATE,
                                AMOUNT = @AMOUNT,
                                FINDERNUMBER = @FINDERNUMBER,
                                SOURCECODE = @SOURCECODE,
                                APPEALID = @APPEALID,
                                BENEFITSWAIVED = @BENEFITSWAIVED,
                                GIVENANONYMOUSLY = @GIVENANONYMOUSLY,
                                MAILINGID = @MAILINGID,
                                CHANNELCODEID = @CHANNELCODEID,
                                DONOTACKNOWLEDGE = @DONOTACKNOWLEDGE,
                                CHANGEDBYID = @CHANGEAGENTID,
                                DATECHANGED = @CURRENTDATE
                            where
                                ID = @ID;

                            if exists (select ID from dbo.REVENUEPOSTED where ID = @ID) begin
                                if @POSTSTATUSCODE <> 0
                                    delete dbo.REVENUEPOSTED where ID = @ID;
                            end
                            else begin
                                if @POSTSTATUSCODE = 0
                                    insert into dbo.REVENUEPOSTED(ID,ADDEDBYID,CHANGEDBYID,DATEADDED,DATECHANGED)
                                        values(@ID,@CHANGEAGENTID,@CHANGEAGENTID,@CURRENTDATE,@CURRENTDATE);
                            end

                            update
                                dbo.REVENUESCHEDULE
                            set
                                FREQUENCYCODE = @FREQUENCYCODE,
                                NUMBEROFINSTALLMENTS = @NUMBEROFINSTALLMENTS,
                                STARTDATE = @STARTDATE,
                                PLEDGESUBTYPEID = @PLEDGESUBTYPEID,
                                SENDPLEDGEREMINDER = @SENDPLEDGEREMINDER,
                                CHANGEDBYID = @CHANGEAGENTID,
                                DATECHANGED = @CURRENTDATE
                            from 
                                dbo.REVENUESCHEDULE 
                            where
                                REVENUESCHEDULE.ID = @ID;

                            exec dbo.USP_REVENUE_GETSPLITS_UPDATEFROMXML @ID, @SPLITS, @CHANGEAGENTID, @CURRENTDATE;        
                            exec dbo.USP_INSTALLMENT_GETINSTALLMENTS_UPDATEFROMXML @ID, @INSTALLMENTS, @CHANGEAGENTID, @CURRENTDATE;        
                            exec dbo.USP_PLEDGE_FIXDEPENDENTSPLITS @ID, @CHANGEAGENTID, @CURRENTDATE
                            exec dbo.USP_REVENUE_GETBENEFITS_UPDATEFROMXML @ID, @BENEFITS, @CHANGEAGENTID, @CURRENTDATE;
                            exec dbo.USP_REVENUE_UPDATESOLICITORS @ID, @CHANGEAGENTID, @CURRENTDATE;                            
                        end try

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

                        return 0;
                    end