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
- โ Display cost center selection interface
- โ Filter cost center activities by date range
- โ Show detailed cost center transactions
- โ Generate expense allocation reports
- โ Provide drill-down capabilities for expense details
Related Controllers
- โข costcenterController.php - Cost center management
- โข expensesController.php - Expense management
- โข costcenterdetailController.php - Cost center transaction details
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **costcenter** | Cost center definitions | id, name, comment, condation | |
| **costcenterdetail** | Cost center transaction details | costcenterdetailid, costcenterid, costdate |
| Table Name | Purpose | Key Columns |
|---|---|---|
| **user** | System users | userid, 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
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) | Default action | Show report form | |
| `do=search` | Search operation | Generate filtered report | |
| `do=sucess` | Success page | Display success message | |
| `do=error` | Error page | Display error message |
Search Report (do=search):
- โข
costCenterId- Cost center ID (required) - โข
start_date- Start date (YYYY-MM-DD, optional) - โข
end_date- End date (YYYY-MM-DD, optional) - โข
detail- Detail level flag (optional)
---
๐งฎ 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
- โข Uses proper input filtering
- โข Parameterized query building
- โข Date format validation
Access Control
- โข Session-based user authentication required
- โข Cost center access may be restricted by user permissions
---
๐ 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
- โข CLAUDE.md - PHP 8.2 migration guide
- โข costcenterController.md - Cost center management
- โข expensesController.php - Expense management
- โข Database Schema Documentation - Table relationships
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur