BEGIN TRANSACTION
ALTER TABLE dbo.Inventory DROP CONSTRAINT FK_Inventory_Products;
ALTER TABLE dbo.Inventory DROP CONSTRAINT FK_Inventory_Vouchers;
ALTER TABLE dbo.Inventory DROP CONSTRAINT DF_Inventory_Discount;
CREATE TABLE dbo.Tmp_Inventory (
	InventoryID uniqueidentifier NOT NULL,
	VoucherID uniqueidentifier NOT NULL,
	ProductID uniqueidentifier NOT NULL,
	Quantity decimal(18, 5) NOT NULL,
	Rate decimal(18, 5) NOT NULL,
	Vat decimal(18, 5) NOT NULL,
	ServiceTax decimal(18, 5) NOT NULL,
	Discount decimal(18, 5) NOT NULL,
	Amount  AS ((([Quantity]*[Rate])*((1)+[ServiceTax]+[Vat]))*((1)-[Discount]))
	);
ALTER TABLE dbo.Tmp_Inventory ADD CONSTRAINT DF_Inventory_Discount DEFAULT ((0)) FOR Discount;
IF EXISTS(SELECT * FROM dbo.Inventory)
	 EXEC('INSERT INTO dbo.Tmp_Inventory (InventoryID, VoucherID, ProductID, Quantity, Rate, Vat, ServiceTax, Discount)
		SELECT InventoryID, VoucherID, ProductID, Quantity, Rate, Tax, 0, Discount FROM dbo.Inventory WITH (HOLDLOCK TABLOCKX)');
DROP TABLE dbo.Inventory;
EXECUTE sp_rename N'dbo.Tmp_Inventory', N'Inventory', 'OBJECT';
ALTER TABLE dbo.Inventory ADD CONSTRAINT PK_Inventory PRIMARY KEY CLUSTERED (InventoryID);
ALTER TABLE dbo.Inventory ADD CONSTRAINT FK_Inventory_Vouchers FOREIGN KEY (VoucherID) REFERENCES dbo.Vouchers (VoucherID);
ALTER TABLE dbo.Inventory ADD CONSTRAINT FK_Inventory_Products FOREIGN KEY (ProductID) REFERENCES dbo.Products (ProductID);
ALTER TABLE dbo.Vouchers DROP CONSTRAINT IX_Vouchers_Code_Type;
ALTER TABLE dbo.Vouchers ADD CONSTRAINT IX_Vouchers_Code UNIQUE NONCLUSTERED (Code);
ALTER TABLE dbo.Ledgers DROP CONSTRAINT FK_Ledgers_CostCenters;
ALTER TABLE dbo.Products DROP CONSTRAINT FK_Products_Ledgers_Sale;
ALTER TABLE dbo.Products DROP CONSTRAINT FK_Products_Ledgers_Purchase;
ALTER TABLE dbo.ComplexProducts DROP CONSTRAINT FK_ComplexProducts_Products;
ALTER TABLE dbo.Customers DROP CONSTRAINT FK_Customers_Ledgers;
DROP TABLE dbo.ComplexProducts;
ALTER TABLE dbo.Products DROP COLUMN timestamp;
ALTER TABLE dbo.Products DROP COLUMN PurchasePrice;
ALTER TABLE dbo.Products DROP COLUMN SaleLedgerID;
ALTER TABLE dbo.Products DROP COLUMN PurchaseLedgerID;
ALTER TABLE dbo.Waiters DROP COLUMN timestamp;
ALTER TABLE dbo.Vouchers DROP COLUMN Ref;
ALTER TABLE dbo.Vouchers DROP COLUMN Type;
ALTER TABLE dbo.Vouchers DROP COLUMN timestamp;
ALTER TABLE dbo.SaleVoucher DROP COLUMN timestamp;
ALTER TABLE dbo.Users DROP COLUMN timestamp;
ALTER TABLE dbo.Customers DROP COLUMN LedgerID;
ALTER TABLE dbo.Customers DROP COLUMN timestamp;
ALTER TABLE dbo.ProductTypes DROP COLUMN timestamp;
ALTER TABLE dbo.Roles DROP COLUMN timestamp;
ALTER TABLE dbo.Tax DROP COLUMN timestamp;
ALTER TABLE dbo.UserRoles DROP COLUMN timestamp;
DROP TABLE CostCenters;
DROP TABLE Ledgers;
exec sp_rename 'dbo.ProductTypes', 'ProductGroups';
exec sp_rename 'dbo.ProductGroups.ProductTypeID', 'ProductGroupID', 'COLUMN';
exec sp_rename 'dbo.Products.ProductTypeID', 'ProductGroupID', 'COLUMN';
exec sp_rename 'dbo.Products.PurchaseTaxID', 'ServiceTaxID', 'COLUMN';
exec sp_rename 'dbo.Products.SaleTaxID', 'VatID', 'COLUMN';
COMMIT

-- ROLLBACK