Payroll Management System

A console-based Python application that manages employee records, tasks, attendance, and payslips.

It stores data in JSON files under a data/ directory.

Administrators can perform CRUD operations; employees can view their own data.

Directory and File Setup

This section covers app initialization and file paths.

  • The data/ directory is created if missing.
  • Five files store the system data and credentials.
Constant Description
EMP_FILE Path to employees JSON (data/employees.json)
TASK_FILE Path to tasks JSON (data/tasks.json)
ATT_FILE Path to attendance JSON (data/attendance.json)
PAY_FILE Path to payslips JSON (data/payslips.json)
ADMIN_FILE Path to admin credentials (data/admin.txt)
# Create data directory if missing
if not os.path.exists("data"):
    os.makedirs("data")

# Create default admin credentials if missing
if not os.path.exists(ADMIN_FILE):
    with open(ADMIN_FILE, "w") as admin_file:
        admin_file.write("admin:admin123")
{
    "title": "Default Admin Credentials",
    "content": "The system initializes with admin:admin123 if no credentials file exists."
}

Data Handling Functions

These utility functions load and save JSON data.

  • load_data: Reads JSON or returns empty list if file missing.
  • save_data: Writes Python data to JSON with indentation.
def load_data(file_path):
    """Loads JSON data from file or returns []."""
    if not os.path.exists(file_path):
        return []
    with open(file_path, "r") as file:
        return json.load(file)

def save_data(file_path, data):
    """Saves data into a JSON file."""
    with open(file_path, "w") as file:
        json.dump(data, file, indent=4)

Authentication Functions

The system supports two login methods:

  • admin_login: Verifies admin user against admin.txt.
  • employee_login: Verifies employee credentials from employees.json.
def admin_login():
    """Handles admin authentication."""
    username = input("Admin Username: ")
    password = input("Admin Password: ")
    with open(ADMIN_FILE, "r") as file:
        stored_username, stored_password = file.read().strip().split(":")
    return username == stored_username and password == stored_password

def employee_login():
    """Handles employee authentication."""
    emp_id = input("Employee ID: ")
    password = input("Password: ")
    for emp in load_data(EMP_FILE):
        if emp["emp_id"] == emp_id and emp["password"] == password:
            return emp
    return None

Admin Operations

Administrators can manage employees, tasks, attendance, and payslips.

  • add_employee: Adds a new employee.
  • view_employees: Lists all employee records.
  • assign_task: Assigns tasks with “Pending” status.
  • mark_attendance: Records presence or absence for a date.
  • create_payslip: Calculates net salary and saves payslip.

add_employee

Adds a unique employee record to employees.json.

  • Prompts for ID, name, salary, department, password.
  • Checks for duplicate ID before saving.
def add_employee():
    employees = load_data(EMP_FILE)
    emp_id = input("Employee ID: ")
    # Prevent duplicates
    if any(emp["emp_id"] == emp_id for emp in employees):
        print("Employee already exists.")
        return
    # Gather details
    name = input("Name: ")
    salary = float(input("Salary: "))
    department = input("Department: ")
    password = input("Password: ")
    employees.append({
        "emp_id": emp_id,
        "name": name,
        "salary": salary,
        "department": department,
        "password": password
    })
    save_data(EMP_FILE, employees)
    print("Employee added successfully.")

view_employees

Displays each employee’s ID, name, department, and salary.

def view_employees():
    employees = load_data(EMP_FILE)
    if not employees:
        print("No employees found.")
        return
    for emp in employees:
        print("---------------------------------")
        print("ID :", emp["emp_id"])
        print("Name :", emp["name"])
        print("Department:", emp["department"])
        print("Salary :", emp["salary"])

assign_task

Creates a new task with default status “Pending”.

  • Prompts employee ID and task description.
  • Appends to tasks.json.
def assign_task():
    tasks = load_data(TASK_FILE)
    emp_id = input("Employee ID: ")
    description = input("Task Description: ")
    tasks.append({"emp_id": emp_id, "description": description, "status": "Pending"})
    save_data(TASK_FILE, tasks)
    print("Task assigned successfully.")

mark_attendance

Records attendance status for an employee on a given date.

  • Prompts ID, date (YYYY-MM-DD), and status.
  • Saves to attendance.json.
def mark_attendance():
    attendance = load_data(ATT_FILE)
    emp_id = input("Employee ID: ")
    date = input("Date (YYYY-MM-DD): ")
    status = input("Present / Absent: ")
    attendance.append({"emp_id": emp_id, "date": date, "status": status})
    save_data(ATT_FILE, attendance)
    print("Attendance recorded.")

create_payslip

Calculates net salary and prevents duplicate payslips.

  • Checks if payslip already exists for employee/month/year.
  • Computes absence deductions, adds bonus, overtime, allowance.
  • Saves to payslips.json.
def create_payslip():
    emp_id = input("Employee ID: ")
    month, year = int(input("Month: ")), int(input("Year: "))
    payslips = load_data(PAY_FILE)
    # Duplicate check
    if any(s["emp_id"] == emp_id and s["month"] == month and s["year"] == year for s in payslips):
        print("Payslip already exists.")
        return
    # Retrieve employee
    employees = load_data(EMP_FILE)
    employee = next((e for e in employees if e["emp_id"] == emp_id), None)
    if not employee:
        print("Employee not found.")
        return
    # Input extras
    bonus = float(input("Bonus: "))
    overtime = float(input("Overtime: "))
    allowance = float(input("Allowance: "))
    # Calculate deduction
    absent_days = sum(1 for r in load_data(ATT_FILE)
                      if r["emp_id"] == emp_id and r["status"].lower() == "absent")
    daily_salary = employee["salary"] / 30
    deduction = daily_salary * absent_days
    net_salary = employee["salary"] + bonus + overtime + allowance - deduction
    # Save payslip
    payslips.append({
        "emp_id": emp_id,
        "month": month,
        "year": year,
        "basic": employee["salary"],
        "bonus": bonus,
        "overtime": overtime,
        "allowance": allowance,
        "deduction": deduction,
        "net": net_salary
    })
    save_data(PAY_FILE, payslips)
    print("Payslip generated successfully.")

Employee Operations

Employees can view their own profile, tasks, attendance, and payslips.

  • view_profile: Prints all employee fields.
  • view_tasks: Lists assigned tasks and statuses.
  • view_attendance: Shows attendance records.
  • view_payslip: Displays all payslips for the employee.
def view_profile(employee):
    """Displays employee profile."""
    print("---------------------------------")
    for key, value in employee.items():
        print(f"{key.capitalize()} : {value}")
def view_tasks(emp_id):
    """Displays assigned tasks."""
    tasks = load_data(TASK_FILE)
    found = False
    for t in tasks:
        if t["emp_id"] == emp_id:
            found = True
            print(t["description"], "-", t["status"])
    if not found:
        print("No tasks assigned.")
def view_attendance(emp_id):
    """Displays attendance records."""
    attendance = load_data(ATT_FILE)
    found = False
    for r in attendance:
        if r["emp_id"] == emp_id:
            found = True
            print(r["date"], "-", r["status"])
    if not found:
        print("No attendance records found.")
def view_payslip(emp_id):
    """Displays payslip information."""
    payslips = load_data(PAY_FILE)
    found = False
    for slip in payslips:
        if slip["emp_id"] == emp_id:
            found = True
            print("---------------------------------")
            for k, v in slip.items():
                print(f"{k.capitalize()} : {v}")
    if not found:
        print("No payslips available.")

Main Application Flow

The program loops through three modes: Admin, Employee, or Exit.

Below is a flowchart of the main loop and login transitions.

flowchart TD
  Start((Start)) --> MainMenu[Main Menu]
  MainMenu -->|1 Admin| AdminLogin[Admin Login]
  MainMenu -->|2 Employee| EmployeeLogin[Employee Login]
  MainMenu -->|3 Exit| Exit((Exit))
  AdminLogin -->|Success| AdminMenu[Admin Operations]
  AdminLogin -->|Fail| MainMenu
  EmployeeLogin -->|Success| EmpMenu[Employee Operations]
  EmployeeLogin -->|Fail| MainMenu
  AdminMenu --> MainMenu
  EmpMenu --> MainMenu

Data Storage Format 📂

All records are serialized as JSON arrays. Below are sample formats:

Employees (employees.json)

[
  {
    "emp_id": "E001",
    "name": "Alice Smith",
    "salary": 4000.0,
    "department": "HR",
    "password": "alice123"
  }
]

Tasks (tasks.json)

[
  {
    "emp_id": "E001",
    "description": "Prepare monthly report",
    "status": "Pending"
  }
]

Attendance (attendance.json)

[
  {
    "emp_id": "E001",
    "date": "2026-01-02",
    "status": "Present"
  }
]

Payslips (payslips.json)

[
  {
    "emp_id": "E001",
    "month": 1,
    "year": 2026,
    "basic": 4000.0,
    "bonus": 200.0,
    "overtime": 50.0,
    "allowance": 100.0,
    "deduction": 133.33,
    "net": 4216.67
  }
]

Key Takeaways

  • The system uses plain-text password storage; consider hashing for security.
  • Files auto-create on first run, ensuring zero manual setup.
  • The console menu guides both admin and employee roles through operations.