USP_REVENUEBATCH_VALIDATESINGLEAPPLICATION

Validates a single application for a batch row.

Parameters

Parameter Parameter Type Mode Description
@AMOUNT money IN
@SINGLEAPPLICATIONID uniqueidentifier IN
@APPLICATIONTYPECODE tinyint IN
@APPLICATIONAMOUNT money IN
@PAYMENTMETHODCODE tinyint IN

Definition

Copy


            CREATE procedure dbo.USP_REVENUEBATCH_VALIDATESINGLEAPPLICATION
            (
                @AMOUNT money,
                @SINGLEAPPLICATIONID uniqueidentifier,
                @APPLICATIONTYPECODE tinyint,
                @APPLICATIONAMOUNT money,                -- Should be in the application's currency

                @PAYMENTMETHODCODE tinyint
            )
            as 
            set nocount on;

            declare @SPLITS xml;
            declare @APPLICATIONCATEGORYCODEID uniqueidentifier;
            declare @APPLICATIONCODE tinyint;

            --Validate GL mappings for events

            if @APPLICATIONTYPECODE = 7 
                exec dbo.USP_EVENT_VALIDATEGLMAPPINGS @SINGLEAPPLICATIONID, 0, @PAYMENTMETHODCODE, 1, 1;
            else
            begin
                --Validate GL mappings for designations

                select @APPLICATIONCODE = case @APPLICATIONTYPECODE
                    when 5 then 2 -- pledge payment

                    when 4 then 3 -- recurring gift payment

                    when 8 then 7 -- matching gift payment

                    when 6 then 6 -- planned gift payment

                    when 10 then 13 -- Donor challenge payment

                    when 1 then 3 -- sponsorship payment

                    when 2 then 5 -- membership payment

                    when 3 then 10 -- order payment

                    when 9 then 8 -- grant award payment

                    else 0 end;

                --This assumes that each split for the application has the same revenue category

                select top 1 
                    @APPLICATIONCATEGORYCODEID = REVENUECATEGORY.GLREVENUECATEGORYMAPPINGID 
                from 
                    dbo.REVENUESPLIT
                left join 
                    dbo.REVENUECATEGORY on REVENUESPLIT.ID = REVENUECATEGORY.ID
                where 
                    REVENUESPLIT.REVENUEID = @SINGLEAPPLICATIONID;

                set @SPLITS = (
                    select
                        REVENUESPLIT.DESIGNATIONID,
                        @APPLICATIONCODE as APPLICATIONCODE,
                        0 as TYPECODE
                    from dbo.REVENUESPLIT
                    where REVENUESPLIT.REVENUEID = @SINGLEAPPLICATIONID
                    for xml raw('ITEM'),type,elements,root('SPLITS'),BINARY BASE64
                )        

                if @SPLITS is not null
                    exec dbo.USP_REVENUESPLITS_VALIDATEGLMAPPINGS @SPLITS, 0, @PAYMENTMETHODCODE, @APPLICATIONCATEGORYCODEID;
            end

            if (@APPLICATIONTYPECODE = 7) and (exists (select top(1) ID from dbo.BATCHREVENUEREGISTRANT where ID = @SINGLEAPPLICATIONID))
            begin                
                if exists(
                    select top(1)
                        REGISTRANT.ID
                    from
                        dbo.REGISTRANT
                    inner join
                        dbo.BATCHREVENUEREGISTRANT on (BATCHREVENUEREGISTRANT.CONSTITUENTID = REGISTRANT.CONSTITUENTID) and (BATCHREVENUEREGISTRANT.EVENTID = REGISTRANT.EVENTID)
                    where
                        BATCHREVENUEREGISTRANT.ID = @SINGLEAPPLICATIONID and REGISTRANT.ID <> @SINGLEAPPLICATIONID
                ) or exists (
                    select top(1)
                        REGISTRANT.ID
                    from
                        dbo.REGISTRANT
                    inner join
                        dbo.BATCHREVENUEREGISTRANT on (BATCHREVENUEREGISTRANT.CONSTITUENTID = REGISTRANT.CONSTITUENTID) and (BATCHREVENUEREGISTRANT.EVENTID = REGISTRANT.EVENTID)
                    where
                        BATCHREVENUEREGISTRANT.GUESTOFREGISTRANTID = @SINGLEAPPLICATIONID and REGISTRANT.ID <> @SINGLEAPPLICATIONID
                )
                begin
                    raiserror('A registrant or guest of a registrant has already been registered for the event', 13, 4);
                    return 4;
                end

                -- if an event registration was created in batch and two payment rows applied toward it, the second row wouldn't be

                -- caught by normal validation.  We chase that down here and raise an error similar to the one above:

                declare @REGISTRANTCONSTITUENTID uniqueidentifier;
                select @REGISTRANTCONSTITUENTID = BATCHREVENUEREGISTRANT.CONSTITUENTID from dbo.BATCHREVENUEREGISTRANT where BATCHREVENUEREGISTRANT.ID = @SINGLEAPPLICATIONID;

                declare @REGISTRANTID uniqueidentifier;
                select
                    @REGISTRANTID = REGISTRANT.ID
                from
                    dbo.BATCHREVENUEREGISTRANT
                    inner join dbo.REGISTRANT 
                        on BATCHREVENUEREGISTRANT.EVENTID = REGISTRANT.EVENTID
                        and @REGISTRANTCONSTITUENTID = REGISTRANT.CONSTITUENTID
                where
                    BATCHREVENUEREGISTRANT.ID = @SINGLEAPPLICATIONID;

                if (@REGISTRANTID is not null)
                begin
                    if (@APPLICATIONAMOUNT > dbo.UFN_EVENTREGISTRANT_GETBALANCE(@REGISTRANTID))
                    begin
                        raiserror('You cannot apply an amount greater than the balance for a commitment.', 13, 3);
                        return 3;
                    end
                end
            end

            --Validate in case of Pledge, Matching Gift claim, Donor Challenge, Planned Gift, Grant Award payment - if we are trying to pay an amount greater than the remaining balance. 

            if @APPLICATIONTYPECODE in (5, 6, 8, 9, 10)
            begin
                if (@APPLICATIONAMOUNT > dbo.UFN_PLEDGE_GETBALANCE(@SINGLEAPPLICATIONID))
                begin
                    raiserror('You cannot apply an amount greater than the balance for a commitment.', 13, 3);
                    return 3;
                end
            end

            --WI378664 - Check that we do not add revenue to a RG that has inactive designations. This includes Sponsorships

            if @APPLICATIONTYPECODE in (1, 4)
            begin
                if exists (
                        select 1
                        from dbo.FINANCIALTRANSACTIONLINEITEM REVENUESPLIT
                        inner join dbo.REVENUESPLIT_EXT on REVENUESPLIT_EXT.ID = REVENUESPLIT.ID
                        inner join dbo.DESIGNATION on REVENUESPLIT_EXT.DESIGNATIONID = DESIGNATION.ID
                        where REVENUESPLIT.FINANCIALTRANSACTIONID = @SINGLEAPPLICATIONID and REVENUESPLIT_EXT.TYPECODE in (0, 4, 9, 17) and REVENUESPLIT.DELETEDON is null and REVENUESPLIT.TYPECODE != 1 and DESIGNATION.ISACTIVE = 0
                        )
                begin
                    raiserror('BBERR_CANNOTPAYINACTIVEDESIGNATIONS : Revenue cannot be added to inactive designations.', 13, 2);
                    return 2;
                end
            end

            if @AMOUNT > 0
            begin
                if exists(
                    select 1
                    from
                        dbo.FINANCIALTRANSACTION
                        inner join dbo.INSTALLMENTSPLIT on INSTALLMENTSPLIT.PLEDGEID = FINANCIALTRANSACTION.ID
                        inner join dbo.DESIGNATION on INSTALLMENTSPLIT.DESIGNATIONID = DESIGNATION.ID
                    where
                        FINANCIALTRANSACTION.ID = @SINGLEAPPLICATIONID
                        and dbo.UFN_INSTALLMENT_GETINSTALLMENTSPLITBALANCE(INSTALLMENTSPLIT.ID) > 0
                        and DESIGNATION.ISACTIVE = 0
                begin
                    raiserror('BBERR_CANNOTPAYINACTIVEDESIGNATIONS : Revenue cannot be added to inactive designations.', 13, 2);
                    return 2;
                end         
            end

            return 0;