USE Gets;
BEGIN TRANSACTION
ALTER TABLE Entities_Waiters ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_FoodTables ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_Customers ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Auth_Groups ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_Modifiers ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Auth_Roles ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_ProductGroups ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_Taxes ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Auth_Users ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_Vouchers ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_Kots ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_Products ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_Inventories ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_InventoryModifiers ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Auth_RoleGroups ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Auth_UserGroups ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_ProductGroupModifiers ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_PrintLocations ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_Reprints ADD ID uniqueidentifier not null DEFAULT newid();
ALTER TABLE Entities_VoucherSettlements ADD ID uniqueidentifier not null DEFAULT newid();
COMMIT
BEGIN TRANSACTION
UPDATE Entities_Customers SET ID = '2C716F4B-0736-429A-AD51-610D7C47CB5E' WHERE CustomerID = 1;
UPDATE Entities_Waiters SET ID = '3ECB3BCE-CBBA-461C-BC16-5823FC075AAA' WHERE WaiterID = 1;
COMMIT
USE Sales
BEGIN TRANSACTION
INSERT INTO Waiters (WaiterID, Name)
SELECT ID, Name from Gets.dbo.Entities_Waiters;
INSERT INTO Customers (CustomerID, Name, Address, Important, Phone, Remarks)
SELECT ID, Name, Address, Important, Phone, Remarks from Gets.dbo.Entities_Customers;
INSERT INTO Auth_Groups (GroupID, Name)
SELECT ID, Name from Gets.dbo.Auth_Groups;
INSERT INTO Modifiers (ModifierID, Name, ShowInBill, Price)
SELECT ID, Name, 0, 0 from Gets.dbo.Entities_Modifiers;
INSERT INTO Auth_Roles (RoleID, Name)
SELECT ID, Name from Gets.dbo.Auth_Roles;
INSERT INTO ProductGroups (ProductGroupID, Name, DiscountLimit, IsModifierCompulsory, Discontinued, SortOrder, GroupType)
SELECT ID, Name, DiscountLimit, IsModifierCompulsory, Discontinued, SortOrder, GroupType from Gets.dbo.Entities_ProductGroups;
INSERT INTO Taxes (TaxID, Name, Rate)
SELECT ID, Name, Rate from Gets.dbo.Entities_Taxes;
INSERT INTO Auth_Users (UserID, Name, MsrString, Password, LockedOut)
SELECT ID, Name, MsrString, Password, LockedOut from Gets.dbo.Auth_Users;

INSERT INTO Vouchers (VoucherID, Date, Pax, UserID, CreationDate, LastEditDate, BillID, TableID, WaiterID, CustomerID, Narration, Void, VoidReason, Printed, VoucherType, KotID)
SELECT ID, Date, Pax, 
(SELECT u.ID FROM Gets.dbo.Auth_Users u WHERE u.UserID = v.UserID), 
CreationDate, LastEditDate, BillID, TableID, 
(SELECT w.ID FROM Gets.dbo.Entities_Waiters w WHERE w.WaiterID = v.WaiterID), 
(SELECT c.ID FROM Gets.dbo.Entities_Customers c WHERE c.CustomerID = v.CustomerID), 
Narration, Void, VoidReason, Printed, VoucherType, KotID
from Gets.dbo.Entities_Vouchers as V;

INSERT INTO Kots (KotID, VoucherID, Code, TableID, Printed, Date, UserID)
SELECT ID, 
(SELECT v.ID FROM Gets.dbo.Entities_Vouchers v WHERE v.VoucherID = k.VoucherID), 
Code, TableID, Printed, Date,
(SELECT u.ID FROM Gets.dbo.Auth_Users u WHERE u.UserID = k.UserID)
from Gets.dbo.Entities_Kots AS k;

INSERT INTO Products (ProductID, Code, Name, Units, ProductGroupID, VatID, ServiceTaxID, ServiceCharge, IsScTaxable, Price, FullPrice, Discontinued, SortOrder, BaseCode, Quantity)
SELECT ID, Code, Name, Units, 
(SELECT pg.ID FROM Gets.dbo.Entities_ProductGroups pg WHERE pg.ProductGroupID = p.ProductGroupID), 
(SELECT vat.ID FROM Gets.dbo.Entities_Taxes vat WHERE vat.TaxID = p.VatID), 
(SELECT st.ID FROM Gets.dbo.Entities_Taxes st WHERE st.TaxID = p.ServiceTaxID), 
ServiceCharge, IsScTaxable, Price, FullPrice, Discontinued, SortOrder, BaseCode, Quantity
from Gets.dbo.Entities_Products AS p;

