/* 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.AccDocHeader
	DROP CONSTRAINT DF_AccDocHeader_UserNumber
GO
ALTER TABLE dbo.AccDocHeader
	DROP CONSTRAINT DF_AccDocHeader_DocValue
GO
ALTER TABLE dbo.AccDocHeader
	DROP CONSTRAINT DF_AccDocHeader_Type
GO
ALTER TABLE dbo.AccDocHeader
	DROP CONSTRAINT DF_AccDocHeader_UpdateDate
GO
ALTER TABLE dbo.AccDocHeader
	DROP CONSTRAINT DF_AccDocHeader_Updater
GO
CREATE TABLE dbo.Tmp_AccDocHeader
	(
	ID bigint NOT NULL IDENTITY (1, 1),
	Number bigint NOT NULL,
	UserNumber bigint NOT NULL,
	DocDate datetime NOT NULL,
	DocValue decimal(22, 2) NOT NULL,
	Description nvarchar(3000) NULL,
	Flags int NULL,
	IsPermanent bit NOT NULL,
	UpdateDate datetime NOT NULL,
	Updater bigint NULL,
	DocDatePer char(10) NULL,
	TempNumber bigint NULL,
	TempDate datetime NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_AccDocHeader ADD CONSTRAINT
	DF_AccDocHeader_UserNumber DEFAULT ((0)) FOR UserNumber
GO
ALTER TABLE dbo.Tmp_AccDocHeader ADD CONSTRAINT
	DF_AccDocHeader_DocValue DEFAULT ((0)) FOR DocValue
GO
ALTER TABLE dbo.Tmp_AccDocHeader ADD CONSTRAINT
	DF_AccDocHeader_Type DEFAULT ((1)) FOR IsPermanent
GO
ALTER TABLE dbo.Tmp_AccDocHeader ADD CONSTRAINT
	DF_AccDocHeader_UpdateDate DEFAULT (getdate()) FOR UpdateDate
GO
ALTER TABLE dbo.Tmp_AccDocHeader ADD CONSTRAINT
	DF_AccDocHeader_Updater DEFAULT ((0)) FOR Updater
GO
SET IDENTITY_INSERT dbo.Tmp_AccDocHeader ON
GO
IF EXISTS(SELECT * FROM dbo.AccDocHeader)
	 EXEC('INSERT INTO dbo.Tmp_AccDocHeader (ID, Number, UserNumber, DocDate, DocValue, Description, Flags, IsPermanent, UpdateDate, Updater, DocDatePer, TempNumber, TempDate)
		SELECT ID, Number, UserNumber, DocDate, DocValue, Description, Flags, IsPermanent, UpdateDate, Updater, DocDatePer, TempNumber, TempDate FROM dbo.AccDocHeader WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_AccDocHeader OFF
GO
ALTER TABLE dbo.AccDocAttach
	DROP CONSTRAINT FK_AccDocAttach_AccDocHeader
GO
ALTER TABLE dbo.AccDocItem
	DROP CONSTRAINT FK_AccDocItem_AccDocHeader
GO
DROP TABLE dbo.AccDocHeader
GO
EXECUTE sp_rename N'dbo.Tmp_AccDocHeader', N'AccDocHeader', 'OBJECT' 
GO
ALTER TABLE dbo.AccDocHeader ADD CONSTRAINT
	PK_AccDocHeader 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.AccDocHeader ADD CONSTRAINT
	IX_AccDocHeader UNIQUE NONCLUSTERED 
	(
	Number
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
CREATE NONCLUSTERED INDEX AccDocHeader_DocDate_ID ON dbo.AccDocHeader
	(
	DocDate,
	ID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE  TRIGGER AccDocHeader_LastUpdate
ON dbo.AccDocHeader FOR UPDATE 
	AS
	if not (UPDATE(UpdateDate))
		update AccDocHeader set UpdateDate=getdate() where id in (select id from inserted)

     	--update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccDocLastUpdate'
	return
GO
CREATE  TRIGGER AccDocHeader_Delete
ON dbo.AccDocHeader For DELETE 
	AS
		insert dbo.AccDocHeaderTemp select * from deleted
     	--update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccDocLastUpdate'
	return
GO
CREATE   TRIGGER AccDocHeader_Insert
ON dbo.AccDocHeader FOR Insert
	AS
     	--update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccDocLastUpdate'
	return
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccDocItem ADD CONSTRAINT
	FK_AccDocItem_AccDocHeader FOREIGN KEY
	(
	HeaderID
	) REFERENCES dbo.AccDocHeader
	(
	ID
	) ON UPDATE  CASCADE 
	 ON DELETE  CASCADE 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccDocAttach ADD CONSTRAINT
	FK_AccDocAttach_AccDocHeader FOREIGN KEY
	(
	DocHeaderID
	) REFERENCES dbo.AccDocHeader
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
