
[dbo].[spStorageGenerateGLNumbers_XLedger]
CREATE PROCEDURE [dbo].[spStorageGenerateGLNumbers_XLedger]
@StorageType varchar(20),
@BATProcessPriKey int,
@SessionID varchar(50),
@inAuthorityID int,
@inStorageItemsID int,
@ErrorCode int output
as
set nocount on
declare
@theOrderId int,
@theSource varchar(50),
@theSourcePriKey int,
@theItemCodePriKey int,
@theIsDeferred smallint,
@theErrorFlag bit,
@theMappingType varchar(16),
@theGLARAPType varchar(2),
@theRevenueExpenseGLNumberAccountingAccountId int,
@theARAPSDGLNumberAccountingAccountId int,
@theFirstGLNumberAccountingAccountId int,
@theSecondGLNumberAccountingAccountId int,
@DEFERRED_STORAGE_TYPE varchar(10),
@REVENUE_STORAGE_TYPE varchar(10),
@COMMISSION_STORAGE_TYPE varchar(10),
@theItemCodeStr varchar(10),
@theIsInboundDeferral bit
select
@theErrorFlag = 0,
@ErrorCode = 0,
@theIsDeferred = 0,
@DEFERRED_STORAGE_TYPE = 'Deferred',
@REVENUE_STORAGE_TYPE = 'Revenue',
@COMMISSION_STORAGE_TYPE = 'Commission'
select
@theOrderId = OrdPriKey,
@theSource = [Source],
@theSourcePriKey = SourceRecord
from BATProcess
where BATProcessPriKey = @BATProcessPriKey
if( not exists ( select top 1 1 from Orders where PriKey = @theOrderId ) )
begin
exec spStorageRecordError @SessionID, @theOrderId, 491
set @ErrorCode = 1
return
end
if( @StorageType not in ( @DEFERRED_STORAGE_TYPE, @REVENUE_STORAGE_TYPE, @COMMISSION_STORAGE_TYPE ) )
begin
exec spStorageRecordError @SessionID, @theOrderId, 1008
set @ErrorCode = 1
return
end
set @theItemCodePriKey =
case @theSource
when 'BillingMinorItem' then ( select ICPriKey from BillingMinorItem where BMinPriKey = @theSourcePriKey )
when 'CommissionedDetail' then
(
select b.ICPriKey
from BillingMinorItem b
inner join CommissionedDetail c on ( c.BMinPriKey = b.BMinPriKey )
where c.CDPriKey = @theSourcePriKey
)
when 'StorageDeferral' then
(
select b.ICPriKey
from BillingMinorItem b
inner join StorageDeferral s on ( s.BMinPriKey = b.BMinPriKey )
where s.DeferralPriKey = @theSourcePriKey
)
end
set @theMappingType =
case @theSource
when 'BillingMinorItem' then 'Revenue'
when 'CommissionedDetail' then 'Commissions'
when 'StorageDeferral' then 'Revenue'
end
select
@theRevenueExpenseGLNumberAccountingAccountId = XLedgerItemCodeMap.AccountingAccountFid
from XLedgerItemCodeMapAccountType
left outer join XLedgerItemCodeMap on XLedgerItemCodeMap.ItemCodeFid = @theItemCodePriKey and
XLedgerItemCodeMap.XLedgerItemCodeMapAccountTypeFid = XLedgerItemCodeMapAccountType.XLedgerItemCodeMapAccountTypeId
where XLedgerItemCodeMapAccountType.MappingType = @theMappingType
if( @theRevenueExpenseGLNumberAccountingAccountId is null )
begin
set @theItemCodeStr = ( select convert( varchar(10), ItemCode ) from itemCode where ICPriKey = @theItemCodePriKey )
exec spStorageRecordError
@inSessionID = @SessionID,
@inOrderID = @theOrderId,
@inErrorCodeID = 1520,
@inParam1 = @theItemCodeStr,
@inParam2 = @theMappingType
set @ErrorCode = 1
return
end
set @theGLARAPType =
case @theSource
when 'BillingMinorItem' then 'AR'
when 'CommissionedDetail' then 'AP'
when 'StorageDeferral' then 'SD'
end
select
@theARAPSDGLNumberAccountingAccountId = XLedgerGLARAP.AccountingAccountFid
from XLedgerGLARAP
where XLedgerGLARAP.TypeName = @theGLARAPType
if( @theARAPSDGLNumberAccountingAccountId is null )
begin
exec spStorageRecordError
@inSessionID = @SessionID,
@inOrderID = @theOrderId,
@inErrorCodeID = 1521,
@inParam1 = @theGLARAPType
set @ErrorCode = 1
return
end
if( @StorageType = @DEFERRED_STORAGE_TYPE )
begin
set @theIsInboundDeferral =
(
select
case
when Processed is null then 1
else 0
end
from StorageDeferral
where DeferralPriKey = @theSourcePriKey
)
set @theFirstGLNumberAccountingAccountId =
case @theIsInboundDeferral
when 1 then @theRevenueExpenseGLNumberAccountingAccountId
else @theARAPSDGLNumberAccountingAccountId
end
set @theSecondGLNumberAccountingAccountId =
case @theIsInboundDeferral
when 1 then @theARAPSDGLNumberAccountingAccountId
else @theRevenueExpenseGLNumberAccountingAccountId
end
end
else if @StorageType = @REVENUE_STORAGE_TYPE
begin
set @theFirstGLNumberAccountingAccountId = @theRevenueExpenseGLNumberAccountingAccountId
set @theSecondGLNumberAccountingAccountId = @theARAPSDGLNumberAccountingAccountId
end
else if @StorageType = @COMMISSION_STORAGE_TYPE
begin
set @theFirstGLNumberAccountingAccountId = @theRevenueExpenseGLNumberAccountingAccountId
set @theSecondGLNumberAccountingAccountId = @theARAPSDGLNumberAccountingAccountId
end
update BATProcess set
GLNumber = isnull( FirstAccountingAccount.Code, '' ),
NominalAccountingAccountFid = @theFirstGLNumberAccountingAccountId,
OpposingGLNumber = isnull( SecondAccountingAccount.Code, '' ),
OpposingAccountingAccountFid = @theSecondGLNumberAccountingAccountId
from BATProcess
left outer join AccountingAccount as FirstAccountingAccount on FirstAccountingAccount.AccountingAccountId = @theFirstGLNumberAccountingAccountId
left outer join AccountingAccount as SecondAccountingAccount on SecondAccountingAccount.AccountingAccountId = @theSecondGLNumberAccountingAccountId
where BATProcess.BATProcessPriKey = @BATProcessPriKey
GO
GRANT EXECUTE ON [dbo].[spStorageGenerateGLNumbers_XLedger] TO [MssExec]
GO