IncomeType Documentation
Income Type Controller Documentation
File: /controllers/incomeTypeController.php
Purpose: Manages hierarchical income category system with tree structure and accounting integration for revenue classification
Last Updated: December 20, 2024
Total Functions: 8+
Lines of Code: ~383
---
๐ Overview
The Income Type Controller manages a comprehensive income categorization system with hierarchical tree structure and chart of accounts integration. It provides:
- โข Hierarchical income category management with parent-child relationships
- โข Integration with chart of accounts tree for financial reporting
- โข Duplicate name validation to prevent conflicts
- โข Default value settings for automated income entries
- โข Tree-structured income type display
- โข Dependency checking before deletion
- โข JSON API support for external integrations
- โข Multi-language support for error messages
Primary Functions
- โ Create hierarchical income categories
- โ Tree-structured category display
- โ Integration with accounting chart of accounts
- โ Duplicate prevention with name validation
- โ Parent-child relationship management
- โ Dependency validation before deletion
- โ CRUD operations with JSON API support
- โ Multi-language error handling
Related Controllers
- โข incomeController.php - Income transaction management
- โข accountstree.md - Chart of accounts management
- โข incomeStatmentForPeriod.php - Income reporting
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **incometype** | Income category hierarchy | incomeTypeId, incomeTypeName, parent, treeId, defaultValue, conditions | |
| **income** | Income transactions | incomeId, incomeTypeId, incomeValue, incomeDate, conditions |
| Table Name | Purpose | Key Columns |
|---|---|---|
| **accountstree** | Chart of accounts | id, name, parent, customName |
| Table Name | Purpose | Key Columns |
|---|---|---|
| **youtubelink** | Tutorial links | youtubelinkid, title, url |
๐ Key Functions
1. Default Action - Add Form Display
Location: Line 76
Purpose: Display income type creation form with parent hierarchy
Process Flow:
$allParents = $IncometypeEX->getParents();
$smarty->assign("allParents", $allParents);
$smarty->display("incomeTypeview/add.html");
---
2. add() - Create Income Type with Validation
Location: Line 195
Purpose: Create new income type with duplicate checking and accounting integration
Function Signature:
function add()
Process Flow:
1. Input Processing:
$name = $_POST['name'];
$descripe = $_POST['descripe'];
$parentid = $_POST['parent'];
$defaultValue = $_POST["defaultValue"];
```
2. **Duplicate Validation**:
```php
$getData = $IncometypeDAO->queryByIncomeTypeName($name);
if (!empty($getData)) {
$checkIsert = '1'; // Duplicate found
}
```
3. **Database Insert**:
```php
if (empty($getData)) {
$incomeType->incomeTypeName = $name;
$incomeType->incomeTypeDetails = $descripe;
$incomeType->conditions = 0;
$incomeType->incomeTypeDate = date("Y-m-d");
$incomeType->userid = $userId;
$incomeType->parent = $parentid;
$incomeType->defaultValue = $defaultValue;
$incomeType->webApiId = (int) $_POST['webApiId'];
$id = $IncometypeDAO->insert($incomeType);
}
```
4. **Chart of Accounts Integration**:
```php
if ($parentid == 0) {
$treeId = addTreeElement($name, 151, 2, 0, 0, '', 0, 0); // Root level (151)
} else {
$oldData2 = $IncometypeDAO->load($parentid);
$treeId2 = $oldData2->treeId;
$treeId = addTreeElement($name, $treeId2, 2, 0, 0, '', 0, 0); // Under parent
}
$incomeType->treeId = $treeId;
$IncometypeDAO->update($incometype);
```
**Return Value**:
php
return array($checkIsert, $id);
// [0] = '0' for success, '1' for duplicate
// [1] = New income type ID
---
### 3. **show()** - Display Income Types
**Location**: Line 116
**Purpose**: Display all income types with tutorial links
**Implementation**:
php
$incomedata = $IncometypeDAO->queryByConditions(0); // Get non-deleted types
$smarty->assign("incomedata", $incomedata);
$youtubes = $youtubeLinkDAO->queryAll();
$smarty->assign("youtubes", $youtubes);
---
### 4. **update()** - Modify Income Type
**Location**: Line 249
**Purpose**: Update income type with accounting tree synchronization
**Process Flow**:
1. **Input Processing**:
```php
$name = $_POST['name'];
$descripe = $_POST['descripe'];
$parentid = $_POST['parent'];
$defaultValue = $_POST["defaultValue"];
$id = $_POST["incomeTypeId"];
```
2. **Chart of Accounts Synchronization**:
```php
$oldData = $IncometypeDAO->load($id);
$treeId = $oldData->treeId;
$getRow = $accountsTreeDAO->load($treeId);
if ($parentid == 0) {
$getRow->parent = 151; // Root level
} else {
$oldData2 = $IncometypeDAO->load($parentid);
$getRow->parent = $oldData2->treeId;
}
$getRow->name = $name;
$getRow->customName = $name;
editTreeElement($getRow);
```
---
### 5. **delete()** - Delete with Dependency Validation
**Location**: Line 311
**Purpose**: Delete income type with comprehensive dependency checking
**Process Flow**:
1. **Dependency Validation**:
```php
// Check for associated income transactions
$incomedata = $incomeDAO->queryByIncomeTypeId($incometypeid);
// Check for child categories
$childCategories = $IncometypeDAO->queryByParent($incometypeid);
if (count($incomedata) > 0 || count($childCategories) > 0) {
// Cannot delete - has dependencies
}
```
2. **Safe Deletion**:
```php
$incomeType = $IncometypeDAO->load($incometypeid);
$incomeType->conditions = 1; // Mark as deleted
$IncometypeDAO->update($incomeType);
delTreeElementById($incomeType->treeId); // Remove from chart of accounts
```
3. **Multi-language Error Handling**:
```php
if ($_SESSION['erp_lang'] == 'ar') {
$note = "ูุง ูู
ูู ุญุฐู ูุฐุง ุงูููุน ูุงูู ู
ุฑุชุจุท ุจุจูุงูุงุช ุงุฎุฑู";
} else {
$note = "This type cannot be deleted because it is associated with other data";
}
```
---
## ๐ Workflows
### Workflow 1: Income Type Creation with Validation
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ START: Create Income Type โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. Load Form Dependencies โ
โ - Load parent income types for dropdown โ
โ - Display creation form โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 2. Process Form Input โ
โ - Extract name, description, parent โ
โ - Extract default value and web API ID โ
โ - Validate required fields โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 3. Duplicate Name Validation โ
โ IF Income type name already exists: โ
โ โโ Return error status โ
โ โโ Display duplicate error message โ
โ ELSE: โ
โ โโ Proceed to creation โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 4. Create Database Record โ
โ - Set income type properties โ
โ - Set creation date and user โ
โ - Insert into incometype table โ
โ - Get new income type ID โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 5. Chart of Accounts Integration โ
โ IF No Parent (parentid = 0): โ
โ โโ Create under root income node (151) โ
โ ELSE: โ
โ โโ Load parent income type โ
โ โโ Get parent's tree ID โ
โ โโ Create under parent tree node โ
โ โ
โ - Create tree element with name โ
โ - Update income type with tree ID โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 6. Return Success Response โ
โ - Return success status and new ID โ
โ - Display success message or redirect โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
---
### Workflow 2: Deletion with Dependency Checking
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ START: Delete Income Type โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. Dependency Validation โ
โ A. Check Income Transactions: โ
โ โโ Query income table by incomeTypeId โ
โ B. Check Child Categories: โ
โ โโ Query incometype table by parent โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 2. Decision Point โ
โ IF Dependencies Found: โ
โ โโ Generate appropriate error message โ
โ โโ Use language-specific text โ
โ โโ Display error notification โ
โ โโ Return to listing โ
โ ELSE: โ
โ โโ Proceed with deletion โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 3. Safe Deletion Process โ
โ - Load income type record โ
โ - Set conditions = 1 (mark as deleted) โ
โ - Update record in database โ
โ - Remove from chart of accounts tree โ
โ - Return success response โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
---
## ๐ URL Routes & Actions
| URL Parameter | Function Called | Description |
|---------------|----------------|-------------|
| `do=` (empty) | Default action | Display income type creation form |
| `do=add` | `add()` | Create new income type |
| `do=show` | `show()` | Display income type listing |
| `do=edit` | `edit()` | Load income type for editing |
| `do=editprint` | `editprint()` | Display printable edit form |
| `do=update` | `update()` | Update existing income type |
| `do=delete` | `delete()` | Delete income type with validation |
### Required Parameters by Action
**Add Income Type** (`do=add`):
- `name` - Income type name (required, must be unique)
- `descripe` - Description (optional)
- `parent` - Parent income type ID (0 for root level)
- `defaultValue` - Default value for automated entries (optional)
- `webApiId` - External system integration ID (optional)
**Edit/Update** (`do=edit`, `do=update`):
- `id` - Income type ID
- Additional parameters same as add for update
**Delete** (`do=delete`):
- `id` - Income type ID to delete
### JSON API Support
php
// API request format
POST /incomeTypeController.php?do=add
Content-Type: application/x-www-form-urlencoded
curlpost=1&name=Service Income&descripe=Revenue from services&parent=0
// Success response
{
"status": 1,
"message": "ุชู ุช ุงูุนู ููู ุจูุฌุงุญ",
"message_en": "Success",
"id": 123
}
// Error response (duplicate name)
{
"status": 2,
"message": "ููุน ุงูุงูุฑุงุฏ ุชู ุงุฏุฎุงูู ู ู ูุจู",
"message_en": "This income type has been added before"
}
---
## ๐งฎ Calculation Methods
### Default Value Processing
php
$defaultValue = $_POST["defaultValue"];
if (empty($defaultValue)) {
$defaultValue = 0;
}
$incomeType->defaultValue = $defaultValue;
### Parent Tree ID Resolution
php
if ($parentid == 0) {
// Root level - use main income tree node
$treeId = addTreeElement($name, 151, 2, 0, 0, '', 0, 0);
} else {
// Child level - use parent's tree ID
$parentData = $IncometypeDAO->load($parentid);
$parentTreeId = $parentData->treeId;
$treeId = addTreeElement($name, $parentTreeId, 2, 0, 0, '', 0, 0);
}
### Duplicate Check Logic
php
function checkDuplicate($name) {
global $IncometypeDAO;
$existing = $IncometypeDAO->queryByIncomeTypeName($name);
return !empty($existing);
}
---
## ๐ Security & Permissions
### Authentication Check
php
// Implied authentication (not explicitly shown in code)
// Should be added for production security
include_once("../public/authentication.php");
### Input Validation
php
// Current validation (basic)
$name = $_POST['name'];
if (!empty($name)) {
// Proceed with operation
}
// Recommended enhanced validation
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
if (empty($name) || strlen($name) < 2) {
throw new InvalidArgumentException("Invalid income type name");
}
### SQL Injection Prevention
- Uses DAO layer with parameterized queries
- Input sanitization should be enhanced
- No direct SQL concatenation observed
### JSON API Security
php
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
// API mode - return JSON
// Should add API key validation
$data = array(
'status' => 1,
'message' => 'Success',
'id' => $id
);
echo json_encode($data);
}
---
## ๐ Performance Considerations
### Database Optimization Tips
1. **Indexes Required**:
```sql
CREATE INDEX idx_incometype_name ON incometype(incomeTypeName);
CREATE INDEX idx_incometype_parent ON incometype(parent, conditions);
CREATE INDEX idx_income_typeid ON income(incomeTypeId);
CREATE UNIQUE INDEX idx_incometype_name_unique ON incometype(incomeTypeName, conditions);
```
2. **Query Optimization**:
- Name uniqueness check should use index
- Parent-child queries optimized with proper indexing
- Dependency checking optimized for large datasets
3. **Caching Considerations**:
- Income type hierarchy could be cached
- Parent dropdown data suitable for caching
- Tree structure caching for frequent access
### Performance Monitoring
php
// Add performance monitoring
$start_time = microtime(true);
$result = $IncometypeDAO->queryByIncomeTypeName($name);
$query_time = microtime(true) - $start_time;
if ($query_time > 0.1) {
error_log("Slow query: Income type name lookup took {$query_time}s");
}
---
## ๐ Common Issues & Troubleshooting
### 1. **Duplicate Name Errors**
**Issue**: Cannot create income type with existing name
**Cause**: Name uniqueness validation
**Debug**:
php
$existing = $IncometypeDAO->queryByIncomeTypeName($name);
echo "Existing records: " . count($existing) . "
";
print_r($existing);
### 2. **Chart of Accounts Integration Errors**
**Issue**: Tree elements not created or linked properly
**Cause**: Failed tree element creation or invalid parent IDs
**Debug**:
sql
SELECT it.incomeTypeId, it.incomeTypeName, it.treeId,
at.name as tree_name, at.parent
FROM incometype it
LEFT JOIN accountstree at ON it.treeId = at.id
WHERE it.treeId IS NULL OR at.id IS NULL;
### 3. **Deletion Blocking**
**Issue**: Cannot delete income type with dependencies
**Cause**: Related income transactions or child categories exist
**Debug**:
php
$incomeCount = count($incomeDAO->queryByIncomeTypeId($incometypeid));
$childCount = count($IncometypeDAO->queryByParent($incometypeid));
echo "Income transactions: $incomeCount
";
echo "Child categories: $childCount
";
### 4. **Parent-Child Relationship Errors**
**Issue**: Circular references or invalid parent assignments
**Cause**: Logic errors in parent validation
**Prevention**:
php
function validateParent($incomeTypeId, $parentId) {
if ($parentId == 0) return true;
if ($parentId == $incomeTypeId) return false; // Self-reference
// Check for circular reference
$current = $parentId;
while ($current != 0) {
if ($current == $incomeTypeId) return false;
$parent = getParentId($current);
$current = $parent;
}
return true;
}
---
## ๐งช Testing Scenarios
### Test Case 1: Basic Income Type Creation
1. Create root level income type
2. Verify tree element creation
3. Check chart of accounts integration
4. Confirm proper ID assignment
### Test Case 2: Hierarchical Structure
1. Create parent income type
2. Create child income type under parent
3. Verify parent-child relationship
4. Test tree structure display
### Test Case 3: Duplicate Prevention
1. Create income type with specific name
2. Attempt to create another with same name
3. Verify error message display
4. Test case-sensitivity handling
### Test Case 4: Deletion Validation
1. Create income type
2. Create income transaction using the type
3. Attempt to delete the type
4. Verify deletion is blocked
5. Test error message display
### Test Case 5: JSON API Integration
1. Send API request with curlpost=1
2. Verify JSON response format
3. Test error scenarios via API
4. Check success response structure
### Debug Mode Enable
php
// Add at top of controller for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Debug duplicate checking
function debug_duplicate_check($name) {
global $IncometypeDAO;
$result = $IncometypeDAO->queryByIncomeTypeName($name);
echo "Checking name: '$name'
";
echo "Found records: " . count($result) . "
";
if (!empty($result)) {
foreach ($result as $record) {
echo "Existing: ID={$record->incomeTypeId}, Name={$record->incomeTypeName}
";
}
}
}
```
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข incomeController.md - Income transaction management
- โข accountstree.md - Chart of accounts management
- โข incomeStatmentForPeriod.md - Income reporting
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur