Chart of Accounts Controller Documentation
File: /controllers/accountstree.php
Purpose: Manages the hierarchical chart of accounts structure for the accounting system
Last Updated: December 19, 2024
Total Functions: 8
Lines of Code: 686
---
๐ Overview
The Chart of Accounts Controller manages the complete financial account structure in the ERP system. It handles:
- โข Hierarchical account tree management (parent-child relationships)
- โข Account classification (assets, liabilities, equity, revenue, expenses)
- โข Account nature configuration (debit/credit)
- โข Account ordering and layout management
- โข Account tree visualization and reporting
- โข Excel import/export for account structure
- โข Opening balance management integration
- โข Tree level configuration (up to 6 levels deep)
Primary Functions
- โ Display hierarchical account tree
- โ Add new accounts with parent-child relationships
- โ Edit account properties and classifications
- โ Delete accounts (with restrictions)
- โ Excel import for bulk account creation
- โ Excel export of account structure
- โ Account tree configuration management
- โ Opening balance entry support
- โ Account ordering and layout management
Related Controllers
- โข dailyentry.php - Journal entries using chart accounts
- โข sellbillController.php - Sales transactions
- โข buyBillController.php - Purchase transactions
- โข expensesController.php - Expense management
- โข saveController.php - Cash management
- โข bankController.php - Bank account management
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **accountstree** | Main chart of accounts | id, customName, parent, itemtype, itemtype2, layingOrder, theValue, accountNature, reportid, listId | |
| **accountstreesetting** | Tree level configuration | levelno, nooffields | |
| **programsetting** | System configuration | Program-wide settings |
| Table Name | Purpose | Relationship | |
|---|---|---|---|
| **dailyentry** | Journal entries | Uses accountstree.id | |
| **dailyentrycreditor** | Credit entries | Links to accountstree.id | |
| **dailyentrydebtor** | Debit entries | Links to accountstree.id | |
| **sellbill** | Sales transactions | Auto-creates journal entries | |
| **buybill** | Purchase transactions | Auto-creates journal entries | |
| **savedaily** | Cash movements | Links to cash accounts |
| Table Name | Purpose | Relationship | |
|---|---|---|---|
| **youtubelink** | Help videos | Training materials | |
| **branch** | Branch management | Multi-branch support |
๐ง Key Functions
1. Main Display (Default Action)
Purpose: Display the main chart of accounts interface
Line: 124
Process Flow:
Key Operations:
- โข Initializes 6-level tree structure
- โข Loads account tree settings
- โข Generates HTML tree display
- โข Applies user permissions for client/supplier visibility
2. display_children($parent, $level, $itr, $itr2)
Purpose: Recursive function to build hierarchical account tree display
Line: 310
Parameters:
- โข
$parent(int) - Parent account ID (0 for root) - โข
$level(int) - Current tree level (0-based) - โข
$itr(string) - Iteration string without zeros - โข
$itr2(string) - Iteration string with zeros
Process Flow:
Business Logic:
- โข Generates hierarchical account codes based on level settings
- โข Updates database laying order for search optimization
- โข Builds interactive tree HTML with edit/delete controls
- โข Calculates account values for parent accounts
3. set()
Purpose: Configure chart of accounts settings
Line: 177
Parameters (via $_POST):
- โข
calTreeNodes- Enable/disable tree node calculations - โข
showClientsAtTree- Show/hide client accounts in tree - โข
showSuppliersAtTree- Show/hide supplier accounts in tree - โข
level1tolevel6- Number of digits for each tree level
Process Flow:
4. addTreeFromExcel()
Purpose: Import complete chart of accounts from Excel file
Line: 492
Excel Columns:
- โข Column B: Account code
- โข Column D: Account name (Arabic)
- โข Column E: Account name (English)
- โข Column F: Item type 3
- โข Column G: Parent code
- โข Column H: Level
- โข Column Q: Account nature (ู ุฏูู/ุฏุงุฆู)
- โข Column R: Report list (ู ูุฒุงููุฉ/ูุงุฆู ุฉ ุงูุฏุฎู)
Process Flow:
Critical Operations:
- โข Truncates existing chart of accounts
- โข Maps parent-child relationships by laying order codes
- โข Converts Arabic nature text to numeric values
- โข Assigns proper report classifications
5. exportAsExcel()
Purpose: Export chart of accounts to Excel format
Line: 225
Excel Output Columns:
- โข A: Account Code (layingOrder)
- โข B: Account Name
- โข C: Parent Name
- โข D: Account Nature (ู ุฏููุฉ/ุฏุงุฆูุฉ)
- โข E: Report Type (ู ุชุงุฌุฑุฉ/ุงุฑุจุงุญ ู ุฎุณุงุฆุฑ)
- โข F: Statement Type (ู ูุฒุงููุฉ/ูุงุฆู ุฉ ุงูุฏุฎู)
Features:
- โข Color-coded rows by tree level
- โข Formatted borders and fonts
- โข Auto-sized columns
- โข Arabic text support
6. getTheTrueValue($parent, $sum)
Purpose: Calculate cumulative values for parent accounts
Line: 411
Logic:
- โข Recursively sums child account values
- โข Respects visibility settings for clients/suppliers
- โข Updates parent account totals dynamically
7. checkStartGoods()
Purpose: Check if opening goods entry is needed
Line: 464
Process:
- โข Checks if any sales/purchase bills exist
- โข Sets flag for opening balance requirement
- โข Integrates with opening entry functionality
8. exportToExcel($type, $level)
Purpose: Format individual account row for Excel export
Line: 618
Formatting:
- โข Level-based color coding
- โข Proper data type handling
- โข Arabic text formatting
---
๐ Business Logic Flow
Account Tree Structure
The system uses a 6-level hierarchical structure:
Level 1: Main Categories (Assets, Liabilities, etc.)
โโโ Level 2: Sub-categories
โโโ Level 3: Account Groups
โโโ Level 4: Sub-groups
โโโ Level 5: Account Types
โโโ Level 6: Individual Accounts
Account Classification System
Account Types (itemtype):
- โข 0: Assets (ุงุตูู)
- โข 1: Liabilities (ุฎุตูู )
- โข 2: Equity (ุญููู ุงูู ูููุฉ)
- โข 3: Expenses (ู ุตุฑููุงุช)
- โข 4: Revenue (ุงูุฑุงุฏุงุช)
- โข 5: Cost of Sales (ุชูููุฉ ุงูู ุจูุนุงุช)
Account Nature (accountNature):
- โข 0: Debit Nature (ู ุฏููุฉ)
- โข 1: Credit Nature (ุฏุงุฆูุฉ)
Report Classifications (reportid/listId):
- โข 1: Balance Sheet (ู ูุฒุงููุฉ)
- โข 2: Income Statement (ูุงุฆู ุฉ ุงูุฏุฎู)
Double-Entry Bookkeeping Integration
---
โ ๏ธ Common Issues
1. Tree Level Configuration
Issue: Incorrect digit allocation for account codes
Solution: Configure level settings with appropriate digit counts:
Level 1: 2 digits (01, 02, 03...)
Level 2: 2 digits (0101, 0102...)
Level 3: 2 digits (010101, 010102...)
2. Excel Import Failures
Issue: Excel import truncates existing data
Prevention:
- โข Always backup before import
- โข Verify Excel format matches template
- โข Test with small dataset first
3. Account Code Conflicts
Issue: Duplicate laying order codes
Solution: System auto-generates codes based on tree structure
4. Visibility Settings
Issue: Accounts hidden unexpectedly
Check: Cookie settings for client/supplier visibility:
- โข
showClientsAtTree - โข
showSuppliersAtTree
---
๐ Dependencies
Required Files
- โข
../public/impOpreation.php- Core operations - โข
../public/config.php- System configuration - โข
../public/include_dao.php- Database layer - โข
dailyentryfun.php- Daily entry functions - โข
../library/uploadImages.php- File upload handling - โข
../library/Classes/PHPExcel/IOFactory.php- Excel processing
Key DAOs
- โข
AccountstreeDAO- Account CRUD operations - โข
AccountstreesettingDAO- Tree configuration - โข
ProgramsettingsDAO- System settings - โข
SellbillDAO- Sales integration - โข
ReturnsellbillDAO- Returns integration
JavaScript Integration
- โข Tree expansion/collapse functionality
- โข Account editing modals
- โข AJAX account operations
- โข Dynamic value calculations
---
๐ฏ Account Tree Configuration
Standard Account Structure
Special Account IDs
- โข 58, 60: Client accounts (can be hidden)
- โข 81, 87: Supplier accounts (can be hidden)
---
Critical Note: This controller manages the foundation of the entire accounting system. All financial transactions ultimately post to accounts defined here. Changes to the account structure should be made carefully and with proper backup procedures.