In this case study, we will develop a simple payroll system using RPG programming. This project will help you understand how to apply the concepts learned in previous modules to a real-world application. We will cover the following steps:

  1. Requirements Analysis
  2. Database Design
  3. Program Structure
  4. Implementation
  5. Testing

  1. Requirements Analysis

Before we start coding, we need to understand the requirements of the payroll system. Here are the key features we need to implement:

  • Employee Management: Add, update, and delete employee records.
  • Salary Calculation: Calculate the monthly salary based on hours worked and hourly rate.
  • Payroll Report: Generate a report of all employees and their salaries.

  1. Database Design

We will use a simple database to store employee information. The database will have a single table named EMPLOYEES with the following fields:

Field Name Data Type Description
EMP_ID CHAR(5) Employee ID (Primary Key)
NAME CHAR(50) Employee Name
HOURS_WORKED DEC(5,2) Hours Worked in the Month
HOURLY_RATE DEC(5,2) Hourly Rate

  1. Program Structure

We will create three main programs:

  1. Employee Management Program: To add, update, and delete employee records.
  2. Salary Calculation Program: To calculate the salary for each employee.
  3. Payroll Report Program: To generate a payroll report.

  1. Implementation

4.1 Employee Management Program

Adding an Employee

Dcl-F EMPLOYEES Usage(*Update);

Dcl-Proc AddEmployee;
  Dcl-Pi AddEmployee;
    empId Char(5);
    name Char(50);
    hoursWorked Packed(5:2);
    hourlyRate Packed(5:2);
  End-Pi;

  Dcl-DS EmployeeRecord;
    EMP_ID Char(5);
    NAME Char(50);
    HOURS_WORKED Packed(5:2);
    HOURLY_RATE Packed(5:2);
  End-DS;

  // Assign input values to the data structure
  EmployeeRecord.EMP_ID = empId;
  EmployeeRecord.NAME = name;
  EmployeeRecord.HOURS_WORKED = hoursWorked;
  EmployeeRecord.HOURLY_RATE = hourlyRate;

  // Write the record to the database
  Write EMPLOYEES EmployeeRecord;

End-Proc;

Updating an Employee

Dcl-F EMPLOYEES Usage(*Update);

Dcl-Proc UpdateEmployee;
  Dcl-Pi UpdateEmployee;
    empId Char(5);
    name Char(50);
    hoursWorked Packed(5:2);
    hourlyRate Packed(5:2);
  End-Pi;

  Dcl-DS EmployeeRecord;
    EMP_ID Char(5);
    NAME Char(50);
    HOURS_WORKED Packed(5:2);
    HOURLY_RATE Packed(5:2);
  End-DS;

  // Chain to the record to update
  Chain (empId) EMPLOYEES;
  If %Found(EMPLOYEES);
    // Update the record
    EmployeeRecord.EMP_ID = empId;
    EmployeeRecord.NAME = name;
    EmployeeRecord.HOURS_WORKED = hoursWorked;
    EmployeeRecord.HOURLY_RATE = hourlyRate;
    Update EMPLOYEES EmployeeRecord;
  Else;
    // Handle record not found
    Dsply 'Employee not found';
  EndIf;

End-Proc;

Deleting an Employee

Dcl-F EMPLOYEES Usage(*Update);

Dcl-Proc DeleteEmployee;
  Dcl-Pi DeleteEmployee;
    empId Char(5);
  End-Pi;

  // Chain to the record to delete
  Chain (empId) EMPLOYEES;
  If %Found(EMPLOYEES);
    // Delete the record
    Delete EMPLOYEES;
  Else;
    // Handle record not found
    Dsply 'Employee not found';
  EndIf;

End-Proc;

4.2 Salary Calculation Program

Dcl-F EMPLOYEES Usage(*Update);

Dcl-Proc CalculateSalary;
  Dcl-Pi CalculateSalary;
    empId Char(5);
  End-Pi;

  Dcl-DS EmployeeRecord;
    EMP_ID Char(5);
    NAME Char(50);
    HOURS_WORKED Packed(5:2);
    HOURLY_RATE Packed(5:2);
  End-DS;

  Dcl-S Salary Packed(7:2);

  // Chain to the employee record
  Chain (empId) EMPLOYEES;
  If %Found(EMPLOYEES);
    // Calculate the salary
    Salary = EmployeeRecord.HOURS_WORKED * EmployeeRecord.HOURLY_RATE;
    Dsply ('Salary for ' + %Trim(EmployeeRecord.NAME) + ': ' + %Char(Salary));
  Else;
    // Handle record not found
    Dsply 'Employee not found';
  EndIf;

End-Proc;

4.3 Payroll Report Program

Dcl-F EMPLOYEES Usage(*Input);

Dcl-Proc GeneratePayrollReport;
  Dcl-Pi GeneratePayrollReport;
  End-Pi;

  Dcl-DS EmployeeRecord;
    EMP_ID Char(5);
    NAME Char(50);
    HOURS_WORKED Packed(5:2);
    HOURLY_RATE Packed(5:2);
  End-DS;

  Dcl-S Salary Packed(7:2);

  // Read each record in the EMPLOYEES file
  Read EMPLOYEES EmployeeRecord;
  Dow Not %Eof(EMPLOYEES);
    // Calculate the salary
    Salary = EmployeeRecord.HOURS_WORKED * EmployeeRecord.HOURLY_RATE;
    Dsply ('Employee: ' + %Trim(EmployeeRecord.NAME) + ', Salary: ' + %Char(Salary));
    Read EMPLOYEES EmployeeRecord;
  EndDo;

End-Proc;

  1. Testing

To ensure our payroll system works correctly, we need to perform the following tests:

  1. Add Employee: Add a few employee records and verify they are stored correctly.
  2. Update Employee: Update an existing employee's details and verify the changes.
  3. Delete Employee: Delete an employee record and ensure it is removed from the database.
  4. Calculate Salary: Calculate the salary for an employee and verify the result.
  5. Generate Payroll Report: Generate a payroll report and verify the output.

Conclusion

In this case study, we developed a simple payroll system using RPG programming. We covered the entire process from requirements analysis to implementation and testing. This project demonstrated how to apply core RPG concepts to build a functional application. By completing this case study, you should have a solid understanding of how to develop real-world applications using RPG.

© Copyright 2024. All rights reserved