IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenDeletions]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenDeletions] (
        ID INT IDENTITY(1,1) PRIMARY KEY,
        TableName NVARCHAR(100) NOT NULL,
        RecordID INT NOT NULL,
        StoreID uniqueidentifier NOT NULL,
        DeletedAt DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME()
    );
END
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenUpdates]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenUpdates] (
        ID INT IDENTITY(1,1) PRIMARY KEY,
        TableName NVARCHAR(100) NOT NULL,
        RecordID INT NOT NULL,
        StoreID uniqueidentifier NOT NULL,
        UpdatedAt DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME()
    );
END
GO
-- RavenSync_Batch
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_Batch]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_Batch] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_Batch] UNIQUE ([RecordID], [StoreID])
    );
END
GO
-- تعریف متغیر برای شناسه فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای Batch
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Batch_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_Batch_Delete]
    ON [dbo].[Batch]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''Batch'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO
-- تعریف متغیر برای شناسه فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای Batch
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Batch_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_Batch_Update]
    ON [dbo].[Batch]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''Batch'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO

-- RavenSync_Customer
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_Customer]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_Customer] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_Customer] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای Customer
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Customer_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_Customer_Delete]
    ON [dbo].[Customer]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''Customer'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO
-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;


-- تریگر UPDATE برای Customer
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Customer_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_Customer_Update]
    ON [dbo].[Customer]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''Customer'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO

-- RavenSync_CustomerAccountPayment
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_CustomerAccountPayment]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_CustomerAccountPayment] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_CustomerAccountPayment] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای CustomerAccountPayment
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_CustomerAccountPayment_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_CustomerAccountPayment_Delete]
    ON [dbo].[CustomerAccountPayment]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''CustomerAccountPayment'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;


-- تریگر UPDATE برای CustomerAccountPayment
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_CustomerAccountPayment_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_CustomerAccountPayment_Update]
    ON [dbo].[CustomerAccountPayment]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''CustomerAccountPayment'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_DropPayout
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_DropPayout]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_DropPayout] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_DropPayout] UNIQUE ([RecordID], [StoreID])
    );
END
GO
-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای DropPayout
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_DropPayout_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_DropPayout_Delete]
    ON [dbo].[DropPayout]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''DropPayout'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO
-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای DropPayout
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_DropPayout_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_DropPayout_Update]
    ON [dbo].[DropPayout]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''DropPayout'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_Employee
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_Employee]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_Employee] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_Employee] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای Employee
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Employee_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_Employee_Delete]
    ON [dbo].[Employee]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''Employee'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO
-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای Employee
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Employee_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_Employee_Update]
    ON [dbo].[Employee]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''Employee'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_InventoryTransferLog
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_InventoryTransferLog]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_InventoryTransferLog] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_InventoryTransferLog] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای InventoryTransferLog
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_InventoryTransferLog_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_InventoryTransferLog_Delete]
    ON [dbo].[InventoryTransferLog]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''InventoryTransferLog'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO
-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای InventoryTransferLog
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_InventoryTransferLog_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_InventoryTransferLog_Update]
    ON [dbo].[InventoryTransferLog]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''InventoryTransferLog'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO

-- RavenSync_Item
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_Item]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_Item] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_Item] UNIQUE ([RecordID], [StoreID])
    );
END
GO
-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای Item
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Item_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_Item_Delete]
    ON [dbo].[Item]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''Item'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای Item
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Item_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_Item_Update]
    ON [dbo].[Item]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''Item'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_ItemReceipt
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_ItemReceipt]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_ItemReceipt] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_ItemReceipt] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای ItemReceipt
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemReceipt_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_ItemReceipt_Delete]
    ON [dbo].[ItemReceipt]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''ItemReceipt'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای ItemReceipt
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemReceipt_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_ItemReceipt_Update]
    ON [dbo].[ItemReceipt]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''ItemReceipt'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_ItemReceiptEntry
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_ItemReceiptEntry]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_ItemReceiptEntry] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_ItemReceiptEntry] UNIQUE ([RecordID], [StoreID])
    );
