SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[spStorageGenerateGLNumbers_Legacy]
@StorageType varchar(20),
@BATProcessPriKey int,
@SessionID varchar(50),
@inAuthorityID int,
@inStorageItemsID int,
@ErrorCode int output
as
set nocount on
declare
@branchPriKey int,
@DivisionPriKey int,
@ordPriKey int,
@source varchar(50),
@sourcePriKey int,
@firstGLNumber varchar(66),
@countGLCSegmets int,
@glsCounter int,
@padLength int,
@glaPriKey int,
@icDetailPriKey int,
@icPriKey int,
@commissionSysuser int,
@transType varchar(2),
@isDeferred smallint,
@commodityPriKey int,
@mtGroupPriKey int,
@dtPriKey int,
@authority varchar(30),
@theErrorFlag bit
set @theErrorFlag = 0
set @ErrorCode = 0
set @isDeferred = 0
set @ordPriKey = ( select OrdPriKey from BATProcess where BATProcessPriKey = @BATProcessPriKey )
set @commodityPriKey = ( select Commodity from Orders where PriKey = @ordPriKey )
set @mtGroupPriKey =
(
select mt.MTGroupPriKey
from MoveType mt
inner join Orders o on (o.MoveType = mt.PriKey)
where o.PriKey = @ordPriKey
)
set @dtPriKey =
(
select s.DTPriKey
from Sysuser s
inner join Trips t on (t.DriverEmpNo = s.SysuserID)
inner join TripOrd tor on (tor.TripPriKey = t.PriKey)
where tor.OrdPriKey = @ordPriKey
)
set @branchPriKey =
(
select Branch.BranchPriKey
from Branch
inner join BATProcess on BATProcess.BranchID = Branch.BranchID
where BATProcess.BATProcessPriKey = @BATProcessPriKey
)
set @source = ( select Source from BATProcess where BATProcessPriKey = @BATProcessPriKey)
set @sourcePriKey = (select SourceRecord from BATProcess where BATProcessPriKey = @BATProcessPriKey )
set @DivisionPriKey =
case @source
when 'BillingMinorItem' then ( select DivisionFID from BillingMinorItem where BMinPriKey = @sourcePriKey )
when 'CommissionedDetail' then
(
select BillingMinorItem.DivisionFID
from BillingMinorItem
inner join CommissionedDetail on CommissionedDetail.BMinPriKey = BillingMinorItem.BMinPriKey
where CommissionedDetail.CDPriKey = @sourcePriKey
)
else
(
select BillingMinorItem.DivisionFID
from BillingMinorItem
inner join StorageDeferral on StorageDeferral.BMinPriKey = BillingMinorItem.BMinPriKey
inner join BATProcess on BATProcess.SourceRecord = StorageDeferral.DeferralPriKey
where BATProcessPriKey = @BATProcessPriKey
)
end
set @authority =
(
select Description
from AuthorityTypes
where AuthPriKey = @inAuthorityID
)
if not exists ( select * from Orders where PriKey = @ordPriKey )
begin
exec spStorageRecordError @SessionID, @OrdPriKey, 491
set @ErrorCode = 1
return
end
if not exists( select * from Branch where BranchPriKey = @branchPriKey )
begin
exec spStorageRecordError @SessionID, @OrdPriKey, 492
set @ErrorCode = 1
return
end
if @StorageType not in ('Deferred', 'Revenue', 'Commission')
begin
exec spStorageRecordError @SessionID, @OrdPriKey, 1008
set @ErrorCode = 1
return
end
set @icPriKey =
case @source
when 'BillingMinorItem' then ( select ICPriKey from BillingMinorItem where BMinPriKey = @sourcePriKey )
when 'CommissionedDetail' then
(
select b.ICPriKey
from BillingMinorItem b
inner join CommissionedDetail c on ( c.BMinPriKey = b.BMinPriKey )
where c.CDPriKey = @sourcePriKey
)
when 'StorageDeferral' then
(
select b.ICPriKey
from BillingMinorItem b
inner join StorageDeferral s on ( s.BMinPriKey = b.BMinPriKey )
where s.DeferralPriKey = @sourcePriKey
)
end
if @StorageType = 'Deferred'
begin
declare @theIsInboundDeferral bit
set @theIsInboundDeferral =
(
select
case
when Processed is null then 1
else 0
end
from StorageDeferral
where DeferralPriKey = @sourcePriKey
)
exec RecurringBillingBuildGLNumber
@SessionID,
@ordPriKey,
@branchPriKey,
'Deferred',
@DivisionPriKey,
@icPriKey,
null,
@inAuthorityID,
@commodityPriKey,
@mtGroupPriKey,
@dtPriKey,
@sourcePriKey,
@firstGLNumber output,
@theErrorFlag output
if( @theErrorFlag = 1 )
begin
set @ErrorCode = 1
return
end
exec RecurringBillingValidateGLNumber
@firstGLNumber,
@BranchPriKey,
@ErrorCode output
if( @ErrorCode != 0 )
begin
exec spStorageRecordError @SessionID, @ordPriKey, @ErrorCode
exec spStorageRecordError @SessionID, @ordPriKey, 954
set @ErrorCode = 1
return
end
else
begin
if( @theIsInboundDeferral = 1 )
begin
update BATProcess
set OpposingGLNumber = @firstGLNumber
where BATProcessPriKey = @BATProcessPriKey
end
else
begin
update BATProcess
set GLNumber = @firstGLNumber
where BATProcessPriKey = @BATProcessPriKey
end
exec RecurringBillingBuildGLNumber
@SessionID,
@ordPriKey,
@branchPriKey,
'Storage',
@DivisionPriKey,
@icPriKey,
null,
@inAuthorityID,
@commodityPriKey,
@mtGroupPriKey,
@dtPriKey,
@sourcePriKey,
@firstGLNumber output,
@theErrorFlag output
if( @theErrorFlag = 1 )
begin
set @ErrorCode = 1
return
end
exec RecurringBillingValidateGLNumber
@firstGLNumber,
@BranchPriKey,
@ErrorCode output
if @ErrorCode <> 0
begin
exec spStorageRecordError @SessionID, @OrdPriKey, @ErrorCode
exec spStorageRecordError @SessionID, @OrdPriKey, 1020
set @ErrorCode = 1
return
end
else
if( @theIsInboundDeferral = 1 )
begin
update BATProcess
set GLNumber = @firstGLNumber
where BATProcessPriKey = @BATProcessPriKey
end
else
begin
update BATProcess
set OpposingGLNumber = @firstGLNumber
where BATProcessPriKey = @BATProcessPriKey
end
end
end
else if @StorageType = 'Revenue'
begin
exec RecurringBillingBuildGLNumber
@SessionID,
@ordPriKey,
@branchPriKey,
'Revenue',
@DivisionPriKey,
@icPriKey,
null,
@inAuthorityID,
@commodityPriKey,
@mtGroupPriKey,
@dtPriKey,
@sourcePriKey,
@firstGLNumber output,
@theErrorFlag output
if( @theErrorFlag = 1 )
begin
set @ErrorCode = 1
return
end
exec RecurringBillingValidateGLNumber
@firstGLNumber,
@BranchPriKey,
@ErrorCode output
if @ErrorCode <> 0
begin
exec spStorageRecordError @SessionID, @OrdPriKey, @ErrorCode
if @isDeferred = 0
begin
exec spStorageRecordError @SessionID, @OrdPriKey, 1031
end
else
begin
exec spStorageRecordError @SessionID, @OrdPriKey, 1032
end
set @ErrorCode = 1
return
end
else
begin
update BATProcess
set GLNumber = @firstGLNumber
where BATProcessPriKey = @BATProcessPriKey
set @branchPriKey = null
select
@branchPriKey = StorageItems.ARBranchFID,
@DivisionPriKey = StorageItems.ARDivisionFID
from StorageItems
where StorageItems.StorageItemsPriKey = @inStorageItemsID
if not exists (select * from Branch where BranchPriKey = @branchPriKey)
begin
set @ErrorCode = 1
exec spStorageRecordError @SessionID, @OrdPriKey, 1106
return
end
set @transType = 'AR'
exec RecurringBillingBuildGLNumber
@SessionID,
@ordPriKey,
@branchPriKey,
'GLARAP',
@DivisionPriKey,
null,
@transType,
@inAuthorityID,
@commodityPriKey,
@mtGroupPriKey,
@dtPriKey,
@sourcePriKey,
@firstGLNumber output,
@theErrorFlag output
exec RecurringBillingValidateGLNumber
@firstGLNumber,
@BranchPriKey,
@ErrorCode output
if @ErrorCode <> 0
begin
exec spStorageRecordError @SessionID, @OrdPriKey, @ErrorCode
exec spStorageRecordError @SessionID, @OrdPriKey, 1033
set @ErrorCode = 1
return
end
else
begin
update BATProcess set
OpposingGLNumber = @firstGLNumber
where BATProcessPriKey = @BATProcessPriKey
end
return
end
end
else if @StorageType = 'Commission'
begin
set @transType = 'AP'
exec RecurringBillingBuildGLNumber
@SessionID,
@ordPriKey,
@branchPriKey,
'Commission',
@DivisionPriKey,
@icPriKey,
null,
@inAuthorityID,
@commodityPriKey,
@mtGroupPriKey,
@dtPriKey,
@sourcePriKey,
@firstGLNumber output,
@theErrorFlag output
if( @theErrorFlag = 1 )
begin
set @ErrorCode = 1
return
end
exec RecurringBillingValidateGLNumber
@firstGLNumber,
@BranchPriKey,
@ErrorCode output
if @ErrorCode <> 0
begin
exec spStorageRecordError @SessionID, @OrdPriKey, @ErrorCode
exec spStorageRecordError @SessionID, @OrdPriKey, 1035
set @ErrorCode = 1
return
end
else
begin
update BATProcess
set GLNumber = @firstGLNumber
where BATProcessPriKey = @BATProcessPriKey
set @glsCounter = 1
set @firstGLNumber = ' '
set @branchPriKey = Case @authority
when 'Van Line' then
(
select w.BranchPriKey
from Warehouse w
inner join StorageItems si on (si.WarehousePriKey = w.WarehousePriKey)
inner join StorageProcessing sp on (sp.StorageItemsPrikey = si.StorageItemsPriKey)
inner join BATProcess bp on (bp.BATSessionID = sp.SessionID)
where bp.BATProcessPriKey = @BATProcessPriKey and
bp.Source = sp.Source and
bp.SourceRecord = sp.SourcePriKey
)
else
(
select BillingMinorItem.BranchFID
from BillingMinorItem
inner join CommissionedDetail on CommissionedDetail.BMinPriKey = BillingMinorItem.BMinPriKey
where CommissionedDetail.CDPriKey = @SourcePriKey
)
end
if @branchPriKey is null
begin
set @branchPriKey =
(
select o.BranchPriKey
from Orders o
inner join BATProcess b on (b.OrdPriKey = o.PriKey)
where b.BATProcessPriKey = @BATProcessPriKey
)
end
if not exists ( select * from Branch where BranchPriKey = @branchPriKey )
begin
set @ErrorCode = 1
exec spStorageRecordError @SessionID, @OrdPriKey, 1107
return
end
exec RecurringBillingBuildGLNumber
@SessionID,
@ordPriKey,
@branchPriKey,
'GLARAP',
@DivisionPriKey,
null,
@transType,
@inAuthorityID,
@commodityPriKey,
@mtGroupPriKey,
@dtPriKey,
@sourcePriKey,
@firstGLNumber output,
@theErrorFlag output
exec RecurringBillingValidateGLNumber
@firstGLNumber,
@BranchPriKey,
@ErrorCode output
if @ErrorCode <> 0
begin
exec spStorageRecordError @SessionID, @OrdPriKey, @ErrorCode
set @ErrorCode = 1
return
end
else
begin
update BATProcess
set OpposingGLNumber = @firstGLNumber
where BATProcessPriKey = @BATProcessPriKey
end
end
end
GO
GRANT EXECUTE ON [dbo].[spStorageGenerateGLNumbers_Legacy] TO [MssExec]
GO