Profitandlossreport Documentation
Profit and Loss Report Controller Documentation
File: /controllers/profitandlossreport.php
Purpose: Generates comprehensive profit and loss statements by analyzing account tree data, calculating gross profit/loss, and presenting financial performance reports
Last Updated: December 20, 2024
Total Functions: 6+
Lines of Code: ~405
---
๐ Overview
The Profit and Loss Report Controller creates detailed financial statements following traditional accounting principles. It processes the chart of accounts (account tree) to generate:
- โข Trading Account: Gross profit/loss calculation from sales vs cost of goods sold
- โข Profit & Loss Statement: Net profit/loss after operating expenses and other income
- โข Account Tree Navigation: Hierarchical display of income and expense accounts
- โข Inventory Valuation: Beginning and ending inventory calculations
- โข Financial Performance Analysis: Complete business profitability overview
Primary Functions
- โ Calculate gross profit from trading operations
- โ Determine net profit after all expenses and income
- โ Process beginning and ending inventory valuations
- โ Organize accounts by revenue and expense categories
- โ Generate formatted profit & loss statements
- โ Support both traditional and modern report formats
- โ Provide detailed account breakdowns
Related Controllers
- โข accountstreeController.php - Chart of accounts management
- โข dailyentryController.php - Journal entry processing
- โข storeController.php - Inventory management
- โข storedetailController.php - Inventory tracking
---
๐๏ธ Database Tables
Primary Analysis Tables
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **accountstree** | Chart of accounts structure | id, name, parent, theValue, customName | |
| **storedetail** | Inventory items and values | storedetailid, storeId, productid, productquantity, productBuyPrice | |
| **store** | Store/warehouse master | storeId, storeName, conditions |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **dailyentry** | Journal entries | dailyentryid, entryComment, entryDate | |
| **dailyentrycreditor** | Credit side entries | dailyentryid, accountstreeid, value | |
| **dailyentrydebtor** | Debit side entries | dailyentryid, accountstreeid, value | |
| **youtubelink** | Tutorial links | youtubelinkid, title, url |
๐ Key Functions
1. Default Action - Traditional P&L Report
Location: Line 91
Purpose: Generate traditional profit and loss statement with trading account
Process Flow:
Phase 1: Trading Account Calculation
// Beginning Inventory (Account ID 19)
$accountsTree = $accountsTreeDAO->load(19);
$startGoodsVal = $accountsTree->theValue;
// Ending Inventory (Current stock valuation)
$endGoodsVal = getStartGoodsVal();
Phase 2: Gross Profit Calculation
// Expenses (Debtor side - Account parent 3)
$debtorArr2 = array();
array_push($myArray, $startGoodsValTreeObj); // Add beginning inventory
display_childrenExcept(3); // Get all expenses
foreach ($myArray as $value) {
array_push($debtorArr2, $value);
$totalDebtor2 += $value->theValue;
}
// Income (Creditor side - Account parent 4)
$creditorArr2 = array();
array_push($myArray, $endGoodsValTreeObj); // Add ending inventory
display_childrenExcept(4); // Get all income
foreach ($myArray as $value) {
array_push($creditorArr2, $value);
$totalCreditor2 += $value->theValue;
}
// Gross Profit/Loss
$netvalue2 = $totalCreditor2 - $totalDebtor2;
Phase 3: Net Profit Calculation
// If gross loss, add to expenses; if gross profit, add to income
if ($netvalue2 < 0) {
$netVal2TreeObj->name = 'ุงุฌู
ุงูู ุงูุฎุณุงุฑุฉ';
// Add gross loss to expense side
} else {
$netVal2TreeObj->name = 'ุงุฌู
ุงูู ุงูุฑุจุญ';
// Add gross profit to income side
}
// Final net profit/loss
$netvalue = $totalCreditor - $totalDebtor;
---
2. incomeList - Modern P&L Format
Location: Line 228
Purpose: Generate income statement using modern chart of accounts structure
Account Categories Processed:
Revenue Section
// Main Revenue (Account 140)
$revenues = $accountsTreeDAO->load(140);
// Income of Activity (Account 141)
$incomeOfActivity = $accountsTreeDAO->load(141);
orderAsTree(141, 1); // Get all sub-accounts
Expense Sections
// Cost of Operations (Account 160)
$workingCost = $accountsTreeDAO->load(160);
// Operating Expenses (Account 412)
$workingExpenses = $accountsTreeDAO->load(412);
// General & Administrative (Account 413)
$generalAndManegeralExpenses = $accountsTreeDAO->load(413);
Other Items
// Allowed Discounts (Account 398)
$allowedDiscount = $accountsTreeDAO->load(398);
// Other Revenues (Account 147)
$otherRevenues = $accountsTreeDAO->load(147);
---
3. display_childrenExcept() - Recursive Account Processing
Location: Line 292
Purpose: Recursively process account tree to get all child accounts
Function Signature:
function display_childrenExcept($parent)
Process Flow:
1. Query all child accounts under specified parent
2. For each account, check if it has further children
3. If has children: Calculate sum of all child values using getTheTrueValue()
4. If no children: Use account's direct value
5. Add account to global $myArray for processing
---
4. getTheTrueValue() - Recursive Value Calculation
Location: Line 321
Purpose: Calculate true value of account by summing all child account values
Function Signature:
function getTheTrueValue($parent, $sum)
Process Flow:
1. Get all child accounts under parent
2. Add each child's value to running sum
3. For accounts with children, recursively call self
4. Return total accumulated value
---
5. orderAsTree() - Hierarchical Account Display
Location: Line 339
Purpose: Create hierarchical tree structure for account display
Function Signature:
function orderAsTree($parent, $level)
Process Flow:
1. Get all children of specified parent account
2. For each child:
- Set level indicator for indentation
- Cast value to float for display
- Add to ordered array
- Recursively process child's children
---
6. getStartGoodsVal() - Inventory Valuation
Location: Line 379
Purpose: Calculate current inventory value from all stores
Function Signature:
function getStartGoodsVal()
Process Flow:
1. Load all active stores
2. For each store:
- Get all products with quantities and buy prices
- Calculate: quantity ร buy_price
- Sum all product values
3. Return total inventory value rounded to 2 decimal places
Calculation:
foreach ($products as $pro) {
$productsTotalValue += $pro->productquantity * $pro->productBuyPrice;
}
$productsTotalValue = round($productsTotalValue, 2);
---
๐ Workflows
Workflow 1: Traditional Profit & Loss Generation
Workflow 2: Modern Income Statement Format
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) | Default | Generate traditional P&L statement | |
| `do=incomeList` | Modern format | Generate modern income statement format | |
| `do=sucess` | Success page | Display success template | |
| `do=error` | Error page | Display error template |
๐งฎ Financial Calculations
Gross Profit Calculation
// Trading Account Formula
Gross Profit = (Sales Revenue + Ending Inventory) - (Beginning Inventory + Cost of Sales)
// In code:
$netvalue2 = $totalCreditor2 - $totalDebtor2;
// Where:
// $totalCreditor2 = Sales + Ending Inventory
// $totalDebtor2 = Beginning Inventory + Purchases/Costs
Inventory Valuation
// Current Inventory Value
foreach ($stores as $store) {
foreach ($products_in_store as $product) {
$inventory_value += $product->quantity * $product->buy_price;
}
}
Net Profit Calculation
// Final P&L Calculation
if ($gross_profit >= 0) {
$net_profit = $gross_profit + $other_income - $operating_expenses;
} else {
$net_profit = $other_income - ($gross_loss + $operating_expenses);
}
Account Tree Value Aggregation
// Recursive value calculation for parent accounts
function getTheTrueValue($parent) {
$total = 0;
foreach ($child_accounts as $child) {
$total += $child->theValue;
if (hasChildren($child)) {
$total += getTheTrueValue($child->id);
}
}
return $total;
}
---
๐ Account Structure
Standard Account Hierarchy
Revenue Accounts (Parent 4):
- โข Sales Revenue
- โข Service Revenue
- โข Other Income
Expense Accounts (Parent 3):
- โข Cost of Goods Sold
- โข Operating Expenses
- โข Administrative Expenses
Specific Account IDs:
- โข Account 19: Beginning Inventory
- โข Account 140: Main Revenues
- โข Account 141: Income of Activity
- โข Account 147: Other Revenues
- โข Account 160: Cost of Operations
- โข Account 398: Allowed Discounts
- โข Account 412: Operating Expenses
- โข Account 413: General & Administrative
---
๐ Security & Permissions
Authentication Requirements
include_once("../public/authentication.php");
Data Access
- โข Read-only access to account tree data
- โข No modification of financial records
- โข Safe for general user access
Performance Safeguards
- โข Efficient recursive algorithms
- โข Cached calculation results
- โข Optimized database queries
---
๐ Performance Considerations
Optimization Strategies
1. Account Tree Indexing: Ensure parent-child relationships are indexed
2. Inventory Calculation: Cache inventory values for better performance
3. Recursive Limits: Monitor depth of account tree recursion
4. Memory Management: Efficient array handling for large account structures
Known Performance Factors
- โข Account tree depth affects recursive calculation time
- โข Large inventory datasets slow ending inventory calculation
- โข Complex account structures require more processing
Recommended Optimizations
-- Index for account tree performance
CREATE INDEX idx_accountstree_parent ON accountstree(parent);
CREATE INDEX idx_storedetail_store ON storedetail(storeId);
-- Query optimization for inventory
SELECT storeId,
SUM(productquantity * productBuyPrice) as store_value
FROM storedetail
GROUP BY storeId;
---
๐ Common Issues & Troubleshooting
1. Incorrect Gross Profit
Issue: Gross profit calculation shows unexpected results
Cause: Beginning/ending inventory values incorrect
Debug Steps:
-- Verify beginning inventory account
SELECT * FROM accountstree WHERE id = 19;
-- Check current inventory calculation
SELECT SUM(productquantity * productBuyPrice) FROM storedetail;
-- Verify account tree structure
SELECT * FROM accountstree WHERE parent IN (3,4);
2. Missing Account Values
Issue: Some accounts show zero values when they shouldn't
Cause: Account tree structure issues or missing daily entries
Solutions:
- โข Verify account parent-child relationships
- โข Check that daily entries are properly linked to accounts
- โข Ensure account tree values are updated from transactions
3. Recursive Function Errors
Issue: Account tree processing fails or times out
Cause: Circular references or very deep account hierarchies
Prevention:
// Add depth limiting to recursive functions
function getTheTrueValue($parent, &$sum, $depth = 0) {
if ($depth > 10) return; // Prevent infinite recursion
// ... rest of function
}
4. Inventory Valuation Discrepancies
Issue: Ending inventory doesn't match expectations
Cause: Product cost changes or quantity errors
Verification:
-- Check for negative quantities
SELECT * FROM storedetail WHERE productquantity < 0;
-- Verify buy prices are reasonable
SELECT MIN(productBuyPrice), MAX(productBuyPrice), AVG(productBuyPrice)
FROM storedetail WHERE productBuyPrice > 0;
---
๐งช Testing Scenarios
Test Case 1: Basic P&L Generation
1. Set up simple account structure with revenue and expenses
2. Add beginning inventory value
3. Create some store inventory
4. Generate P&L report
5. Verify gross and net profit calculations manually
Test Case 2: Complex Account Hierarchy
1. Create multi-level account tree (parent->child->grandchild)
2. Add values at different levels
3. Run recursive value calculation
4. Verify parent accounts show sum of all children
Test Case 3: Inventory Valuation
1. Add products to multiple stores
2. Set various quantities and buy prices
3. Calculate expected total value manually
4. Run getStartGoodsVal() function
5. Compare results with manual calculation
Test Case 4: Modern Format Display
1. Set up accounts using modern account IDs (140, 141, etc.)
2. Generate modern format report
3. Verify hierarchical display with proper indentation
4. Check that all account categories appear correctly
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข Account Tree Management - Chart of accounts setup
- โข Daily Entry System - Journal entry processing
- โข Inventory Management - Store and product tracking
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur