/****
This SQL script was generated by the Configure Data Synchronization
dialog box. This script contains statements that create the
change-tracking columns, deleted-items table, and triggers on the
server database. These database objects are required by Synchronization
Services to successfully synchronize data between client and server
databases. For more information, see the
‘How to: Configure a Database Server for Synchronization’ topic in Help.
****/


IF @@TRANCOUNT > 0
set ANSI_NULLS ON 
set QUOTED_IDENTIFIER ON 

GO
BEGIN TRANSACTION;


IF @@TRANCOUNT > 0
ALTER TABLE [dbo].[tblContracts] 
ADD [LastEditDate] DateTime NULL CONSTRAINT [DF_tblContracts_LastEditDate] DEFAULT (GETUTCDATE()) WITH VALUES
GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;


IF @@TRANCOUNT > 0
ALTER TABLE [dbo].[tblContracts] 
ADD [CreationDate] DateTime NULL CONSTRAINT [DF_tblContracts_CreationDate] DEFAULT (GETUTCDATE()) WITH VALUES
GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;


IF @@TRANCOUNT > 0
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblContracts_Tombstone]')) 
BEGIN 
CREATE TABLE [dbo].[tblContracts_Tombstone]( 
    [KharidarID] Int NOT NULL,
    [ForooshandehID] Int NOT NULL,
    [MelkID] Int NOT NULL,
    [MoamelehTypeID] Int NOT NULL,
    [datOdMoaMeleh] NVarChar(10) NOT NULL,
    [DeletionDate] DateTime NULL
)END 

GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;


IF @@TRANCOUNT > 0
ALTER TABLE [dbo].[tblContracts_Tombstone] ADD CONSTRAINT [PKDEL_tblContracts_Tombstone_KharidarIDForooshandehIDMelkIDMoamelehTypeIDdatOdMoaMeleh]
   PRIMARY KEY CLUSTERED
    ([KharidarID], [ForooshandehID], [MelkID], [MoamelehTypeID], [datOdMoaMeleh])
GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;


IF @@TRANCOUNT > 0
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblContracts_DeletionTrigger]') AND type = 'TR') 
   DROP TRIGGER [dbo].[tblContracts_DeletionTrigger] 

GO
CREATE TRIGGER [dbo].[tblContracts_DeletionTrigger] 
    ON [tblContracts] 
    AFTER DELETE 
AS 
SET NOCOUNT ON 
UPDATE [dbo].[tblContracts_Tombstone] 
    SET [DeletionDate] = GETUTCDATE() 
    FROM deleted 
    WHERE deleted.[KharidarID] = [tblContracts_Tombstone].[KharidarID] 
    AND deleted.[ForooshandehID] = [tblContracts_Tombstone].[ForooshandehID] 
    AND deleted.[MelkID] = [tblContracts_Tombstone].[MelkID] 
    AND deleted.[MoamelehTypeID] = [tblContracts_Tombstone].[MoamelehTypeID] 
    AND deleted.[datOdMoaMeleh] = [tblContracts_Tombstone].[datOdMoaMeleh] 
IF @@ROWCOUNT = 0 
BEGIN 
    INSERT INTO [dbo].[tblContracts_Tombstone] 
    ([KharidarID], [ForooshandehID], [MelkID], [MoamelehTypeID], [datOdMoaMeleh], DeletionDate)
    SELECT [KharidarID], [ForooshandehID], [MelkID], [MoamelehTypeID], [datOdMoaMeleh], GETUTCDATE()
    FROM deleted 
END 

GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;


IF @@TRANCOUNT > 0
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblContracts_UpdateTrigger]') AND type = 'TR') 
   DROP TRIGGER [dbo].[tblContracts_UpdateTrigger] 

GO
CREATE TRIGGER [dbo].[tblContracts_UpdateTrigger] 
    ON [dbo].[tblContracts] 
    AFTER UPDATE 
AS 
BEGIN 
    SET NOCOUNT ON 
    UPDATE [dbo].[tblContracts] 
    SET [LastEditDate] = GETUTCDATE() 
    FROM inserted 
    WHERE inserted.[KharidarID] = [tblContracts].[KharidarID] 
    AND inserted.[ForooshandehID] = [tblContracts].[ForooshandehID] 
    AND inserted.[MelkID] = [tblContracts].[MelkID] 
    AND inserted.[MoamelehTypeID] = [tblContracts].[MoamelehTypeID] 
    AND inserted.[datOdMoaMeleh] = [tblContracts].[datOdMoaMeleh] 
END;
GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;


IF @@TRANCOUNT > 0
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblContracts_InsertTrigger]') AND type = 'TR') 
   DROP TRIGGER [dbo].[tblContracts_InsertTrigger] 

GO
CREATE TRIGGER [dbo].[tblContracts_InsertTrigger] 
    ON [dbo].[tblContracts] 
    AFTER INSERT 
AS 
BEGIN 
    SET NOCOUNT ON 
    UPDATE [dbo].[tblContracts] 
    SET [CreationDate] = GETUTCDATE() 
    FROM inserted 
    WHERE inserted.[KharidarID] = [tblContracts].[KharidarID] 
    AND inserted.[ForooshandehID] = [tblContracts].[ForooshandehID] 
    AND inserted.[MelkID] = [tblContracts].[MelkID] 
    AND inserted.[MoamelehTypeID] = [tblContracts].[MoamelehTypeID] 
    AND inserted.[datOdMoaMeleh] = [tblContracts].[datOdMoaMeleh] 
END;
GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;
COMMIT TRANSACTION;
