
[dbo].[spBATGetXLedgerXgls]
CREATE PROCEDURE [dbo].[spBATGetXLedgerXgls]
@inOrderID int,
@inBatSessionID varchar(50),
@inErrorLoggingSessionId varchar(50),
@inBatProcessPriKey int = null,
@inIsStorageProcessing bit = null
as
set nocount on
declare @theHadError bit
declare @theXGlId int
declare @theExpenseTypeRowCount int
declare @theCounter int
declare @theExpenseTypeName varchar(16)
select
@theHadError = 0,
@theXGlId = null,
@inBatProcessPriKey = case
when isnull( @inBatProcessPriKey, 0 ) > 0 then @inBatProcessPriKey
else null
end
declare @theBATProcessInfo table
(
BATProcessID int,
TransactionType varchar(30),
GlArAp varchar(2),
Source varchar(50),
SourceRecord int,
ExpenseTypeName varchar(16),
LaborTypeFid int
)
declare @theExpenseTypesInUse table
(
ID int identity( 1, 1 ) not null,
ExpenseTypeName varchar(16)
)
insert into @theBATProcessInfo
(
BATProcessID,
TransactionType,
Source,
SourceRecord
)
select
BATProcessID = BATProcess.BATProcessPriKey,
TransactionType = BATProcess.TransactionType,
Source = BATProcess.Source,
SourceRecord = BATProcess.SourceRecord
from BATProcess
where @inBatProcessPriKey is null and
BATProcess.OrdPriKey = @inOrderID and
BATProcess.BATSessionID = @inBatSessionID
union all
select
BATProcessID = BATProcess.BATProcessPriKey,
TransactionType = BATProcess.TransactionType,
Source = BATProcess.Source,
SourceRecord = BATProcess.SourceRecord
from BATProcess
where @inBatProcessPriKey is not null and
BATProcess.BATProcessPriKey = @inBatProcessPriKey
update @theBATProcessInfo set
GlArAp = case TransactionType
when 'A/P Credit Memo' then 'AP'
when 'A/P Invoice' then 'AP'
when 'A/R Cash Receipt' then 'AR'
when 'A/R Credit Memo' then 'AR'
when 'A/R Debit Memo' then 'AR'
when 'A/R Sales / Invoice' then 'AR'
when 'Journal Entry' then 'GL'
else '??'
end,
ExpenseTypeName = case Source
when 'ClaimItemDueFrom' then 'Claims'
when 'ClaimHandlingRevenue' then 'Claims'
when 'ClaimSettlement' then 'Claims'
when 'ClaimLiabilityItem' then 'Claims'
when 'ThirdParty' then 'ThirdParty'
else ''
end
from @theBATProcessInfo
update @theBATProcessInfo set
ExpenseTypeName = XLedgerExpenseType.[TypeName]
from @theBATProcessInfo as theBATProcessInfo
inner join CommissionedDetail on CommissionedDetail.CDPriKey = theBATProcessInfo.SourceRecord
inner join XLedgerLaborTypeToExpenseTypeMap on XLedgerLaborTypeToExpenseTypeMap.LaborTypeFID = CommissionedDetail.LaborTypeFID
inner join XLedgerExpenseType on XLedgerExpenseType.XLedgerExpenseTypeID = XLedgerLaborTypeToExpenseTypeMap.XLedgerExpenseTypeFID
where theBATProcessInfo.ExpenseTypeName = '' and
theBATProcessInfo.Source = 'CommissionedDetail' and
theBATProcessInfo.GlArAp = 'AP'
update @theBATProcessInfo set
ExpenseTypeName = XLedgerExpenseType.[TypeName]
from @theBATProcessInfo as theBATProcessInfo
inner join SplitCommissions on SplitCommissions.SCPriKey = theBATProcessInfo.SourceRecord
inner join XLedgerLaborTypeToExpenseTypeMap on XLedgerLaborTypeToExpenseTypeMap.LaborTypeFID = SplitCommissions.LaborTypeFID
inner join XLedgerExpenseType on XLedgerExpenseType.XLedgerExpenseTypeID = XLedgerLaborTypeToExpenseTypeMap.XLedgerExpenseTypeFID
where theBATProcessInfo.ExpenseTypeName = '' and
theBATProcessInfo.Source = 'SplitCommissions' and
theBATProcessInfo.GlArAp = 'AP'
update @theBATProcessInfo set
ExpenseTypeName = XLedgerExpenseType.[TypeName]
from @theBATProcessInfo as theBATProcessInfo
inner join Advance on Advance.PriKey = theBATProcessInfo.SourceRecord
inner join SysUser on SysUser.SysUserID = Advance.AdvanceToSysUserFID
inner join SysUserLaborTypeMap on SysUserLaborTypeMap.SysUserFID = Sysuser.SysUserID and SysUserLaborTypeMap.[Default] = 1
inner join XLedgerLaborTypeToExpenseTypeMap on XLedgerLaborTypeToExpenseTypeMap.LaborTypeFID = SysUserLaborTypeMap.LaborTypeFID
inner join XLedgerExpenseType on XLedgerExpenseType.XLedgerExpenseTypeID = XLedgerLaborTypeToExpenseTypeMap.XLedgerExpenseTypeFID
where theBATProcessInfo.ExpenseTypeName = '' and
theBATProcessInfo.Source = 'Advance'
update @theBATProcessInfo set
ExpenseTypeName = 'None'
from @theBATProcessInfo as theBATProcessInfo
where theBATProcessInfo.ExpenseTypeName = ''
insert into @theExpenseTypesInUse
(
ExpenseTypeName
)
select distinct
ExpenseTypeName
from @theBATProcessInfo
set @theExpenseTypeRowCount = @@rowcount
set @theCounter = 1
while( @theCounter <= @theExpenseTypeRowCount )
begin
select
@theExpenseTypeName = theExpenseTypesInUse.ExpenseTypeName
from @theExpenseTypesInUse as theExpenseTypesInUse
where theExpenseTypesInUse.ID = @theCounter
exec spBATGetSingleXLedgerXglId
@inOrderID = @inOrderID,
@inSessionID = @inErrorLoggingSessionId,
@inXLedgerExpenseType = @theExpenseTypeName,
@inIsStorageProcessing = @inIsStorageProcessing,
@outXGlId = @theXGlId out,
@outHadError = @theHadError out
if( @theHadError = 0 )
begin
update BATProcess set
XglFid = @theXGlId
from @theBATProcessInfo as theBATProcessInfo
inner join BATProcess on BATProcess.BATProcessPriKey = theBATProcessInfo.BATProcessID
where theBATProcessInfo.ExpenseTypeName = @theExpenseTypeName
end
set @theCounter = @theCounter + 1
end
GO
GRANT EXECUTE ON [dbo].[spBATGetXLedgerXgls] TO [MssExec]
GO