Stored Procedures [dbo].[BATBuildARAPGLNumber_Legacy]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)Direction
@inSessionIDvarchar(50)50
@inOrderIDint4
@inTransactionTypevarchar(30)30
@inSourcevarchar(50)50
@inSourceIDint4
@inObjectvarchar(15)15
@inObjectIDint4
@inBranchIDint4
@inDivisionIDint4
@inAuthorityIDint4
@outGLNumbervarchar(66)66Out
@outAccountingAccountIdint4Out
@outErrorCodeint4Out
Permissions
TypeActionOwning Principal
GrantExecuteMssExec
SQL Script
/**
*    
*    This procedure concatenates the AR/AP Natural, Sub, Company, Branch, and Division
*    segments into a general ledger number.  This stored procedure is used in the
* stored procs to build the GL number.  This is somewhat duplicating the functionality found in
* the other part of BAT where spBATGenerateSecondCommGLNumber_Legacy is called.  So, if you need
* to modify this sp, please be sure to check that one as well.  This sp is much more
* set-based, whereas the other is more procedural.  The other should be eliminated and
* replaced by this one at some point, but not until the rest of BAT is refactored to be
* less procedural.
*    
*    Error Codes:
*    @ERROR_CODE_NO_MANUAL_GL
*    @ERROR_CODE_SEGMENT_LENGTH
*    
*    Parameters:
*    @param @inSessionID The unique id of the current session.
*    @param @inOrderID The primary key of the order.
*    @param @inTransactionType The description of the type of transaction (i.e. AR, AP)
*    @param @inSource The source used to log errors.
*    @param @inSourceID The primary key for the source.
*    @param @inObject The object(more generic than source i.e. Orders) used to log errors.
*    @param @inObjectID The primary key for the object.
*    @param @inBranchID The primary key for the branch.
*    @param @inDivisionID The primary key for the division.
*    @param @inAuthorityID The primary key for the authority type.
*    @param @outGLNumber The general ledger number.
* @param @outAccountingAccountId Always returns null (this only returns a value from the XLedger version of this stored proc, BATBuildARAPGLNumber_XLedger).
*    @param @outErrorCode Error code indicating what happened during the call.
*/


create procedure [dbo].[BATBuildARAPGLNumber_Legacy]
    @inSessionID varchar(50),
    @inOrderID int,
    @inTransactionType varchar(30),
    @inSource varchar(50),
    @inSourceID int,
    @inObject varchar(15),
    @inObjectID int,
    @inBranchID int,
    @inDivisionID int,
    @inAuthorityID int,
    @outGLNumber varchar(66) output,
    @outAccountingAccountId int output,
    @outErrorCode     int output
as
set nocount on

-- Set the output parameters.
set @outGLNumber = ' '
set @outErrorCode = 0
set @outAccountingAccountId = null

-- Error codes and constants.
declare @ERROR_CODE_NO_MANUAL_GL int
set @ERROR_CODE_NO_MANUAL_GL    = 1220

declare @theGLARAPID int

-- Get the primary key for the 'Natural' and 'Sub' AR/AP account segments
-- that we use to build the general ledger number.
exec BATGetGLARAP
    @inSessionID            = @inSessionID,
    @inOrderID                = @inOrderID,
    @inTransactionType        = @inTransactionType,
    @inSource                = @inSource,
    @inSourceID                = @inSourceID,
    @inObject                = @inObject,
    @inObjectID                = @inObjectID,
    @inBranchID                = @inBranchID,
    @inAuthorityID            = @inAuthorityID,
    @outErrorCode            = @outErrorCode output,
    @outGLARAPID            = @theGLARAPID output

-- If there is a primary key for the account segments, check if
-- the default general ledger number is used.
if ( 0 != @theGLARAPID )
begin

    -- The variable will be set to a valid gl number if a manual setting is found, blank or error if an invalid or null
    -- entry is in the gl number field and manual is set, or null if there is no override and we need to build it
    set @outGLNumber = ( select isnull( OverrideGLNumber, 'Error' ) from GLARAP where GLAPriKey = @theGLARAPID and ManualGL = 1 )
        
    -- If the default general ledger number is not valid, log the error.
    if ( ' ' = @outGLNumber  or 'Error' = @outGLNumber )
    begin
        exec @outErrorCode = spBATRecordError @inSessionID, @inSource, @inSourceID, @ERROR_CODE_NO_MANUAL_GL, @inObject, @inObjectID
    end
    
    -- Otherwise, check the general ledger segments.
    else if ( @outGLNumber is null )
    begin
        declare @theMainAccount varchar(16)
        declare @theSubAccount    varchar(16)

        -- Get the main and sub accounts for the given GLARAP record
        select
            @theMainAccount = GLARAP.GLAccount,
            @theSubAccount    = GLARAP.GLSubAccount
        from GLARAP
        where ( GLARAP.GLAPriKey = @theGLARAPID )
        
        exec BATCheckSegmentLengths
            @inSessionID = @inSessionID,
            @inSource                = @inSource,
            @inSourceID                = @inSourceID,
            @inObject                = @inObject,
            @inObjectID                = @inObjectID,
            @inMainAccount            = @theMainAccount,
            @inSubAccount            = @theSubAccount,
            @inBranchID                = @inBranchID,
            @inDivisionID            = @inDivisionID,
            @outErrorCode            = @outErrorCode output
        
        -- If the segments are valid, build the general ledger number.
        if ( 0 = @outErrorCode )
        begin
            exec BATBuildGLNumber
                @inMainAccount = @theMainAccount,
                @inSubAccount = @theSubAccount,
                @inBranchID = @inBranchID,
                @inDivisionID = @inDivisionID,
                @outGlNumber = @outGLNumber output,
                @outErrorCode = @outErrorCode output

            if( @outErrorCode != 0 )
            begin
                exec @outErrorCode = spBATRecordError @inSessionID, @inSource, @inSourceID, @outErrorCode, @inObject, @inObjectID
            end
        end
    end
end
GO
GRANT EXECUTE ON  [dbo].[BATBuildARAPGLNumber_Legacy] TO [MssExec]
GO
Uses
Used By