/* 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.AccSellFactor
	DROP CONSTRAINT DF_AccSellFactor_IsPermanent
GO
ALTER TABLE dbo.AccSellFactor
	DROP CONSTRAINT DF_AccSellFactor_IsFalsi
GO
ALTER TABLE dbo.AccSellFactor
	DROP CONSTRAINT DF_AccSellFactor_Zir5Darsad
GO
ALTER TABLE dbo.AccSellFactor
	DROP CONSTRAINT DF_AccSellFactor_HaghAmalKari
GO
CREATE TABLE dbo.Tmp_AccSellFactor
	(
	ID bigint NOT NULL IDENTITY (1, 1),
	Number bigint NULL,
	OrderDate datetime NULL,
	OrderTotalID bigint NULL,
	OrderControlID bigint NULL,
	OrderDetailID1 bigint NULL,
	OrderDetailID2 bigint NULL,
	Type bigint NULL,
	Source nvarchar(100) NULL,
	BuyerTotalID bigint NULL,
	BuyerControlID bigint NULL,
	BuyerDetailID1 bigint NULL,
	BuyerDetailID2 bigint NULL,
	SellerTotalID bigint NULL,
	SellerControlID bigint NULL,
	SellerDetailID1 bigint NULL,
	SellerDetailID2 bigint NULL,
	Request decimal(28, 5) NOT NULL,
	RequestUnit bigint NULL,
	RequestText nvarchar(100) NULL,
	SendDate datetime NULL,
	ControlA bit NOT NULL,
	ControlB bit NOT NULL,
	ControlC bit NOT NULL,
	ControlD bit NOT NULL,
	SellType int NULL,
	SellTime int NULL,
	SellPrice decimal(28, 5) NOT NULL,
	BuyType int NULL,
	BuyTime int NULL,
	BuyPrice decimal(28, 5) NOT NULL,
	City nvarchar(100) NULL,
	Destination nvarchar(100) NULL,
	Address nvarchar(250) NULL,
	DocItemID bigint NULL,
	DocItemRelationID int NULL,
	DocItemType int NULL,
	Description nvarchar(3000) NULL,
	UpdateDate datetime NOT NULL,
	Updater bigint NULL,
	Trailer bit NULL,
	PrintCount int NOT NULL,
	CancelDate datetime NULL,
	ProfitType int NULL,
	BuyerRef bigint NULL,
	AddDescriptionToDoc bit NULL,
	BuyerPaid bit NULL,
	SellerPaid bit NULL,
	IsPermanent bit NULL,
	MelliCode nchar(15) NULL,
	Tel nchar(50) NULL,
	BuyPrice2 decimal(28, 5) NULL,
	SellPrice2 decimal(28, 5) NULL,
	InsertDate datetime NULL,
	Inserter bigint NULL,
	Barbari nvarchar(100) NULL,
	Canceler bigint NULL,
	PermanentDate datetime NULL,
	Permanenter bigint NULL,
	Sender bigint NULL,
	WareOutID bigint NULL,
	WareInID bigint NULL,
	PostalCode nchar(10) NULL,
	FinancialCode nchar(20) NULL,
	WareOrderID bigint NULL,
	AppData nvarchar(1000) NULL,
	FactorID bigint NULL,
	Sign1User bigint NULL,
	Sign1Date datetime NULL,
	Sign2User bigint NULL,
	Sign2Date datetime NULL,
	Flags bigint NULL,
	IsFalsi bit NOT NULL,
	Zir5Darsad bit NOT NULL,
	HaghAmalKari bit NOT NULL,
	Type1Code int NULL,
	CityCode char(7) NULL,
	KalaTypeID int NULL,
	DestCode char(12) NULL,
	MoreStr0 nvarchar(500) NULL,
	MoreStr1 nvarchar(500) NULL,
	MoreStr2 nvarchar(500) NULL,
	MoreStr3 nvarchar(500) NULL,
	MoreStr4 nvarchar(500) NULL,
	MoreStr5 nvarchar(500) NULL,
	MoreStr6 nvarchar(500) NULL,
	MoreStr7 nvarchar(500) NULL,
	MoreStr8 nvarchar(500) NULL,
	MoreStr9 nvarchar(500) NULL,
	MoreNum0 nvarchar(30) NULL,
	MoreNum1 nvarchar(30) NULL,
	MoreNum2 nvarchar(30) NULL,
	MoreNum3 nvarchar(30) NULL,
	MoreNum4 nvarchar(30) NULL,
	MoreNum5 nvarchar(30) NULL,
	MoreNum6 nvarchar(30) NULL,
	MoreNum7 nvarchar(30) NULL,
	MoreNum8 nvarchar(30) NULL,
	MoreNum9 nvarchar(30) NULL,
	MoreDate0 datetime NULL,
	MoreDate1 datetime NULL,
	MoreDate2 datetime NULL,
	MoreDate3 datetime NULL,
	MoreDate4 datetime NULL,
	MoreDate5 datetime NULL,
	MoreDate6 datetime NULL,
	MoreDate7 datetime NULL,
	MoreDate8 datetime NULL,
	MoreDate9 datetime NULL,
	MoreFlags bigint NULL,
	Cottage char(10) NULL,
	BuyerCostCenter bigint NULL,
	BuyerProject bigint NULL,
	SellerCostCenter bigint NULL,
	SellerProject bigint NULL,
	BPeygiri nvarchar(15) NULL,
	BOstan int NULL,
	BShahr int NULL,
	BPardakhti decimal(28, 3) NULL,
	BKhShomHesab nvarchar(25) NULL,
	BShivPardakht nchar(10) NULL,
	BShenArzeh nchar(20) NULL,
	BKhShenasMelli nchar(11) NULL,
	BKhCodePosti nchar(10) NULL,
	BKhAddress nvarchar(250) NULL,
	BShenVariz nchar(15) NULL,
	BKhMobile nvarchar(50) NULL,
	BKhYekta nchar(15) NULL,
	BTak bit NULL,
	BJoft bit NULL,
	BBarnamehNashodeh decimal(28, 3) NULL,
	BTakhsis nchar(15) NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_AccSellFactor ADD CONSTRAINT
	DF_AccSellFactor_IsPermanent DEFAULT ((0)) FOR IsPermanent
GO
ALTER TABLE dbo.Tmp_AccSellFactor ADD CONSTRAINT
	DF_AccSellFactor_IsFalsi DEFAULT ((1)) FOR IsFalsi
GO
ALTER TABLE dbo.Tmp_AccSellFactor ADD CONSTRAINT
	DF_AccSellFactor_Zir5Darsad DEFAULT ((0)) FOR Zir5Darsad
GO
ALTER TABLE dbo.Tmp_AccSellFactor ADD CONSTRAINT
	DF_AccSellFactor_HaghAmalKari DEFAULT ((0)) FOR HaghAmalKari
GO
SET IDENTITY_INSERT dbo.Tmp_AccSellFactor ON
GO
IF EXISTS(SELECT * FROM dbo.AccSellFactor)
	 EXEC('INSERT INTO dbo.Tmp_AccSellFactor (ID, Number, OrderDate, OrderTotalID, OrderControlID, OrderDetailID1, OrderDetailID2, Type, Source, BuyerTotalID, BuyerControlID, BuyerDetailID1, BuyerDetailID2, SellerTotalID, SellerControlID, SellerDetailID1, SellerDetailID2, Request, RequestUnit, RequestText, SendDate, ControlA, ControlB, ControlC, ControlD, SellType, SellTime, SellPrice, BuyType, BuyTime, BuyPrice, City, Destination, Address, DocItemID, DocItemRelationID, DocItemType, Description, UpdateDate, Updater, Trailer, PrintCount, CancelDate, ProfitType, BuyerRef, AddDescriptionToDoc, BuyerPaid, SellerPaid, IsPermanent, MelliCode, Tel, BuyPrice2, SellPrice2, InsertDate, Inserter, Barbari, Canceler, PermanentDate, Permanenter, Sender, WareOutID, WareInID, PostalCode, FinancialCode, WareOrderID, AppData, FactorID, Sign1User, Sign1Date, Sign2User, Sign2Date, Flags, IsFalsi, Zir5Darsad, HaghAmalKari, Type1Code, CityCode, KalaTypeID, DestCode, MoreStr0, MoreStr1, MoreStr2, MoreStr3, MoreStr4, MoreStr5, MoreStr6, MoreStr7, MoreStr8, MoreStr9, MoreNum0, MoreNum1, MoreNum2, MoreNum3, MoreNum4, MoreNum5, MoreNum6, MoreNum7, MoreNum8, MoreNum9, MoreDate0, MoreDate1, MoreDate2, MoreDate3, MoreDate4, MoreDate5, MoreDate6, MoreDate7, MoreDate8, MoreDate9, MoreFlags, Cottage, BuyerCostCenter, BuyerProject, SellerCostCenter, SellerProject, BPeygiri, BOstan, BShahr, BPardakhti, BKhShomHesab, BShivPardakht, BShenArzeh, BKhShenasMelli, BKhCodePosti, BKhAddress, BShenVariz, BKhMobile, BKhYekta, BTak, BJoft, BBarnamehNashodeh, BTakhsis)
		SELECT ID, Number, OrderDate, OrderTotalID, OrderControlID, OrderDetailID1, OrderDetailID2, Type, Source, BuyerTotalID, BuyerControlID, BuyerDetailID1, BuyerDetailID2, SellerTotalID, SellerControlID, SellerDetailID1, SellerDetailID2, Request, RequestUnit, RequestText, SendDate, ControlA, ControlB, ControlC, ControlD, SellType, SellTime, SellPrice, BuyType, BuyTime, BuyPrice, City, Destination, Address, DocItemID, DocItemRelationID, DocItemType, Description, UpdateDate, Updater, Trailer, PrintCount, CancelDate, ProfitType, BuyerRef, AddDescriptionToDoc, BuyerPaid, SellerPaid, IsPermanent, MelliCode, Tel, BuyPrice2, SellPrice2, InsertDate, Inserter, Barbari, Canceler, PermanentDate, Permanenter, Sender, WareOutID, WareInID, PostalCode, FinancialCode, WareOrderID, AppData, FactorID, Sign1User, Sign1Date, Sign2User, Sign2Date, Flags, IsFalsi, Zir5Darsad, HaghAmalKari, Type1Code, CityCode, KalaTypeID, DestCode, MoreStr0, MoreStr1, MoreStr2, MoreStr3, MoreStr4, MoreStr5, MoreStr6, MoreStr7, MoreStr8, MoreStr9, MoreNum0, MoreNum1, MoreNum2, MoreNum3, MoreNum4, MoreNum5, MoreNum6, MoreNum7, MoreNum8, MoreNum9, MoreDate0, MoreDate1, MoreDate2, MoreDate3, MoreDate4, MoreDate5, MoreDate6, MoreDate7, MoreDate8, MoreDate9, MoreFlags, Cottage, BuyerCostCenter, BuyerProject, SellerCostCenter, SellerProject, BPeygiri, BOstan, BShahr, BPardakhti, BKhShomHesab, BShivPardakht, BShenArzeh, BKhShenasMelli, BKhCodePosti, BKhAddress, BShenVariz, BKhMobile, BKhYekta, BTak, BJoft, BBarnamehNashodeh, BTakhsis FROM dbo.AccSellFactor WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_AccSellFactor OFF
GO
ALTER TABLE dbo.AccSellFactorTrucks
	DROP CONSTRAINT FK_AccSellFactorTrucks_AccSellFactor
GO
DROP TABLE dbo.AccSellFactor
GO
EXECUTE sp_rename N'dbo.Tmp_AccSellFactor', N'AccSellFactor', 'OBJECT' 
GO
ALTER TABLE dbo.AccSellFactor ADD CONSTRAINT
	PK_DocSellFactor 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_AccSellFactor_DocID ON dbo.AccSellFactor
	(
	DocItemID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX IX_AccSellFactorNumber ON dbo.AccSellFactor
	(
	Number
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE  TRIGGER DocSellFactor_Insert
ON dbo.AccSellFactor FOR Insert
	AS
     	--update AppSetting set TheValue =  dbo.StrDate(getdate()) where [Name]='DocSellFactorLastUpdate'
	return
GO
CREATE  TRIGGER DocSellFactor_LastUpdate
ON dbo.AccSellFactor FOR UPDATE 
	AS
	if not (UPDATE(UpdateDate))
		update AccSellFactor set UpdateDate=getdate() where id in (select id from inserted)
     	--update AppSetting set TheValue =  dbo.StrDate(getdate()) where [Name]='DocSellFactorLastUpdate'
	return
GO
CREATE  TRIGGER DocSellFactor_Delete
ON dbo.AccSellFactor for DELETE 
	AS
		INSERT INTO AccSellFactorTemp select * from deleted
     	--update AppSetting set TheValue =  dbo.StrDate(getdate()) where [Name]='DocSellFactorLastUpdate'
	return
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccSellFactorTrucks ADD CONSTRAINT
	FK_AccSellFactorTrucks_AccSellFactor FOREIGN KEY
	(
	SellFactorID
	) REFERENCES dbo.AccSellFactor
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
