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:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Analysis Tables

Table NamePurposeKey Columns
**accountstree**Chart of accounts structureid, name, parent, theValue, customName
**storedetail**Inventory items and valuesstoredetailid, storeId, productid, productquantity, productBuyPrice
**store**Store/warehouse masterstoreId, storeName, conditions
### Supporting Tables

Table NamePurposeKey Columns
**dailyentry**Journal entriesdailyentryid, entryComment, entryDate
**dailyentrycreditor**Credit side entriesdailyentryid, accountstreeid, value
**dailyentrydebtor**Debit side entriesdailyentryid, accountstreeid, value
**youtubelink**Tutorial linksyoutubelinkid, 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

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Generate P&L Statement
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Calculate Beginning Inventory
- Load account ID 19 (Beginning Inventory)
- Get recorded beginning value
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Calculate Ending Inventory
- Query all stores and products
- Sum (quantity ร— buy_price) for all items
- Round to 2 decimal places
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Process Trading Account (Gross Profit)
EXPENSES (Debit Side):
โ†’ Beginning Inventory
โ”‚ โ””โ”€โ†’ All accounts under parent 3 (Cost of Sales) โ”‚
INCOME (Credit Side):
โ†’ Ending Inventory
โ”‚ โ””โ”€โ†’ All accounts under parent 4 (Sales Revenue) โ”‚
CALCULATE: Gross Profit = Credit Total - Debit Total
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Process Operating Accounts
IF Gross Profit > 0:
โ”‚ โ””โ”€โ†’ Add gross profit to income side โ”‚
IF Gross Loss < 0:
โ”‚ โ””โ”€โ†’ Add gross loss to expense side โ”‚
LOAD: All other operating income and expenses
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Calculate Net Profit/Loss
Net Result = Total Income - Total Expenses
IF Net > 0: Display "ุตุงูู‰ ุงู„ุฑุจุญ" (Net Profit)
IF Net < 0: Display "ุตุงูู‰ ุงู„ุฎุณุงุฑุฉ" (Net Loss)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Format Report for Display
- Create balanced two-column format
- Align debits and credits properly
- Show totals and net result
- Display via template
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Workflow 2: Modern Income Statement Format

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Generate Modern Format
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Process Revenue Section
- Main Revenues (Account 140)
- Income of Activity (Account 141 + children)
- Build hierarchical revenue tree
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Process Cost Sections
- Cost of Operations (Account 160 + children)
- Operating Expenses (Account 412 + children)
- General & Administrative (Account 413 + children)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Process Other Items
- Allowed Discounts (Account 398)
- Other Revenues (Account 147 + children)
- Each with full hierarchical structure
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Build Hierarchical Display
FOR EACH account section:
โ†’ Show parent account
โ†’ Indent and show child accounts
โ†’ Calculate subtotals
โ”‚ โ””โ”€โ†’ Maintain level indicators โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Display Modern Format Report
- Income statement format
- Detailed account breakdowns
- Professional presentation
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)DefaultGenerate traditional P&L statement
`do=incomeList`Modern formatGenerate modern income statement format
`do=sucess`Success pageDisplay success template
`do=error`Error pageDisplay 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):

Expense Accounts (Parent 3):

Specific Account IDs:

---

๐Ÿ”’ Security & Permissions

Authentication Requirements

include_once("../public/authentication.php");

Data Access

Performance Safeguards

---

๐Ÿ“Š 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

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:

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

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur