USP_PLANNEDGIFT_VALIDATESPLITS

Validates splits.

Parameters

Parameter Parameter Type Mode Description
@SPLITS xml IN
@PLANNEDGIFTAMOUNT money IN

Definition

Copy


            CREATE procedure dbo.USP_PLANNEDGIFT_VALIDATESPLITS

            @SPLITS xml,  
            @PLANNEDGIFTAMOUNT money 

            as  

            set nocount on

            if @SPLITS is null 
            begin 
                raiserror('Please enter at least one designation.', 13, 1); 
                return 1
            end 

            declare @SUM money; 
            declare @NONNULL_DESIGSPLITCOUNT int
            declare @DISTINCTDESIGSPLITCOUNT int
            declare @TOTALSPLITCOUNT int;

            declare @SPLITSTABLE table 
            ( 
                AMOUNT money, 
                DESIGNATIONID uniqueidentifier 
            ); 

            insert into @SPLITSTABLE(AMOUNT, DESIGNATIONID) 
                select AMOUNT, DESIGNATIONID
                from dbo.UFN_PLANNEDGIFT_DESIGNATION_2_FROMITEMLISTXML(@SPLITS); 

            select 
                @SUM = sum(AMOUNT),
                @TOTALSPLITCOUNT=count(*),
                @DISTINCTDESIGSPLITCOUNT= count(distinct DESIGNATIONID),
                @NONNULL_DESIGSPLITCOUNT=count(DESIGNATIONID)
            from @SPLITSTABLE

            if @SUM <> @PLANNEDGIFTAMOUNT 
            begin 
                raiserror('The total amount must equal the sum of the designations.',13,2); 
                return 2
            end 

            if @TOTALSPLITCOUNT = 0 
            begin 
                raiserror('Please enter at least one designation.', 13, 3); 
                return 3
            end 

            if @NONNULL_DESIGSPLITCOUNT <> @TOTALSPLITCOUNT  
            begin 
                raiserror('Every distribution must have a designation.', 13, 4); 
                return 4
            end 

            if @TOTALSPLITCOUNT <> @DISTINCTDESIGSPLITCOUNT 
            begin 
                raiserror('Duplicate designations cannot be specified.', 13, 5); 
                return 5
            end 

            return 0