
[dbo].[spBATGetXLedgerXgls]
CREATE procedure [dbo].[spBATGetXLedgerXgls]
@inOrderID int,
@inBatSessionID varchar(50),
@inErrorLoggingSessionId varchar(50),
@inBatProcessPriKey int = null,
@inIsStorageProcessing bit = null,
@inSourceTable varchar(50) = null,
@inSourceID int = null,
@inAllowNullsAuthorityMoveTypeCommodity bit = 0,
@inObjectTable varchar(20) = null,
@inObjectID int = null
as
set nocount on
declare @theReturnValue int = 0
declare @theHadError bit
declare @theXGlId int
declare @theExpenseTypeRowCount int
declare @theCounter int
declare @theOrderId int
declare @theExpenseTypeName varchar(16)
declare @theSourceTable varchar(30) = @inSourceTable
declare @theSourceID int
declare @theObjectTable varchar(20) = @inObjectTable
declare @theObjectID int
if( @inSourceTable is null )
begin
select
@theSourceTable = 'Order',
@theObjectTable = 'Order'
end
select
@theHadError = 0,
@theXGlId = null,
@inBatProcessPriKey = case
when isnull( @inBatProcessPriKey, 0 ) > 0 then @inBatProcessPriKey
else null
end
declare @theBATProcessInfo table
(
BATProcessID int,
OrderId int,
TransactionType varchar(30),
GlArAp varchar(2),
Source varchar(50),
SourceRecord int,
ExpenseTypeName varchar(16),
LaborTypeFid int
)
declare @theOrderExpenseTypesInUse table
(
ID int identity( 1, 1 ) not null,
OrderId int,
ExpenseTypeName varchar(16),
SourceId int
)
insert into @theBATProcessInfo
(
BATProcessID,
OrderId,
TransactionType,
Source,
SourceRecord
)
select
BATProcessID = BATProcess.BATProcessPriKey,
OrderId = case
when BATProcess.OrdPriKey > 0 then BATProcess.OrdPriKey
else null
end,
TransactionType = BATProcess.TransactionType,
Source = BATProcess.Source,
SourceRecord = BATProcess.SourceRecord
from BATProcess
left outer join Xgl on Xgl.XglId = BATProcess.XglFid
where @inBatProcessPriKey is null and
BATProcess.OrdPriKey = @inOrderID and
Xgl.XglId is null and
BATProcess.BATSessionID = @inBatSessionID
union all
select
BATProcessID = BATProcess.BATProcessPriKey,
OrderId = case
when BATProcess.OrdPriKey > 0 then BATProcess.OrdPriKey
else null
end,
TransactionType = BATProcess.TransactionType,
Source = BATProcess.Source,
SourceRecord = BATProcess.SourceRecord
from BATProcess
left outer join Xgl on Xgl.XglId = BATProcess.XglFid
where @inBatProcessPriKey is not null and
Xgl.XglId is null and
BATProcess.BATProcessPriKey = @inBatProcessPriKey
union all
select
BATProcessID = BATProcess.BATProcessPriKey,
OrderId = case
when BATProcess.OrdPriKey > 0 then BATProcess.OrdPriKey
else null
end,
TransactionType = BATProcess.TransactionType,
Source = BATProcess.Source,
SourceRecord = BATProcess.SourceRecord
from BATProcess
left outer join Xgl on Xgl.XglId = BATProcess.XglFid
where @inBatProcessPriKey is null and
@inOrderID is null and
Xgl.XglId is null and
BATProcess.BATSessionID = @inBatSessionID
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 @theOrderExpenseTypesInUse
(
OrderId,
ExpenseTypeName,
SourceId
)
select distinct
OrderId = theBATProcessInfo.OrderId,
ExpenseTypeName = theBATProcessInfo.ExpenseTypeName,
SourceId = case
when @theSourceTable = 'Advance' then theBATProcessInfo.SourceRecord
else null
end
from @theBATProcessInfo as theBATProcessInfo
set @theExpenseTypeRowCount = @@rowcount
set @theCounter = 1
while( @theCounter <= @theExpenseTypeRowCount )
begin
select
@theOrderId = theOrderExpenseTypesInUse.OrderId,
@theExpenseTypeName = theOrderExpenseTypesInUse.ExpenseTypeName,
@theSourceID = case
when @theSourceTable = 'Advance' then theOrderExpenseTypesInUse.SourceId
when @theSourceTable = 'Order' then theOrderExpenseTypesInUse.OrderId
else @inSourceID
end,
@theObjectID = case
when @theObjectTable = 'Order' then theOrderExpenseTypesInUse.OrderId
else @inObjectID
end
from @theOrderExpenseTypesInUse as theOrderExpenseTypesInUse
where theOrderExpenseTypesInUse.ID = @theCounter
exec spBATGetSingleXLedgerXglId
@inOrderID = @theOrderId,
@inSessionID = @inErrorLoggingSessionId,
@inSourceTable = @theSourceTable,
@inSourceID = @theSourceID,
@inObjectTable = @theObjectTable,
@inObjectID = @theObjectID,
@inXLedgerExpenseType = @theExpenseTypeName,
@inIsStorageProcessing = @inIsStorageProcessing,
@inAllowNullsAuthorityMoveTypeCommodity = @inAllowNullsAuthorityMoveTypeCommodity,
@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 and
(
( @theOrderId is not null and theBATProcessInfo.OrderId = @theOrderId ) or
( @theOrderId is null and theBATProcessInfo.OrderId is null )
) and
(
( @theSourceTable = 'Advance' and theBATProcessInfo.SourceRecord = @theSourceID ) or
( @theSourceTable != 'Advance' )
)
end
else
begin
set @theReturnValue = 1
end
set @theCounter = @theCounter + 1
end
return @theReturnValue
GO
GRANT EXECUTE ON [dbo].[spBATGetXLedgerXgls] TO [MssExec]
GO