/* 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.AccFactorDetail
	DROP CONSTRAINT DF_AccFactorDetail_Status
GO
ALTER TABLE dbo.AccFactorDetail
	DROP CONSTRAINT DF_AccFactorDetail_UpdateDate
GO
ALTER TABLE dbo.AccFactorDetail
	DROP CONSTRAINT DF_AccFactorDetail_Updater
GO
CREATE TABLE dbo.Tmp_AccFactorDetail
	(
	ID bigint NOT NULL IDENTITY (1, 1),
	FactorID bigint NOT NULL,
	OrderTotalID bigint NOT NULL,
	OrderControlID bigint NOT NULL,
	OrderDetailID1 bigint NOT NULL,
	OrderDetailID2 bigint NOT NULL,
	Request decimal(19, 5) NOT NULL,
	RequestUnit bigint NOT NULL,
	NetPrice decimal(19, 5) NOT NULL,
	TotalPrice decimal(28, 5) NOT NULL,
	DocItemID bigint NOT NULL,
	DocItemRelationID int NULL,
	DocHeaderID bigint NULL,
	Status int NULL,
	OtherTotalID bigint NULL,
	OtherControlID bigint NULL,
	OtherDetailID1 bigint NULL,
	OtherDetailID2 bigint NULL,
	Value2 decimal(19, 5) NULL,
	Unit2 bigint NULL,
	UpdateDate datetime NOT NULL,
	Updater bigint NOT NULL,
	Discont decimal(28, 5) NULL,
	Maliat decimal(28, 5) NULL,
	Avarez decimal(28, 5) NULL,
	Heigh decimal(18, 5) NULL,
	Lenght decimal(18, 5) NULL,
	Width decimal(18, 5) NULL,
	ZowjInUnit decimal(18, 5) NULL,
	Pakaging int NULL,
	Oft float(53) NULL,
	WarehouseNo bigint NULL,
	SumInFactor bit NULL,
	OrderID bigint NULL,
	SayerVojoh decimal(19, 5) NULL,
	Nw decimal(19, 5) NULL,
	Ssrv decimal(19, 5) NULL,
	Sscv decimal(19, 5) NULL,
	Haml decimal(28, 5) NULL,
	HamlTotalID bigint NULL,
	HamlControlID bigint NULL,
	HamlDetailID1 bigint NULL,
	HamlDetailID2 bigint NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_AccFactorDetail ADD CONSTRAINT
	DF_AccFactorDetail_Status DEFAULT ((0)) FOR Status
GO
ALTER TABLE dbo.Tmp_AccFactorDetail ADD CONSTRAINT
	DF_AccFactorDetail_UpdateDate DEFAULT (getdate()) FOR UpdateDate
GO
ALTER TABLE dbo.Tmp_AccFactorDetail ADD CONSTRAINT
	DF_AccFactorDetail_Updater DEFAULT ((0)) FOR Updater
GO
SET IDENTITY_INSERT dbo.Tmp_AccFactorDetail ON
GO
IF EXISTS(SELECT * FROM dbo.AccFactorDetail)
	 EXEC('INSERT INTO dbo.Tmp_AccFactorDetail (ID, FactorID, OrderTotalID, OrderControlID, OrderDetailID1, OrderDetailID2, Request, RequestUnit, NetPrice, TotalPrice, DocItemID, DocItemRelationID, DocHeaderID, Status, OtherTotalID, OtherControlID, OtherDetailID1, OtherDetailID2, Value2, Unit2, UpdateDate, Updater, Discont, Maliat, Avarez, Heigh, Lenght, Width, ZowjInUnit, Pakaging, Oft, WarehouseNo, SumInFactor, OrderID, SayerVojoh, Nw, Ssrv, Sscv)
		SELECT ID, FactorID, OrderTotalID, OrderControlID, OrderDetailID1, OrderDetailID2, Request, RequestUnit, NetPrice, TotalPrice, DocItemID, DocItemRelationID, DocHeaderID, Status, OtherTotalID, OtherControlID, OtherDetailID1, OtherDetailID2, Value2, Unit2, UpdateDate, Updater, Discont, Maliat, Avarez, Heigh, Lenght, Width, ZowjInUnit, Pakaging, Oft, WarehouseNo, SumInFactor, OrderID, SayerVojoh, Nw, Ssrv, Sscv FROM dbo.AccFactorDetail WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_AccFactorDetail OFF
GO
DROP TABLE dbo.AccFactorDetail
GO
EXECUTE sp_rename N'dbo.Tmp_AccFactorDetail', N'AccFactorDetail', 'OBJECT' 
GO
ALTER TABLE dbo.AccFactorDetail ADD CONSTRAINT
	PK_AccFactorDetail PRIMARY KEY CLUSTERED 
	(
	ID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
CREATE TRIGGER [dbo].[AccFactorDetail_Delete]
ON dbo.AccFactorDetail for DELETE 
	AS
		insert into dbo.AccFactorDetailTemp  select *,GETDATE() from deleted
	return
GO
CREATE TRIGGER [dbo].[AccFactorDetail_LastUpdate]
ON dbo.AccFactorDetail FOR UPDATE 
	AS
	SET NOCOUNT ON;
	if not (UPDATE(UpdateDate))
		update AccFactorDetail set UpdateDate=getdate() where id in (select id from inserted)

     	--update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccFactorDetailLastUpdate'
	return
GO
CREATE TRIGGER [dbo].[AccFactorDetail_CheckDelete]
ON dbo.AccFactorDetail INSTEAD OF DELETE 
	AS
		DECLARE @TaxUID as nvarchar(100)
		DECLARE @FactorID as bigint
		DECLARE @ID as bigint
		
		DECLARE DelCursor CURSOR LOCAL FAST_FORWARD READ_ONLY FOR 
			select ID, FactorID from Deleted
		OPEN DelCursor
		FETCH NEXT FROM DelCursor INTO @ID, @FactorID
		WHILE @@FETCH_STATUS = 0 
		BEGIN
			select @TaxUID=TaxUID from AccFactor with(nolock) where ID=@FactorID 
			if @TaxUID is null or @TaxUID = CAST(CAST(0x0 AS UNIQUEIDENTIFIER) as nvarchar(100)) or @TaxUID=''
			BEGIN
				delete From dbo.AccFactorDetail where id=@ID
			END
			else
				RAISERROR('امکان حذف فاکتور ارسال شده به سامانه نیست.',16,20) WITH LOG, SETERROR
			FETCH NEXT FROM DelCursor INTO @ID, @FactorID
		END
		CLOSE DelCursor
		DEALLOCATE DelCursor
	return
GO
COMMIT
