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.Products DROP COLUMN MinimumLevel;
ALTER TABLE dbo.Products DROP COLUMN MaximumLevel;
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';
INSERT INTO Tax(TaxID, Name, Rate, Type) VALUES ('27B6FA07-F9D2-49C6-8E37-31A5EFC04FBB', '4.944% Service Tax', 0.049444, 'V');
UPDATE Products SET ServiceTaxID = '3D1413EE-D3EA-412F-B762-BAE37BD503B6' WHERE VatID != '8C200738-374B-484E-B239-9BD0CB9CCF36';
UPDATE Products SET ServiceTaxID = '27B6FA07-F9D2-49C6-8E37-31A5EFC04FBB' WHERE VatID = '8C200738-374B-484E-B239-9BD0CB9CCF36';
UPDATE Products SET VatID = '48E10C74-9508-44CB-9D7D-7B71ABDAA289' WHERE VatID ='8C200738-374B-484E-B239-9BD0CB9CCF36';
update Products set productgroupid = 'A331DC08-EAA5-4C87-8DE0-0F9CBA2558F7'
where productgroupid in (
'80EC988A-448E-4F81-B753-1B70FF8B233C',
'28846C15-6391-48D4-A50E-2A40BA8156C0',
'B75B3C3A-4364-4B36-8DEA-C0C0247449B2',
'F76B54C6-21CE-450F-AC73-E1A3A43DAB43',
'CC099265-97FF-45CA-80A6-F7BB189C18B7',
'389A3E09-AC97-4C4B-BCE1-342D2CF991DB'
);
delete from productgroups where productgroupid not in (
select distinct productgroupid from products)
update inventory set servicetax = 0.04944, vat = 0.12500 from inventory where vat = 0.17444
COMMIT

-- ROLLBACK