Stored Procedures [dbo].[spBATGenerateFirstCommGLNumber_XLedger]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)
@ICPriKeyint4
@BranchPriKeyint4
@DivisionIDint4
@OrdPriKeyint4
@CommissionSysuserint4
@BATProcessPriKeyint4
@Sourcevarchar(50)50
@SourcePriKeyint4
Permissions
TypeActionOwning Principal
GrantExecuteMssExec
SQL Script
/**
* Don't use this stored proc directly but instead use spBATGenerateFirstCommGLNumber_Synonym.
* spBATGenerateFirstCommGLNumber_Synonym will either point to this stored proc or to
* spBATGenerateFirstCommGLNumber_Legacy if that is active.
*
*
* Description: If parameters are valid, new GLNumbers is generated and error code 0 is returned.
*
* Error Codes:
* ============
* 1605 if no XLedgerItemCodeMap exists for this ItemCode for account type 'Commissions'
*/


CREATE PROCEDURE [dbo].[spBATGenerateFirstCommGLNumber_XLedger]
    @ICPriKey int,
    @BranchPriKey int,
    @DivisionID int,
    @OrdPriKey int,
    @CommissionSysuser int, -- SysuserPriKey for person getting a commission.
    @BATProcessPriKey int,
    @Source varchar(50),
    @SourcePriKey int
as

set nocount on

declare @batErrorCode int = 0
declare @firstGLNumberAccountingAccountId int
declare @itemCode int

-- create a blank Session ID.  Claims will be the only one using an actual Session ID.
declare @theBlankSessionID varchar( 50 )
set @theBlankSessionID = ''

select
    @itemCode = BATProcess.ItemCode,
    @firstGLNumberAccountingAccountId = XLedgerItemCodeMap.AccountingAccountFid
from BATProcess
left outer join XLedgerItemCodeMapAccountType on XLedgerItemCodeMapAccountType.MappingType = 'Expense'
left outer join XLedgerItemCodeMap on XLedgerItemCodeMap.ItemCodeFid = BATProcess.ICPriKey and
    XLedgerItemCodeMap.XLedgerItemCodeMapAccountTypeFid = XLedgerItemCodeMapAccountType.XLedgerItemCodeMapAccountTypeId
where BATProcess.BATProcessPriKey = @BATProcessPriKey

if( @firstGLNumberAccountingAccountId is null )
begin
    exec @batErrorCode = spBATRecordError
        @theBlankSessionID,
        @Source,
        @SourcePriKey,
        1605,
        'Order',
        @OrdPriKey,
        @itemCode
end

-- Only if no errors then we can update the record in BATProcess
if( @batErrorCode = 0 )
begin
    update BATProcess set
        GLNumber = AccountingAccount.Code,
        NominalAccountingAccountFid = @firstGLNumberAccountingAccountId
    from BATProcess
    inner join AccountingAccount on AccountingAccount.AccountingAccountId = @firstGLNumberAccountingAccountId
    where BATProcessPriKey = @BATProcessPriKey
end
GO
GRANT EXECUTE ON  [dbo].[spBATGenerateFirstCommGLNumber_XLedger] TO [MssExec]
GO
Uses