USP_SALESORDER_VERIFYZEROBALANCE

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier IN

Definition

Copy


create procedure dbo.USP_SALESORDER_VERIFYZEROBALANCE(@ID uniqueidentifier) as begin
    set transaction isolation level read committed;

    declare @ORDERBALANCE money
    select @ORDERBALANCE = BALANCE from dbo.UFN_SALESORDER_TOTALS(@ID);

    begin try
        if @ORDERBALANCE > 0 begin
            raiserror('BBERR_PAYMENTREQUIRED.', 13, 1);
        end
        else begin
            if @ORDERBALANCE < 0 begin
                if (-@ORDERBALANCE) > ( select coalesce(sum(SALESORDERPAYMENT.AMOUNTTENDERED),0)
                                            from dbo.SALESORDERPAYMENT
                                                inner join dbo.REVENUEPAYMENTMETHOD on REVENUEPAYMENTMETHOD.REVENUEID = SALESORDERPAYMENT.PAYMENTID
                                            where
                                                SALESORDERPAYMENT.SALESORDERID = @ID and
                                                REVENUEPAYMENTMETHOD.PAYMENTMETHODCODE = 0 )
                begin
                    raiserror('BBERR_NONCASHOVERPAY', 13, 1);
                end
            end
        end
    end try

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

    return 0;
end