/*
   Thursday, July 10, 202511:57:04 ق.ظ
   User: 
   Server: TEST2\SQL19
   Database: Ava 1404
   Application: 
*/

/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccChequeOutItemInfo
	DROP CONSTRAINT FK_AccChequeOutItemInfo_AccDocItem
GO
ALTER TABLE dbo.AccDocItem SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
select Has_Perms_By_Name(N'dbo.AccDocItem', 'Object', 'ALTER') as ALT_Per, Has_Perms_By_Name(N'dbo.AccDocItem', 'Object', 'VIEW DEFINITION') as View_def_Per, Has_Perms_By_Name(N'dbo.AccDocItem', 'Object', 'CONTROL') as Contr_Per BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccChequeOutItemInfo
	DROP CONSTRAINT FK_AccChequeOutItemInfo_AccChequeInfo
GO
ALTER TABLE dbo.AccChequeInfo SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
select Has_Perms_By_Name(N'dbo.AccChequeInfo', 'Object', 'ALTER') as ALT_Per, Has_Perms_By_Name(N'dbo.AccChequeInfo', 'Object', 'VIEW DEFINITION') as View_def_Per, Has_Perms_By_Name(N'dbo.AccChequeInfo', 'Object', 'CONTROL') as Contr_Per BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccChequeOutItemInfo
	DROP CONSTRAINT FK_AccChequeItemInfo_AccChequeOut
GO
ALTER TABLE dbo.AccChequeOut SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
select Has_Perms_By_Name(N'dbo.AccChequeOut', 'Object', 'ALTER') as ALT_Per, Has_Perms_By_Name(N'dbo.AccChequeOut', 'Object', 'VIEW DEFINITION') as View_def_Per, Has_Perms_By_Name(N'dbo.AccChequeOut', 'Object', 'CONTROL') as Contr_Per BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccChequeOutItemInfo
	DROP CONSTRAINT DF_AccChequeOutItemInfo_Cashed
GO
ALTER TABLE dbo.AccChequeOutItemInfo
	DROP CONSTRAINT DF_AccChequeItemInfo_UpdateDate
GO
ALTER TABLE dbo.AccChequeOutItemInfo
	DROP CONSTRAINT DF_AccChequeItemInfo_Updater
