narsil/Sql/2014.10.12 Step 1. Create N...

436 lines
13 KiB
Transact-SQL

USE [Sales]
GO
create table dbo.Vouchers (
VoucherID UNIQUEIDENTIFIER not null,
Date DATETIME not null,
Pax INT null,
UserID UNIQUEIDENTIFIER not null,
CreationDate DATETIME not null,
LastEditDate DATETIME not null,
BillID int,
TableID UNIQUEIDENTIFIER not null,
WaiterID UNIQUEIDENTIFIER not null,
CustomerID UNIQUEIDENTIFIER not null,
Narration NVARCHAR(255) null,
Void BIT not null,
VoidReason NVARCHAR(255) null,
Printed BIT not null,
VoucherType INT not null,
KotID int not null,
primary key (VoucherID)
)
create table dbo.Auth_Users (
UserID UNIQUEIDENTIFIER not null,
Name NVARCHAR(255) not null unique,
MsrString NVARCHAR(255) null,
Password NVARCHAR(255) not null,
LockedOut BIT not null,
primary key (UserID)
)
create table dbo.Waiters (
WaiterID UNIQUEIDENTIFIER not null,
Name NVARCHAR(255) not null unique,
primary key (WaiterID)
)
create table dbo.Customers (
CustomerID UNIQUEIDENTIFIER not null,
Name NVARCHAR(255) not null unique,
Address NVARCHAR(255) null,
Important BIT null,
Phone NVARCHAR(255) null,
Remarks NVARCHAR(255) null,
primary key (CustomerID)
)
create table dbo.Auth_UserGroups (
UserGroupID UNIQUEIDENTIFIER not null,
UserID UNIQUEIDENTIFIER not null,
GroupID UNIQUEIDENTIFIER not null,
primary key (UserGroupID)
)
create table dbo.Kots (
KotID UNIQUEIDENTIFIER not null,
VoucherID UNIQUEIDENTIFIER not null,
Code int not null unique,
TableID UNIQUEIDENTIFIER not null,
Printed BIT not null,
[Date] DATETIME not null,
UserID UNIQUEIDENTIFIER not null,
primary key (KotID)
)
create table dbo.Reprints (
ReprintID UNIQUEIDENTIFIER not null,
UserID UNIQUEIDENTIFIER not null,
[Date] DATETIME not null,
VoucherID UNIQUEIDENTIFIER not null,
primary key (ReprintID)
)
create table dbo.Inventories (
InventoryID UNIQUEIDENTIFIER not null,
KotID UNIQUEIDENTIFIER not null,
ProductID UNIQUEIDENTIFIER not null,
SortOrder int not null,
Quantity DECIMAL(19,5) not null,
Price DECIMAL(19,5) not null,
FullPrice DECIMAL(19,5) not null,
ServiceCharge DECIMAL(19,5) not null,
IsScTaxable BIT not null,
ServiceTaxRate DECIMAL(19,5) not null,
VatRate DECIMAL(19,5) not null,
ServiceTaxID UNIQUEIDENTIFIER not null,
VatID UNIQUEIDENTIFIER not null,
Discount DECIMAL(19,5) not null,
Amount AS (case when [IsScTaxable]=(1) then ((([Quantity]*[Price])*((1)-[Discount]))*((1)+[ServiceCharge]))*(((1)+[ServiceTaxRate])+[VatRate]) else (([Quantity]*[Price])*((1)-[Discount]))*((((1)+[ServiceCharge])+[ServiceTaxRate])+[VatRate]) end),
primary key (InventoryID)
)
create table dbo.InventoryModifiers (
InventoryModifierID UNIQUEIDENTIFIER not null,
InventoryID UNIQUEIDENTIFIER not null,
ModifierID UNIQUEIDENTIFIER not null,
primary key (InventoryModifierID)
)
create table dbo.Auth_Groups (
GroupID UNIQUEIDENTIFIER not null,
Name NVARCHAR(255) not null unique,
primary key (GroupID)
)
create table dbo.Auth_RoleGroups (
RoleGroupID UNIQUEIDENTIFIER not null,
RoleID UNIQUEIDENTIFIER not null,
GroupID UNIQUEIDENTIFIER not null,
primary key (RoleGroupID)
)
create table dbo.Products (
ProductID UNIQUEIDENTIFIER not null,
Code INT null,
Name NVARCHAR(255) not null,
Units NVARCHAR(255) not null,
ProductGroupID UNIQUEIDENTIFIER null,
VatID UNIQUEIDENTIFIER null,
ServiceTaxID UNIQUEIDENTIFIER null,
ServiceCharge DECIMAL(19,5) not null,
IsScTaxable BIT not null,
Price DECIMAL(19,5) not null,
FullPrice DECIMAL(19,5) not null,
IsActive BIT not null,
IsNotAvailable BIT not null,
SortOrder INT not null,
BaseCode INT not null,
Quantity DECIMAL(19,5) not null,
primary key (ProductID),
unique (Name, Units)
)
create table dbo.Modifiers (
ModifierID UNIQUEIDENTIFIER not null,
Name NVARCHAR(255) not null unique,
ShowInBill BIT not null,
Price DECIMAL(19,5) not null,
primary key (ModifierID)
)
create table dbo.ProductGroupModifiers (
ProductGroupModifierID UNIQUEIDENTIFIER not null,
ProductGroupID UNIQUEIDENTIFIER null,
ModifierID UNIQUEIDENTIFIER null,
ShowAutomatically BIT not null,
primary key (ProductGroupModifierID)
)
create table dbo.Auth_Roles (
RoleID UNIQUEIDENTIFIER not null,
Name NVARCHAR(255) not null unique,
primary key (RoleID)
)
create table dbo.ProductGroups (
ProductGroupID UNIQUEIDENTIFIER not null,
Name NVARCHAR(255) not null unique,
DiscountLimit DECIMAL(19,5) not null,
IsModifierCompulsory BIT not null,
IsActive BIT not null,
SortOrder INT not null,
GroupType NVARCHAR(255) not null,
primary key (ProductGroupID)
)
create table dbo.PrintLocations (
PrintLocationID UNIQUEIDENTIFIER not null,
ProductGroupID UNIQUEIDENTIFIER null,
Location NVARCHAR(255) null,
Printer NVARCHAR(255) null,
Copies INT null,
CutCode NVARCHAR(255) null,
primary key (PrintLocationID)
)
create table dbo.Taxes (
TaxID UNIQUEIDENTIFIER not null,
Name NVARCHAR(255) not null unique,
Rate DECIMAL(19,5) not null,
primary key (TaxID)
)
create table dbo.VoucherSettlements (
VoucherSettlementID UNIQUEIDENTIFIER not null,
VoucherID UNIQUEIDENTIFIER not null,
Settled INT not null,
Amount DECIMAL(19,5) not null,
primary key (VoucherSettlementID)
)
create table dbo.FoodTables (
FoodTableID UNIQUEIDENTIFIER not null,
Name NVARCHAR(255) not null unique,
IsActive BIT not null,
Location NVARCHAR(255) null,
Status NVARCHAR(255) null,
SortOrder INT not null,
VoucherID UNIQUEIDENTIFIER null,
primary key (FoodTableID)
)
alter table dbo.Vouchers
add constraint FK415A8D77B9E9332A
foreign key (UserID)
references dbo.Auth_Users
alter table dbo.Vouchers
add constraint FK415A8D77B9E93DDD
foreign key (TableID)
references dbo.FoodTables
alter table dbo.Kots
add constraint FK415A8D77B9E93BBB
foreign key (TableID)
references dbo.FoodTables
alter table dbo.Vouchers
add constraint FK415A8D773E16E17E
foreign key (WaiterID)
references dbo.Waiters
alter table dbo.Vouchers
add constraint FK415A8D7737EDAED4
foreign key (CustomerID)
references dbo.Customers
alter table dbo.Auth_UserGroups
add constraint FKF726684AB9E9332A
foreign key (UserID)
references dbo.Auth_Users
alter table dbo.Auth_UserGroups
add constraint FKF726684A26F423BC
foreign key (GroupID)
references dbo.Auth_Groups
alter table dbo.Kots
add constraint FKF9C1219798E59CD2
foreign key (VoucherID)
references dbo.Vouchers
alter table dbo.Kots
add constraint FKF9C12197B9E9332A
foreign key (UserID)
references dbo.Auth_Users
alter table dbo.Reprints
add constraint FK5F5D4BA5B9E9332A
foreign key (UserID)
references dbo.Auth_Users
alter table dbo.Reprints
add constraint FK5F5D4BA598E59CD2
foreign key (VoucherID)
references dbo.Vouchers
alter table dbo.Inventories
add constraint FK8C0CFB22DB70F42
foreign key (KotID)
references dbo.Kots
alter table dbo.Inventories
add constraint FK8C0CFB223F88CAB6
foreign key (ProductID)
references dbo.Products
alter table dbo.Inventories
add constraint FK8C0CFB221DECC269
foreign key (ServiceTaxID)
references dbo.Taxes
alter table dbo.Inventories
add constraint FK8C0CFB22EB4DE5BC
foreign key (VatID)
references dbo.Taxes
alter table dbo.InventoryModifiers
add constraint FK80820FB4BA29671A
foreign key (InventoryID)
references dbo.Inventories
alter table dbo.InventoryModifiers
add constraint FK80820FB4C0C73B24
foreign key (ModifierID)
references dbo.Modifiers
alter table dbo.Auth_RoleGroups
add constraint FK656F81475D2D5938
foreign key (RoleID)
references dbo.Auth_Roles
alter table dbo.Auth_RoleGroups
add constraint FK656F814726F423BC
foreign key (GroupID)
references dbo.Auth_Groups
alter table dbo.Products
add constraint FK4065562A95976D16
foreign key (ProductGroupID)
references dbo.ProductGroups
alter table dbo.Products
add constraint FK4065562AEB4DE5BC
foreign key (VatID)
references dbo.Taxes
alter table dbo.Products
add constraint FK4065562A1DECC269
foreign key (ServiceTaxID)
references dbo.Taxes
alter table dbo.ProductGroupModifiers
add constraint FK445AB60E95976D16
foreign key (ProductGroupID)
references dbo.ProductGroups
alter table dbo.ProductGroupModifiers
add constraint FK445AB60EC0C73B24
foreign key (ModifierID)
references dbo.Modifiers
alter table dbo.PrintLocations
add constraint FK8AE807795976D16
foreign key (ProductGroupID)
references dbo.ProductGroups
alter table dbo.VoucherSettlements
add constraint FK61CD43EC98E59CD2
foreign key (VoucherID)
references dbo.Vouchers
GO
CREATE PROCEDURE KotInsert
@VoucherID uniqueidentifier,
@TableID uniqueidentifier,
@Printed bit,
@UserID uniqueidentifier,
@KotID uniqueidentifier
AS
BEGIN
DECLARE @Date datetime;
DECLARE @Code int;
SELECT @Date = GETDATE();
SELECT @Code = ISNULL(MAX(Code) + 1, 1) FROM Kots
INSERT INTO Kots (KotID, VoucherID, Code, TableID, Printed, Date, UserID)
VALUES (@KotID, @VoucherID, @Code, @TableID, @Printed, @Date, @UserID);
END
GO
CREATE PROCEDURE ReprintInsert
@UserID uniqueidentifier,
@VoucherID uniqueidentifier,
@ReprintID uniqueidentifier
AS
BEGIN
DECLARE @Date datetime;
SELECT @Date = GETDATE();
INSERT INTO Reprints (ReprintID, UserID, Date, VoucherID)
VALUES (@ReprintID, @UserID, @Date, @VoucherID);
END
GO
CREATE PROCEDURE VoucherInsert
@Pax int,
@UserID uniqueidentifier,
@TableID uniqueidentifier,
@WaiterID uniqueidentifier,
@CustomerID uniqueidentifier,
@Narration nvarchar(255),
@Void bit,
@VoidReason nvarchar(255),
@Printed bit,
@VoucherType int,
@VoucherID uniqueidentifier
AS
BEGIN
DECLARE @Date datetime;
DECLARE @BillID int;
DECLARE @KotID int;
SELECT @Date = GETDATE(), @BillID = null;
SELECT @KotID = ISNULL(MAX(KotID) + 1, 1) FROM Vouchers;
IF @Printed = 1
BEGIN
IF @VoucherType IN (1,3)
SELECT @BillID = ISNULL(MAX(BillID) + 1, 1) FROM Vouchers WHERE VoucherType IN (1, 3);
ELSE
SELECT @BillID = ISNULL(MAX(BillID) + 1, 1) FROM Vouchers WHERE VoucherType = @VoucherType;
IF @BillID % 10000 = 0
SELECT @BillID = @BillID + 1;
END
INSERT INTO Vouchers (VoucherID, Date, Pax, UserID, CreationDate, LastEditDate, BillID, TableID, WaiterID, CustomerID, Narration, Void, VoidReason, Printed, VoucherType, KotID)
VALUES (@VoucherID, @Date, @Pax, @UserID, @Date, @Date, @BillID, @TableID, @WaiterID, @CustomerID, @Narration, @Void, @VoidReason, @Printed, @VoucherType, @KotID);
END
GO
CREATE PROCEDURE VoucherUpdate
@Pax int,
@UserID uniqueidentifier,
@TableID uniqueidentifier,
@WaiterID uniqueidentifier,
@CustomerID uniqueidentifier,
@Narration nvarchar(255),
@Void bit,
@VoidReason nvarchar(255),
@Printed bit,
@VoucherType int,
@VoucherID uniqueidentifier
AS
BEGIN
DECLARE @LastEditDate datetime;
DECLARE @Date datetime;
DECLARE @BillID int;
SELECT @Printed = CASE WHEN @Printed = 1 THEN 1 ELSE Printed END,
@BillID = BillID, @Date = Date, @LastEditDate = GETDATE()
FROM Vouchers WHERE VoucherID = @VoucherID;
IF @Printed = 1 AND @BillID IS NULL
BEGIN
IF @VoucherType IN (1,3)
SELECT @BillID = ISNULL(MAX(BillID) + 1, 1) FROM Vouchers WHERE VoucherType IN (1, 3);
ELSE
SELECT @BillID = ISNULL(MAX(BillID) + 1, 1) FROM Vouchers WHERE VoucherType = @VoucherType;
IF @BillID % 10000 = 0
SELECT @BillID = @BillID + 1;
SELECT @Date = @LastEditDate;
END
UPDATE Vouchers SET Pax = @Pax, UserID = @UserID, TableID = @TableID,
WaiterID = @WaiterID, CustomerID = @CustomerID, Narration = @Narration, Void = @Void,
VoidReason = @VoidReason, Printed = @Printed, VoucherType = @VoucherType, LastEditDate = @LastEditDate,
Date = @Date, BillID = @BillID
WHERE VoucherID = @VoucherID;
END
GO