Expenses Documentation
Expenses Controller Documentation
File: /controllers/expensesController.php
Purpose: Manages business expenses, supplier payments, and expense accounting
Last Updated: December 19, 2024
Total Functions: 18
Lines of Code: 1951
---
๐ Overview
The Expenses Controller manages all expense-related operations in the ERP system. It handles:
- โข General business expense recording
- โข Expense classification by type and category
- โข Multi-payment method support (cash, bank, credit)
- โข Supplier payment management
- โข Client expense allocations
- โข Tax calculations on expenses
- โข Cost center allocation
- โข Real estate unit expense tracking
- โข Travel/transportation expense management
- โข Automated journal entry generation
- โข Cash register and bank account updates
- โข Expense approval workflows
Primary Functions
- โ Record business expenses with multiple categories
- โ Process supplier payments and debt management
- โ Handle client expense allocations
- โ Manage cash and bank payments
- โ Calculate and apply taxes on expenses
- โ Allocate expenses to cost centers
- โ Track real estate unit expenses
- โ Record travel/transportation costs
- โ Generate automatic journal entries
- โ Update cash registers and bank balances
- โ Provide expense reporting and analytics
Related Controllers
- โข dailyentry.php - Auto journal entries for expenses
- โข saveController.php - Cash register management
- โข bankController.php - Bank account management
- โข supplierController.php - Supplier management
- โข clientController.php - Client management
- โข accountstree.php - Chart of accounts integration
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **expenses** | Main expense records | id, expensesname, expensevalue, expensetype, saveid, supplierId, paymethod, taxExist, tax, taxVal | |
| **expensestype** | Expense categories | id, name, parent, accountstreeid | |
| **costcenterdetail** | Cost center allocations | expenseId, costcenterId, amount |
| Table Name | Purpose | Relationship | |
|---|---|---|---|
| **save** | Cash registers | Updated by cash expenses | |
| **savedaily** | Cash movements | Records cash expense transactions | |
| **bankaccount** | Bank accounts | Updated by bank expenses | |
| **accountmovement** | Bank movements | Records bank expense transactions | |
| **dailyentry** | Journal entries | Auto-generated for all expenses | |
| **dailyentrycreditor** | Credit entries | Cash/bank credit sides | |
| **dailyentrydebtor** | Debit entries | Expense account debit sides |
| Table Name | Purpose | Relationship | |
|---|---|---|---|
| **supplier** | Suppliers | For supplier payments | |
| **supplierdebtchange** | Supplier debt tracking | Payment history | |
| **client** | Clients | For client expense allocations | |
| **clientdebtchange** | Client debt tracking | Expense allocations | |
| **accountstree** | Chart of accounts | Expense account mapping | |
| **costcenter** | Cost centers | Expense allocation tracking |
| Table Name | Purpose | Relationship | |
|---|---|---|---|
| **product** | Products | For product-related expenses | |
| **sellbill** | Sales orders | For order-specific expenses | |
| **realestateunit** | Real estate | Property expense tracking | |
| **travel** | Transportation | Travel expense tracking |
๐ง Key Functions
1. Main Display (Default Action)
Purpose: Display expense entry form and recent expenses
Line: 317
Process Flow:
2. add()
Purpose: Create new expense record with full financial integration
Line: 704
Parameters (via $_POST):
- โข
expensesname- Expense description - โข
expensevalue- Expense amount - โข
expensetype- Expense category ID - โข
expensecomment- Additional notes - โข
Costcenterid- Cost center allocation - โข
saveid- Cash register ID - โข
paymethod- Payment method (0=cash, 1=bank, 2=credit) - โข
supplier- Supplier ID for payments - โข
clientid- Client ID for allocations - โข
taxExist- Tax calculation flag - โข
tax- Tax rate percentage - โข
taxVal- Calculated tax amount
Process Flow:
Critical Business Logic:
- โข Validates cash/bank balance before processing
- โข Calculates taxes if applicable
- โข Updates supplier debt for supplier payments
- โข Creates automatic journal entries
- โข Allocates to cost centers
- โข Records cash/bank movements
3. show()
Purpose: Display expense listing with search and filter capabilities
Line: 358
Features:
- โข Date range filtering
- โข Expense type filtering
- โข Supplier/client filtering
- โข Cost center filtering
- โข Pagination support
- โข Export capabilities
4. delete($expenseid)
Purpose: Delete expense and reverse all financial effects
Line: 1088
Process Flow:
5. executeOperation()
Purpose: Process bulk operations on multiple expenses
Line: 1042
Operations Supported:
- โข Bulk approval
- โข Bulk rejection
- โข Bulk export
- โข Bulk assignment
6. edit()
Purpose: Load expense for editing
Line: 1379
Process:
- โข Loads expense data
- โข Loads related cost center allocations
- โข Prepares edit form
7. update()
Purpose: Update existing expense with full reversal and re-entry
Line: 1390
Process Flow:
Note: Updates work by reversing the original expense effects and creating new entries with updated values.
8. expensesType()
Purpose: Get hierarchical expense type structure
Line: 686
Returns: Tree structure of expense categories for dropdown/tree display
9. getExpensesByType($typeid)
Purpose: Get all expenses by expense type
Line: 696
Parameters:
- โข
$typeid- Expense type ID
10. getSaveValue($saveid)
Purpose: Get current cash register balance
Line: 1715
Parameters:
- โข
$saveid- Cash register ID (optional)
11. updateSave($saveid, $savevalueafter)
Purpose: Update cash register balance
Line: 1762
12. insertSavedaily(...)
Purpose: Record cash movement in daily cash log
Line: 1775
Parameters:
- โข
$savedailysavebefore- Balance before - โข
$savedailychangeamount- Change amount - โข
$savedailychangetype- Change type (0=decrease, 1=increase) - โข
$saveid- Cash register ID - โข
$processname- Process description - โข
$savedailymodelid- Related record ID
13. insertAccountmovement(...)
Purpose: Record bank account movement
Line: 1797
14. affectSupplierWithExpense(...)
Purpose: Update supplier debt for supplier payments
Line: 1820
15. insertSupplierDebtChangeupdate(...)
Purpose: Log supplier debt change
Line: 1857
16. display_children($parent, $level)
Purpose: Build hierarchical expense type tree display
Line: 1880
17. CURL_IT2($data_arr, $url)
Purpose: API integration for expense data synchronization
Line: 1915
18. tempdelete($expenseid) / returndelete($expenseid)
Purpose: Soft delete and restore expense functionality
Lines: 1191, 1281
---
๐ Business Logic Flow
Expense Payment Methods
Cash Payment (paymethod = 0)
Bank Payment (paymethod = 1)
Credit Payment (paymethod = 2)
Tax Calculation Logic
if ($taxExist == 1) {
$taxVal = ($expensevalue * $tax) / 100;
$expensevalueAfterTax = $expensevalue + $taxVal;
}
Cost Center Allocation
---
โ ๏ธ Common Issues
1. Insufficient Cash/Bank Balance
Issue: Attempting expenses without sufficient funds
Prevention: Balance validation before processing:
if ($saveValueafterValid >= 0) {
// Process expense
} else {
// Display error
}
2. Tax Calculation Errors
Issue: Incorrect tax calculations
Solution: Validate tax rate and calculations:
$taxVal = ($expensevalue * $tax) / 100;
$expensevalueAfterTax = $expensevalue + $taxVal;
3. Supplier Debt Management
Issue: Supplier debt inconsistencies
Prevention: Always use debt change logging:
insertSupplierDebtChangeupdate(
$supplierId,
$beforeDebt,
$expensevalue,
$changeType,
$processname,
$expenseId,
$afterDebt
);
4. Journal Entry Imbalance
Issue: Debits โ Credits in auto-generated entries
Solution: Ensure all expense amounts include taxes in journal entries
5. Cost Center Allocation
Issue: Missing or incorrect cost center assignments
Prevention: Validate cost center requirements for expense types
---
๐ Dependencies
Required Files
- โข
../public/impOpreation.php- Core operations - โข
../public/authentication.php- Security - โข
dailyentryfun.php- Journal entry functions - โข
affectplugins.php- Plugin system integration - โข
initiateStaticSessionCommingWithCurl.php- Session management
Critical DAOs
- โข
ExpenseDAO- Main expense operations - โข
ExpensestypeDAO- Expense categorization - โข
SaveDAO- Cash register management - โข
BankaccountDAO- Bank account management - โข
SupplierDAO- Supplier payment integration - โข
DailyentryDAO- Journal entry automation - โข
CostcenterdetailDAO- Cost allocation
External Integrations
- โข Multi-language support
- โข Excel export capabilities
- โข API synchronization (CURL_IT2)
- โข Real estate management
- โข Travel management
- โข Product management
---
๐ฏ Expense Categories and Classification
Standard Expense Types
1. Administrative Expenses
โโโ Office Supplies
โโโ Utilities
โโโ Communication
2. Operational Expenses
โโโ Raw Materials
โโโ Transportation
โโโ Maintenance
3. Marketing Expenses
โโโ Advertising
โโโ Promotions
โโโ Events
4. Financial Expenses
โโโ Bank Charges
โโโ Interest
โโโ Exchange Differences
Payment Method Codes
- โข 0: Cash payment (immediate deduction from cash register)
- โข 1: Bank payment (immediate deduction from bank account)
- โข 2: Credit payment (added to supplier debt)
Tax Handling
- โข Tax calculations include percentage-based taxes
- โข Tax amounts recorded separately for reporting
- โข Total expense amount includes tax for accounting purposes
---
๐ฒ Best Practices
1. Expense Entry
- โข Always verify cash/bank balance before processing
- โข Include detailed expense descriptions
- โข Assign appropriate expense categories
- โข Allocate to relevant cost centers
2. Supplier Payments
- โข Verify supplier details and outstanding amounts
- โข Record proper payment references
- โข Maintain debt tracking accuracy
3. Tax Management
- โข Ensure tax rates are current and accurate
- โข Separate tax recording for compliance
- โข Include taxes in total expense amounts
4. Cost Center Allocation
- โข Use cost centers for expense tracking
- โข Maintain consistent allocation logic
- โข Regular cost center reporting
---
Critical Note: This controller handles all business expense recording and directly impacts cash flow, supplier relationships, and financial reporting. All expense entries generate automatic journal entries that must maintain double-entry bookkeeping integrity.