GO
CREATE TABLE dbo.Tmp_AccChequeOutItemInfo
	(
	ID bigint NOT NULL IDENTITY (1, 1),
	ChequeOutID bigint NOT NULL,
	ChequeInfoID bigint NOT NULL,
	TotalID bigint NOT NULL,
	ControlID bigint NOT NULL,
	DetailID1 bigint NOT NULL,
	DetailID2 bigint NOT NULL,
	ChequeNo bigint NOT NULL,
	ChequeDate datetime NOT NULL,
	ChequeValue decimal(28, 5) NOT NULL,
	CashDocItemID bigint NULL,
	CashDocItemRelationID int NULL,
	Status int NOT NULL,
	CashDate datetime NULL,
	UpdateDate datetime NOT NULL,
	Updater bigint NOT NULL,
	PrintCount int NULL,
	Flags int NULL,
	ReturnDate datetime NULL,
	ReturnDocItemID bigint NULL,
	ReturnItemRelationID int NULL,
	ForType int NULL,
	ForID bigint NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_AccChequeOutItemInfo SET (LOCK_ESCALATION = TABLE)
GO
ALTER TABLE dbo.Tmp_AccChequeOutItemInfo ADD CONSTRAINT
	DF_AccChequeOutItemInfo_Cashed DEFAULT ((0)) FOR Status
GO
ALTER TABLE dbo.Tmp_AccChequeOutItemInfo ADD CONSTRAINT
	DF_AccChequeItemInfo_UpdateDate DEFAULT (getdate()) FOR UpdateDate
GO
ALTER TABLE dbo.Tmp_AccChequeOutItemInfo ADD CONSTRAINT
	DF_AccChequeItemInfo_Updater DEFAULT ((0)) FOR Updater
GO
SET IDENTITY_INSERT dbo.Tmp_AccChequeOutItemInfo ON
GO
IF EXISTS(SELECT * FROM dbo.AccChequeOutItemInfo)
	 EXEC('INSERT INTO dbo.Tmp_AccChequeOutItemInfo (ID, ChequeOutID, ChequeInfoID, TotalID, ControlID, DetailID1, DetailID2, ChequeNo, ChequeDate, ChequeValue, CashDocItemID, CashDocItemRelationID, Status, CashDate, UpdateDate, Updater, PrintCount, Flags, ReturnDate, ReturnDocItemID, ReturnItemRelationID, ForType, ForID)
		SELECT ID, ChequeOutID, ChequeInfoID, TotalID, ControlID, DetailID1, DetailID2, ChequeNo, ChequeDate, ChequeValue, CashDocItemID, CashDocItemRelationID, Status, CashDate, UpdateDate, Updater, PrintCount, Flags, ReturnDate, ReturnDocItemID, ReturnItemRelationID, ForType, ForID FROM dbo.AccChequeOutItemInfo WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_AccChequeOutItemInfo OFF
GO
DROP TABLE dbo.AccChequeOutItemInfo
GO
EXECUTE sp_rename N'dbo.Tmp_AccChequeOutItemInfo', N'AccChequeOutItemInfo', 'OBJECT' 
GO
ALTER TABLE dbo.AccChequeOutItemInfo ADD CONSTRAINT
	PK_AccChequeItemInfo PRIMARY KEY CLUSTERED 
	(
	ID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
CREATE NONCLUSTERED INDEX IX_AccChequeOutItemInfo_ChequeID ON dbo.AccChequeOutItemInfo
	(
	ChequeOutID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IX_AccChequeOutItemInfo_CashID ON dbo.AccChequeOutItemInfo
	(
	CashDocItemID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IX_AccChequeOutItemInfo_ReturnID ON dbo.AccChequeOutItemInfo
	(
	ReturnDocItemID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE dbo.AccChequeOutItemInfo ADD CONSTRAINT
	FK_AccChequeItemInfo_AccChequeOut FOREIGN KEY
	(
	ChequeOutID
	) REFERENCES dbo.AccChequeOut
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccChequeOutItemInfo ADD CONSTRAINT
	FK_AccChequeOutItemInfo_AccChequeInfo FOREIGN KEY
	(
	ChequeInfoID
	) REFERENCES dbo.AccChequeInfo
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccChequeOutItemInfo ADD CONSTRAINT
	FK_AccChequeOutItemInfo_AccDocItem FOREIGN KEY
	(
	CashDocItemID
	) REFERENCES dbo.AccDocItem
	(
	ID
	) ON UPDATE  CASCADE 
	 ON DELETE  CASCADE 
	
GO
Create  TRIGGER AccChequeOutItemInfo_Delete
ON dbo.AccChequeOutItemInfo For DELETE 
	AS
		insert AccChequeOutItemInfoTemp select * from  deleted
      --update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccChequeOutLastUpdate'
	return
GO
create  TRIGGER AccChequeOutItemInfo_LastUpdate
ON dbo.AccChequeOutItemInfo FOR UPDATE 
	AS
	if not (UPDATE(UpdateDate))
		update AccChequeOutItemInfo set UpdateDate=getdate() where id in (select id from inserted)

        --update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccChequeOutLastUpdate'

return
GO
Create  TRIGGER AccChequeOutItemInfo_Insert
ON dbo.AccChequeOutItemInfo FOR Insert
	AS
        --update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccChequeOutLastUpdate'
	return
GO
COMMIT
select Has_Perms_By_Name(N'dbo.AccChequeOutItemInfo', 'Object', 'ALTER') as ALT_Per, Has_Perms_By_Name(N'dbo.AccChequeOutItemInfo', 'Object', 'VIEW DEFINITION') as View_def_Per, Has_Perms_By_Name(N'dbo.AccChequeOutItemInfo', 'Object', 'CONTROL') as Contr_Per 