CostcenterReport Documentation

Cost Center Report Controller Documentation

File: /controllers/costcenterReportController.php

Purpose: Generates detailed reports for cost center activities and expense allocations with date filtering

Last Updated: December 20, 2024

Total Functions: 2

Lines of Code: ~128

---

๐Ÿ“‹ Overview

The Cost Center Report Controller provides comprehensive reporting functionality for cost center activities, allowing users to view expense allocations, track cost center usage, and analyze spending patterns by date ranges and specific cost centers.

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**costcenter**Cost center definitionsid, name, comment, condation
**costcenterdetail**Cost center transaction detailscostcenterdetailid, costcenterid, costdate
### Reference Tables

Table NamePurposeKey Columns
**user**System usersuserid, username, employeename
---

๐Ÿ”‘ Key Functions

1. Default Action - Report Form Display

Location: Line 56

Purpose: Display cost center selection form for reporting

Function Signature:

// Triggered when: empty($do)

Process Flow:

1. Load all active cost centers for selection dropdown

2. Display report form with date range inputs

3. Present cost center selection interface

Implementation:

$allCostCenter = $CostcenterDAO->queryByCondation(0); // Get active cost centers only
$smarty->assign("allCostCenter", $allCostCenter);
$smarty->display("costcenterview/report.html");

---

2. search - Generate Cost Center Report

Location: Line 62

Purpose: Execute cost center activity report with filtering

Function Signature:

// Triggered when: do=search

Process Flow:

1. Retrieve search parameters from form

2. Build dynamic date filter query

3. Execute cost center detail query

4. Display results with original search form

Parameter Processing:

$costCenterId = filter_input(INPUT_POST, 'costCenterId');
$start_date = filter_input(INPUT_POST, 'start_date');
$end_date = filter_input(INPUT_POST, 'end_date');
$details = filter_input(INPUT_POST, 'detail');

Dynamic Query Building:

$costcenter = " where 1 ";

if ($start_date != '' && $end_date != '') {
    $costcenter .= ' and costdate >= "' . $start_date . '" and costdate <= "' . $end_date . '" and costcenterid = "' . $costCenterId . '"';
} else {
    if ($start_date != '') {
        $costcenter .= ' and costdate >= "' . $start_date . '" and costcenterid = "' . $costCenterId . '"';
    }
    if ($end_date != '') {
        $costcenter .= ' and costdate <= "' . $end_date . '" and costcenterid = "' . $costCenterId . '"';
    }
}

// Default to today if no date range provided
$today = date('Y-m-d');
if ($costcenter == " where 1 ") {
    $costcenter .= ' and costdate >= "' . $today . '" and costdate <= "' . $today . '" and costcenterid = "' . $costCenterId . '"';
}

Report Execution:

$allDetails = $CostcenterdetailEX->getbydate($costcenter);
$smarty->assign("allDetails", $allDetails);
$smarty->assign("details", $details);

---

๐Ÿ”„ Workflows

Workflow 1: Cost Center Report Generation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: User Requests Cost Center Report
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Select Report Parameters
- Choose cost center from dropdown
- Set start date (optional)
- Set end date (optional)
- Choose detail level
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Validate Date Range
- Check date format and validity
- Ensure start_date <= end_date
- Default to today if no dates provided
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Build Dynamic Query
- Start with base WHERE clause
- Add cost center filter
- Add date range filters if provided
- Apply default date if no range specified
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Execute Query
- Query costcenterdetail table with filters
- Join with related tables for complete information
- Order results by date
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Display Results
- Show filtered cost center activities
- Maintain original search form
- Provide drill-down capabilities
- Display summary totals
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default actionShow report form
`do=search`Search operationGenerate filtered report
`do=sucess`Success pageDisplay success message
`do=error`Error pageDisplay error message
### Required Parameters by Action

Search Report (do=search):

---

๐Ÿงฎ Calculation Methods

Date Filter Logic

// Both dates provided
if ($start_date != '' && $end_date != '') {
    $filter = ' and costdate >= "' . $start_date . '" and costdate <= "' . $end_date . '"';
}

// Only start date
if ($start_date != '' && $end_date == '') {
    $filter = ' and costdate >= "' . $start_date . '"';
}

// Only end date  
if ($start_date == '' && $end_date != '') {
    $filter = ' and costdate <= "' . $end_date . '"';
}

// No dates - default to today
if ($start_date == '' && $end_date == '') {
    $today = date('Y-m-d');
    $filter = ' and costdate >= "' . $today . '" and costdate <= "' . $today . '"';
}

Cost Center Filter

// Always include cost center filter
$filter .= ' and costcenterid = "' . $costCenterId . '"';

---

๐Ÿ”’ Security & Permissions

Input Validation

$costCenterId = filter_input(INPUT_POST, 'costCenterId');
$start_date = filter_input(INPUT_POST, 'start_date');
$end_date = filter_input(INPUT_POST, 'end_date');
$details = filter_input(INPUT_POST, 'detail');

SQL Injection Prevention

Access Control

---

๐Ÿ› Common Issues & Troubleshooting

1. No Data Returned

Issue: Report shows no results for valid cost center

Cause: Date range too restrictive or no activities recorded

Debug:

-- Check if cost center has any activities
SELECT COUNT(*) FROM costcenterdetail WHERE costcenterid = [ID];

-- Check date range
SELECT MIN(costdate), MAX(costdate) FROM costcenterdetail WHERE costcenterid = [ID];

2. Invalid Date Range

Issue: Query returns unexpected results

Cause: Date format issues or reversed date range

Debug:

echo "Start Date: " . $start_date . "<br>";
echo "End Date: " . $end_date . "<br>";
echo "Query: " . $costcenter . "<br>";

3. Missing Cost Center Data

Issue: Cost center not appearing in dropdown

Cause: Cost center marked as deleted or inactive

Debug:

SELECT id, name, condation FROM costcenter WHERE id = [ID];
-- condation: 0 = active, 1 = deleted

---

๐Ÿงช Testing Scenarios

Test Case 1: Basic Report Generation

1. Select active cost center
2. Leave date range empty (should default to today)
3. Generate report
4. Verify today's activities shown
5. Check query building logic

Test Case 2: Date Range Filtering

1. Select cost center with known activities
2. Set specific date range covering activities
3. Generate report
4. Verify only activities in range shown
5. Test edge cases (same start/end date)

Test Case 3: Edge Case Testing

1. Test with cost center having no activities
2. Test with invalid date ranges
3. Test with future date ranges
4. Verify appropriate handling of empty results

---

๐Ÿ“Š Performance Considerations

Database Optimization Tips

1. Indexes Required:

- costcenterdetail(costcenterid, costdate) - For filtered queries

- costcenter(condation) - For active cost center selection

2. Query Optimization:

- Use date range indexes effectively

- Avoid unnecessary data retrieval

- Efficient WHERE clause construction

3. Memory Management:

- Limit result sets for large date ranges

- Process large datasets in chunks

- Clear template variables appropriately

Performance Monitoring

-- Check query performance
EXPLAIN SELECT * FROM costcenterdetail 
WHERE costcenterid = ? 
AND costdate >= ? 
AND costdate <= ?;

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur