IncomeStatmentForPeriod Documentation

Income Statement For Period Controller Documentation

File: /controllers/incomeStatmentForPeriod.php

Purpose: Generates comprehensive income statements with profit/loss calculations for specified periods using multiple pricing methods

Last Updated: December 20, 2024

Total Functions: 25+

Lines of Code: ~991

---

๐Ÿ“‹ Overview

The Income Statement For Period Controller is a sophisticated financial reporting module that generates detailed profit and loss statements. It provides comprehensive financial analysis with:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Sales Revenue Tables

Table NamePurposeKey Columns
**sellbill**Sales transactionssellbillid, sellbillaftertotalbill, sellbilltotalpayed, sellbilldate, conditions
**sellbilldetail**Sales line itemssellbilldetailid, sellbillid, sellbilldetailquantity, sellbilldetailtotalprice, productunitid
**returnsellbill**Sales returnsreturnsellbillid, returnsellbillaftertotalbill, returnsellbillsysdate
**returnsellbilldetail**Return line itemsreturnsellbilldetailid, returnsellbillid, returnsellbilldetailquantity
**sellbillandrutern**Combined sell/return billssellbillid, sellbillprice, returnsellbillprice, sellbilldate
**sellandruternbilldetail**Combined bill detailssellandruternbilldetailid, sellbillid, selltype
### Cost of Goods Tables

Table NamePurposeKey Columns
**product**Product master dataproductid, lastbuyprice, meanbuyprice, lastbuyprice_withDiscount, meanbuyprice_withDiscount
**productunit**Product unitsproductunitid, productnumber
**buybilldetail**Purchase details for costbuybilldetailid, buyprice, buydiscount, buydiscounttype
**restaurantrawdestruction**Raw material wasteid, productid, quantity, productBuyPrice, lastbuyprice, meanbuyprice
### Expense Tables

Table NamePurposeKey Columns
**expenses**Operating expensesexpensesid, expensesValue, expensesdate, expensestypeid
**expensestype**Expense categoriesexpensestypeid, expensestypename
**salaryreport**Payroll costssalaryreportid, salaryreportnet, salaryreportdate
**employeepersonnel**Employee transactionsemployeepersonnelid, employeepersonneltype, employeepersonnelvalue
### Service Revenue Tables

Table NamePurposeKey Columns
**bills**Service billsbillid, finalnetbillvalue, billdate
**billsproducts**Service line itemsbillsproductsid, productno, productBuyPrice
**billsreturn**Service returnsbillsreturnid, finalnetbillvalue
### Configuration Tables

Table NamePurposeKey Columns
**programsettings**System settingsprogramsettingsid, reportsPlusHours, vatValue
**lastbillidspricesupdated**Price trackingid, buybill, sell, retsell, buyquantity
---

๐Ÿ”‘ Key Functions

1. show() / Default Action - Generate Income Statement

Location: Line 284

Purpose: Main function that orchestrates income statement generation

Process Flow:

