
[dbo].[ARCUpdateCustomerAlertsConvertToXLedger]
create procedure [dbo].[ARCUpdateCustomerAlertsConvertToXLedger]
@inCommitTransaction bit = null,
@inClearOutOldAccountingSystemRecordsOption int = null,
@outXLedgerRecordCount int output,
@outLegacyRecordCount int output,
@outOldRecordsConverted int output,
@outOldRecordsDeleted int output,
@outErrorCode int output
as
begin
set nocount on;
declare @inOriginalErrorCode int = isnull( @outErrorCode, 0 );
declare @NO_ERROR int = 0;
declare @LEGACY_RECORDS_EXIST_WITH_XLEDGER_SYSTEM int = 1;
declare @XLEDGER_RECORDS_EXIST_WITH_NON_XLEDGER_SYSTEM int = 2;
declare @BOTH_XLEDGER_AND_LEGACY_RECORDS_EXIST_NO_CAN_DO int = 3;
declare @INVALID_ClearOutOldAccountingSystemRecordsOption_FOR_CURRENT_STATE int = 90;
declare @USER_OPTION_DOES_NOT_MATCH_CURRENT_STATE int = 91;
declare @GLOBAL_SYSTEM_OPTION_NAME varchar(64) = 'ARCCustomerInformationRecordsConvertedToXLedger'
select
@outOldRecordsConverted = null,
@outOldRecordsDeleted = null,
@outErrorCode = @NO_ERROR
declare @theOkToContinue bit = 1;
declare @theOkToDeleteLegacyRecords bit = 0
declare @theMarkConversionAsCompleted bit = 0;
declare @theRowCount int
set @outXLedgerRecordCount = ( select count(*) from ARCCustomerInformation where AccountingCustomerFid is not null );
set @outLegacyRecordCount = ( select count(*) from ARCCustomerInformation where AccountingCustomerFid is null );
declare @theAccountingSystemType varchar(2) = dbo.GetAccountingSystemType()
if( @theAccountingSystemType = 'XL' )
begin
if( @outLegacyRecordCount > 0 and @outXLedgerRecordCount = 0 )
begin
set @outErrorCode = @LEGACY_RECORDS_EXIST_WITH_XLEDGER_SYSTEM
end
else if( @outLegacyRecordCount = 0 )
begin
set @theOkToContinue = 0
set @theMarkConversionAsCompleted = 1
end
end
else
begin
if( @outLegacyRecordCount = 0 and @outXLedgerRecordCount > 0 )
begin
set @outErrorCode = @XLEDGER_RECORDS_EXIST_WITH_NON_XLEDGER_SYSTEM
end
end
if( @outLegacyRecordCount > 0 and @outXLedgerRecordCount > 0 )
begin
set @outErrorCode = @BOTH_XLEDGER_AND_LEGACY_RECORDS_EXIST_NO_CAN_DO
end
begin transaction;
if( @theOkToContinue = 1 )
begin
if( @inCommitTransaction = 0 )
begin
if( isnull( @inClearOutOldAccountingSystemRecordsOption, 1 ) != 0 )
begin
set @theOkToContinue = 0
end
end
else
begin
if( ( @outErrorCode != @NO_ERROR and @inOriginalErrorCode != @outErrorCode ) or
( @outErrorCode != @NO_ERROR and isnull( @inClearOutOldAccountingSystemRecordsOption, 0 ) = 0 ) )
begin
set @theOkToContinue = 0
set @outErrorCode = @INVALID_ClearOutOldAccountingSystemRecordsOption_FOR_CURRENT_STATE
end
end
end
if( @theOkToContinue = 1 )
begin
if( @outErrorCode != @NO_ERROR )
begin
if( @outErrorCode = @LEGACY_RECORDS_EXIST_WITH_XLEDGER_SYSTEM )
begin
if( @inClearOutOldAccountingSystemRecordsOption = 1 )
begin
set @theOkToDeleteLegacyRecords = 1
end
if( @inClearOutOldAccountingSystemRecordsOption = 2 or @inCommitTransaction = 0 )
begin
update ARCCustomerInformation set
AccountingCustomerFid = CustomerInformation.CustomerId
from ARCCustomerInformation
inner join CustomerInformation_Synonym as CustomerInformation on
CustomerInformation.CustomerNumber = ARCCustomerInformation.CustomerNumber
where ARCCustomerInformation.AccountingCustomerFid is null
set @outOldRecordsConverted = @@ROWCOUNT;
set @theOkToDeleteLegacyRecords = 1
end
end
if( @theOkToDeleteLegacyRecords = 1 )
begin
delete
from ARCCustomerInformation
where AccountingCustomerFid is null
set @outOldRecordsDeleted = @@ROWCOUNT;
set @theOkToContinue = 0
if( 0 = ( select count(*) from ARCCustomerInformation where AccountingCustomerFid is null ) )
begin
select
@theOkToContinue = 1,
@theMarkConversionAsCompleted = @inCommitTransaction,
@outErrorCode = case
when @inCommitTransaction = 1 then @NO_ERROR
else @outErrorCode
end
end
end
end
else if( @inClearOutOldAccountingSystemRecordsOption != 0 or @inOriginalErrorCode != @NO_ERROR )
begin
select
@theOkToContinue = 0,
@outErrorCode = @USER_OPTION_DOES_NOT_MATCH_CURRENT_STATE
end
end
if( @theMarkConversionAsCompleted = 1 )
begin
insert into GlobalSystemOption
(
[Name],
[Value]
)
select
[Name] = @GLOBAL_SYSTEM_OPTION_NAME,
[Value] = 'true'
where not exists (select top 1 1 from GlobalSystemOption where [Name] = @GLOBAL_SYSTEM_OPTION_NAME )
set @theRowCount = @@ROWCOUNT
if( @theRowCount = 0 )
begin
update GlobalSystemOption set
[Value] = 'true'
where [Name] = @GLOBAL_SYSTEM_OPTION_NAME
end
set @inCommitTransaction = 1
end
if( @inCommitTransaction = 1 and @outErrorCode = @NO_ERROR )
begin
commit transaction;
end
else
begin
rollback transaction;
end
end;
GO
GRANT EXECUTE ON [dbo].[ARCUpdateCustomerAlertsConvertToXLedger] TO [MssExec]
GO