END
GO
-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای ItemReceiptEntry
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemReceiptEntry_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_ItemReceiptEntry_Delete]
    ON [dbo].[ItemReceiptEntry]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''ItemReceiptEntry'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای ItemReceiptEntry
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemReceiptEntry_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_ItemReceiptEntry_Update]
    ON [dbo].[ItemReceiptEntry]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''ItemReceiptEntry'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_ItemReceiptEntryDetail
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_ItemReceiptEntryDetail]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_ItemReceiptEntryDetail] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_ItemReceiptEntryDetail] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای ItemReceiptEntryDetail
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemReceiptEntryDetail_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_ItemReceiptEntryDetail_Delete]
    ON [dbo].[ItemReceiptEntryDetail]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''ItemReceiptEntryDetail'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای ItemReceiptEntryDetail
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemReceiptEntryDetail_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_ItemReceiptEntryDetail_Update]
    ON [dbo].[ItemReceiptEntryDetail]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''ItemReceiptEntryDetail'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_ItemRefund
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_ItemRefund]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_ItemRefund] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_ItemRefund] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای ItemRefund
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemRefund_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_ItemRefund_Delete]
    ON [dbo].[ItemRefund]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''ItemRefund'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای ItemRefund
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemRefund_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_ItemRefund_Update]
    ON [dbo].[ItemRefund]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''ItemRefund'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_ItemRefundEntry
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_ItemRefundEntry]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_ItemRefundEntry] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_ItemRefundEntry] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای ItemRefundEntry
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemRefundEntry_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_ItemRefundEntry_Delete]
    ON [dbo].[ItemRefundEntry]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''ItemRefundEntry'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای ItemRefundEntry
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemRefundEntry_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_ItemRefundEntry_Update]
    ON [dbo].[ItemRefundEntry]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''ItemRefundEntry'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_ItemRefundEntryDetail
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_ItemRefundEntryDetail]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_ItemRefundEntryDetail] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_ItemRefundEntryDetail] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای ItemRefundEntryDetail
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemRefundEntryDetail_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_ItemRefundEntryDetail_Delete]
    ON [dbo].[ItemRefundEntryDetail]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''ItemRefundEntryDetail'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای ItemRefundEntryDetail
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemRefundEntryDetail_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_ItemRefundEntryDetail_Update]
    ON [dbo].[ItemRefundEntryDetail]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''ItemRefundEntryDetail'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO

-- RavenSync_ItemUsage
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_ItemUsage]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_ItemUsage] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_ItemUsage] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای ItemUsage
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemUsage_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_ItemUsage_Delete]
    ON [dbo].[ItemUsage]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''ItemUsage'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای ItemUsage
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemUsage_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_ItemUsage_Update]
    ON [dbo].[ItemUsage]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''ItemUsage'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_ItemUsageEntry
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_ItemUsageEntry]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_ItemUsageEntry] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_ItemUsageEntry] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای ItemUsageEntry
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemUsageEntry_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_ItemUsageEntry_Delete]
    ON [dbo].[ItemUsageEntry]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''ItemUsageEntry'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای ItemUsageEntry
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemUsageEntry_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_ItemUsageEntry_Update]
    ON [dbo].[ItemUsageEntry]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''ItemUsageEntry'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_ItemWastage
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_ItemWastage]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_ItemWastage] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_ItemWastage] UNIQUE ([RecordID], [StoreID])
    );
END
GO
-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای ItemWastage
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemWastage_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_ItemWastage_Delete]
    ON [dbo].[ItemWastage]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''ItemWastage'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای ItemWastage
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemWastage_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_ItemWastage_Update]
    ON [dbo].[ItemWastage]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''ItemWastage'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_ItemWastageEntry
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_ItemWastageEntry]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_ItemWastageEntry] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_ItemWastageEntry] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای ItemWastageEntry
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemWastageEntry_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_ItemWastageEntry_Delete]
    ON [dbo].[ItemWastageEntry]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''ItemWastageEntry'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای ItemWastageEntry
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_ItemWastageEntry_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_ItemWastageEntry_Update]
    ON [dbo].[ItemWastageEntry]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''ItemWastageEntry'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_Location
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_Location]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_Location] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_Location] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای Location
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Location_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_Location_Delete]
    ON [dbo].[Location]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''Location'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای Location
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Location_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_Location_Update]
    ON [dbo].[Location]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''Location'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_LocationItem
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_LocationItem]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_LocationItem] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_LocationItem] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای LocationItem
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_LocationItem_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_LocationItem_Delete]
    ON [dbo].[LocationItem]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''LocationItem'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای LocationItem
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_LocationItem_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_LocationItem_Update]
    ON [dbo].[LocationItem]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''LocationItem'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO

-- RavenSync_Tender
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_Tender]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_Tender] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_Tender] UNIQUE ([RecordID], [StoreID])
    );
END
GO
-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای Tender
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Tender_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_Tender_Delete]
    ON [dbo].[Tender]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''Tender'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای Tender
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Tender_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_Tender_Update]
    ON [dbo].[Tender]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''Tender'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO


