﻿CREATE TABLE [dbo].[ControllerAccessDesc](
	[ContAccId] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[Action] [varchar](150) NOT NULL,
	[Controller] [varchar](150) NOT NULL,
	[ToolTipName] [varchar](150) NOT NULL,
	[IconClassId] [int] NULL,
	[ElementMenuId] [int] NULL,
	[CreatedBy] [varchar](150) NOT NULL,
	[UpdatedBy] [varchar](150) NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[UpdatedDate] [datetime] NOT NULL,
)
CREATE TABLE [dbo].[UserAccess](
	[UserAccessId] bigint IDENTITY(1,1) NOT NULL PRIMARY KEY,
	ContAccId INT NOT NULL,
	ControllerAccessId INT NOT NULL,
	[AccessTypeId] [int] NOT NULL,
	ToolTipName [varchar](150) NOT NULL,
	[CreatedBy] [varchar](150) NOT NULL,
	[UpdatedBy] [varchar](150) NOT NULL,
	[CreatedDate] [datetime] NOT NULL,
	[UpdatedDate] [datetime] NOT NULL,
	IsActive BIT DEFAULT(1),
	FOREIGN KEY (ContAccId) REFERENCES [dbo].[ControllerAccessDesc] (ContAccId) ,
	FOREIGN KEY (ControllerAccessId) REFERENCES [dbo].[ControllerAccess] (ControllerAccessId) 
)
CREATE TABLE Approver (
	ApproverId SMALLINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ApprovalStageId TINYINT NOT NULL,
	UserId NVARCHAR(450),
	IsActive BIT DEFAULT 1,
	CreatedDate datetime DEFAULT GETDATE(),
	CreatedBy VARCHAR(100),
	UpdatedBy VARCHAR(100),	
	UpdatedDate datetime DEFAULT GETDATE(),
	FOREIGN KEY (ApprovalStageId) REFERENCES ApprovalStages(ApprovalStageId),
	FOREIGN KEY (UserId) REFERENCES Users(Id),
);
CREATE TABLE DocType (
	DocTypeId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	DocTypeName VARCHAR(10),
);
CREATE TABLE ApprovalStages (
	ApprovalStageId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ApprovalStageName VARCHAR(100) NOT NULL,
	IsActive BIT DEFAULT 1,
);
CREATE TABLE PackagingTypes (
	PackagingTypeId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	PackagingTypeName VARCHAR(100) NOT NULL,
	IsActive BIT DEFAULT 1,
	CreatedDate datetime DEFAULT GETDATE(),
	CreatedBy VARCHAR(100),
	UpdatedBy VARCHAR(100),	
	UpdatedDate datetime DEFAULT GETDATE(),
);
CREATE TABLE PRTypes (
	PRTypeId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	PRTypeName VARCHAR(100) NOT NULL,
	IsActive BIT DEFAULT 1,
	CreatedDate datetime DEFAULT GETDATE(),
	CreatedBy VARCHAR(100),
	UpdatedBy VARCHAR(100),	
	UpdatedDate datetime DEFAULT GETDATE(),
);
CREATE TABLE Priorities (
	UrgencyId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	UrgencyName VARCHAR(100) NOT NULL,
	IsActive BIT DEFAULT 1,
	CreatedDate datetime DEFAULT GETDATE(),
	CreatedBy VARCHAR(100),
	UpdatedBy VARCHAR(100),	
	UpdatedDate datetime DEFAULT GETDATE(),
);
CREATE TABLE Companies (
	CompanyId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	CompanyName VARCHAR(100) NOT NULL,
	IsActive BIT DEFAULT 1,
	CreatedDate datetime DEFAULT GETDATE(),
	CreatedBy VARCHAR(100),
	UpdatedBy VARCHAR(100),	
	UpdatedDate datetime DEFAULT GETDATE(),
	foreign key (AddressId) references CompanyAddress(AddressId),
);
CREATE TABLE CompanyAddresses (
	AddressId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	Address1 VARCHAR(500) NOT NULL,
	Address2 VARCHAR(500) NOT NULL,
	Address3 VARCHAR(500) NOT NULL,
	IsActive BIT DEFAULT 1,
	CreatedDate datetime DEFAULT GETDATE(),
	CreatedBy VARCHAR(100),
	UpdatedBy VARCHAR(100),	
	UpdatedDate datetime DEFAULT GETDATE(),
);
CREATE TABLE ItemClass (
	ItemClassId SMALLINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ItemClassName VARCHAR(50) NOT NULL,
	ItemClassDescript VARCHAR(150) NOT NULL,
	IsActive BIT DEFAULT 1,
	CreatedDate datetime DEFAULT GETDATE(),
	CreatedBy VARCHAR(100),
	UpdatedBy VARCHAR(100),	
	UpdatedDate datetime DEFAULT GETDATE(),
);
CREATE TABLE ItemType (
	ItemTypeId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ItemTypeName VARCHAR(50) NOT NULL,
	ItemTypeDescript VARCHAR(150) NOT NULL,
	IsActive BIT DEFAULT 1,
	CreatedDate datetime DEFAULT GETDATE(),
	CreatedBy VARCHAR(100),
	UpdatedBy VARCHAR(100),	
	UpdatedDate datetime DEFAULT GETDATE(),
);
CREATE TABLE ItemCode (
	ItemCodeId BIGINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ItemName VARCHAR(200) NOT NULL,
	ItemDescription VARCHAR(500) NOT NULL,
	DepartmentId INT NOT NULL,
	ItemCategoryId  SMALLINT NOT NULL,
	Status SMALLINT NOT NULL,
	IsActive BIT DEFAULT 1,
	CreatedDate datetime DEFAULT GETDATE(),
	CreatedBy VARCHAR(100),
	UpdatedBy VARCHAR(100),	
	UpdatedDate datetime DEFAULT GETDATE(),
	FOREIGN KEY (DepartmentId) REFERENCES Departments(DepartmentId),
	FOREIGN KEY (Status) REFERENCES StatusDetails(StatusId),
	FOREIGN KEY (ItemCategoryId) REFERENCES ItemCategories(ItemCategoryId),
);
CREATE TABLE ItemColor (
	ItemColorId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ItemColorName VARCHAR(100) NOT NULL
);
CREATE TABLE RequestTypes (
	RequestTypeId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	RequestTypeName VARCHAR(100) NOT NULL,
	IsActive BIT DEFAULT 1
);
CREATE TABLE Items ( 
	[ItemNo] [bigint] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[ItemCodeId] [bigint] NOT NULL,
	[ItemLocalId] [smallint] NULL,
	[ItemTypeId] [smallint] NULL,
	[ItemClassId] [smallint] NULL,
	[UOMId] [int] NULL,
	[ItemColorId] [int] NULL,
	[UserId] [nvarchar](450) NULL,
	[PackagingTypeId] [tinyint] NULL,
	[PRTypeId] TINYINT NULL,
	[Qty] [int] NULL,
	[IsCommon] bit NULL,
	RequestTypeId TINYINT NULL,
	[IsActive] [bit] NULL,
	[CreatedDate] [datetime] NULL,
	[CreatedBy] [varchar](100) NULL,
	[UpdatedBy] [varchar](100) NULL,
	[UpdatedDate] [datetime] NULL,
	FOREIGN KEY (ItemCodeId) REFERENCES ItemCode(ItemCodeId),	
	FOREIGN KEY (ItemLocalId) REFERENCES ItemLocalization(ItemLocalId),
	FOREIGN KEY (ItemTypeId) REFERENCES ItemType(ItemTypeId),
	FOREIGN KEY (UOMId) REFERENCES UnitOfMessures(UOMId),
	FOREIGN KEY (ItemColorId) REFERENCES ItemColor(ItemColorId),
	FOREIGN KEY (RequestTypeId) REFERENCES RequestTypes(RequestTypeId),
);
CREATE TABLE ItemCategories (
	ItemCategoryId SMALLINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ItemCategoryName VARCHAR(100) NOT NULL,
	IsActive BIT DEFAULT 1
);
CREATE TABLE ItemLocalization (
	ItemLocalId SMALLINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ItemLocalName VARCHAR(150) NOT NULL,
	IsActive BIT DEFAULT 1
);
CREATE TABLE ItemAttachements (
	ItemAttachId BIGINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ItemAttachPath VARCHAR(350) NOT NULL,
	ItemNo BIGINT NOT NULL,
	ExtensionId INT NOT NULL,
	ItemCount INT,
	IsActive BIT DEFAULT 1,
	FOREIGN KEY (ItemNo) REFERENCES Items(ItemNo),
	FOREIGN KEY (ExtensionId) REFERENCES AttachmentExtensions(Id),
);
CREATE TABLE ServerPathByUserId (
	Id BIGINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ServerPath nvarchar(450),
	UserId nvarchar(450) NOT NULL,
	FOREIGN KEY (UserId) REFERENCES Users(Id),
);
CREATE TABLE ItemCart (
	ItemCartId BIGINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ItemNo BIGINT NOT NULL,
	UserId NVARCHAR(450),
	PRTypeId tinyint NULL,
	Qty int NULL,
	DepartmentId int,
	IsActive BIT DEFAULT 1,
	CreatedDate datetime DEFAULT GETDATE(),
	CreatedBy VARCHAR(100),
	UpdatedBy VARCHAR(100),	
	UpdatedDate datetime DEFAULT GETDATE(),
	FOREIGN KEY (ItemNo) REFERENCES Items(ItemNo),	
	FOREIGN KEY (PRTypeId) REFERENCES PRTypes(PRTypeId),
	FOREIGN KEY (UserId) REFERENCES Users(Id),
	FOREIGN KEY (DepartmentId) REFERENCES Users(DepartmentId),
);
CREATE TABLE StatusDetails (
	StatusId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	StatusName VARCHAR(100) NOT NULL,	
	IsActive BIT DEFAULT 1,
);
CREATE TABLE [dbo].[PRInclusion](
	[PRInclusionId] tinyint IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[DepartmentId] INT NOT NULL,
	[UserName]  [varchar](80) NOT NULL,
	[CreatedBy] [varchar](80) NULL,
	[CreatedDate] DATETIME DEFAULT(GETDATE()),
) 
CREATE TABLE PR (
	PRId BIGINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	PRNo BIGINT NOT NULL,
	NewPRNo VARCHAR(150),
	UserId NVARCHAR(450) NOT NULL,
	ChargeToDepId INT NULL,
	ItemCount INT NOT NULL,
	ItemCartId BIGINT NOT NULL,
	IsCompleted  BIT DEFAULT 0,
	IsActive BIT DEFAULT 1,
	FOREIGN KEY (UserId) REFERENCES Users(Id),
	FOREIGN KEY (ItemCodeId) REFERENCES ItemCode(ItemCodeId),	
);
CREATE TABLE PRDetails (
	PRDetailsId BIGINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	PRId BIGINT NOT NULL,
	ItemName VARCHAR(200) NOT NULL,
	ItemDescription VARCHAR(1000) NOT NULL,
	ItemLocalId  SMALLINT NOT NULL,
	ItemTypeId SMALLINT NOT NULL,
	UOMId  INT NOT NULL,
	ItemNo BIGINT NOT NULL,
	ItemColorId  INT NOT NULL,
	ItemCategoryId  SMALLINT NOT NULL,
	PackagingTypeId tinyint NOT NULL,
	Qty INT NOT NULL,
	PRTypeId tinyint  NOT NULL,
	ItemAttachId bigint NOT NULL,
	Remarks VARCHAR(150),
	DateNeeded datetime NULL,
	Status SMALLINT NOT NULL,
	CreatedDate datetime DEFAULT GETDATE(),
	CreatedBy VARCHAR(100),
	UpdatedBy VARCHAR(100),	
	UpdatedDate datetime DEFAULT GETDATE(),
	IsActive BIT DEFAULT 1,
	FOREIGN KEY (ItemCategoryId) REFERENCES ItemCategories(ItemCategoryId),
	FOREIGN KEY (PRId) REFERENCES PR(PRId),
	FOREIGN KEY (ItemNo) REFERENCES Items(ItemNo),	
	FOREIGN KEY (Status) REFERENCES [dbo].[StatusDetails](StatusId),
	FOREIGN KEY (ItemCategoryId) REFERENCES ItemCategories(ItemCategoryId),
	FOREIGN KEY (ItemLocalId) REFERENCES ItemLocalization(ItemLocalId),
	FOREIGN KEY (ItemTypeId) REFERENCES ItemType(ItemTypeId),
	FOREIGN KEY (UOMId) REFERENCES UnitOfMessures(UOMId),
	FOREIGN KEY (ItemColorId) REFERENCES ItemColor(ItemColorId),
);
CREATE TABLE Currencies (
	CurrencyId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	CurrencyName VARCHAR(100) NOT NULL,
	VatRate MONEY NULL,
	IsActive BIT DEFAULT 1
);
CREATE TABLE Bank (
	BankId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	BankName VARCHAR(100) NOT NULL,
	IsActive BIT DEFAULT 1
);
CREATE TABLE SMTPType (
	SMTPTypeId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	SMTPTypeName varchar(100),	
);
CREATE TABLE SMTPCredential (
	SMTPCredentialId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	SMTPTypeId TINYINT,
	SMTPCredentialName VARCHAR(450) NOT NULL,
	SenderEmail VARCHAR(150) NULL,
	SenderUserName VARCHAR(80) NULL,
	SenderDisplayName VARCHAR(80) NULL,
	Password VARCHAR(8000) NULL,
	Server VARCHAR(500) NULL,
	OutgoingPort INT,
	IncomingPort INT,
	IsActive BIT DEFAULT 1,
	FOREIGN KEY (SMTPTypeId) REFERENCES SMTPType(SMTPTypeId),	
);
CREATE TABLE UnitOfMessures (
	UOMId SMALLINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	UOMName VARCHAR(150) NOT NULL,
	IsActive BIT DEFAULT 1
);
CREATE TABLE Suppliers (
	SupplierId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	SupplierName VARCHAR(450) NOT NULL,
	EmailAddress VARCHAR(150) NULL,
	Address VARCHAR(550) NULL,
	ContactNo VARCHAR(20) NULL,
	ContactPerson VARCHAR(50) NULL,
	PaymentTermsId TINYINT NULL,
	IsVatable BIT DEFAULT 1,
	CurrencyId TINYINT NULL,
	Leadtime VARCHAR(80) NULL,
	IsActive BIT DEFAULT 1,
	FOREIGN KEY (ItemNo) REFERENCES Items(ItemNo),	
);
CREATE TABLE [dbo].[SuppliersHis](
	[SupplierId] [int] NOT NULL,
	[SupplierName] [varchar](450) NOT NULL,
	[EmailAddress] [varchar](200) NULL,
	[Address] [varchar](550) NULL,
	[ContactNo] [varchar](100) NULL,
	[ContactPerson] [varchar](150) NULL,
	[PaymentTermsId] [tinyint] NULL,
	[IsVatable] [bit] NULL,
	[CurrencyId] [tinyint] NULL,
	[LeadTime] [varchar](40) NULL,
	[TinNo] [varchar](50) NULL,
	[IsActive] [bit] NULL,
	UpdatedBy VARCHAR(80),
	UpdatedDate DATETIME,
	FOREIGN KEY (SupplierId) REFERENCES Suppliers(SupplierId)
);
CREATE TABLE SupplierItems (
	SupplierItemId BIGINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	SupplierId INT NOT NULL,
	ItemNo BIGINT NOT NULL,
	IsActive BIT DEFAULT 1,
	FOREIGN KEY (ItemNo) REFERENCES Items(ItemNo),
	FOREIGN KEY (SupplierId) REFERENCES Suppliers(SupplierId),
);
CREATE TABLE Canvass (
	CanvassId BIGINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	CanvassNo INT NOT NULL,
	PRNo BIGINT NOT NULL,
	IsActive BIT DEFAULT 1,
);
CREATE TABLE ApprovalType (
	ApprovalTypeId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ApprovalTypeName VARCHAR(10) NOT NULL,
	IsActive BIT DEFAULT 1,
);
CREATE TABLE Approved (
	ApprovedId BIGINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ApprovalTypeId TINYINT NOT NULL,
	ApproverId SMALLINT NOT NULL,
	PRNo BIGINT NULL,
	CanvassId BIGINT NULL,
	PONo BIGINT NULL,
	ItemNo BIGINT NOT NULL,
	IsActive BIT DEFAULT 1,
	FOREIGN KEY (ApprovalTypeId) REFERENCES ApprovalType(ApprovalTypeId),	
	FOREIGN KEY (ApproverId) REFERENCES Approver(ApproverId),	
	FOREIGN KEY (ItemNo) REFERENCES Items(ItemNo),	
);
CREATE TABLE Hold (
	HoldId BIGINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ApprovalTypeId TINYINT NOT NULL,
	ApproverId SMALLINT NOT NULL,
	PRNo BIGINT NULL,
	CanvassId BIGINT NULL,
	PRDetailsId BIGINT NULL,
	PONo BIGINT NULL,
	ItemNo BIGINT NOT NULL,
	IsActive BIT DEFAULT 1,
	FOREIGN KEY (ApprovalTypeId) REFERENCES ApprovalType(ApprovalTypeId),	
	FOREIGN KEY (ApproverId) REFERENCES Approver(ApproverId),	
	FOREIGN KEY (ItemNo) REFERENCES Items(ItemNo),	
	FOREIGN KEY (PRDetailsId) REFERENCES PRDetails(PRDetailsId),	
);
CREATE TABLE [dbo].[Checkers](
	[CheckerId] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[ApproverId] smallint NOT NULL,
	[ApprovalTypeId] [tinyint] NOT NULL,
	[IsActive] [bit] NULL,
	[CreatedDate] [datetime] NULL,
	[CreatedBy] [varchar](100) NULL,
	[UpdatedBy] [varchar](100) NULL,
	[UpdatedDate] [datetime] NULL,
	FOREIGN KEY ([ApproverId]) REFERENCES [dbo].[Approver]([ApproverId]),
)
CREATE TABLE Facilitators(
	[FacilitatorId] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
	FacilitatorName VARCHAR(50) NOT NULL,
	Status SMALLINT NOT NULL,
	[IsActive] [bit] DEFAULT 1 NULL,
	[CreatedDate] [datetime] NULL,
	[CreatedBy] [varchar](100) NULL,
	[UpdatedBy] [varchar](100) NULL,
	[UpdatedDate] [datetime] NULL,	
	FOREIGN KEY (Status) REFERENCES [dbo].[StatusDetails](StatusId),
);
CREATE TABLE PRBatches(
	PRBatchesId BIGINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	PRId BIGINT NULL,
	[IsActive] [bit] DEFAULT 1 NULL,
);
CREATE TABLE OtherCharges (
	OtherChargesId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	OtherChargesName VARCHAR(100) NOT NULL,
);
CREATE TABLE [dbo].[CanvassSuppliers](
	[CanvassSupplierId] [bigint] IDENTITY(1,1) NOT NULL  PRIMARY KEY,
	[CanvassId] [bigint] NOT NULL,
	[SupplierId] [int] NOT NULL,
	Token varchar(1000),
	RequestedBy varchar(100),
	CreatedDate DATETIME,
	IsActive BIT,
	IsSubmitted BIT,
	CreatedBy varchar(100),
	FOREIGN KEY (CanvassId) REFERENCES [dbo].[Canvass](CanvassId),
	FOREIGN KEY (SupplierId) REFERENCES [dbo].[Suppliers](SupplierId),
)
CREATE TABLE [dbo].[CanvassSupplierDetails](
	[CanvassSupplierDtlId] [bigint] IDENTITY(1,1) NOT NULL  PRIMARY KEY,
	[CanvassSupplierId] [bigint] NOT NULL,
	CanvassId BIGINT NOT NULL,
	PRDetailId BIGINT NOT NULL,
	SupplierId INT NOT NULL,
	FOREIGN KEY (CanvassId) REFERENCES [dbo].Canvass(CanvassId),
	FOREIGN KEY ([CanvassSupplierId]) REFERENCES [dbo].CanvassSuppliers([CanvassSupplierId]),
)
CREATE TABLE [dbo].[CanvassDetails](
	[CanvassDetailId] [bigint] IDENTITY(1,1) NOT NULL  PRIMARY KEY,
	[CanvassSupplierId] [bigint] NOT NULL,
	[PRDetailsId] [bigint] NOT NULL,
	ItemNo  [bigint] NOT NULL,
	[UnitPrice] [decimal](18, 2) NOT NULL,
	[Terms] [varchar](500) NOT NULL,
	[Remarks] [varchar](1000) NOT NULL,
	[CommitmentDate] [datetime] NULL,
	[VatInc] [bit] NOT NULL,
	[CurrencyId] [tinyint] NOT NULL,
	[Discount] [decimal](18, 2) NULL,
	Manufacturer [varchar](450)  NULL,
	SuggestedSupplier BIT DEFAULT(0),
	UpdatedBy [varchar](80)  NULL,
	UpdatedDate [datetime] NULL,
	FOREIGN KEY (PRDetailsId) REFERENCES [dbo].[PRDetails](PRDetailsId),
	FOREIGN KEY (CanvassSupplierId) REFERENCES [dbo].[CanvassSuppliers](CanvassSupplierId),
	FOREIGN KEY (CurrencyId) REFERENCES [dbo].[Currencies](CurrencyId),
	FOREIGN KEY (ItemNo) REFERENCES [dbo].[Items](ItemNo),
	FOREIGN KEY (OtherChargesId) REFERENCES [dbo].[OtherCharges](OtherChargesId),
);
CREATE TABLE POTypes (
	POTypeId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	POTypeName VARCHAR(20) NOT NULL,
	IsActive BIT DEFAULT 1
);
CREATE TABLE [dbo].[PO](
	[POId] [bigint] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[PONo] [bigint] NOT NULL,
	POTypeId TINYINT NOT NULL,
	[Submit] [bit] NOT NULL,
	[UserId] [Nvarchar](450) NOT NULL,
	IsPOClosed [bit] NOT NULL,
	[IsActive] [bit] NOT NULL,
	FOREIGN KEY ([UserId]) REFERENCES [dbo].[Users](Id),
	FOREIGN KEY (POTypeId) REFERENCES [dbo].[POTypes](POTypeId),
);
CREATE TABLE [dbo].[POHis](
	[POId] [bigint] NOT NULL,
	[PONo] [varchar](30) NOT NULL,
	[POTypeId] [tinyint] NOT NULL,
	[Submit] [bit] NOT NULL,
	[UserId] [nvarchar](450) NOT NULL,
	[GrossAmount] [decimal](18, 6) NULL,
	[FinalAmount] [decimal](18, 6) NULL,
	[UpdatedDate] [datetime] NULL,
	[PreparedBy] [varchar](450) NULL,
	[UpdatedBy] [varchar](100) NULL,
	[IsPOClosed] [bit] NULL,
	[IsActive] [bit] NOT NULL,
	[IsManual] [bit] NULL,
	[Reason] VARCHAR(1000)
);
CREATE TABLE [dbo].[PODetails](
	[PODetailId] [bigint] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[POId] [bigint] NOT NULL,
	PRNo [bigint] NOT NULL,	
	CanvassDetailId [bigint] NOT NULL,	
	ItemNo [bigint] NOT NULL,	
	Balance DECIMAL(18,2),
	TotalAmount DECIMAL(18,2),
	PreparedBy varchar(100),
	ApprovedBy varchar(100),
	IncoTermsId TINYINT,
	DocRequirmentId TINYINT,
	PODId TINYINT,
	ProfInvoiceNo  varchar(50),
	ShippingInstructionId TINYINT,
	Amount DECIMAL(16,6) NULL,
	UnitPrice DECIMAL(16,6) NULL
	FOREIGN KEY ([POId]) REFERENCES [dbo].[PO](POId),
	FOREIGN KEY (CanvassDetailId) REFERENCES [dbo].[CanvassDetails](CanvassDetailId),
) 
CREATE TABLE Manufacturer (
	ManufacturerId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	ManufacturerName VARCHAR(10) NOT NULL,
);
CREATE TABLE ApprovedSuppliers (
	ApprovedSupplierId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	POId BIGINT NOT NULL,
	SupplierId INT NOT NULL,
	[Remarks] [varchar](1000) NULL,
	[InternalRemarks] [varchar](1000) NULL,	
	IsPrepared BIT DEFAULT(1),
	IsApproved BIT DEFAULT(0),
	IsActive BIT DEFAULT(1),
	FOREIGN KEY (POId) REFERENCES [dbo].[PO]([POId]),
	FOREIGN KEY (SupplierId) REFERENCES [dbo].[Suppliers](SupplierId),
);
CREATE TABLE AuditLogs (
	Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	TraceId BIGINT NULL,
	Description [varchar](1000) NULL,
	[Remarks] [varchar](1000) NULL,
);
CREATE TABLE [dbo].[RR](
	[RRId] [bigint] IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[RRNo] BIGINT NOT NULL,
	[PONo] BIGINT NOT NULL,
	POTypeId TINYINT NOT NULL,
	CreatedDate datetime not null,
	IsCompleted bit default(0), 
	IsActive [bit] NOT NULL,
)
CREATE TABLE [dbo].[RRDetails](
	[RRDetailId] [bigint] IDENTITY(1,1) NOT NULL,
	[RRId] [bigint] NOT NULL,
	DRNo varchar(50) NULL,
	DocTypeId TINYINT NULL,
	[PRDetailId] [bigint] NULL,
	[PODetailId] [bigint] NOT NULL,
	[ItemNo] [bigint] NOT NULL,
	[Quantity] INT NOT NULL,
	QuantityReceived INT NULL,
	RemainingQty INT NULL,
	[UnitPrice] [decimal](18, 4) NOT NULL,
	CurrencyId TINYINT NULL,
	[ExpiryDate] varchar(100) NULL,
	[Approve] [bit] NULL,
	[Denied] [bit] NULL,
	[Received] [bit] NULL,
	ReceivedBy VARCHAR(100),
	AcknowledgeBy VARCHAR(100),
	[AcknowledgeDate]  VARCHAR(100) NULL,
	[ReceivedDate] [datetime] NULL,
	[UpdatedDate] [datetime] NULL,
	[Remarks] [varchar](2000) NULL,
	[TotalAmount] [decimal](18, 4) NOT NULL,
	[Completed] [bit] NULL,
	[InCompleted] [bit] NULL,
	[SupplierId] [int] NULL,
	FOREIGN KEY ([RRId]) REFERENCES [RR]([RRId]),
	FOREIGN KEY (ItemNo) REFERENCES Items(ItemNo),
	FOREIGN KEY (SupplierId) REFERENCES Suppliers(SupplierId),
)
CREATE TABLE [dbo].[TransTypes](
	[TransTypeId] TINYINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[TransTypeName] VARCHAR(50),
)
CREATE TABLE [dbo].[Inventory](
	[InventoryId] INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[ItemNo] BIGINT NOT NULL,
	[QtyOnHand] INT NOT NULL,
	[QtyIn] INT NOT NULL,
	[QtyOut] INT NULL,
	[LotId] INT,
	[UserId] VARCHAR(450) NULL,
	IsActive [bit] NOT NULL,
	FOREIGN KEY (ItemNo) REFERENCES Items(ItemNo),
)
CREATE TABLE [dbo].[InventTrans](
	[InventTransId] INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[InventoryId] INT NULL,
	[RRNo] BIGINT NULL,
	CreatedBy VARCHAR(50),
	FOREIGN KEY ([InventoryId]) REFERENCES [Inventory]([InventoryId]),
)
CREATE TABLE [dbo].[InventTransDetail](
	[InventTransDetailId] BIGINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[InventTransId] INT,
	[TransTypeId] TINYINT,
	[RRDetailId] BIGINT NULL,
	PRDetailId BIGINT NULL,
	[QtyIn] INT NULL,
	[QtyOut] INT NULL,
	CreatedDate DATETIME,
	Remarks VARCHAR(500),
	IsActive [bit] NOT NULL,
	FOREIGN KEY ([InventTransId]) REFERENCES [InventTrans]([InventTransId]),
	FOREIGN KEY ([TransTypeId]) REFERENCES [TransTypes]([TransTypeId]),
)
CREATE TABLE [dbo].[RequestItems](
	[RequestItemId] BIGINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[InventoryId] INT NOT NULL,
	[QtyRequest] INT NOT NULL,
	[QtyReceived] INT  NULL,
	ItemNo  BIGINT  NULL,
	[UserId] NVARCHAR(450) NULL,
	Status SMALLINT NULL,
	CreatedDate datetime,
	IsActive [bit] NOT NULL,
	FOREIGN KEY ([InventoryId]) REFERENCES Inventory([InventoryId]),
	FOREIGN KEY ([UserId]) REFERENCES Users(Id),
)
CREATE TABLE [dbo].[LotType](
	[LotTypeId] TINYINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[LotTypeName] VARCHAR(100),
)
CREATE TABLE [dbo].[Lot](
	[LotId] INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	InventoryId INT,
	[LotTypeId] TINYINT,
	[LotName] VARCHAR(100),
	UserId VARCHAR(450),
	CreatedBy VARCHAR(50),
	CreatedDate DATETIME,
	UpdatedBy VARCHAR(50),
	UpdatedDate DATETIME,
	FOREIGN KEY (InventoryId) REFERENCES Inventory(InventoryId),
)
CREATE TABLE TrackingStatuses (
	TrackingStatusId BIGINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	[PRDetailId] BIGINT NOT NULL,
	PRCreatedDate datetime NULL,
	PRApprover1 datetime NULL,
	PRApprover2 datetime NULL,
	CanvassCreatedDate datetime NULL,
	CanvassSubmitedDate datetime NULL,
	SuppBidDate datetime NULL,
	CanvassApprovedDate datetime NULL,
	POPreparationDate datetime NULL,
	PoCreatedDate datetime NULL,
	PoApprovedDate datetime NULL,
	POForwardedDate datetime NULL,	
	ReceivedDate datetime NULL,	
	ItemArrivedDate datetime NULL,	
	POClosed datetime NULL,	
	IsActive BIT NOT NULL,
); 
CREATE TABLE [dbo].[IncoTerms](
	[IncoTermsId] TINYINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[IncoTerms] VARCHAR(120),
)
CREATE TABLE [dbo].[PortOfDischarge](
	[PODId] TINYINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[PortOfDischarge] VARCHAR(120),
	[IncoTermsId] TINYINT NOT NULL,
	FOREIGN KEY ([IncoTermsId]) REFERENCES [IncoTerms]([IncoTermsId]),
)
CREATE TABLE [dbo].[DocumentsRequired](
	[DocsRequiredId] SMALLINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[Documents] VARCHAR(100),
)
CREATE TABLE [dbo].[Qualities](
	[QualityId] SMALLINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[Quality] VARCHAR(200),
)
CREATE TABLE [dbo].[ShippingMarks](
	[ShippingMarkId] SMALLINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[ShippingMarks] VARCHAR(200),
)
CREATE TABLE [dbo].[Packing](
	[PackingId] SMALLINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[Packing] VARCHAR(150),
)
CREATE TABLE [dbo].[PaymentTerms](
	[PaymentTermsId] TINYINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[PaymentTerms] VARCHAR(50),
)
CREATE TABLE [dbo].[ShippingInstructions](
	[ShippingInstructionId] TINYINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[ShippingInstruction] VARCHAR(100),
)
CREATE TABLE SysApplication (
	AppsId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	AppsName VARCHAR(100) NOT NULL,
);
CREATE TABLE CentralPONo (
	CPONoId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	PONo VARCHAR(40) NOT NULL,
	POTypeId TINYINT NOT NULL,
	AppsId TINYINT NOT NULL,
	CreatedBy VARCHAR(150) NULL,
	CreatedDate datetime DEFAULT(GETDATE()) NOT NULL,
	IsActive BIT DEFAULT(1) NOT NULL,
	FOREIGN KEY (AppsId) REFERENCES SysApplication(AppsId)
);
CREATE TABLE POPattern (
	POPatternId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	POPatternName VARCHAR(5) NOT NULL,
	AppsId TINYINT NOT NULL,
	FOREIGN KEY (AppsId) REFERENCES SysApplication(AppsId)
);
CREATE TABLE DocRequirements (
	DocRequirementId TINYINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	DocName VARCHAR(50),
);
CREATE TABLE SuppDocRequirements (
	SuppDocRequirementId BIGINT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	PONo VARCHAR(30),
	DocRequirementId TINYINT,
	FOREIGN KEY (DocRequirementId) REFERENCES DocRequirements(DocRequirementId)
);
CREATE TABLE [dbo].[FilteredCanvassPerDept](
	[FilCanPerDeptId] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
	[DepartmentId] INT NOT NULL,
	[UserName] [varchar](50) NOT NULL,
)

CREATE TABLE Discounts(
DiscountId INT NOT NULL PRIMARY KEY IDENTITY(1,1),
PRDetailsId BIGINT DEFAULT(0),
DiscountTypeId BIT,
Discount DECIMAL(14,4),
POId BIGINT,
FOREIGN KEY (POId) REFERENCES PO(POId)
);
CREATE TABLE Charges(
ChargesId INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
POId BIGINT NOT NULL,
OtherChargesId INT NOT NULL,
POTypeId TINYINT NOT NULL,
Amount DECIMAL(14,6) NOT NULL,
FOREIGN KEY (POId) REFERENCES PO(POId),
FOREIGN KEY (OtherChargesId) REFERENCES OtherCharges(OtherChargesId),
);
CREATE TABLE SupplierAssigned (
SupplierAssignedId INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
SupplierId INT NOT NULL,
USERNAME VARCHAR(80) NOT NULL,
);