trả/mượn vtth
This commit is contained in:
@@ -244,7 +244,8 @@ BEGIN
|
||||
ConsumableName NVARCHAR(255) NOT NULL,
|
||||
Unit NVARCHAR(50) NULL,
|
||||
ExportQuantity INT NOT NULL DEFAULT 1,
|
||||
RecipientName NVARCHAR(100) NOT NULL,
|
||||
RecipientUserId INT NULL,
|
||||
RecipientName NVARCHAR(100) NULL,
|
||||
ProjectName NVARCHAR(150) NULL,
|
||||
ExportedByName NVARCHAR(100) NOT NULL,
|
||||
ExportNote NVARCHAR(1000) NULL,
|
||||
@@ -287,6 +288,16 @@ BEGIN
|
||||
ALTER TABLE ConsumableExportHistory ADD RecipientName NVARCHAR(100) NULL;
|
||||
END
|
||||
|
||||
IF COL_LENGTH('dbo.ConsumableExportHistory', 'RecipientUserId') IS NULL
|
||||
BEGIN
|
||||
ALTER TABLE ConsumableExportHistory ADD RecipientUserId INT NULL;
|
||||
END
|
||||
|
||||
IF COL_LENGTH('dbo.ConsumableExportHistory', 'RecipientName') IS NOT NULL
|
||||
BEGIN
|
||||
ALTER TABLE ConsumableExportHistory ALTER COLUMN RecipientName NVARCHAR(100) NULL;
|
||||
END
|
||||
|
||||
IF COL_LENGTH('dbo.ConsumableExportHistory', 'ProjectName') IS NULL
|
||||
BEGIN
|
||||
ALTER TABLE ConsumableExportHistory ADD ProjectName NVARCHAR(150) NULL;
|
||||
@@ -358,6 +369,25 @@ BEGIN
|
||||
FOREIGN KEY (ConsumableId) REFERENCES ConsumableInventory(ConsumableId) ON DELETE CASCADE;
|
||||
END
|
||||
|
||||
UPDATE exports
|
||||
SET RecipientUserId = matchedUser.UserId
|
||||
FROM ConsumableExportHistory exports
|
||||
CROSS APPLY (
|
||||
SELECT TOP 1 users.UserId
|
||||
FROM Users users
|
||||
WHERE NULLIF(LTRIM(RTRIM(exports.ProjectName)), '') IS NULL
|
||||
AND (
|
||||
LOWER(LTRIM(RTRIM(ISNULL(users.FullName, '')))) = LOWER(LTRIM(RTRIM(ISNULL(exports.RecipientName, ''))))
|
||||
OR LOWER(LTRIM(RTRIM(ISNULL(users.Username, '')))) = LOWER(LTRIM(RTRIM(ISNULL(exports.RecipientName, ''))))
|
||||
)
|
||||
ORDER BY CASE
|
||||
WHEN LOWER(LTRIM(RTRIM(ISNULL(users.FullName, '')))) = LOWER(LTRIM(RTRIM(ISNULL(exports.RecipientName, '')))) THEN 0
|
||||
ELSE 1
|
||||
END,
|
||||
users.UserId
|
||||
) matchedUser
|
||||
WHERE exports.RecipientUserId IS NULL;
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM sys.foreign_key_columns fkc
|
||||
@@ -374,6 +404,88 @@ BEGIN
|
||||
FOREIGN KEY (CreatedBy) REFERENCES Users(UserId) ON DELETE SET NULL;
|
||||
END
|
||||
|
||||
-- ===========================================
|
||||
-- 6B. CREATE CONSUMABLE RETURN HISTORY TABLE
|
||||
-- ===========================================
|
||||
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'ConsumableReturnHistory')
|
||||
BEGIN
|
||||
CREATE TABLE ConsumableReturnHistory (
|
||||
ReturnHistoryId INT PRIMARY KEY IDENTITY(1,1),
|
||||
ExportHistoryId INT NOT NULL,
|
||||
ConsumableId INT NOT NULL,
|
||||
ReturnQuantity INT NOT NULL DEFAULT 1,
|
||||
ReturnedByName NVARCHAR(100) NOT NULL,
|
||||
ReturnNote NVARCHAR(1000) NULL,
|
||||
PreviousExportInPeriod INT NOT NULL DEFAULT 0,
|
||||
NextExportInPeriod INT NOT NULL DEFAULT 0,
|
||||
PreviousEndingBalance INT NOT NULL DEFAULT 0,
|
||||
NextEndingBalance INT NOT NULL DEFAULT 0,
|
||||
CreatedBy INT NULL,
|
||||
ReturnedDate DATETIME NOT NULL DEFAULT (DATEADD(HOUR, 7, SYSUTCDATETIME())),
|
||||
CreatedDate DATETIME NOT NULL DEFAULT (DATEADD(HOUR, 7, SYSUTCDATETIME())),
|
||||
UpdatedDate DATETIME NOT NULL DEFAULT (DATEADD(HOUR, 7, SYSUTCDATETIME())),
|
||||
FOREIGN KEY (ExportHistoryId) REFERENCES ConsumableExportHistory(ExportHistoryId) ON DELETE CASCADE,
|
||||
FOREIGN KEY (CreatedBy) REFERENCES Users(UserId) ON DELETE SET NULL
|
||||
);
|
||||
PRINT 'Table ConsumableReturnHistory created successfully.';
|
||||
END
|
||||
|
||||
-- ===========================================
|
||||
-- 6C. CREATE CONSUMABLE BORROW REQUESTS TABLE
|
||||
-- ===========================================
|
||||
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'ConsumableBorrowRequests')
|
||||
BEGIN
|
||||
CREATE TABLE ConsumableBorrowRequests (
|
||||
BorrowRequestId INT PRIMARY KEY IDENTITY(1,1),
|
||||
ConsumableId INT NOT NULL,
|
||||
RequestType NVARCHAR(20) NOT NULL DEFAULT 'borrow',
|
||||
BorrowerName NVARCHAR(100) NOT NULL,
|
||||
BorrowQuantity INT NOT NULL DEFAULT 1,
|
||||
Unit NVARCHAR(50) NULL,
|
||||
BorrowDate DATE NOT NULL DEFAULT (CAST(DATEADD(HOUR, 7, SYSUTCDATETIME()) AS DATE)),
|
||||
RequestStatus NVARCHAR(20) NOT NULL DEFAULT 'pending',
|
||||
RequestNote NVARCHAR(500) NULL,
|
||||
RejectReason NVARCHAR(1000) NULL,
|
||||
ExportHistoryId INT NULL,
|
||||
CreatedBy INT NULL,
|
||||
ProcessedBy INT NULL,
|
||||
ProcessedByName NVARCHAR(100) NULL,
|
||||
ProcessedDate DATETIME NULL,
|
||||
CreatedDate DATETIME NOT NULL DEFAULT (DATEADD(HOUR, 7, SYSUTCDATETIME())),
|
||||
UpdatedDate DATETIME NOT NULL DEFAULT (DATEADD(HOUR, 7, SYSUTCDATETIME())),
|
||||
FOREIGN KEY (ConsumableId) REFERENCES ConsumableInventory(ConsumableId) ON DELETE CASCADE,
|
||||
FOREIGN KEY (CreatedBy) REFERENCES Users(UserId) ON DELETE SET NULL
|
||||
);
|
||||
PRINT 'Table ConsumableBorrowRequests created successfully.';
|
||||
END
|
||||
|
||||
IF COL_LENGTH('dbo.ConsumableBorrowRequests', 'RequestType') IS NULL
|
||||
BEGIN
|
||||
ALTER TABLE ConsumableBorrowRequests
|
||||
ADD RequestType NVARCHAR(20) NOT NULL CONSTRAINT DF_ConsumableBorrowRequests_RequestType DEFAULT('borrow');
|
||||
END
|
||||
|
||||
UPDATE ConsumableBorrowRequests
|
||||
SET RequestType = 'borrow'
|
||||
WHERE RequestType IS NULL OR LTRIM(RTRIM(RequestType)) = '';
|
||||
|
||||
DECLARE @ConsumableBorrowProcessedByFk NVARCHAR(128);
|
||||
SELECT TOP 1 @ConsumableBorrowProcessedByFk = fk.name
|
||||
FROM sys.foreign_keys fk
|
||||
INNER JOIN sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id
|
||||
INNER JOIN sys.columns c
|
||||
ON c.object_id = fkc.parent_object_id
|
||||
AND c.column_id = fkc.parent_column_id
|
||||
WHERE fk.parent_object_id = OBJECT_ID('dbo.ConsumableBorrowRequests')
|
||||
AND c.name = 'ProcessedBy';
|
||||
|
||||
IF @ConsumableBorrowProcessedByFk IS NOT NULL
|
||||
BEGIN
|
||||
DECLARE @DropConsumableBorrowProcessedByFkSql NVARCHAR(MAX) =
|
||||
N'ALTER TABLE ConsumableBorrowRequests DROP CONSTRAINT ' + QUOTENAME(@ConsumableBorrowProcessedByFk);
|
||||
EXEC sp_executesql @DropConsumableBorrowProcessedByFkSql;
|
||||
END
|
||||
|
||||
-- ===========================================
|
||||
-- 7. CREATE ASSET DEPARTMENTS TABLE
|
||||
-- ===========================================
|
||||
@@ -441,8 +553,7 @@ BEGIN
|
||||
CreatedDate DATETIME DEFAULT (DATEADD(HOUR, 7, SYSUTCDATETIME())),
|
||||
UpdatedDate DATETIME DEFAULT (DATEADD(HOUR, 7, SYSUTCDATETIME())),
|
||||
FOREIGN KEY (AssetId) REFERENCES AssetInventory(AssetId) ON DELETE CASCADE,
|
||||
FOREIGN KEY (CreatedBy) REFERENCES Users(UserId) ON DELETE SET NULL,
|
||||
FOREIGN KEY (ProcessedBy) REFERENCES Users(UserId) ON DELETE SET NULL
|
||||
FOREIGN KEY (CreatedBy) REFERENCES Users(UserId) ON DELETE SET NULL
|
||||
);
|
||||
PRINT 'Table AssetBorrowRequests created successfully.';
|
||||
END
|
||||
@@ -502,13 +613,6 @@ BEGIN
|
||||
ALTER TABLE AssetBorrowRequests ADD ProcessedDate DATETIME NULL;
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_AssetBorrowRequests_ProcessedBy')
|
||||
BEGIN
|
||||
ALTER TABLE AssetBorrowRequests
|
||||
ADD CONSTRAINT FK_AssetBorrowRequests_ProcessedBy
|
||||
FOREIGN KEY (ProcessedBy) REFERENCES Users(UserId) ON DELETE SET NULL;
|
||||
END
|
||||
|
||||
UPDATE AssetBorrowRequests
|
||||
SET RequestType = ISNULL(NULLIF(LTRIM(RTRIM(RequestType)), ''), 'borrow');
|
||||
|
||||
@@ -805,6 +909,56 @@ BEGIN
|
||||
CREATE INDEX IX_ConsumableExportHistory_ExportedDate ON ConsumableExportHistory(ExportedDate DESC);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_ConsumableExportHistory_RecipientUserId')
|
||||
BEGIN
|
||||
CREATE INDEX IX_ConsumableExportHistory_RecipientUserId ON ConsumableExportHistory(RecipientUserId);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_ConsumableReturnHistory_ExportHistoryId')
|
||||
BEGIN
|
||||
CREATE INDEX IX_ConsumableReturnHistory_ExportHistoryId ON ConsumableReturnHistory(ExportHistoryId);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_ConsumableReturnHistory_ConsumableId')
|
||||
BEGIN
|
||||
CREATE INDEX IX_ConsumableReturnHistory_ConsumableId ON ConsumableReturnHistory(ConsumableId);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_ConsumableReturnHistory_ReturnedDate')
|
||||
BEGIN
|
||||
CREATE INDEX IX_ConsumableReturnHistory_ReturnedDate ON ConsumableReturnHistory(ReturnedDate DESC);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_ConsumableBorrowRequests_ConsumableId')
|
||||
BEGIN
|
||||
CREATE INDEX IX_ConsumableBorrowRequests_ConsumableId ON ConsumableBorrowRequests(ConsumableId);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_ConsumableBorrowRequests_CreatedBy')
|
||||
BEGIN
|
||||
CREATE INDEX IX_ConsumableBorrowRequests_CreatedBy ON ConsumableBorrowRequests(CreatedBy);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_ConsumableBorrowRequests_RequestStatus')
|
||||
BEGIN
|
||||
CREATE INDEX IX_ConsumableBorrowRequests_RequestStatus ON ConsumableBorrowRequests(RequestStatus);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_ConsumableBorrowRequests_CreatedDate')
|
||||
BEGIN
|
||||
CREATE INDEX IX_ConsumableBorrowRequests_CreatedDate ON ConsumableBorrowRequests(CreatedDate DESC);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_ConsumableBorrowRequests_RequestType')
|
||||
BEGIN
|
||||
CREATE INDEX IX_ConsumableBorrowRequests_RequestType ON ConsumableBorrowRequests(RequestType);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_ConsumableBorrowRequests_ExportHistoryId')
|
||||
BEGIN
|
||||
CREATE INDEX IX_ConsumableBorrowRequests_ExportHistoryId ON ConsumableBorrowRequests(ExportHistoryId);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'UX_AssetDepartments_DepartmentName')
|
||||
BEGIN
|
||||
CREATE UNIQUE INDEX UX_AssetDepartments_DepartmentName ON AssetDepartments(DepartmentName);
|
||||
|
||||
Reference in New Issue
Block a user