/* ===================================================================== LFA_DB_Changes.sql Leave Fare Assistance (Core Payroll > LFA). Three tables: dbo.LFARequest who asked for LFA, for which leave (LFA Request tab) dbo.LFAPayment one payment batch (like SalaryProcess) dbo.LFAPaymentDetail one employee of a batch (like SalaryMonthly) - Part 0 DROPS the three tables, Parts 1-3 create them again. The drop is refused, and nothing runs, when any of the tables already holds rows. - LFAYear is a DATETIME in all three tables: the last date of the Payment Date's month (paid 08-Dec-2026 = 31-Dec-2026), as SalaryMonth is the last date of its month. The LFA is for the calendar year of that date, so "once in a calendar year" is enforced on YEAR(LFAYear) (LFACalendarYear). - IDs are generated by the application (tc.GenerateID), no IDENTITY. - A detail row is a snapshot: grade, department, bank account and basic are kept as they were when the batch was processed. - Columns that point to existing tables (EMPLOYEE, DEPARTMENT, GRADES, CRG, BRANCHES, LEAVEENTRY ...) have no foreign key, as in Calendar_DB_Changes.sql. Run order: once per database, together with the LFA code. ===================================================================== */ SET ANSI_NULLS ON; SET QUOTED_IDENTIFIER ON; GO /* ---------- 0. Drop (child table first) ---------- */ IF EXISTS (SELECT 1 FROM sys.partitions WHERE object_id IN (OBJECT_ID(N'dbo.LFAPaymentDetail', N'U'), OBJECT_ID(N'dbo.LFAPayment', N'U'), OBJECT_ID(N'dbo.LFARequest', N'U')) AND index_id IN (0, 1) AND rows > 0) BEGIN RAISERROR('The LFA tables hold data. Nothing was dropped or created. Back up and clear them first.', 16, 1); SET NOEXEC ON; -- skips the rest of this script END GO DROP TABLE IF EXISTS dbo.LFAPaymentDetail; DROP TABLE IF EXISTS dbo.LFAPayment; DROP TABLE IF EXISTS dbo.LFARequest; GO /* ---------- 1. dbo.LFARequest ---------- */ CREATE TABLE dbo.LFARequest ( LFARequestID INT NOT NULL, PayrollTypeID INT NOT NULL, EmployeeID INT NOT NULL, LFAYear DATETIME NOT NULL, -- last date of the calendar year the LFA is for LeaveEntryID INT NULL, -- LEAVEENTRY.TRANID of the LFA leave RequestDate DATETIME NOT NULL, LeaveFromDate DATETIME NOT NULL, -- LFA Start Date LeaveToDate DATETIME NOT NULL, -- LFA End Date LeaveDays DECIMAL(9, 2) NOT NULL, Remarks NVARCHAR(500) NULL, Status TINYINT NOT NULL CONSTRAINT DF_LFARequest_Status DEFAULT (1), -- 1 Pending, 2 Approved, 3 Rejected, 4 Cancelled WFStatus INT NULL, -- enumwfStatus; NULL = not submitted CreatedBy INT NOT NULL, CreationDate DATETIME NOT NULL, ModifiedBy INT NULL, ModifiedDate DATETIME NULL, CONSTRAINT PK_LFARequest PRIMARY KEY (LFARequestID), CONSTRAINT CK_LFARequest_Dates CHECK (LeaveToDate >= LeaveFromDate), CONSTRAINT CK_LFARequest_Status CHECK (Status IN (1, 2, 3, 4)) ); GO /* one open or approved request per employee per calendar year */ CREATE UNIQUE INDEX UX_LFARequest_EmployeeYear ON dbo.LFARequest (EmployeeID, LFAYear) WHERE Status IN (1, 2); GO /* LFA Request tab: approved requests of a payroll type */ CREATE INDEX IX_LFARequest_List ON dbo.LFARequest (PayrollTypeID, Status, LeaveFromDate); GO /* ---------- 2. dbo.LFAPayment ---------- */ CREATE TABLE dbo.LFAPayment ( LFAPaymentID INT NOT NULL, BatchNumber VARCHAR(30) NOT NULL, -- LFA-2026-001 PayrollTypeID INT NOT NULL, SalaryMonth DATETIME NOT NULL, -- last date of PAYROLLTYPE.NEXTPAYPROCESSDATE's month LFAYear DATETIME NOT NULL, -- last date of PaymentDate's month PaymentDate DATETIME NOT NULL, Remarks NVARCHAR(500) NULL, IsTaxUpdated BIT NOT NULL CONSTRAINT DF_LFAPayment_IsTaxUpdated DEFAULT (0), -- 1 = the batch is already in the income tax data WFStatus INT NULL, -- enumwfStatus; NULL = not submitted ApprovedBy INT NULL, ApprovedDate DATETIME NULL, CreatedBy INT NOT NULL, CreationDate DATETIME NOT NULL, ModifiedBy INT NULL, ModifiedDate DATETIME NULL, CONSTRAINT PK_LFAPayment PRIMARY KEY (LFAPaymentID), CONSTRAINT UQ_LFAPayment_BatchNumber UNIQUE (PayrollTypeID, BatchNumber) ); GO /* Payment, Undo and Approval pages: the batches of a payroll type and month */ CREATE INDEX IX_LFAPayment_Month ON dbo.LFAPayment (PayrollTypeID, SalaryMonth) INCLUDE (WFStatus); GO /* Payment History tab: date range */ CREATE INDEX IX_LFAPayment_PaymentDate ON dbo.LFAPayment (PayrollTypeID, PaymentDate); GO /* ---------- 3. dbo.LFAPaymentDetail ---------- */ CREATE TABLE dbo.LFAPaymentDetail ( LFAPaymentDetailID INT NOT NULL, LFAPaymentID INT NOT NULL, EmployeeID INT NOT NULL, LFAYear DATETIME NOT NULL, -- same value as LFAPayment.LFAYear LFACalendarYear AS (YEAR(LFAYear)) PERSISTED, -- computed, never written: for the once-a-year index /* employee snapshot */ JoiningDate DATETIME NOT NULL, ConfirmationDate DATETIME NULL, GradeID INT NULL, DesignationID INT NULL, DepartmentID INT NULL, CostCenterID INT NULL, -- CRG.CRGID BranchID INT NULL, -- bank branch, for Bank Advice AccountNo VARCHAR(50) NULL, /* calculation */ JoiningBasic MONEY NULL, LastDecBasic MONEY NULL, -- basic as of 31 Dec of the preceding year; NULL = joined in LFAYear BasicSalary MONEY NOT NULL CONSTRAINT DF_LFAPayDtl_BasicSalary DEFAULT (0), -- basic the LFA was calculated on CalculatedAmount MONEY NOT NULL CONSTRAINT DF_LFAPayDtl_CalculatedAmount DEFAULT (0), -- before rounding Amount MONEY NOT NULL CONSTRAINT DF_LFAPayDtl_Amount DEFAULT (0), -- LFA amount to pay Remarks NVARCHAR(500) NULL, CreatedBy INT NOT NULL, CreationDate DATETIME NOT NULL, ModifiedBy INT NULL, ModifiedDate DATETIME NULL, CONSTRAINT PK_LFAPaymentDetail PRIMARY KEY (LFAPaymentDetailID), CONSTRAINT FK_LFAPayDtl_Payment FOREIGN KEY (LFAPaymentID) REFERENCES dbo.LFAPayment (LFAPaymentID), CONSTRAINT CK_LFAPayDtl_Amount CHECK (Amount >= 0) ); GO /* policy: LFA once in a calendar year (Undo deletes the rows, which frees the employee again) */ CREATE UNIQUE INDEX UX_LFAPayDtl_EmployeeYear ON dbo.LFAPaymentDetail (EmployeeID, LFACalendarYear); GO /* the rows of one batch */ CREATE INDEX IX_LFAPayDtl_Payment ON dbo.LFAPaymentDetail (LFAPaymentID); GO SET NOEXEC OFF; GO /* ---------- 4. Drop LFAPaymentDetail.PaymentStatus (no longer used) ---------- Runs even when Part 0 refused (the tables hold data): a database that already has the column loses it, with its default and check constraints. Re-runnable. */ IF OBJECT_ID(N'dbo.CK_LFAPayDtl_PaymentStatus', N'C') IS NOT NULL ALTER TABLE dbo.LFAPaymentDetail DROP CONSTRAINT CK_LFAPayDtl_PaymentStatus; GO IF OBJECT_ID(N'dbo.DF_LFAPayDtl_PaymentStatus', N'D') IS NOT NULL ALTER TABLE dbo.LFAPaymentDetail DROP CONSTRAINT DF_LFAPayDtl_PaymentStatus; GO IF COL_LENGTH(N'dbo.LFAPaymentDetail', N'PaymentStatus') IS NOT NULL BEGIN ALTER TABLE dbo.LFAPaymentDetail DROP COLUMN PaymentStatus; PRINT 'LFAPaymentDetail.PaymentStatus dropped.'; END GO