-- For clients who applied 303 temporal taxes in each leaf org: take the 
-- data from one of the orgs (the one where the temporal taxes dataset was 
-- first applied) and move them to the specified organization
UPDATE C_TAX
SET
	AD_ORG_ID = '0'
WHERE
	C_TAX_ID IN (
		'007BB1247DAA4CD49761F482CCA70861',
		'E6482967E4AF4CD9A625A8306557F9DF',
		'F1BD529ED960476A9CDFEC99A8077521',
		'1906AFB5C7B3437693BC792CBF1F428D',
		'2E309A10D0C74B668035284212C48D5C',
		'33F912834A41422688EBD9EBC7C168AA',
		'3CE11BC0D1CE4A5E9DE0DB95670C4B03',
		'4DC2BB94C269456881810CF519561BBE',
		'5AFAF9EC75564C0484D2AE79A77F84A9',
		'6744609CE1444521A5C0B64D501CEB1A',
		'7313287CA82F4325B7E4207BB71A09C3',
		'80924F0146E14817B50B8CC5898094CB',
		'82B7CE1191374113B0E017EC6843345D',
		'84AE16DD3F144D509BEA058578163CC7',
		'A00A9420CB3B45A589226EC7EFE9EAEC',
		'8F51C3C1057C4115A8F1E23D5DBADB9B',
		'96662540AFE24E6E85BD26C7426FF5E1',
		'5244ED21C6FF404E8E78D3AA33E6E4CF',
		'BA759F0014374A45837CB93840397C86',
		'DB431BF92CA147979D445E8799137866',
		'ED590CC9194D40E482606B9DFB0B6AB6',
		'F09890D302E848D2AAF6CA35CE7D2911',
		'F3DF38CF66764A5FBE57EA475ECF6AF1',
		'F736152B945E4313A50B41D1CE2AEAC8',
		'FB25E96739414257A4A7D3BFEBE4D203',
		'FB5219E81C684E16BE322E65FA0A1A1F',
		'FB776EDC73CC4DC2A27B6AF2B57741F0'
	)
	AND NOT EXISTS (
		SELECT
			1
		FROM
			C_TAX T2
		WHERE
			T2.PARENT_TAX_ID = C_TAX.PARENT_TAX_ID
			AND T2.NAME = C_TAX.NAME
			AND T2.AD_CLIENT_ID = C_TAX.AD_CLIENT_ID
			AND T2.AD_ORG_ID = '0'
	);

-- Fix for taxes dataset error with IDs. Change taxes generic IDs from OB ones to ET
UPDATE AD_REF_DATA_LOADED
SET
	GENERIC_ID = CASE GENERIC_ID
		WHEN '0D1160D33D514C7789E2893B9CEA90F0' THEN '3654D4E296CD441A9E74DD337947AEF5'
		WHEN '7D4DE4D7B066426F948B5F4D1CE0B215' THEN 'B7BDDC4E3ED64A2BB7FE4A097588ADB6'
		WHEN '5244ED21C6FF404E8E78D3AA33E6E4CF' THEN 'B1E24C97E38644C1B3728F59B231B5F0'
		WHEN 'A00A9420CB3B45A589226EC7EFE9EAEC' THEN '8E9CDFFDF29A463994FA5D5B80652D1D'
		WHEN 'E6482967E4AF4CD9A625A8306557F9DF' THEN '08F38FECA207422E9E95AE69A7B811A4'
		WHEN 'F1BD529ED960476A9CDFEC99A8077521' THEN '18C3B1128B464A30B58491E97971608C'
	END
WHERE
	GENERIC_ID IN (
		'0D1160D33D514C7789E2893B9CEA90F0',
		'7D4DE4D7B066426F948B5F4D1CE0B215',
		'5244ED21C6FF404E8E78D3AA33E6E4CF',
		'A00A9420CB3B45A589226EC7EFE9EAEC',
		'E6482967E4AF4CD9A625A8306557F9DF',
		'F1BD529ED960476A9CDFEC99A8077521'
	)
	AND SPECIFIC_ID = GENERIC_ID;

-- Also change IDs for temporal taxes whose IDs did not change from OB to ET,
-- for them to not interfere when applying other fixes. This change will be 
-- made only for taxes not in * organization
UPDATE AD_REF_DATA_LOADED
SET
	GENERIC_ID = GET_UUID ()
WHERE
	GENERIC_ID IN (
		'F736152B945E4313A50B41D1CE2AEAC8',
		'8F51C3C1057C4115A8F1E23D5DBADB9B',
		'3CE11BC0D1CE4A5E9DE0DB95670C4B03',
		'80924F0146E14817B50B8CC5898094CB',
		'84AE16DD3F144D509BEA058578163CC7',
		'FB25E96739414257A4A7D3BFEBE4D203',
		'FB5219E81C684E16BE322E65FA0A1A1F',
		'7313287CA82F4325B7E4207BB71A09C3',
		'ED590CC9194D40E482606B9DFB0B6AB6',
		'82B7CE1191374113B0E017EC6843345D',
		'1906AFB5C7B3437693BC792CBF1F428D'
	)
	AND GENERIC_ID <> SPECIFIC_ID;