SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[spBATGenerateFirstThirdPartyGLNumber_Legacy]
@ICPriKey int,
@BranchPriKey int,
@DivisionID int,
@OrdPriKey int,
@BATProcessPriKey int,
@Source varchar(50),
@SourcePriKey int
AS
SET NOCOUNT ON
DECLARE
@glNumber varchar(50),
@glcPriKey int,
@countGLCSegmets int,
@glcSourceName varchar(10),
@glsCounter int,
@padLength int,
@glAccount varchar(100),
@firstGLNumber varchar(66),
@batErrorCode int,
@icdPriKey int,
@theItemCode int,
@theCommodity varchar( 26 ),
@theAuthority varchar( 30 ),
@theMoveType varchar( 15 )
declare @theBlankSessionID varchar( 50 )
set @theBlankSessionID = ''
SET @batErrorCode = 0
SET @glsCounter = 1
SET @firstGLNumber = ' '
set @theItemCode = ( select rtrim( str( ItemCode.ItemCode ) ) from ItemCode where ItemCode.ICPRiKey = @ICPriKey )
IF( not exists (SELECT * FROM Branch WHERE BranchPriKey = @BranchPriKey) )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 704, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
ELSE
BEGIN
SET @glcPriKey = (SELECT GLCPriKey FROM Branch WHERE BranchPriKey = @BranchPriKey)
SET @countGLCSegmets = (SELECT Count(*) FROM GLDetail WHERE GLCPriKey = @glcPriKey)
END
IF( (SELECT Count(*) FROM GLSource WHERE SourceName = 'Natural') > 1 )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 692, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
IF( (SELECT Count(*) FROM GLSource WHERE SourceName = 'Sub') > 1 )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 693, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
IF( (SELECT Count(*) FROM GLSource WHERE SourceName = 'Company') > 1 )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 694, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
IF( (SELECT Count(*) FROM GLSource WHERE SourceName = 'Branch') > 1 )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 695, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
IF( (SELECT Count(*) FROM GLSource WHERE SourceName = 'Division') > 1 )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 696, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
IF( @glcPriKey is null )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 192, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
ELSE
begin
EXEC @icdPriKey = spBATSelectionProcess @ICPriKey, @glcPriKey, @OrdPriKey, 'ICDetail', @Source, @SourcePriKey, null
end
IF( @batErrorCode = 0 )
BEGIN
WHILE( @glsCounter <= @countGLCSegmets )
BEGIN
SET @padLength = (SELECT SegmentLength FROM GLDetail WHERE SegmentNumber = @glsCounter and GLCPriKey = @glcPriKey)
SET @glcSourceName = (
SELECT
gls.SourceName
FROM GLSource gls
Inner Join GLDetail gld on (gld.GLSPriKey = gls.GLSPriKey)
WHERE gld.GLCPriKey = @glcPriKey and
gld.SegmentNumber = @glsCounter
)
IF( @glcSourceName = 'Natural' )
BEGIN
SET @glNumber = (SELECT Ltrim(Rtrim(PayGLAccount)) FROM ICDetail WHERE ICDPriKey = @icdPriKey)
IF( @glNumber is null )
BEGIN
exec GetOrderAuthorityCommodityMoveType
@inOrderID = @OrdPriKey,
@outAuthority = @theAuthority output,
@outCommodity = @theCommodity output,
@outMoveType = @theMoveType output
exec @batErrorCode =
spBATRecordError
@theBlankSessionID,
@Source,
@SourcePriKey,
193,
'Order',
@OrdPriKey,
@theItemCode,
@theMoveType,
@theCommodity,
@theAuthority
SET @firstGLNumber = null
END
ELSE IF( Len(Rtrim(@glNumber)) > @padLength )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 194, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
ELSE
BEGIN
EXEC @glAccount = udfLeftPadding @padLength, @glNumber
IF( @glsCounter > 1 and @firstGLNumber <> ' ' )
begin
SET @firstGLNumber = Ltrim(Rtrim(@firstGLNumber)) + '-' + Ltrim(Rtrim(@glAccount))
end
ELSE
begin
SET @firstGLNumber = Ltrim(Rtrim(@firstGLNumber)) + Ltrim(Rtrim(@glAccount))
end
END
END
ELSE IF( @glcSourceName = 'Sub' )
BEGIN
SET @glNumber = (SELECT Ltrim(Rtrim(PayGLSubAccount)) FROM ICDetail WHERE ICDPriKey = @icdPriKey)
IF( @glNumber is null )
BEGIN
exec GetOrderAuthorityCommodityMoveType
@inOrderID = @OrdPriKey,
@outAuthority = @theAuthority output,
@outCommodity = @theCommodity output,
@outMoveType = @theMoveType output
EXEC @batErrorCode =
spBATRecordError
@theBlankSessionID,
@Source,
@SourcePriKey,
195,
'Order',
@OrdPriKey,
@theItemCode,
@theMoveType,
@theCommodity,
@theAuthority
SET @firstGLNumber = null
END
ELSE IF( Len(Rtrim(@glNumber)) > @padLength )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 196, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
ELSE
BEGIN
EXEC @glAccount = udfLeftPadding @padLength, @glNumber
IF( @glsCounter > 1 and @firstGLNumber <> ' ' )
begin
SET @firstGLNumber = Ltrim(Rtrim(@firstGLNumber)) + '-' + Ltrim(Rtrim(@glAccount))
end
ELSE
begin
SET @firstGLNumber = Ltrim(Rtrim(@firstGLNumber)) + Ltrim(Rtrim(@glAccount))
end
END
END
ELSE IF( @glcSourceName = 'Company' )
BEGIN
SET @glNumber = (SELECT CompanyGLCode FROM Branch WHERE BranchPriKey = @BranchPriKey)
IF( @glNumber is null )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 197, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
ELSE IF( Len(Rtrim(@glNumber)) > @padLength )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 198, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
ELSE
BEGIN
EXEC @glAccount = udfLeftPadding @padLength, @glNumber
IF( @glsCounter > 1 and @firstGLNumber <> ' ' )
begin
SET @firstGLNumber = Ltrim(Rtrim(@firstGLNumber)) + '-' + Ltrim(Rtrim(@glAccount))
end
ELSE
begin
SET @firstGLNumber = Ltrim(Rtrim(@firstGLNumber)) + Ltrim(Rtrim(@glAccount))
end
END
END
ELSE IF( @glcSourceName = 'Branch' )
BEGIN
SET @glNumber = (SELECT Ltrim(Rtrim(GLCode)) FROM Branch WHERE BranchPriKey = @BranchPriKey)
IF( @glNumber is null )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 199, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
ELSE IF( Len(Rtrim(@glNumber)) > @padLength )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 200, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
ELSE
BEGIN
EXEC @glAccount = udfLeftPadding @padLength, @glNumber
IF( @glsCounter > 1 and @firstGLNumber <> ' ' )
begin
SET @firstGLNumber = Ltrim(Rtrim(@firstGLNumber)) + '-' + Ltrim(Rtrim(@glAccount))
end
ELSE
begin
SET @firstGLNumber = Ltrim(Rtrim(@firstGLNumber)) + Ltrim(Rtrim(@glAccount))
end
END
END
ELSE IF( @glcSourceName = 'Division' )
BEGIN
IF( not exists (SELECT * FROM Division WHERE DivisionID = @DivisionID) )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 241, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
SET @glNumber = (SELECT Ltrim(Rtrim(GLCode)) FROM Division WHERE DivisionID = @DivisionID)
IF( @glNumber is null )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 201, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
ELSE IF( Len(Rtrim(@glNumber)) > @padLength )
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 202, 'Order', @OrdPriKey
SET @firstGLNumber = null
END
ELSE
BEGIN
EXEC @glAccount = udfLeftPadding @padLength, @glNumber
IF( @glsCounter > 1 and @firstGLNumber <> ' ' )
begin
SET @firstGLNumber = Ltrim(Rtrim(@firstGLNumber)) + '-' + Ltrim(Rtrim(@glAccount))
end
ELSE
begin
SET @firstGLNumber = Ltrim(Rtrim(@firstGLNumber)) + Ltrim(Rtrim(@glAccount))
end
END
END
ELSE
BEGIN
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 203, 'Order', @OrdPriKey
SET @glsCounter = @countGLCSegmets + 1
SET @firstGLNumber = null
END
SET @glsCounter = @glsCounter + 1
END
END
IF( @firstGLNumber is not null )
BEGIN
EXEC @batErrorCode =
spBATCheckGLNumber
@theBlankSessionID,
@firstGLNumber,
@branchPriKey,
@Source,
@SourcePriKey,
'Order',
@OrdPriKey
IF( @batErrorCode = 0 )
begin
UPDATE BATProcess set
GLNumber = @firstGLNumber
WHERE BATProcessPriKey = @BATProcessPriKey
end
ELSE
begin
EXEC @batErrorCode = spBATRecordError @theBlankSessionID, @Source, @SourcePriKey, 745, 'Order', @OrdPriKey, @firstGLNumber, @theItemCode
end
END
GO
GRANT EXECUTE ON [dbo].[spBATGenerateFirstThirdPartyGLNumber_Legacy] TO [MssExec]
GO