/* 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.AccSellFactorTrucks
	DROP CONSTRAINT DF_AccSellFactorTrucks_Status
GO
CREATE TABLE dbo.Tmp_AccSellFactorTrucks
	(
	ID bigint NOT NULL IDENTITY (1, 1),
	SellFactorID bigint NOT NULL,
	Date datetime NULL,
	TruckNo nvarchar(25) NOT NULL,
	Weigh decimal(18, 5) NULL,
	Unit bigint NULL,
	FDate  AS ([dbo].[FARSI_DATE2]([Date])),
	DocItemID bigint NULL,
	DocItemRelationID int NULL,
	DocHeaderID bigint NULL,
	Status int NULL,
	Flags int NULL,
	BarName nvarchar(25) NULL,
	Description nvarchar(100) NULL,
	ReportDate datetime NULL,
	Pack int NULL,
	TruckTel nvarchar(200) NULL,
	HamlTotalID bigint NULL,
	HamlControlID bigint NULL,
	HamlDetailID1 bigint NULL,
	HamlDetailID2 bigint NULL,
	HamlPrice numeric(19, 2) NULL,
	ChaperTotalID bigint NULL,
	ChaperControlID bigint NULL,
	ChaperDetailID1 bigint NULL,
	ChaperDetailID2 bigint NULL,
	ChaperPrice decimal(19, 2) NULL,
	ShipID bigint NULL,
	UpdateDate datetime NULL,
	Updater bigint NULL,
	InsertDate datetime NULL,
	Inserter bigint NULL,
	CancelDate datetime NULL,
	Canceler bigint NULL,
	FactorID bigint NULL,
	BSK nchar(20) NULL,
	WareOutID bigint NULL,
	WareInID bigint NULL,
	TruckName nvarchar(100) NULL,
	BarnameSerial nvarchar(25) NULL,
	TaghirMaghsad bit NULL,
	BarnameAvval nvarchar(25) NULL,
	BarnameSerialAvval nvarchar(25) NULL,
	DestinationAvval nvarchar(100) NULL,
	CottageAvval char(10) NULL,
	NumberAvval bigint NULL,
	BKharidId bigint NULL,
	BTakhsisId bigint NULL,
	ShomNameh nvarchar(50) NULL,
	TarikhNameh datetime NULL,
	companyCode nvarchar(50) NULL,
	wH1 decimal(18, 5) NULL,
	Wh2 decimal(18, 5) NULL,
	Kra1 nvarchar(50) NULL,
	Kra2 nvarchar(50) NULL,
	ka_E_Code nvarchar(50) NULL,
	tarGetName nvarchar(500) NULL,
	barExp nvarchar(50) NULL,
	mbkhaal nvarchar(50) NULL,
	ghErtebat nvarchar(500) NULL,
	kaGrp nvarchar(50) NULL,
	receiverShKharid nvarchar(50) NULL,
	receiverShTakhsis nvarchar(50) NULL,
	billLadingPlaceCode nvarchar(50) NULL,
	billLadingPlaceName nvarchar(50) NULL,
	n52 nvarchar(50) NULL,
	billofLadingCoutageCode nvarchar(50) NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_AccSellFactorTrucks SET (LOCK_ESCALATION = TABLE)
GO
ALTER TABLE dbo.Tmp_AccSellFactorTrucks ADD CONSTRAINT
	DF_AccSellFactorTrucks_Status DEFAULT ((0)) FOR Status
GO
SET IDENTITY_INSERT dbo.Tmp_AccSellFactorTrucks ON
GO
IF EXISTS(SELECT * FROM dbo.AccSellFactorTrucks)
	 EXEC('INSERT INTO dbo.Tmp_AccSellFactorTrucks (ID, SellFactorID, Date, TruckNo, Weigh, Unit, DocItemID, DocItemRelationID, DocHeaderID, Status, Flags, BarName, Description, ReportDate, Pack, TruckTel, HamlTotalID, HamlControlID, HamlDetailID1, HamlDetailID2, HamlPrice, ChaperTotalID, ChaperControlID, ChaperDetailID1, ChaperDetailID2, ChaperPrice, ShipID, UpdateDate, Updater, InsertDate, Inserter, CancelDate, Canceler, FactorID, BSK, WareOutID, WareInID, TruckName, BarnameSerial, TaghirMaghsad, BarnameAvval, BarnameSerialAvval, DestinationAvval, CottageAvval, NumberAvval, BKharidId, BTakhsisId, ShomNameh, TarikhNameh, companyCode, wH1, Wh2, Kra1, Kra2, ka_E_Code, tarGetName, barExp, mbkhaal, ghErtebat, kaGrp, receiverShKharid, receiverShTakhsis, billLadingPlaceCode, billLadingPlaceName, n52, billofLadingCoutageCode)
		SELECT ID, SellFactorID, Date, TruckNo, Weigh, Unit, DocItemID, DocItemRelationID, DocHeaderID, Status, Flags, BarName, Description, ReportDate, Pack, TruckTel, HamlTotalID, HamlControlID, HamlDetailID1, HamlDetailID2, HamlPrice, ChaperTotalID, ChaperControlID, ChaperDetailID1, ChaperDetailID2, ChaperPrice, ShipID, UpdateDate, Updater, InsertDate, Inserter, CancelDate, Canceler, FactorID, BSK, WareOutID, WareInID, TruckName, BarnameSerial, TaghirMaghsad, BarnameAvval, BarnameSerialAvval, DestinationAvval, CottageAvval, NumberAvval, BKharidId, BTakhsisId, ShomNameh, TarikhNameh, companyCode, wH1, Wh2, Kra1, Kra2, ka_E_Code, tarGetName, barExp, mbkhaal, ghErtebat, kaGrp, receiverShKharid, receiverShTakhsis, billLadingPlaceCode, billLadingPlaceName, n52, billofLadingCoutageCode FROM dbo.AccSellFactorTrucks WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_AccSellFactorTrucks OFF
GO
DROP TABLE dbo.AccSellFactorTrucks
GO
EXECUTE sp_rename N'dbo.Tmp_AccSellFactorTrucks', N'AccSellFactorTrucks', 'OBJECT' 
GO
ALTER TABLE dbo.AccSellFactorTrucks ADD CONSTRAINT
	PK_DocSellFactorTrucks 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_AccSellFactorTrucks_DocID ON dbo.AccSellFactorTrucks
	(
	DocItemID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IX_AccSellFactorTrucks_FactorID ON dbo.AccSellFactorTrucks
	(
	SellFactorID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX AccSellFactorTrucks_SellFactorID_ID_DocHeaderID_DocItemID_DocItemRelationID ON dbo.AccSellFactorTrucks
	(
	SellFactorID,
	ID,
	DocHeaderID,
	DocItemID,
	DocItemRelationID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
create  TRIGGER DocSellFactorTrucks_Delete
ON dbo.AccSellFactorTrucks for DELETE 
	AS
		INSERT INTO AccSellFactorTrucksTemp(ID,SellFactorID,Date,TruckNo,Weigh,Unit,DocItemID,DocItemRelationID,Status,Flags,BarName,Description,ReportDate,Pack,TruckTel) select ID,SellFactorID,Date,TruckNo,Weigh,Unit,DocItemID,DocItemRelationID,Status,Flags,BarName,Description,ReportDate,Pack,TruckTel from deleted
		 --update AppSetting set TheValue =  dbo.StrDate(getdate()) where [Name]='DocSellFactorLastUpdate'
	return
GO
create  TRIGGER DocSellFactorTrucks_LastUpdate
ON dbo.AccSellFactorTrucks FOR UPDATE 
	AS

     	--update AppSetting set TheValue =  dbo.StrDate(getdate()) where [Name]='DocSellFactorLastUpdate'
	return
GO
Create  TRIGGER DocSellFactorTrucks_Insert
ON dbo.AccSellFactorTrucks FOR Insert
	AS
     	--update AppSetting set TheValue =  dbo.StrDate(getdate()) where [Name]='DocSellFactorLastUpdate'
	return
GO
COMMIT
