BEGIN TRANSACTION
GO
ALTER TABLE dbo.Entities_Inventories
	DROP CONSTRAINT FK53E4F7FE3F88CAB6
GO
ALTER TABLE dbo.Entities_Inventories
	DROP CONSTRAINT FK53E4F7FEDB70F42
GO
CREATE TABLE dbo.Tmp_Entities_Inventories
	(
	InventoryID int NOT NULL IDENTITY (1, 1),
	Quantity decimal(19, 5) NOT NULL,
	Price decimal(19, 5) NOT NULL,
	FullPrice decimal(19, 5) NOT NULL,
	ServiceTax decimal(19, 5) NOT NULL,
	Vat decimal(19, 5) NOT NULL,
	Discount decimal(19, 5) NOT NULL,
	ServiceCharge decimal(19, 5) NOT NULL,
	IsScTaxable bit NOT NULL,
	Amount  AS CASE WHEN IsScTaxable = 1 THEN Quantity * Price * (1 - Discount) * (1 + ServiceCharge) * (1 + ServiceTax + Vat) ELSE Quantity * Price * (1 - Discount) * (1 + ServiceCharge + ServiceTax + Vat) END,
	KotID int NULL,
	ProductID int NULL
	)  ON [PRIMARY]
GO
SET IDENTITY_INSERT dbo.Tmp_Entities_Inventories ON
GO
IF EXISTS(SELECT * FROM dbo.Entities_Inventories)
	 EXEC('INSERT INTO dbo.Tmp_Entities_Inventories (InventoryID, Quantity, Price, FullPrice, ServiceTax, Vat, Discount, ServiceCharge, IsScTaxable, KotID, ProductID)
		SELECT InventoryID, Quantity, Price, FullPrice, 0, Tax, Discount, ServiceCharge, 1, KotID, ProductID FROM dbo.Entities_Inventories WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_Entities_Inventories OFF
GO
ALTER TABLE dbo.Entities_InventoryModifiers
	DROP CONSTRAINT FK695304F8BA29671A
GO
DROP TABLE dbo.Entities_Inventories
GO
EXECUTE sp_rename N'dbo.Tmp_Entities_Inventories', N'Entities_Inventories', 'OBJECT' 
GO
ALTER TABLE dbo.Entities_Inventories ADD CONSTRAINT
	PK__Entities_Inventories PRIMARY KEY CLUSTERED 
	(
	InventoryID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
CREATE NONCLUSTERED INDEX IX_Entities_Inventories ON dbo.Entities_Inventories
	(
	KotID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE dbo.Entities_Inventories ADD CONSTRAINT
	FK53E4F7FEDB70F42 FOREIGN KEY
	(
	KotID
	) REFERENCES dbo.Entities_Kots
	(
	KotID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.Entities_Inventories ADD CONSTRAINT
	FK53E4F7FE3F88CAB6 FOREIGN KEY
	(
	ProductID
	) REFERENCES dbo.Entities_Products
	(
	ProductID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.Entities_InventoryModifiers ADD CONSTRAINT
	FK695304F8BA29671A FOREIGN KEY
	(
	InventoryID
	) REFERENCES dbo.Entities_Inventories
	(
	InventoryID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
