/* 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.AccControl
	DROP CONSTRAINT DF_AccControl_UpdateDate
GO
ALTER TABLE dbo.AccControl
	DROP CONSTRAINT DF_AccControl_Updator
GO
ALTER TABLE dbo.AccControl
	DROP CONSTRAINT DF_AccControl_Active
GO
CREATE TABLE dbo.Tmp_AccControl
	(
	ID bigint NOT NULL IDENTITY (1, 1),
	Code nvarchar(20) NOT NULL,
	Title nvarchar(100) NOT NULL,
	Type int NOT NULL,
	CreditType int NOT NULL,
	Description nvarchar(2000) NULL,
	Credit decimal(19, 2) NULL,
	Debit decimal(19, 2) NULL,
	UpdateDate datetime NOT NULL,
	Updater bigint NULL,
	Active bit NULL,
	AttachPath nvarchar(1000) NULL,
	TitleNoSpace nvarchar(500) NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_AccControl ADD CONSTRAINT
	DF_AccControl_UpdateDate DEFAULT (getdate()) FOR UpdateDate
GO
ALTER TABLE dbo.Tmp_AccControl ADD CONSTRAINT
	DF_AccControl_Updator DEFAULT ((0)) FOR Updater
GO
ALTER TABLE dbo.Tmp_AccControl ADD CONSTRAINT
	DF_AccControl_Active DEFAULT ((1)) FOR Active
GO
SET IDENTITY_INSERT dbo.Tmp_AccControl ON
GO
IF EXISTS(SELECT * FROM dbo.AccControl)
	 EXEC('INSERT INTO dbo.Tmp_AccControl (ID, Code, Title, Type, CreditType, Description, Credit, Debit, UpdateDate, Updater, Active, AttachPath, TitleNoSpace)
		SELECT ID, CONVERT(nvarchar(20), Code), Title, Type, CreditType, Description, Credit, Debit, UpdateDate, Updater, Active, AttachPath, TitleNoSpace FROM dbo.AccControl WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_AccControl OFF
GO
ALTER TABLE dbo.AccControlDetail
	DROP CONSTRAINT FK_AccControlDetail_AccControl
GO
ALTER TABLE dbo.AccDocItem
	DROP CONSTRAINT FK_AccDocItem_AccControl
GO
ALTER TABLE dbo.AccTotalControl
	DROP CONSTRAINT FK_AccTotalControl_AccControl
GO
ALTER TABLE dbo.AccWarehouseInfo
	DROP CONSTRAINT FK_AccWarehouseInfo_AccControl
GO
ALTER TABLE dbo.AccPersonInfo
	DROP CONSTRAINT FK_AccPersonInfo_AccControl
GO
DROP TABLE dbo.AccControl
GO
EXECUTE sp_rename N'dbo.Tmp_AccControl', N'AccControl', 'OBJECT' 
GO
ALTER TABLE dbo.AccControl ADD CONSTRAINT
	PK_AccControlID PRIMARY KEY CLUSTERED 
	(
	ID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
ALTER TABLE dbo.AccControl ADD CONSTRAINT
	IX_AccControlTitle UNIQUE NONCLUSTERED 
	(
	Title
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
CREATE TRIGGER AccControl_Delete
ON dbo.AccControl For DELETE 
	AS
		insert into dbo.AccControlTemp select * from deleted
		update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccControlLastUpdate'
	return
GO
CREATE TRIGGER AccControl_Insert
ON dbo.AccControl FOR Insert
	AS
	update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccControlLastUpdate'
	return
GO
CREATE TRIGGER AccControl_LastUpdate
ON dbo.AccControl FOR UPDATE 
	AS
	if not (UPDATE(UpdateDate))
		update AccControl set UpdateDate=getdate() where id in (select id from inserted)
        	
	update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccControlLastUpdate'
	
	return
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccPersonInfo WITH NOCHECK ADD CONSTRAINT
	FK_AccPersonInfo_AccControl FOREIGN KEY
	(
	AccID
	) REFERENCES dbo.AccControl
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccPersonInfo
	NOCHECK CONSTRAINT FK_AccPersonInfo_AccControl
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccWarehouseInfo WITH NOCHECK ADD CONSTRAINT
	FK_AccWarehouseInfo_AccControl FOREIGN KEY
	(
	AccID
	) REFERENCES dbo.AccControl
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccWarehouseInfo
	NOCHECK CONSTRAINT FK_AccWarehouseInfo_AccControl
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccTotalControl WITH NOCHECK ADD CONSTRAINT
	FK_AccTotalControl_AccControl FOREIGN KEY
	(
	ControlID
	) REFERENCES dbo.AccControl
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccDocItem WITH NOCHECK ADD CONSTRAINT
	FK_AccDocItem_AccControl FOREIGN KEY
	(
	ControlID
	) REFERENCES dbo.AccControl
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccDocItem
	NOCHECK CONSTRAINT FK_AccDocItem_AccControl
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccControlDetail WITH NOCHECK ADD CONSTRAINT
	FK_AccControlDetail_AccControl FOREIGN KEY
	(
	ControlID
	) REFERENCES dbo.AccControl
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
