๐Ÿ“š ERP Documentation Viewer

Beautiful, colorful documentation for your ERP system

Salary Report Controller Documentation

File: /controllers/salaryReportController.php

Purpose: Manages salary calculations, payroll reports, and salary payment processing

Last Updated: December 19, 2024

Total Functions: 10

Lines of Code: ~975

---

๐Ÿ“‹ Overview

The Salary Report Controller is the core component for managing payroll operations in the ERP system. It handles:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
`salaryreport`Monthly salary reports`salaryreportid`, `employeeid`, `salarymonth`, `salaryreportnet`
`employeepersonnel`Employee transactions/deductions`employeeid`, `employeepersonneltype`, `employeepersonnelvalue`
`employee`Employee master data`employeeId`, `employeeName`, `employeeSalary`, `treeId2`
`save`Cash/bank accounts for payment`saveid`, `savename`, `savevalue`
`savedaily`Daily cash movements`saveid`, `savedailychangeamount`, `savedailychangetype`
### Related Tables (References)

Table NamePurposeKey Columns
`dailyentry`Payroll accounting entries`dailyentryid`, `entryComment`, `thedate`
`dailyentrycreditor`Credit side of entries`dailyentryid`, `accountstreeid`, `value`
`dailyentrydebtor`Debit side of entries`dailyentryid`, `accountstreeid`, `value`
`accountstree`Chart of accounts`id`, `customName`, `parentId`
`costcenter`Cost center allocation`costcenterid`, `costcentername`
`costcenterdetail`Cost center details`costcenterid`, `accountstreeid`, `value`
### Reference Tables (Lookups)

Table NamePurposeKey Columns
`programsettings`System configuration`settingName`, `settingValue`
`user`User preferences for saves`userid`, `saveids`, `searchinonesave`
`branch`Branch-based payroll`branchId`, `branchName`
---

๐Ÿ”ง Key Functions

Main Controller Actions

Default Action (Salary Report View)

if (!isset($_GET['do'])) // Line 168

- from (POST): Salary month (YYYY-MM format)

โ”Œโ”€ Load User Settings โ”€โ”
โ€ข Save preferences
โ€ข Cost centers
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Calculate Salaries โ”€โ”
โ€ข Base salary
โ€ข Bonuses & rewards
โ€ข Deductions & fines
โ€ข Previous balance
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Format Report โ”€โ”€โ”€โ”€โ”€โ”
allEmpsalary array
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Show Finalized Salaries

elseif ($do == "show") // Line 260

Edit Print Receipt

elseif ($do == "editprint") // Line 267

Process Receipt Payment

elseif ($do == "Receipt") // Line 274

Core Business Functions

show() Function

function show() // Line 506

โ”Œโ”€ Load Month Data โ”€โ”
GET['from'] param
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Query Payments โ”€โ”€โ”
SalaryreportEX->
queryAll()
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Assign to View โ”€โ”
Smarty template
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Receipt() Function

function Receipt() // Line 584

- empIds[]: Array of employee IDs

- amounts[]: Corresponding payment amounts

- salarymonthdate: Payment month

- saveid: Payment account ID

- costcenterid: Cost center for allocation

- note: Payment note/reference

โ”Œโ”€ Start Transaction โ”€โ”
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Loop Employees โ”€โ”€โ”€โ”€โ”
For each empId:
โ€ข Create salary rec
โ€ข Update cash acc
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Create Accounting โ”€โ”
โ€ข Debit: Cash/Bank
โ€ข Credit: Employee
โ€ข Daily entries
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Cost Center โ”€โ”€โ”€โ”€โ”€โ”€โ”
Allocation if set
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Commit/Rollback โ”€โ”€โ”
Based on success
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

// Create salary record
$Salaryreport->employeeid = $empId;
$Salaryreport->salaryreportnet = $amount;
$Salaryreport->salarymonth = $salarymonthdate;
$Salaryreport->sysdate = date("Y-m-d H:i:s");
$Salaryreport->conditions = 0;
$Salaryreport->userid = $_SESSION['userid'];

// Update cash account
$saveValueBefore = getSaveValueBefore($saveid);
$saveValueAfter = $saveValueBefore - $amount;
updateSave($saveid, $saveValueAfter);

// Create daily entry
insertSavedaily($saveValueBefore, $amount, 2, $saveid, 
    "ุงุณุชู„ุงู… ู…ุฑุชุจ $employeeName", $salaryId, $saveValueAfter, "salaryreport");

delete() Function

function delete() // Line 827

- id (GET): Salary report ID to delete

โ”Œโ”€ Load Salary Record โ”
SalaryreportDAO->
load($id)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Reverse Payment โ”€โ”€โ”€โ”
โ€ข Add back to cash
โ€ข Mark as deleted
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Update Employee โ”€โ”€โ”€โ”
Remove paid status
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Salary Entitlement Entry

elseif ($do == "salaryEntitlementEntry-notFinised") // Line 306