1. Date Range Processing:

   $startDate = $_REQUEST['from'];
   $endDate = $_REQUEST['to'];
   $buyPriceType = $_POST["buyPriceType"];
   
   if (empty($startDate) && empty($endDate)) {
       $startDate = $endDate = date('Y-m-d');
   }
   ```

2. **Time Zone Adjustment**:
   ```php
   if (isset($Programsetting->reportsPlusHours) && !empty($Programsetting->reportsPlusHours)) {
       $reportsPlusHours = $Programsetting->reportsPlusHours + 24;
       $endDate = date('Y-m-d H:i:s', strtotime('+' . $reportsPlusHours . ' hour', strtotime($endDate)));
       $startDate = date('Y-m-d H:i:s', strtotime('+' . $Programsetting->reportsPlusHours . ' hour', strtotime($startDate)));
   }
   ```

3. **Revenue Calculation**:
   ```php
   $totalSellPrice = getTotalSellbillByDate($startDate, $endDate) + getTotalAditionalSellbillByDate($startDate, $endDate);
   $totalReturnSellPrice = getTotalReturnSellbillByDate($startDate, $endDate) + getTotalAditionalReturnSellbillByDate($startDate, $endDate);
   $totalSellCost = $totalSellPrice - $totalReturnSellPrice;
   ```

4. **Cost of Goods Sold**:
   ```php
   $bills_sellcost = get_total_sellcost($startDate, $endDate);
   $total_sellcost = 0;
   foreach ($bills_sellcost as $value) {
       switch ($buyPriceType) {
           case "first": $pro_price = (float) $value->buyprice; break;
           case "last": $pro_price = (float) $value->lastbuyprice; break;
           case "mean": $pro_price = (float) $value->meanbuyprice; break;
           case "last_discount": $pro_price = (float) $value->lastbuyprice_withDiscount; break;
           case "mean_discount": $pro_price = (float) $value->meanbuyprice_withDiscount; break;
       }
       $total_sellcost += ($value->productno * $pro_price);
   }
   ```

5. **Expense and Salary Tracking**:
   ```php
   $totalExpenses = getTotalExpensesByDate($startDate, $endDate);
   $totalsalary = gettotalsalary($startDate, $endDate);
   ```

6. **Final Profit Calculation**:
   ```php
   $total_profit = $totalSellCost - $totalBuyCost - $restaurantRawDestruction;
   $profitFinal = $total_profit - ($totalExpenses + $totalsalary);
   ```

---

### 2. **getTotalSellbillByDate()** - Sales Revenue Calculation
**Location**: Line 500  
**Purpose**: Calculate total sales revenue for specified period

**Implementation**:
php

function getTotalSellbillByDate($startDate, $endDate) {

global $mySellbillEx;

$totalSellbill = $mySellbillEx->queryWithDateAndConditionsExt($startDate, $endDate);

return $totalSellbill;

}

---

### 3. **getTotalQuantityBuyPriceByDate()** - COGS Calculation
**Location**: Line 561  
**Purpose**: Calculate cost of goods sold using selected pricing method

**Process Flow**:
1. **Query Sales Data**:
   ```php
   $sellbilldetailData = $mySellbilldetailEx->queryWithDateAndConditionsExt($startDate, $endDate);
   ```

2. **Price Method Selection**:
   ```php
   switch ($buyPriceType) {
       case "first": $buyPrice = (float) $sellbilldetail->buyprice; break;
       case "last": $buyPrice = (float) $sellbilldetail->lastbuyprice; break;
       case "mean": $buyPrice = (float) $sellbilldetail->meanbuyprice; break;
       case "last_discount": $buyPrice = (float) $sellbilldetail->lastbuyprice_withDiscount; break;
       case "mean_discount": $buyPrice = (float) $sellbilldetail->meanbuyprice_withDiscount; break;
       case "tax": $buyPrice = (float) $sellbilldetail->lastbuyprice_withTax; break;
       case "mean_tax": $buyPrice = (float) $sellbilldetail->meanbuyprice_withTax; break;
   }
   ```

3. **Unit Conversion and Calculation**:
   ```php
   $quantity = $sellbilldetail->sellbilldetailquantity;
   $productnumber = $myProductunitEx->getProductNumber($productunitId);
   if ($productnumber == 0) $productnumber = 1;
   
   $productBuyPrice = $buyPrice * $productnumber;
   $totalQuantityBuyPrice += ($quantity * $productBuyPrice);
   ```

---

### 4. **gettotalsalary()** - Payroll Cost Calculation
**Location**: Line 870  
**Purpose**: Calculate total salary and employee-related costs

**Process Flow**:
1. **Base Salary Calculation**:
   ```php
   $allsalary = $SalaryreportEX->queryAllbydata($startDate, $endDate);
   $sumsalary = 0;
   foreach ($allsalary as $myallsalary) {
       $sumsalary += $myallsalary->salaryreportnet;
   }
   ```

2. **Employee Personnel Transactions**:
   ```php
   $allpersonaldata = $EmployeepersonnelEX->queryAllbydateforreport($startDate, $endDate);
   foreach ($allpersonaldata as $myallpersonaldata) {
       if ($myallpersonaldata->employeepersonneltype == 4) {
           $Borrow += $myallpersonaldata->employeepersonnelvalue;
       } elseif ($myallpersonaldata->employeepersonneltype == 3) {
           $Withdrawal += $myallpersonaldata->employeepersonnelvalue;
       } elseif ($myallpersonaldata->employeepersonneltype == 6) {
           $Repayment += $myallpersonaldata->employeepersonnelvalue;
       }
   }
   ```

3. **Final Calculation**:
   ```php
   $allfinal = ($sumsalary + $Withdrawal + $Borrow) - $Repayment;
   return $allfinal;
   ```

---

### 5. **getRestaurantRawDestruction()** - Material Waste Tracking
**Location**: Line 960  
**Purpose**: Calculate cost of destroyed/wasted raw materials

**Implementation**:
php

function getRestaurantRawDestruction($startDate, $endDate, $buyPriceType) {

global $restaurantRawDestructionEX;

$restaurantRawDestruction = $restaurantRawDestructionEX->queryBySysdateBetween($startDate, $endDate);

$rawDestructionCost = 0;

foreach ($restaurantRawDestruction as $value) {

switch ($buyPriceType) {

case 'first': $rawDestructionCost += $value->productBuyPrice * $value->quantity; break;

case 'last': $rawDestructionCost += $value->lastbuyprice * $value->quantity; break;

case "last_discount": $rawDestructionCost += $value->lastbuyprice_withDiscount * $value->quantity; break;

case "mean_discount": $rawDestructionCost += $value->meanbuyprice_withDiscount * $value->quantity; break;

default: $rawDestructionCost += $value->meanbuyprice * $value->quantity; break;

}

}

return $rawDestructionCost;

}

---

## ๐Ÿ”„ Workflows

### Workflow 1: Complete Income Statement Generation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ START: Income Statement Request โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 1. Date Range Processing โ”‚

โ”‚ - Parse from/to dates โ”‚

โ”‚ - Apply timezone adjustments โ”‚

โ”‚ - Set default to current date if empty โ”‚

โ”‚ - Add time bounds (00:00:00 to 23:59:59) โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 2. Revenue Calculation โ”‚

โ”‚ A. Sales Revenue: โ”‚

โ”‚ โ”œโ”€ Regular sales (sellbill) โ”‚

โ”‚ โ”œโ”€ Combined sell/return transactions โ”‚

โ”‚ โ””โ”€ Service bills (optical/restaurant) โ”‚

โ”‚ B. Sales Returns: โ”‚

โ”‚ โ”œโ”€ Return bills (returnsellbill) โ”‚

โ”‚ โ”œโ”€ Return portions of combined bills โ”‚

โ”‚ โ””โ”€ Service return bills โ”‚

โ”‚ C. Net Sales = Sales Revenue - Sales Returns โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 3. Cost of Goods Sold Calculation โ”‚

โ”‚ A. Choose Pricing Method: โ”‚

โ”‚ โ”œโ”€ First purchase price โ”‚

โ”‚ โ”œโ”€ Last purchase price โ”‚

โ”‚ โ”œโ”€ Mean (average) purchase price โ”‚

โ”‚ โ”œโ”€ Prices with discount applied โ”‚

โ”‚ โ””โ”€ Prices with tax included โ”‚

โ”‚ B. Calculate for Each Item Sold: โ”‚

โ”‚ โ”œโ”€ Get quantity sold โ”‚

โ”‚ โ”œโ”€ Get product unit conversion โ”‚

โ”‚ โ”œโ”€ Apply selected pricing method โ”‚

โ”‚ โ””โ”€ Sum: quantity ร— unit conversion ร— price โ”‚

โ”‚ C. Include Service Item Costs โ”‚

โ”‚ D. Subtract Return Item Costs โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 4. Operating Expenses โ”‚

โ”‚ A. Direct Expenses: โ”‚

โ”‚ โ”œโ”€ Query expenses table by date range โ”‚

โ”‚ โ”œโ”€ Filter by expense type โ”‚

โ”‚ โ””โ”€ Sum all expense values โ”‚

โ”‚ B. Payroll Costs: โ”‚

โ”‚ โ”œโ”€ Base salaries from salary reports โ”‚

โ”‚ โ”œโ”€ Employee withdrawals โ”‚

โ”‚ โ”œโ”€ Employee loans/advances โ”‚

โ”‚ โ””โ”€ Loan repayments (subtract) โ”‚

โ”‚ C. Restaurant-Specific: โ”‚

โ”‚ โ””โ”€ Raw material destruction/waste costs โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 5. Profit Calculations โ”‚

โ”‚ A. Gross Profit: โ”‚

โ”‚ โ””โ”€ Net Sales - Cost of Goods Sold - Material Waste โ”‚

โ”‚ B. Net Profit: โ”‚

โ”‚ โ””โ”€ Gross Profit - Operating Expenses - Payroll โ”‚

โ”‚ C. Assign All Values to Template โ”‚

โ”‚ D. Generate Report Display โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

## ๐ŸŒ URL Routes & Actions

| URL Parameter | Function Called | Description |
|---------------|----------------|-------------|
| `do=show` or `do=` (empty) | Default action | Generate income statement report |
| `do=sucess` | Success page | Display success message |
| `do=error` | Error page | Display error message |

### Required Parameters
**Generate Report** (`do=show`):
- `from` - Start date (YYYY-MM-DD)
- `to` - End date (YYYY-MM-DD)
- `buyPriceType` - Pricing method (first/last/mean/last_discount/mean_discount/tax/mean_tax)

### URL Examples

Basic report for current month

incomeStatmentForPeriod.php?from=2024-01-01&to=2024-01-31

With specific pricing method

incomeStatmentForPeriod.php?from=2024-01-01&to=2024-01-31&buyPriceType=mean_discount

---

## ๐Ÿงฎ Calculation Methods

### Buy Price Method Selection
php

switch ($buyPriceType) {

case "first":

$message .= " ูˆ ุงู„ุญุณุงุจ ุจู€ ุณุนุฑ ุดุฑุงุก";

$buyPrice = (float) $sellbilldetail->buyprice;

break;

case "last":

$message .= " ูˆ ุงู„ุญุณุงุจ ุจู€ ุงุฎุฑ ุณุนุฑ ุดุฑุงุก";

$buyPrice = (float) $sellbilldetail->lastbuyprice;

break;

case "mean":

$message .= " ูˆ ุงู„ุญุณุงุจ ุจู€ ู…ุชูˆุณุท ุณุนุฑ ุงู„ุดุฑุงุก";

$buyPrice = (float) $sellbilldetail->meanbuyprice;

break;

case "last_discount":

$message .= " ูˆ ุงู„ุญุณุงุจ ุจู€ ุงุฎุฑ ุณุนุฑ ุดุฑุงุก ุจุนุฏ ุงู„ุฎุตู…";

$buyPrice = (float) $sellbilldetail->lastbuyprice_withDiscount;

break;

case "mean_discount":

$message .= " ูˆ ุงู„ุญุณุงุจ ุจู€ ู…ุชูˆุณุท ุณุนุฑ ุงู„ุดุฑุงุก ุจุนุฏ ุงู„ุฎุตู…";

$buyPrice = (float) $sellbilldetail->meanbuyprice_withDiscount;

break;

case "tax":

$message .= " ูˆ ุงู„ุญุณุงุจ ุจู€ ุงุฎุฑ ุณุนุฑ ุดุฑุงุก ุดุงู…ู„ ุงู„ู‚ูŠู…ุฉ ุงู„ู…ุถุงูุฉ";

$buyPrice = (float) $sellbilldetail->lastbuyprice_withTax;

break;

case "mean_tax":

$message .= " ูˆ ุงู„ุญุณุงุจ ุจู€ ู…ุชูˆุณุท ุณุนุฑ ุงู„ุดุฑุงุก ุดุงู…ู„ ุงู„ู‚ูŠู…ุฉ ุงู„ู…ุถุงูุฉ";

$buyPrice = (float) $sellbilldetail->meanbuyprice_withTax;

break;

}

### Unit Conversion Handling
php

$quantity = $sellbilldetail->sellbilldetailquantity;

$productunitId = $sellbilldetail->productunitid;

$productnumber = $myProductunitEx->getProductNumber($productunitId);

if ($productnumber == 0) {

$productnumber = 1; // Prevent division by zero

}

$productBuyPrice = $buyPrice * $productnumber;

$totalQuantityBuyPrice += ($quantity * $productBuyPrice);

### Salary Cost Calculation
php

// Base salaries

$sumsalary = 0;

foreach ($allsalary as $myallsalary) {

$sumsalary += $myallsalary->salaryreportnet;

}

// Employee transactions

$Withdrawal = 0; $Borrow = 0; $Repayment = 0;

foreach ($allpersonaldata as $myallpersonaldata) {

switch ($myallpersonaldata->employeepersonneltype) {

case 4: $Borrow += $myallpersonaldata->employeepersonnelvalue; break;

case 3: $Withdrawal += $myallpersonaldata->employeepersonnelvalue; break;

case 6: $Repayment += $myallpersonaldata->employeepersonnelvalue; break;

}

}

$totalSalaryCost = ($sumsalary + $Withdrawal + $Borrow) - $Repayment;

---

## ๐Ÿ”’ Security & Permissions

### Authentication Check
php

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

### Input Validation
- Date format validation for start/end dates
- Pricing method validation against allowed values
- SQL injection prevention via DAO layer
- Numeric validation for calculations

### Data Access Control
- All queries filtered by date range
- Condition-based filtering (conditions = 0) for active records
- User session validation for access rights

---

## ๐Ÿ“Š Performance Considerations

### Database Optimization Tips
1. **Critical Indexes Required**:
   ```sql
   CREATE INDEX idx_sellbill_date_conditions ON sellbill(sellbilldate, conditions);
   CREATE INDEX idx_sellbilldetail_date ON sellbilldetail(sellbilldate);
   CREATE INDEX idx_returnsellbill_date ON returnsellbill(returnsellbillsysdate, conditions);
   CREATE INDEX idx_expenses_date ON expenses(expensesdate, conditions);
   CREATE INDEX idx_salaryreport_date ON salaryreport(salaryreportdate);
   ```

2. **Query Optimization**:
   - Use of proper date range queries
   - Efficient JOIN operations in extended DAOs
   - Minimal data retrieval with specific column selection

3. **Memory Management**:
   - Large datasets may consume significant memory
   - Consider pagination for very long date ranges
   - Efficient array processing for calculations

### Known Performance Bottlenecks
php

// This section can be slow for large datasets

foreach ($sellbilldetailData as $sellbilldetail) {

$productnumber = $myProductunitEx->getProductNumber($productunitId);

// Consider caching product unit numbers

}

**Solution**:
php

// Cache product unit numbers

static $unitCache = array();

if (!isset($unitCache[$productunitId])) {

$unitCache[$productunitId] = $myProductunitEx->getProductNumber($productunitId);

}

$productnumber = $unitCache[$productunitId];

---

## ๐Ÿ› Common Issues & Troubleshooting

### 1. **Incorrect Profit Calculations**
**Issue**: Profit figures don't match expected values  
**Cause**: Wrong pricing method or missing data

**Debug**:
php

// Add debugging output

echo "Sales Total: " . $totalSellPrice . "
";

echo "Returns Total: " . $totalReturnSellPrice . "
";

echo "COGS Total: " . $totalQuantityBuyPrice . "
";

echo "Expenses Total: " . $totalExpenses . "
";

echo "Salary Total: " . $totalsalary . "
";

### 2. **Date Range Issues**
**Issue**: No data returned for valid date range  
**Cause**: Timezone conversion or date format problems

**Debug**:
php

echo "Start Date: " . $startDate . "
";

echo "End Date: " . $endDate . "
";

echo "Reports Plus Hours: " . $Programsetting->reportsPlusHours . "
";

### 3. **Missing Revenue Data**
**Issue**: Sales data not appearing in report  
**Cause**: Condition filtering or date field mismatches

**Debug**:
sql

-- Check data availability

SELECT COUNT(*) FROM sellbill

WHERE sellbilldate BETWEEN '2024-01-01' AND '2024-01-31'

AND conditions = 0;

### 4. **Memory Exhaustion**
**Issue**: Script timeout or memory errors for large date ranges  
**Cause**: Processing too much data at once

**Solution**:
php

// Add memory monitoring

echo "Memory usage: " . memory_get_usage(true) / 1024 / 1024 . " MB
";

ini_set('memory_limit', '512M');

---

## ๐Ÿงช Testing Scenarios

### Test Case 1: Basic Income Statement

1. Set date range to known transaction period

2. Select "mean" pricing method

3. Verify all revenue sections populate

4. Check COGS calculations against manual calculation

5. Confirm final profit matches expected result

### Test Case 2: Different Pricing Methods

1. Run same date range with different pricing methods

2. Verify COGS changes appropriately

3. Compare first vs last vs mean price impacts

4. Test discount and tax inclusive methods

### Test Case 3: Edge Cases

1. Test empty date range (no transactions)

2. Test single day with mixed transaction types

3. Test period spanning year boundary

4. Verify timezone adjustment accuracy

### Debug Mode Enable
php

// Add at top of controller for debugging

error_reporting(E_ALL);

ini_set('display_errors', 1);

// Add calculation debugging

function debug_calculation($label, $value) {

echo "$label: " . number_format($value, 2) . "
";

}

```

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur