/*============================================================================= 

Update the ftp password in system_site_transfert table on the central from a pharmacy, for this pharmacy

Parameters
----------------------


Context
----------------------
This script is run in a pharmacy, during the preparation / migration of the pharmacy

Creation : 07.03.2024 / TSC
Modifications:

=============================================================================*/
IF EXISTS ( SELECT 1
              FROM [ARIZONACASH].[Arizona].[dbo].[System_site] SS
              JOIN [ARIZONACASH].[Arizona].[dbo].[System_site_transfer] SST
                ON [SST].[SST_dest_system_site] = [SS].[System_site_ID]
             WHERE [SS].[SS_server_name] = '$fqdn'
               AND [SST].[SST_bmc_task]  = 109)
BEGIN

  UPDATE [ARIZONACASH].[Arizona].[dbo].[System_site_transfer]
     SET [SST_connection_info] = '<ConnectionInfos><Ftp username="centralinfra\svc-APH-trans" pwd="$ftppass"/></ConnectionInfos>'
    FROM [ARIZONACASH].[Arizona].[dbo].[System_site] SS
    JOIN [ARIZONACASH].[Arizona].[dbo].[System_site_transfer] SST
      ON [SST].[SST_dest_system_site] = [SS].[System_site_ID]
   WHERE [SS].[SS_server_name] = '$fqdn';

END;
ELSE
BEGIN

  IF EXISTS ( SELECT 1
                FROM [ARIZONACASH].Arizona.[dbo].[System_site]
               WHERE [SS_server_name] = '$fqdn')
  BEGIN

    UPDATE [ARIZONACASH].Arizona.[dbo].[Bmc_ID_counter]
       SET [BMCID_last_number] = ( SELECT [BMCID_last_number] + 1
                                     FROM [ARIZONACASH].Arizona.[dbo].[Bmc_ID_counter]
                                    WHERE [BMCID_key] = 'system_site_transfer')
      FROM [ARIZONACASH].Arizona.[dbo].[Bmc_ID_counter]
     WHERE [BMCID_key] = 'system_site_transfer';

    INSERT INTO [ARIZONACASH].[Arizona].[dbo].[System_site_transfer] ([System_site_transfer_ID],
                                                                      [SST_source_system_site],
                                                                      [SST_dest_system_site],
                                                                      [SST_bmc_task],
                                                                      [SST_transfer_type],
                                                                      [SST_VGUID],
                                                                      [SST_master_ID],
                                                                      [SST_connection_info],
                                                                      [SST_start_date],
                                                                      [SST_end_date])
    VALUES (( SELECT [BMCID_last_number]
                FROM [ARIZONACASH].Arizona.[dbo].[Bmc_ID_counter]
               WHERE [BMCID_key] = 'system_site_transfer'), 102, ( SELECT [System_site_ID]
                                                                     FROM [ARIZONACASH].Arizona.[dbo].[System_site]
                                                                    WHERE [SS_server_name] = '$fqdn'), 109, 1, DEFAULT,
            NULL, '<ConnectionInfos><Ftp username="centralinfra\svc-APH-trans" pwd="$ftppass"/></ConnectionInfos>',
            (SELECT CONVERT(CHAR(10), GETDATE(), 126)), NULL);

  END;
END;
