
[dbo].[ReEnqueueFailedTransactions]
CREATE procedure [dbo].[ReEnqueueFailedTransactions]
@failedJournalEntryBatchItemIds IntList readonly,
@sysUserId int
as
begin
set nocount on
declare @outXLedgerJournalEntryBatches table
(
SourceBatchId int,
NewBatchId int
)
;with SubmittedInfo as
(
select
SysUserId = @sysUserId,
CreatedOn = sysdatetimeoffset()
),
SubmittedBatches as
(
select distinct
BatchId = XLedgerJournalEntryBatchItem.XLedgerJournalEntryBatchFid
from @failedJournalEntryBatchItemIds as failedBatchItemIds
inner join XLedgerJournalEntryBatchItem on XLedgerJournalEntryBatchItem.XLedgerJournalEntryBatchItemId = failedBatchItemIds.Item
)
insert into XLedgerJournalEntryBatch
(
[Description],
SubmittedBySysUserFid,
CreatedOn,
RetryOfXLedgerJournalEntryBatchFid
)
output INSERTED.RetryOfXLedgerJournalEntryBatchFid, INSERTED.XLedgerJournalEntryBatchId
into @outXLedgerJournalEntryBatches( SourceBatchId, NewBatchId )
select
[Description] = XLedgerJournalEntryBatch.[Description],
SubmittedBySysUserFid = SysUser.SysUserId,
CreatedOn = SubmittedInfo.CreatedOn,
RetryOfXLedgerJournalEntryBatchFid = SubmittedBatches.BatchId
from SubmittedBatches
inner join XLedgerJournalEntryBatch on XLedgerJournalEntryBatch.XLedgerJournalEntryBatchId = SubmittedBatches.BatchId
cross join SubmittedInfo
left outer join SysUser on SysUser.SysUserId = SubmittedInfo.SysUserId
declare @newBatchItemIds table
(
SourceItemId int,
NewItemId int
)
declare @newBatchItemIdsRowCount int
;with Items as
(
select
SourceItemId = SourceXLedgerJournalEntryBatchItem.XLedgerJournalEntryBatchItemId,
XLedgerJournalEntryBatchFid = outXLedgerJournalEntryBatches.NewBatchId,
AcctTransactionFid = SourceXLedgerJournalEntryBatchItem.AcctTransactionFid,
XglFid = SourceXLedgerJournalEntryBatchItem.XglFid,
NominalXLedgerCompanyXLedgerDbId = SourceXLedgerJournalEntryBatchItem.NominalXLedgerCompanyXLedgerDbId,
OpposingXLedgerCompanyXLedgerDbId = SourceXLedgerJournalEntryBatchItem.OpposingXLedgerCompanyXLedgerDbId,
NominalBranchObjectValueXLedgerDbId = SourceXLedgerJournalEntryBatchItem.NominalBranchObjectValueXLedgerDbId,
OpposingBranchObjectValueXLedgerDbId = SourceXLedgerJournalEntryBatchItem.OpposingBranchObjectValueXLedgerDbId,
NominalAccountingAccountFid = SourceXLedgerJournalEntryBatchItem.NominalAccountingAccountFid,
OpposingAccountingAccountFid = SourceXLedgerJournalEntryBatchItem.OpposingAccountingAccountFid,
XLedgerCustomerXLedgerDbId = SourceXLedgerJournalEntryBatchItem.XLedgerCustomerXLedgerDbId,
XLedgerSupplierXLedgerDbId = SourceXLedgerJournalEntryBatchItem.XLedgerSupplierXLedgerDbId,
RequestGroupIdentifier = SourceXLedgerJournalEntryBatchItem.RequestGroupIdentifier,
XLedgerBankAccountFid = SourceXLedgerJournalEntryBatchItem.XLedgerBankAccountFid
from @failedJournalEntryBatchItemIds as failedJournalEntryBatchItemIds
inner join XLedgerJournalEntryBatchItem as SourceXLedgerJournalEntryBatchItem on
SourceXLedgerJournalEntryBatchItem.XLedgerJournalEntryBatchItemId = failedJournalEntryBatchItemIds.Item
inner join @outXLedgerJournalEntryBatches as outXLedgerJournalEntryBatches on
outXLedgerJournalEntryBatches.SourceBatchId = SourceXLedgerJournalEntryBatchItem.XLedgerJournalEntryBatchFid
where SourceXLedgerJournalEntryBatchItem.Resolved = 0
)
merge XLedgerJournalEntryBatchItem with (tablock) using Items on 1 = 2
when not matched then
insert
(
XLedgerJournalEntryBatchFid,
AcctTransactionFid,
XglFid,
NominalXLedgerCompanyXLedgerDbId,
OpposingXLedgerCompanyXLedgerDbId,
NominalBranchObjectValueXLedgerDbId,
OpposingBranchObjectValueXLedgerDbId,
NominalAccountingAccountFid,
OpposingAccountingAccountFid,
XLedgerCustomerXLedgerDbId,
XLedgerSupplierXLedgerDbId,
RequestGroupIdentifier,
XLedgerBankAccountFid,
Resolved
)
values
(
Items.XLedgerJournalEntryBatchFid,
Items.AcctTransactionFid,
Items.XglFid,
Items.NominalXLedgerCompanyXLedgerDbId,
Items.OpposingXLedgerCompanyXLedgerDbId,
Items.NominalBranchObjectValueXLedgerDbId,
Items.OpposingBranchObjectValueXLedgerDbId,
Items.NominalAccountingAccountFid,
Items.OpposingAccountingAccountFid,
Items.XLedgerCustomerXLedgerDbId,
Items.XLedgerSupplierXLedgerDbId,
Items.RequestGroupIdentifier,
Items.XLedgerBankAccountFid,
0
)
output Items.SourceItemId, inserted.XLedgerJournalEntryBatchItemId
into @newBatchItemIds( SourceItemId, NewItemId );
set @newBatchItemIdsRowCount = @@ROWCOUNT
declare @newResolvedBatchItemIds IntList
declare @newResolvedBatchItemIdsRowCount int
update XLedgerJournalEntryBatchItem set
Resolved = 1
output INSERTED.XLedgerJournalEntryBatchItemId
into @newResolvedBatchItemIds( Item )
from @newBatchItemIds as newBatchItemIds
inner join XLedgerJournalEntryBatchItem on
XLedgerJournalEntryBatchItem.XLedgerJournalEntryBatchItemId = newBatchItemIds.SourceItemId
where XLedgerJournalEntryBatchItem.Resolved = 0
set @newResolvedBatchItemIdsRowCount = @@ROWCOUNT
if( @newBatchItemIdsRowCount != @newResolvedBatchItemIdsRowCount )
begin
delete from XLedgerJournalEntryBatchItem
from @newBatchItemIds as newBatchItemIds
inner join XLedgerJournalEntryBatchItem on
XLedgerJournalEntryBatchItem.XLedgerJournalEntryBatchItemId = newBatchItemIds.SourceItemId
left outer join @newResolvedBatchItemIds as newResolvedBatchItemIds on
newResolvedBatchItemIds.Item = newBatchItemIds.SourceItemId
where newResolvedBatchItemIds.Item is null
end
if( @newBatchItemIdsRowCount = 0 or @newBatchItemIdsRowCount != @newResolvedBatchItemIdsRowCount )
begin
;with EmptyBatches as (
select
Id = XLedgerJournalEntryBatch.XLedgerJournalEntryBatchId
from @outXLedgerJournalEntryBatches as outXLedgerJournalEntryBatches
inner join XLedgerJournalEntryBatch on
XLedgerJournalEntryBatch.XLedgerJournalEntryBatchId = outXLedgerJournalEntryBatches.NewBatchId
left outer join XLedgerJournalEntryBatchItem on
XLedgerJournalEntryBatchItem.XLedgerJournalEntryBatchFid = XLedgerJournalEntryBatch.XLedgerJournalEntryBatchId
group by XLedgerJournalEntryBatch.XLedgerJournalEntryBatchId
having count( XLedgerJournalEntryBatchItem.XLedgerJournalEntryBatchItemId ) = 0
)
delete from XLedgerJournalEntryBatch
from XLedgerJournalEntryBatch
inner join EmptyBatches on
EmptyBatches.Id = XLedgerJournalEntryBatch.XLedgerJournalEntryBatchId
end
declare @finalBatchCount int
insert into XLedgerJournalEntryBatchQueue
(
XLedgerJournalEntryBatchFid
)
select
XLedgerJournalEntryBatchFid = outXLedgerJournalEntryBatches.NewBatchId
from @outXLedgerJournalEntryBatches as outXLedgerJournalEntryBatches
inner join XLedgerJournalEntryBatch on
XLedgerJournalEntryBatch.XLedgerJournalEntryBatchId = outXLedgerJournalEntryBatches.NewBatchId
set @finalBatchCount = @@ROWCOUNT
if( @finalBatchCount > 0 )
begin
insert into MssWebEvent( MssWebEventTypeFid )
select MssWebEventType.MssWebEventTypeId
from MssWebEventType
where MssWebEventType.EventTypeName = 'XLedgerTransactionUploadRequested'
end
select
XLedgerJournalEntryBatchId = outXLedgerJournalEntryBatches.NewBatchId
from @outXLedgerJournalEntryBatches as outXLedgerJournalEntryBatches
inner join XLedgerJournalEntryBatch on
XLedgerJournalEntryBatch.XLedgerJournalEntryBatchId = outXLedgerJournalEntryBatches.NewBatchId
end
GO
GRANT EXECUTE ON [dbo].[ReEnqueueFailedTransactions] TO [MssExec]
GO