-- RavenSync_TenderEntry
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_TenderEntry]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_TenderEntry] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_TenderEntry] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر DELETE برای TenderEntry
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_TenderEntry_Delete')
BEGIN
    DECLARE @sqlDelete NVARCHAR(MAX);
    SET @sqlDelete = '
    CREATE TRIGGER [tr_TenderEntry_Delete]
    ON [dbo].[TenderEntry]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''TenderEntry'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sqlDelete);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- تریگر UPDATE برای TenderEntry
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_TenderEntry_Update')
BEGIN
    DECLARE @sqlUpdate NVARCHAR(MAX);
    SET @sqlUpdate = '
    CREATE TRIGGER [tr_TenderEntry_Update]
    ON [dbo].[TenderEntry]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''TenderEntry'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sqlUpdate);
END
GO

-- RavenSync_TenderTotals
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_TenderTotals]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_TenderTotals] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_TenderTotals] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- Trigger for DELETE on TenderTotals
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_TenderTotals_Delete')
BEGIN
    DECLARE @sql NVARCHAR(MAX);
    SET @sql = '
    CREATE TRIGGER [tr_TenderTotals_Delete]
    ON [dbo].[TenderTotals]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''TenderTotals'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sql);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- Trigger for UPDATE on TenderTotals
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_TenderTotals_Update')
BEGIN
    DECLARE @sql NVARCHAR(MAX);
    SET @sql = '
    CREATE TRIGGER [tr_TenderTotals_Update]
    ON [dbo].[TenderTotals]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''TenderTotals'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sql);
END
GO

-- RavenSync_Transaction
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_Transaction]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_Transaction] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_Transaction] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- Trigger for DELETE on Transaction
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Transaction_Delete')
BEGIN
    DECLARE @sql NVARCHAR(MAX);
    SET @sql = '
    CREATE TRIGGER [tr_Transaction_Delete]
    ON [dbo].[Transaction]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''Transaction'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sql);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- Trigger for UPDATE on Transaction
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_Transaction_Update')
BEGIN
    DECLARE @sql NVARCHAR(MAX);
    SET @sql = '
    CREATE TRIGGER [tr_Transaction_Update]
    ON [dbo].[Transaction]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''Transaction'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sql);
END
GO

-- RavenSync_TransactionEntry
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_TransactionEntry]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_TransactionEntry] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_TransactionEntry] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- Trigger for DELETE on TransactionEntry
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_TransactionEntry_Delete')
BEGIN
    DECLARE @sql NVARCHAR(MAX);
    SET @sql = '
    CREATE TRIGGER [tr_TransactionEntry_Delete]
    ON [dbo].[TransactionEntry]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''TransactionEntry'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sql);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- Trigger for UPDATE on TransactionEntry
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_TransactionEntry_Update')
BEGIN
    DECLARE @sql NVARCHAR(MAX);
    SET @sql = '
    CREATE TRIGGER [tr_TransactionEntry_Update]
    ON [dbo].[TransactionEntry]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''TransactionEntry'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sql);
END
GO

-- RavenSync_TransactionEntryDetail
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RavenSync_TransactionEntryDetail]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[RavenSync_TransactionEntryDetail] (
        [RecordID] INT PRIMARY KEY,
        [StoreID] uniqueidentifier NOT NULL,
        [LastSyncedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
        [SyncGuid] UNIQUEIDENTIFIER NOT NULL,
        CONSTRAINT [UQ_RavenSync_TransactionEntryDetail] UNIQUE ([RecordID], [StoreID])
    );
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- Trigger for DELETE on TransactionEntryDetail
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_TransactionEntryDetail_Delete')
BEGIN
    DECLARE @sql NVARCHAR(MAX);
    SET @sql = '
    CREATE TRIGGER [tr_TransactionEntryDetail_Delete]
    ON [dbo].[TransactionEntryDetail]
    AFTER DELETE
    AS
    BEGIN
        INSERT INTO RavenDeletions (TableName, RecordID, StoreID, DeletedAt)
        SELECT ''TransactionEntryDetail'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM deleted;
    END';
    EXEC(@sql);
END
GO

-- دریافت SyncGUID فروشگاه محلی
DECLARE @LocalStoreID UNIQUEIDENTIFIER;
SELECT @LocalStoreID = SyncGUID FROM dbo.Store WHERE IsLocalStore = 1;

-- Trigger for UPDATE on TransactionEntryDetail
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'tr_TransactionEntryDetail_Update')
BEGIN
    DECLARE @sql NVARCHAR(MAX);
    SET @sql = '
    CREATE TRIGGER [tr_TransactionEntryDetail_Update]
    ON [dbo].[TransactionEntryDetail]
    AFTER UPDATE
    AS
    BEGIN
        INSERT INTO RavenUpdates (TableName, RecordID, StoreID, UpdatedAt)
        SELECT ''TransactionEntryDetail'', ID, ''' + CAST(@LocalStoreID AS NVARCHAR(36)) + ''', SYSUTCDATETIME() FROM inserted;
    END';
    EXEC(@sql);
END
GO
