narsil/Sql/Remove Companies.sql
2010-03-02 23:26:21 +05:30

676 lines
14 KiB
Transact-SQL

BEGIN TRANSACTION
GO
ALTER TABLE dbo.BasicTransactions
DROP CONSTRAINT FK_BasicTransactions_Companies
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.BasicTransactions
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CostCenters
DROP CONSTRAINT FK_CostCenters_Companies
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CostCenters
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Customers
DROP CONSTRAINT FK_Customers_Companies
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Customers
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Employees
DROP CONSTRAINT FK_Employees_Companies
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Employees
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Inventory
DROP CONSTRAINT FK_Inventory_Companies
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Inventory
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Ledgers
DROP CONSTRAINT FK_Ledgers_Companies
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Ledgers
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Products
DROP CONSTRAINT FK_Products_Companies
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Products
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.ProductTypes
DROP CONSTRAINT FK_ProductTypes_Companies
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.ProductTypes
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Roles
DROP CONSTRAINT FK_Roles_Companies
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Roles
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Tax
DROP CONSTRAINT FK_Tax_Companies
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Tax
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
DROP TABLE dbo.TransactionContra
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.TransactionSale
DROP CONSTRAINT FK_TransactionSale_Companies
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.TransactionSale
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.UserRoles
DROP CONSTRAINT IX_UserRoles
GO
ALTER TABLE dbo.UserRoles ADD CONSTRAINT
IX_UserRoles UNIQUE NONCLUSTERED
(
UserID,
Role
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE dbo.UserRoles
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Users
DROP CONSTRAINT FK_Users_Companies
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Users
DROP COLUMN CompanyID
GO
COMMIT
BEGIN TRANSACTION
GO
DROP TABLE Companies
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Waiters
(
WaiterID uniqueidentifier NOT NULL,
Name nvarchar(200) NOT NULL,
timestamp timestamp NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE dbo.Waiters ADD CONSTRAINT
PK_Waiters PRIMARY KEY CLUSTERED
(
WaiterID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IX_Waiters_Name ON dbo.Waiters
(
Name
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
COMMIT
INSERT INTO Waiters (WaiterID, Name)
SELECT l.LedgerID, l.Name FROM Ledgers l INNER JOIN Employees e ON l.LedgerID = e.LedgerID ORDER BY Name
BEGIN TRANSACTION
GO
ALTER TABLE dbo.TransactionSale
DROP CONSTRAINT FK_TransactionSale_Employees
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.TransactionSale ADD CONSTRAINT
FK_TransactionSale_Waiters FOREIGN KEY
(
WaiterID
) REFERENCES dbo.Waiters
(
WaiterID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CashPayments
DROP CONSTRAINT FK_CashPayments_Employees
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CashPayments ADD CONSTRAINT
FK_CashPayments_Users FOREIGN KEY
(
CashierID
) REFERENCES dbo.Users
(
UserID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
COMMIT
DROP TABLE CashPayments
DROP TABLE Employees
DELETE FROM Ledgers WHERE Type = 'S'
BEGIN TRANSACTION
GO
ALTER TABLE dbo.BasicTransactions
DROP CONSTRAINT FK_BasicTransactions_Users_PosterID
GO
COMMIT
BEGIN TRANSACTION
GO
EXECUTE sp_rename N'dbo.BasicTransactions', N'Vouchers', 'OBJECT'
GO
EXECUTE sp_rename N'dbo.Vouchers.TransactionID', N'VoucherID', 'COLUMN'
GO
ALTER TABLE dbo.Vouchers
DROP COLUMN Posted, Review, PosterID, Autolocked
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Inventory
DROP CONSTRAINT FK_Inventory_BasicTransactions
GO
ALTER TABLE dbo.TransactionSale
DROP CONSTRAINT FK_TransactionSale_BasicTransactions
GO
ALTER TABLE dbo.Vouchers
DROP CONSTRAINT IX_BasicTransactions_Code_Type
GO
ALTER TABLE dbo.Vouchers
DROP CONSTRAINT PK_BasicTransactions
GO
ALTER TABLE dbo.Vouchers ADD CONSTRAINT
PK_Vouchers PRIMARY KEY CLUSTERED
(
VoucherID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE dbo.Vouchers ADD CONSTRAINT
IX_Vouchers_Code_Type UNIQUE NONCLUSTERED
(
Code,
Type
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.TransactionSale ADD CONSTRAINT
FK_TransactionSale_BasicTransactions FOREIGN KEY
(
TransactionID
) REFERENCES dbo.Vouchers
(
VoucherID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Inventory ADD CONSTRAINT
FK_Inventory_BasicTransactions FOREIGN KEY
(
TransactionID
) REFERENCES dbo.Vouchers
(
VoucherID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.TransactionSale
DROP CONSTRAINT FK_TransactionSale_BasicTransactions
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.TransactionSale
DROP CONSTRAINT FK_TransactionSale_Waiters
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.TransactionSale
DROP CONSTRAINT FK_TransactionSale_Customers
GO
COMMIT
BEGIN TRANSACTION
GO
EXECUTE sp_rename N'dbo.TransactionSale', N'SaleVoucher', 'OBJECT'
GO
EXECUTE sp_rename N'dbo.SaleVoucher.TransactionID', N'VoucherID', 'COLUMN'
GO
ALTER TABLE dbo.SaleVoucher ADD CONSTRAINT
FK_SaleVoucher_Customers FOREIGN KEY
(
CustomerID
) REFERENCES dbo.Customers
(
CustomerID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
ALTER TABLE dbo.SaleVoucher ADD CONSTRAINT
FK_SaleVoucher_Waiters FOREIGN KEY
(
WaiterID
) REFERENCES dbo.Waiters
(
WaiterID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
ALTER TABLE dbo.SaleVoucher ADD CONSTRAINT
FK_SaleVoucher_Vouchers FOREIGN KEY
(
VoucherID
) REFERENCES dbo.Vouchers
(
VoucherID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Inventory
DROP CONSTRAINT FK_Inventory_BasicTransactions
GO
COMMIT
BEGIN TRANSACTION
GO
EXECUTE sp_rename N'dbo.Inventory.TransactionID', N'VoucherID', 'COLUMN'
GO
ALTER TABLE dbo.Inventory ADD CONSTRAINT
FK_Inventory_Vouchers FOREIGN KEY
(
VoucherID
) REFERENCES dbo.Vouchers
(
VoucherID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
COMMIT
GO
DROP PROCEDURE [dbo].[BasicTransactions_Insert]
DROP PROCEDURE [dbo].[BasicTransactions_Update]
GO
-- Product Sorting
UPDATE Products
SET SortOrder = inv.Sort
FROM (SELECT ProductID, Count(*) AS Sort FROM Inventory GROUP BY ProductID) AS inv
WHERE Products.ProductID = inv.ProductID
GO
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Vouchers
DROP CONSTRAINT DF_BasicTransactions_TransactionID
GO
COMMIT
GO
DROP TRIGGER [dbo].[BasicTransactions_Update_Auto]
GO
DROP TRIGGER [dbo].[BasicTransactions_Insert_Auto]
GO
DROP PROCEDURE [dbo].[TransactionSale_Update]
GO
DROP PROCEDURE [dbo].[TransactionSale_Insert]
GO
CREATE PROCEDURE [dbo].[SaleVoucher_Update]
(
@VoucherID uniqueidentifier,
@BillID nvarchar(10) OUTPUT,
@TableID nvarchar(10),
@WaiterID uniqueidentifier,
@CustomerID uniqueidentifier,
@Paid bit,
@Void bit,
@VoidReason nvarchar(50),
@Printed bit OUTPUT,
@Alarm datetime,
@KotID nvarchar(10) OUTPUT,
@CreditCard bit,
@IsImportant bit,
@timestamp timestamp OUTPUT
)
AS
BEGIN
IF @Printed = 1 AND (SELECT Printed FROM SaleVoucher WHERE VoucherID = @VoucherID) = 0
BEGIN
SELECT @BillID = ISNULL(CAST(MAX(CAST(REPLACE(BillID, '-', '') AS int)) + 1 AS nvarchar(9)), '010001') FROM SaleVoucher WHERE BillID LIKE '__-____'
IF LEN(@BillID) = 5
SET @BillID = '0' + @BillID
SET @BillID = SUBSTRING(@BillID, 1, 2) + '-' + SUBSTRING(@BillID, 3, 7)
IF SUBSTRING(@BillID,3,7) = '-0000'
SET @BillID = SUBSTRING(@BillID, 1, 2) + '-0001'
END
ELSE
BEGIN
SELECT @BillID = BillID FROM SaleVoucher WHERE VoucherID = @VoucherID
END
IF @TableID = ''
BEGIN
SELECT @TableID = 'C' + CAST(ISNULL(MAX(CAST(SUBSTRING(TableID, 2, 10) AS int)), 0) + 1 AS nvarchar(10)) FROM SaleVoucher WHERE TableID LIKE 'C%' AND Paid = 0 AND Void = 0
END
IF (SELECT Printed FROM SaleVoucher WHERE VoucherID = @VoucherID) = 1
SET @Printed = 1
UPDATE [dbo].[SaleVoucher] SET
[BillID] = @BillID,
[TableID] = @TableID,
[WaiterID] = @WaiterID,
[CustomerID] = @CustomerID,
[Paid] = @Paid,
[Void] = @Void,
[VoidReason] = @VoidReason,
[Printed] = @Printed,
[Alarm] = @Alarm,
[CreditCard] = @CreditCard,
[IsImportant] = @IsImportant
WHERE VoucherID = @VoucherID
UPDATE Vouchers SET LastEditDate = GETDATE() WHERE VoucherID = @VoucherID
SELECT @timestamp = timestamp, @KotID = KotID FROM [dbo].[SaleVoucher] WHERE VoucherID = @VoucherID
END
GO
CREATE PROCEDURE [dbo].[SaleVoucher_Insert]
(
@VoucherID uniqueidentifier,
@BillID nvarchar(10) OUTPUT,
@TableID nvarchar(10),
@WaiterID uniqueidentifier,
@CustomerID uniqueidentifier,
@Paid bit,
@Void bit,
@VoidReason nvarchar(50),
@Printed bit,
@Alarm datetime,
@KotID nvarchar(10) OUTPUT,
@CreditCard bit,
@IsImportant bit,
@timestamp timestamp OUTPUT
)
AS
BEGIN
SELECT @KotID = ISNULL('K-' + CAST(MAX(CAST(SUBSTRING(KotID, 3,8) AS int)) + 1 AS nvarchar(8)), 'K-1') FROM [dbo].[SaleVoucher]
IF @Printed = 1
BEGIN
SELECT @BillID = ISNULL(CAST(MAX(CAST(REPLACE(BillID, '-', '') AS int)) + 1 AS nvarchar(9)), '010001') FROM SaleVoucher WHERE BillID LIKE '__-____'
IF LEN(@BillID) = 5
SET @BillID = '0' + @BillID
SET @BillID = SUBSTRING(@BillID, 1, 2) + '-' + SUBSTRING(@BillID, 3, 7)
IF SUBSTRING(@BillID,3,7) = '-0000'
SET @BillID = SUBSTRING(@BillID, 1, 2) + '-0001'
END
ELSE
BEGIN
SET @BillID = @KotID
END
IF @TableID = ''
BEGIN
SELECT @TableID = 'C' + CAST(ISNULL(MAX(CAST(SUBSTRING(TableID, 2, 10) AS int)), 0) + 1 AS nvarchar(10)) FROM SaleVoucher WHERE TableID LIKE 'C%' AND Paid = 0 AND Void = 0
END
INSERT INTO [dbo].[SaleVoucher] ([VoucherID], [BillID], [TableID], [WaiterID], [CustomerID], [Paid], [Void], [VoidReason], [Printed], [Alarm], [KotID], [CreditCard] ,[IsImportant])
VALUES (@VoucherID, @BillID, @TableID, @WaiterID, @CustomerID, @Paid, @Void, @VoidReason, @Printed, @Alarm, @KotID, @CreditCard, @IsImportant)
SELECT @timestamp = timestamp FROM [dbo].[SaleVoucher] WHERE VoucherID = @VoucherID
END
GO
-- Waiters
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_Waiters
(
WaiterID uniqueidentifier NOT NULL,
Code int NULL,
Name nvarchar(200) NOT NULL,
timestamp timestamp NOT NULL
) ON [PRIMARY]
GO
IF EXISTS(SELECT * FROM dbo.Waiters)
EXEC('INSERT INTO dbo.Tmp_Waiters (WaiterID, Name)
SELECT WaiterID, Name FROM dbo.Waiters WITH (HOLDLOCK TABLOCKX)')
GO
ALTER TABLE dbo.SaleVoucher
DROP CONSTRAINT FK_SaleVoucher_Waiters
GO
DROP TABLE dbo.Waiters
GO
EXECUTE sp_rename N'dbo.Tmp_Waiters', N'Waiters', 'OBJECT'
GO
ALTER TABLE dbo.Waiters ADD CONSTRAINT
PK_Waiters PRIMARY KEY CLUSTERED
(
WaiterID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IX_Waiters_Name ON dbo.Waiters
(
Name
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.SaleVoucher ADD CONSTRAINT
FK_SaleVoucher_Waiters FOREIGN KEY
(
WaiterID
) REFERENCES dbo.Waiters
(
WaiterID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
COMMIT
-- Add Waiter
DECLARE @Code int
SELECT @Code = 1
UPDATE Waiters SET @Code = Code = @Code + 1
INSERT INTO Waiters (WaiterID, Code, Name)
VALUES (NEWID(), 1, 'Waiter');
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_Waiters
(
WaiterID uniqueidentifier NOT NULL,
Code int NOT NULL,
Name nvarchar(200) NOT NULL,
timestamp timestamp NOT NULL
) ON [PRIMARY]
GO
IF EXISTS(SELECT * FROM dbo.Waiters)
EXEC('INSERT INTO dbo.Tmp_Waiters (WaiterID, Code, Name)
SELECT WaiterID, Code, Name FROM dbo.Waiters WITH (HOLDLOCK TABLOCKX)')
GO
ALTER TABLE dbo.SaleVoucher
DROP CONSTRAINT FK_SaleVoucher_Waiters
GO
DROP TABLE dbo.Waiters
GO
EXECUTE sp_rename N'dbo.Tmp_Waiters', N'Waiters', 'OBJECT'
GO
ALTER TABLE dbo.Waiters ADD CONSTRAINT
PK_Waiters PRIMARY KEY CLUSTERED
(
WaiterID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IX_Waiters_Name ON dbo.Waiters
(
Name
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX IX_Waiters ON dbo.Waiters
(
Code
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.SaleVoucher ADD CONSTRAINT
FK_SaleVoucher_Waiters FOREIGN KEY
(
WaiterID
) REFERENCES dbo.Waiters
(
WaiterID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
COMMIT
-- Waiter Role
INSERT INTO Roles (Role, Description) VALUES ('Waiter/Master', 'Add / Edit / Delete Waiters')
--INSERT INTO Roles (Role, Description) VALUES ('Log/View', 'View the event Log')
---- User Modification
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Users
DROP CONSTRAINT DF_Users_CreationDate
GO
ALTER TABLE dbo.Users
DROP COLUMN Email, Approved, Question, Answer, Comment, CreationDate, LastLoginDate, LastActivityDate, LastPasswordChangedDate, LastLockoutDate
GO
COMMIT