/* 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.AccTotal
	DROP CONSTRAINT FK_AccTotal_AccCommittee
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccTotal
	DROP CONSTRAINT DF_AccTotal_UpdateDate
GO
ALTER TABLE dbo.AccTotal
	DROP CONSTRAINT DF_AccTotal_Updator
GO
CREATE TABLE dbo.Tmp_AccTotal
	(
	ID bigint NOT NULL IDENTITY (1, 1),
	Code nvarchar(20) NOT NULL,
	Title nvarchar(400) NOT NULL,
	Type int NOT NULL,
	CreditType int NOT NULL,
	Description nvarchar(2000) NULL,
	CommitteRef bigint NULL,
	Credit decimal(19, 2) NULL,
	Debit decimal(19, 2) NULL,
	UpdateDate datetime NOT NULL,
	Updater bigint NULL,
	AttachPath nvarchar(1000) NULL,
	TitleNoSpace nvarchar(500) NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_AccTotal ADD CONSTRAINT
	DF_AccTotal_UpdateDate DEFAULT (getdate()) FOR UpdateDate
GO
ALTER TABLE dbo.Tmp_AccTotal ADD CONSTRAINT
	DF_AccTotal_Updator DEFAULT ((0)) FOR Updater
GO
SET IDENTITY_INSERT dbo.Tmp_AccTotal ON
GO
IF EXISTS(SELECT * FROM dbo.AccTotal)
	 EXEC('INSERT INTO dbo.Tmp_AccTotal (ID, Code, Title, Type, CreditType, Description, CommitteRef, Credit, Debit, UpdateDate, Updater, AttachPath, TitleNoSpace)
		SELECT ID, CONVERT(nvarchar(20), Code), Title, Type, CreditType, Description, CommitteRef, Credit, Debit, UpdateDate, Updater, AttachPath, TitleNoSpace FROM dbo.AccTotal WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_AccTotal OFF
GO
ALTER TABLE dbo.AccDocItem
	DROP CONSTRAINT FK_AccDocItem_AccTotal
GO
ALTER TABLE dbo.AccTotalControl
	DROP CONSTRAINT FK_AccTotalControl_AccTotal
GO
DROP TABLE dbo.AccTotal
GO
EXECUTE sp_rename N'dbo.Tmp_AccTotal', N'AccTotal', 'OBJECT' 
GO
ALTER TABLE dbo.AccTotal ADD CONSTRAINT
	PK_AccTotalID 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.AccTotal ADD CONSTRAINT
	IX_AccTotalTitle UNIQUE NONCLUSTERED 
	(
	Title
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
CREATE UNIQUE NONCLUSTERED INDEX IX_AccTotalCode ON dbo.AccTotal
	(
	Code
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE dbo.AccTotal WITH NOCHECK ADD CONSTRAINT
	FK_AccTotal_AccCommittee FOREIGN KEY
	(
	CommitteRef
	) REFERENCES dbo.AccCommittee
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccTotal
	NOCHECK CONSTRAINT FK_AccTotal_AccCommittee
GO
CREATE TRIGGER AccTotal_LastUpdate
ON dbo.AccTotal FOR UPDATE 
	AS
	if not (UPDATE(UpdateDate))
		update AccTotal set UpdateDate=getdate() where id in (select id from inserted)

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

	return
GO
CREATE TRIGGER AccTotal_Delete
ON dbo.AccTotal for DELETE 
	AS
		INSERT INTO AccTotalTemp select * from deleted
     	update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccTotalLastUpdate'
return
GO
CREATE       TRIGGER AccTotal_Insert ON dbo.AccTotal FOR Insert
	AS
       	update AppSetting set TheValue = Convert(varchar,getdate(), 101)+' '+Convert(varchar,getdate(), 108) where [Name]='AccTotalLastUpdate'
	return
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccTotalControl WITH NOCHECK ADD CONSTRAINT
	FK_AccTotalControl_AccTotal FOREIGN KEY
	(
	TotalID
	) REFERENCES dbo.AccTotal
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccDocItem WITH NOCHECK ADD CONSTRAINT
	FK_AccDocItem_AccTotal FOREIGN KEY
	(
	TotalID
	) REFERENCES dbo.AccTotal
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccDocItem
	NOCHECK CONSTRAINT FK_AccDocItem_AccTotal
GO
COMMIT