INSERT INTO Inventories (InventoryID, KotID, ProductID, Quantity, Price, FullPrice, ServiceCharge, IsScTaxable, ServiceTaxRate, VatRate, ServiceTaxID, VatID, Discount)
SELECT ID, 
(SELECT k.ID FROM Gets.dbo.Entities_Kots k WHERE k.KotID = i.KotID), 
(SELECT p.ID FROM Gets.dbo.Entities_Products p WHERE p.ProductID = i.ProductID), 
Quantity, Price, FullPrice, ServiceCharge, IsScTaxable, ServiceTax, Vat, 
(SELECT s.ID FROM Gets.dbo.Entities_Taxes s WHERE s.Rate = i.ServiceTax), 
(SELECT v.ID FROM Gets.dbo.Entities_Taxes v WHERE v.Rate = i.Vat), 
Discount
from Gets.dbo.Entities_Inventories as i;

INSERT INTO InventoryModifiers (InventoryModifierID, InventoryID, ModifierID)
SELECT ID, 
(SELECT i.ID FROM Gets.dbo.Entities_Inventories i WHERE i.InventoryID = im.InventoryID), 
(SELECT m.ID FROM Gets.dbo.Entities_Modifiers m WHERE m.ModifierID = im.ModifierID)
from Gets.dbo.Entities_InventoryModifiers as im;

INSERT INTO Auth_RoleGroups (RoleGroupID, RoleID, GroupID)
SELECT ID, 
(SELECT r.ID FROM Gets.dbo.Auth_Roles r WHERE r.RoleID = rg.RoleID),
(SELECT g.ID FROM Gets.dbo.Auth_Groups g WHERE g.GroupID = rg.GroupID)
from Gets.dbo.Auth_RoleGroups as rg;

INSERT INTO Auth_UserGroups (UserGroupID, UserID, GroupID)
SELECT ID, 
(SELECT u.ID FROM Gets.dbo.Auth_Users u WHERE u.UserID = ug.UserID), 
(SELECT g.ID FROM Gets.dbo.Auth_Groups g WHERE g.GroupID = ug.GroupID)
from Gets.dbo.Auth_UserGroups as ug;

INSERT INTO ProductGroupModifiers (ProductGroupModifierID, ProductGroupID, ModifierID, ShowAutomatically)
SELECT ID, 
(SELECT pg.ID FROM Gets.dbo.Entities_ProductGroups pg WHERE pg.ProductGroupID = pgm.ProductGroupID), 
(SELECT m.ID FROM Gets.dbo.Entities_Modifiers m WHERE m.ModifierID = pgm.ModifierID),
ShowAutomatically
from Gets.dbo.Entities_ProductGroupModifiers as pgm;

INSERT INTO PrintLocations (PrintLocationID, ProductGroupID, Location, Printer, Copies, CutCode)
SELECT ID, 
(SELECT pg.ID FROM Gets.dbo.Entities_ProductGroups pg WHERE pg.ProductGroupID = pl.ProductGroupID), 
Location, Printer, Copies, CutCode
from Gets.dbo.Entities_PrintLocations as pl;

INSERT INTO Reprints (ReprintID, UserID, Date, VoucherID)
SELECT ID, 
(SELECT u.ID FROM Gets.dbo.Auth_Users u WHERE u.UserID = r.UserID),
 Date, 
(SELECT v.ID FROM Gets.dbo.Entities_Vouchers v WHERE v.VoucherID = r.VoucherID)
from Gets.dbo.Entities_Reprints as r;

INSERT INTO VoucherSettlements (VoucherSettlementID, VoucherID, Settled, Amount)
SELECT ID, 
(SELECT v.ID FROM Gets.dbo.Entities_Vouchers v WHERE v.VoucherID = s.VoucherID),
Settled, Amount
from Gets.dbo.Entities_VoucherSettlements as s;

INSERT INTO FoodTables (FoodTableID, Name, Location, Status, VoucherID, SortOrder)
SELECT ID, Name, Location, Status, 
(SELECT v.ID FROM Gets.dbo.Entities_Vouchers v WHERE v.VoucherID = ft.VoucherID),
FoodTableID
from Gets.dbo.Entities_FoodTables as ft;

COMMIT