SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/*
Rollback della patch prenotazione plist.

Ripristina dal backup salvato da:
- apply_plist_reservation_patch.sql

Oggetti ripristinati:
- dbo.XMag_ViewPackingList
- dbo.sp_xExePackingListPallet
- dbo.sp_xExePackingListPalletPrenota
- dbo.sp_ControllaPrenotazionePackingListPalletNew

La tabella dbo.PickingListReservation NON viene rimossa:
- può restare nel DB
- il codice legacy non la usa
*/

DECLARE @BackupTag varchar(64) = 'plist_reservation_fix_alpha2';
DECLARE @Sql nvarchar(max);

IF OBJECT_ID(N'dbo.WarehouseObjectBackup', N'U') IS NULL
BEGIN
    RAISERROR('dbo.WarehouseObjectBackup non esiste. Impossibile fare rollback.', 16, 1);
    RETURN;
END;

IF NOT EXISTS (
    SELECT 1
    FROM dbo.WarehouseObjectBackup
    WHERE BackupTag = @BackupTag
)
BEGIN
    RAISERROR('Backup tag %s non trovato. Impossibile fare rollback.', 16, 1, @BackupTag);
    RETURN;
END;

SELECT @Sql = Definition
FROM dbo.WarehouseObjectBackup
WHERE BackupTag = @BackupTag
  AND ObjectName = N'dbo.XMag_ViewPackingList';

IF @Sql IS NOT NULL EXEC(@Sql);

SELECT @Sql = Definition
FROM dbo.WarehouseObjectBackup
WHERE BackupTag = @BackupTag
  AND ObjectName = N'dbo.sp_xExePackingListPallet';

IF @Sql IS NOT NULL EXEC(@Sql);

SELECT @Sql = Definition
FROM dbo.WarehouseObjectBackup
WHERE BackupTag = @BackupTag
  AND ObjectName = N'dbo.sp_xExePackingListPalletPrenota';

IF @Sql IS NOT NULL EXEC(@Sql);

SELECT @Sql = Definition
FROM dbo.WarehouseObjectBackup
WHERE BackupTag = @BackupTag
  AND ObjectName = N'dbo.sp_ControllaPrenotazionePackingListPalletNew';

IF @Sql IS NOT NULL EXEC(@Sql);