โ”Œโ”€ Calculate Entitlements โ”€โ”
โ€ข Base salaries
โ€ข Bonuses & rewards
โ€ข Deductions & penalties
โ€ข Social insurance
โ€ข Work tax
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Create Journal Entry โ”€โ”€โ”€โ”
Date: Last day of month
Comment: "ุงุณุชุญู‚ุงู‚ ู…ุฑุชุจุงุช"
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Debit Employee Accounts โ”
Individual employee GL
accounts with amounts
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€ Credit Liability Accts โ”€โ”
โ€ข Salaries payable
โ€ข Bonuses payable
โ€ข Insurance payable
โ€ข Tax payable
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Utility Functions

getSaveValueBefore($saveid)

function getSaveValueBefore($saveid) // Line 877

updateSave($saveid, $savevalueafter)

function updateSave($saveid, $savevalueafter) // Line 925

insertSavedaily()

function insertSavedaily($savedailysavebefore, $savedailychangeamount, $savedailychangetype, 
    $saveid, $processname, $savedailymodelid, $savedailysaveafter, $tablename) // Line 941

---

๐Ÿ”„ Business Logic Flow

Monthly Salary Calculation Workflow

โ”Œโ”€ START: Monthly Report โ”€โ”
โ–ผ
โ”Œโ”€ Select Month โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”โ”‚
User input: YYYY-MM
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ–ผ
โ”Œโ”€ Load Employee Data โ”€โ”€โ”€โ”โ”‚
โ€ข Active employees
โ€ข Branch filtering
โ€ข Base salaries
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ–ผ
โ”Œโ”€ Calculate Components โ”€โ”โ”‚
For each employee:
โ€ข Base salary
โ€ข Bonuses
โ€ข Rewards
โ€ข Deductions
โ€ข Absences
โ€ข Advances/loans
โ€ข Previous balance
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ–ผ
โ”Œโ”€ Apply Formulas โ”€โ”€โ”€โ”€โ”€โ”€โ”โ”‚
Net = (Salary + Bonus
+ Reward) - (Discount
+ Withdraw + Absence
+ Repayment + Leave)
+ Previous Balance
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ–ผ
โ”Œโ”€ Generate Report โ”€โ”€โ”€โ”€โ”€โ”โ”‚
Array format for
display/printing
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ–ผ
โ”Œโ”€ END: Display Report โ”€โ”€โ”˜

Salary Payment Processing

โ”Œโ”€ START: Pay Salaries โ”€โ”€โ”
โ–ผ
โ”Œโ”€ Select Employees โ”€โ”€โ”€โ”€โ”โ”‚
โ€ข Checkbox selection
โ€ข Enter amounts
โ€ข Choose payment acc
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ–ผ
โ”Œโ”€ Validate Data โ”€โ”€โ”€โ”€โ”€โ”€โ”โ”‚
โ€ข Employee exists
โ€ข Amount > 0
โ€ข Account has funds
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ–ผ
โ”Œโ”€ Start Transaction โ”€โ”€โ”โ”‚
Database transaction
for atomicity
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ–ผ
โ”Œโ”€ Process Each Employeeโ”โ”‚
For each selected:
โ€ข Deduct from account
โ€ข Create daily entry
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ–ผ
โ”Œโ”€ Generate Accounting โ”€โ”โ”‚
Dr: Cash/Bank Account
Cr: Employee Account
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ–ผ
โ”Œโ”€ Cost Center Alloc โ”€โ”€โ”€โ”โ”‚
to cost center
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ–ผ
โ”Œโ”€ Commit Transaction โ”€โ”€โ”โ”‚
Save all changes
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ–ผ
โ”Œโ”€ END: Success/Error โ”€โ”€โ”€โ”˜

Salary Component Calculations

โ”Œโ”€ Basic Salary โ”€โ”€โ”€โ”€โ”
โ””โ”€ Bonuses โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”Œโ”€ Performance Bonusโ”
โ””โ”€ Retroactive Payโ”€โ”˜
โ”Œโ”€ Absences โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ””โ”€ Other Discountsโ”˜

---

โš ๏ธ Common Issues

Known Bugs & Limitations

1. Transaction Safety

- Issue: Some operations lack proper transaction wrapping

- Location: Various functions

- Impact: Data inconsistency if operations fail mid-process

- Solution: Add transaction management to all multi-table operations

2. Duplicate Object Creation

- Issue: Same objects created multiple times

- Location: Lines 54-57, 123-125 (Savedaily objects)

- Impact: Memory waste and confusion

- Solution: Remove duplicate object creation

3. Array Index Safety

- Issue: Direct array access without isset() checks

- Location: Receipt() function processing $_POST arrays

- Impact: PHP warnings for missing indices

- Solution: Add proper array validation

4. Date Format Validation

- Issue: No validation for date format inputs

- Location: Month parameter processing

- Impact: SQL errors or incorrect calculations

- Solution: Add date format validation

PHP 8.2 Compatibility

1. Object Initialization

- All stdClass objects properly initialized before property assignment

- No "attempt to assign property on null" errors

2. Type Casting

- Proper float casting for salary calculations

- Safe array handling for employee data

---

๐Ÿ”— Dependencies

Required Files

Required DAOs

Related Controllers

Template Files

Key Payroll Features

โ†‘