Designing a Payroll Calculation Engine in Oracle PL/SQL: Elements, Proration, Tax and Reruns
How to design an element-based payroll engine in Oracle PL/SQL: pay elements with effective dates, calculation order, proration, tax slabs in tables, loan recoveries, reruns, locking and month-on-month reconciliation.
Oracle PL/SQL · Payroll
Designing a Payroll Calculation Engine in Oracle PL/SQL
Most payroll problems are not calculation errors in a single formula. They come from design: allowances hard-coded in a procedure, tax slabs written into IF statements, no record of how a figure was produced, and no safe way to run the payroll again after a correction. When something changes – a new allowance, a new tax year, a mid-month increment – the code has to be edited, and the next month’s run carries a new risk.
This guide describes an element-based payroll engine in Oracle PL/SQL: the tables, the calculation order, proration, tax, loans, reruns and the audit trail. It is the structure I recommend for factories, schools and companies that run their own HR and payroll on Oracle Database or Oracle APEX.
1. Think in pay elements, not columns
A payslip is a list of earnings and deductions. Instead of adding a column for every allowance (house_rent, conveyance, medical …), store each one as a pay element with a rule that says how it is calculated. Adding a new allowance then becomes a setup task, not a code change.
CREATE TABLE pay_element (
element_code VARCHAR2(20) PRIMARY KEY,
element_name VARCHAR2(100) NOT NULL,
element_type VARCHAR2(10) NOT NULL
CHECK (element_type IN ('EARNING','DEDUCTION','INFO')),
calc_method VARCHAR2(20) NOT NULL
CHECK (calc_method IN ('FIXED','PCT_OF','PER_DAY',
'PER_HOUR','FORMULA','SLAB')),
base_element VARCHAR2(20), -- for PCT_OF, e.g. 'BASIC'
calc_seq NUMBER NOT NULL, -- order of calculation
prorate_flag CHAR(1) DEFAULT 'Y',
taxable_flag CHAR(1) DEFAULT 'Y',
round_to NUMBER DEFAULT 0 -- 0 = whole rupees
);
CREATE TABLE emp_element (
emp_id NUMBER NOT NULL,
element_code VARCHAR2(20) NOT NULL REFERENCES pay_element,
amount_or_rate NUMBER NOT NULL,
effective_from DATE NOT NULL,
effective_to DATE,
CONSTRAINT emp_element_pk PRIMARY KEY (emp_id, element_code, effective_from)
);
Effective dates on emp_element are essential. An increment from the 15th is a new row with effective_from = 15th, not an update. History is never overwritten, so you can always recalculate an old month exactly as it was.
2. Payroll runs and run results
CREATE TABLE pay_run (
run_id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
period_start DATE NOT NULL,
period_end DATE NOT NULL,
run_type VARCHAR2(10) DEFAULT 'REGULAR',
status VARCHAR2(10) DEFAULT 'DRAFT'
CHECK (status IN ('DRAFT','CALCULATED','APPROVED','LOCKED')),
created_by VARCHAR2(60),
created_on DATE DEFAULT SYSDATE
);
CREATE TABLE pay_result (
run_id NUMBER NOT NULL REFERENCES pay_run,
emp_id NUMBER NOT NULL,
element_code VARCHAR2(20) NOT NULL,
base_value NUMBER, -- the input used
units NUMBER, -- days, hours, etc.
result_value NUMBER NOT NULL,
calc_note VARCHAR2(400), -- human-readable explanation
CONSTRAINT pay_result_pk PRIMARY KEY (run_id, emp_id, element_code)
);
The calc_note column is small but powerful. Writing “Basic 60,000 × 18/30 days (joined 13th)” next to the result answers most payroll queries without anyone opening the code.
3. Calculation order
Elements depend on each other: house rent is a percentage of basic, overtime depends on an hourly rate, tax depends on taxable income. Process elements in ascending calc_seq so every element can read the results already calculated for the same employee in the same run.
A typical order:
- Basic salary (prorated for the days in service)
- Fixed allowances and percentage allowances
- Attendance-based items: absent deductions, late deductions
- Overtime (hours × rate)
- Gross pay (an INFO element that sums earnings)
- Statutory and policy deductions (provident fund, contributions, loans)
- Income tax withholding
- Net pay
4. The calculation procedure
The core loop reads the employee’s active elements for the period and calculates each one by method. A simplified version:
CREATE OR REPLACE PROCEDURE calc_employee (
p_run_id IN NUMBER,
p_emp_id IN NUMBER
) IS
l_start DATE;
l_end DATE;
l_days NUMBER; -- days in period
l_paid NUMBER; -- days in service in period
l_result NUMBER;
FUNCTION result_of (p_code VARCHAR2) RETURN NUMBER IS
l_val NUMBER;
BEGIN
SELECT result_value INTO l_val
FROM pay_result
WHERE run_id = p_run_id AND emp_id = p_emp_id
AND element_code = p_code;
RETURN l_val;
EXCEPTION WHEN NO_DATA_FOUND THEN RETURN 0;
END;
BEGIN
SELECT period_start, period_end INTO l_start, l_end
FROM pay_run WHERE run_id = p_run_id;
l_days := l_end - l_start + 1;
l_paid := service_days(p_emp_id, l_start, l_end); -- joiners / leavers
DELETE FROM pay_result WHERE run_id = p_run_id AND emp_id = p_emp_id;
FOR e IN (SELECT pe.*, ee.amount_or_rate
FROM pay_element pe
JOIN emp_element ee ON ee.element_code = pe.element_code
WHERE ee.emp_id = p_emp_id
AND ee.effective_from <= l_end
AND NVL(ee.effective_to, l_end) >= l_start
ORDER BY pe.calc_seq)
LOOP
l_result := CASE e.calc_method
WHEN 'FIXED' THEN e.amount_or_rate
WHEN 'PCT_OF' THEN result_of(e.base_element) * e.amount_or_rate / 100
ELSE 0
END;
IF e.prorate_flag = 'Y' AND e.calc_method = 'FIXED' THEN
l_result := l_result * l_paid / l_days;
END IF;
l_result := ROUND(l_result, e.round_to);
INSERT INTO pay_result (run_id, emp_id, element_code,
base_value, units, result_value, calc_note)
VALUES (p_run_id, p_emp_id, e.element_code,
e.amount_or_rate, l_paid, l_result,
e.calc_method || ' ' || e.amount_or_rate
|| CASE WHEN e.prorate_flag = 'Y'
THEN ' x ' || l_paid || '/' || l_days || ' days' END);
END LOOP;
END;
Notes on this skeleton:
- Percentage elements are not prorated again, because their base (for example basic) is already prorated. Prorating both is a common source of underpayment.
- If an element has two rows in one period (an increment on the 15th), handle it as two segments – calculate each part for its own days and add them. Do not simply take the latest rate for the whole month.
- Rounding happens per element, with a rule stored in setup. Decide with finance whether rounding is per element or only on net pay, and apply it consistently.
- For thousands of employees, call the procedure from a driver that processes employees in batches and logs failures per employee, so one bad record does not stop the whole run.
5. Tax and statutory rates belong in tables
Income tax slabs, contribution rates and their ceilings change by tax year or by notification. Store them in a slab table keyed by year, and have the SLAB method read from it:
CREATE TABLE pay_tax_slab (
tax_year VARCHAR2(9) NOT NULL, -- e.g. '2026-2027'
income_from NUMBER NOT NULL, -- annual taxable income
income_to NUMBER,
fixed_tax NUMBER DEFAULT 0,
rate_pct NUMBER DEFAULT 0, -- on income above income_from
CONSTRAINT pay_tax_slab_pk PRIMARY KEY (tax_year, income_from)
);
For monthly withholding, project the annual taxable income (salary paid so far in the tax year plus expected remaining salary), calculate annual tax from the slab, subtract tax already deducted and divide the remainder over the remaining months. This cumulative method corrects itself after increments or arrears, instead of over- or under-deducting until a year-end adjustment. Always load the official slabs for the tax year from the government notification and have finance verify them before the first run of the year.
6. Loans and advances
Keep loans in their own tables – a loan header with amount and instalment, and a ledger of recoveries. The payroll run creates a deduction from the instalment due, capped at the outstanding balance, and writes the recovery to the loan ledger only when the run is approved. If you post recoveries at calculation time, every rerun of a draft payroll will deduct the instalment again.
7. Reruns, approval and locking
A draft payroll will be calculated several times as attendance and adjustments are corrected. Design for that:
- DRAFT / CALCULATED: results can be deleted and recalculated freely for one employee or everyone.
- APPROVED: results are final; loan recoveries and leave balances are posted; the bank transfer file and payslips are generated.
- LOCKED: after salaries are paid, nothing in the period can change. Corrections go into the next month as arrears or recoveries with a clear note, never by editing a locked run.
Enforce the status in the database (for example, a trigger on pay_result that rejects changes when the run is approved or locked), not only in the APEX screens.
8. Reconciliation: prove the numbers before people are paid
Before approval, compare the run with the previous month at element level:
SELECT element_code,
NVL(SUM(CASE WHEN run_id = :prev_run THEN result_value END), 0) AS last_month,
NVL(SUM(CASE WHEN run_id = :this_run THEN result_value END), 0) AS this_month,
NVL(SUM(CASE WHEN run_id = :this_run THEN result_value END), 0)
- NVL(SUM(CASE WHEN run_id = :prev_run THEN result_value END), 0) AS difference
FROM pay_result
WHERE run_id IN (:prev_run, :this_run)
GROUP BY element_code
ORDER BY ABS(NVL(SUM(CASE WHEN run_id = :this_run THEN result_value END), 0)
- NVL(SUM(CASE WHEN run_id = :prev_run THEN result_value END), 0)) DESC;
Every large difference should be explained by something HR knows about – new joiners, leavers, increments, overtime in a busy month. Unexplained differences are where errors hide. When replacing an old payroll system, run both systems in parallel for at least two months and reconcile employee by employee before switching off the old one.
Quick checklist
- Earnings and deductions stored as pay elements with calculation methods
- Employee element values with effective dates – never overwritten
- Calculation sequence defined in setup, not in code order
- Proration applied once, to fixed elements, by days in service
- Tax slabs and statutory rates in tables, by tax year
- Loan recoveries posted only on approval
- Run status enforced in the database; corrections as arrears after locking
- Element-level reconciliation with the previous month before approval
Related guides: calculating night-shift attendance in Oracle, the payroll software features checklist, PL/SQL error logging and BULK COLLECT and FORALL for processing thousands of employees quickly.
Need help with your project?
AbairTech builds Oracle APEX, ERP/POS, HRMS and business software. Explore our live demos or discuss your requirement.
Related implementation services
Need help applying these concepts in a business system? Explore Oracle PL/SQL development services or Custom ERP software.