/*============================================================================= 
TPDT-283

Update central system_site values for a specific OU

Parameters
----------------------
In script parameters updated from Powershell are present:
    @ou_id  The id ofthe OU we need to update. This id is fetched from the local machine being migrated.
    @fqdn   The FQDN of the machine being migrated to Azure. This is fetched from a DNS request, not built.
    @alias  The alias of the machine being migrated to Azure. This is logically built from the FQDN

Context
----------------------
This script is called from a powershell in the task sequence during the migration to Azure.
The excution context is the local pharmacy being moved to Azure, but updating the central via the linked server arizonaCash.
It ensure that the system_site reflect the new hostname of the machine, so that labels are delivered correctly.

Creation : 04.03.2024 / TSC
Modifications:
    07.03.2024  TSC Rewrote to connect to the central through the pharmacy linked server ArizonaCash
                    The values in the local pharmacy are updated as well
=============================================================================*/ 
USE [master];

DECLARE @ou_id INT;
DECLARE @fqdn VARCHAR(100) = '$fqdn';
DECLARE @alias VARCHAR(100) = '$alias';

EXECUTE [Arizona].[dbo].[sp_bmc_Bmc_Applic_Default] 
    @in_job_type = 3,
    @in_param_int_1 = NULL, /* Company */
    @in_param_int_2 = NULL, /* Subsidiary */
    @in_param_varchar_1 = 'cvCurrentOrganizationalUnit',
    @out_default_value = @ou_id OUTPUT,
    @out_param_int_1 = NULL;

--central
UPDATE ss
    SET [ss].[SS_server_name] = @alias
    , [ss].[SS_IP_server_address] = @alias
    , [ss].[SS_domain_name] = @fqdn
FROM [arizonaCash].[arizona].[dbo].[Organizational_unit] ou
    JOIN [arizonaCash].[arizona].[dbo].[System_site] ss ON ss.[System_site_ID] = ou.[OU_system_site]
WHERE ou.[Organizational_unit_ID] = @ou_id;

UPDATE ssi
    SET [ssi].[SSSI_SQL_instance_name] = @alias+'\APSSQL'
FROM [arizonaCash].[arizona].[dbo].[Organizational_unit] ou
    JOIN [arizonaCash].[arizona].[dbo].[System_site] ss ON ss.[System_site_ID] = ou.[OU_system_site]
    JOIN [arizonaCash].[arizona].[dbo].[System_site_SQL_instance] ssi ON ssi.[SSSI_system_site] = ss.[System_site_ID]
WHERE ou.[Organizational_unit_ID] = @ou_id;

--local
UPDATE ss
    SET [ss].[SS_server_name] = @alias
    , [ss].[SS_IP_server_address] = @alias
    , [ss].[SS_domain_name] = @fqdn
FROM [arizona].[dbo].[Organizational_unit] ou
    JOIN [arizona].[dbo].[System_site] ss ON ss.[System_site_ID] = ou.[OU_system_site]
WHERE ou.[Organizational_unit_ID] = @ou_id;

UPDATE ssi
    SET [ssi].[SSSI_SQL_instance_name] = @alias+'\APSSQL'
FROM [arizona].[dbo].[Organizational_unit] ou
    JOIN [arizona].[dbo].[System_site] ss ON ss.[System_site_ID] = ou.[OU_system_site]
    JOIN [arizona].[dbo].[System_site_SQL_instance] ssi ON ssi.[SSSI_system_site] = ss.[System_site_ID]
WHERE ou.[Organizational_unit_ID] = @ou_id;
