๐Ÿ“š ERP Documentation Viewer

Beautiful, colorful documentation for your ERP system

Executors Report Controller Documentation

File: /controllers/executorsreport.php

Purpose: Financial analysis and profitability reporting for sales bill execution assignments with comprehensive cost tracking

Last Updated: December 20, 2024

Total Functions: 4+

Lines of Code: ~164

---

๐Ÿ“‹ Overview

The Executors Report Controller provides advanced financial analysis for sales bill execution assignments, focusing on profitability analysis, cost tracking, and comprehensive financial reporting. It integrates sales bill data with execution costs and client payment tracking to provide detailed insights into the financial performance of executed sales orders.

Key Capabilities

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Core Execution Tables

Table NamePurposeKey Columns
**executors**Execution assignmentsid, executorsclientid, executorssellid, executorsuserids, executorsdate, del
### Financial Analysis Tables

Table NamePurposeKey Columns
**sellbill**Sales bill master datasellbillid, sellbillclientid, sellbillaftertotalbill, sellbilldate, datestarting, conditions
**expenses**Execution-related costsid, sellbillid, expensesValue, expensesdate, conditions
**clientdebtchange**Payment trackingid, billid, clientdebtchangeamount, tablename, debtchangedate
### Reference Tables

Table NamePurposeKey Columns
**client**Customer informationclientid, clientname, conditions
---

๐Ÿ”‘ Key Functions

1. Default Display - Report Interface

Location: Line 8-12

Purpose: Display the main executors financial report interface

Implementation:

if (empty($do)) {  
    $smarty->display("header.html");
    $smarty->display("executorsreport/show.html");
    $smarty->display("footer.html");
}

Features:

---

2. select2client() - Client Search for Filtering

Location: Line 21-37

Purpose: Provide Ajax-powered client search for report filtering

Function Signature:

function select2client()
// POST Parameter: searchTerm - Client name search

Implementation:

$name = $_POST['searchTerm'];

$productsData = R::getAll("SELECT clientid, clientname as name
FROM client 
WHERE conditions = 0 and clientname LIKE '%" . $name . "%'  limit 50");

foreach ($productsData as $pro) {
    $row_array['id'] = $pro['clientid'];
    $row_array['text'] = $pro['name'];
    array_push($return_arr, $row_array);
}

echo json_encode($return_arr);

Features:

---

3. select2sellbill() - Sales Bill Search with Client Context

Location: Line 41-57

Purpose: Search sales bills for specific clients with execution context

Function Signature:

function select2sellbill()
// POST Parameters: searchTerm, clientid

Implementation:

$name = $_POST['searchTerm'];
$clientid = $_POST['clientid'];

$productsData = R::getAll("SELECT sellbillid, CONCAT(sellbillid,'/',datestarting) as texts
FROM sellbill 
WHERE conditions = 0 and CONCAT(sellbillid,'/',datestarting) LIKE '%" . $name . "%' 
and sellbill.sellbillclientid = $clientid limit 50");

foreach ($productsData as $pro) {
    $row_array['id'] = $pro['sellbillid'];
    $row_array['text'] = $pro['texts'];
    array_push($return_arr, $row_array);
}

Features:

---

4. showajax() - Comprehensive Financial Report

Location: Line 61-157

Purpose: Generate detailed financial analysis for execution assignments

Function Signature:

function showajax()
// POST Parameters: fromdate, todate, data1 (client), data2 (sellbill), DataTables parameters

Filter Building:

$searchQuery = " ";
if($data1 != ''){
  $searchQuery .=  " and executors.executorsclientid = ".$data1. " ";
}

if($data2 != ''){
   $searchQuery .= " and executors.executorssellid = ".$data2. " ";
}

if($start_date != '' && $end_date != ''){
   $searchQuery .='and sellbill.datestarting >= "' . $start_date . '" and sellbill.datestarting <= "' . $end_date . '" ';
}

Main Query:

$rResult = R::getAll('SELECT executors.* ,clientname, expensesValue, sellbillaftertotalbill, sellbilldate, datestarting 
    FROM `executors`
    LEFT JOIN sellbill ON executors.executorssellid = sellbill.sellbillid 
    LEFT JOIN client ON executors.executorsclientid = client.clientid   
    LEFT JOIN expenses ON expenses.sellbillid = sellbill.sellbillid 
    WHERE 1 '.$searchQuery.' ');

Financial Calculations for Each Record:

foreach ($rResult as $row) {
    // 1. Get client payments for this bill
    $clientdebtchangeamount = R::getCell('SELECT sum(clientdebtchangeamount) as clientdebtchangeamount 
        FROM clientdebtchange 
        WHERE billid = '. $row["executorssellid"]. ' 
        and tablename = "clientPayedDeptSellBillsController.php"');
    
    // 2. Financial KPI calculations
    $sub_array[] = $row["id"];                    // Execution ID
    $sub_array[] = $row["clientname"];            // Client Name
    $sub_array[] = $row["executorssellid"];       // Sales Bill ID
    $sub_array[] = $row["sellbilldate"];          // Bill Date
    $sub_array[] = $row["sellbillaftertotalbill"]; // Sales Amount
    $sub_array[] = $clientdebtchangeamount;       // Client Payments
    $sub_array[] = $row["expensesValue"];         // Execution Costs
    $sub_array[] = $row["sellbillaftertotalbill"] - $clientdebtchangeamount; // Outstanding Amount
    $sub_array[] = $row["sellbillaftertotalbill"] - $row["expensesValue"];   // Gross Profit
    $sub_array[] = $clientdebtchangeamount - $row["expensesValue"];          // Net Profit
}

---

๐Ÿงฎ Financial Calculation Methods

1. Sales Amount

$salesAmount = $row["sellbillaftertotalbill"];

2. Client Payments Collection

$clientdebtchangeamount = R::getCell('SELECT sum(clientdebtchangeamount) as clientdebtchangeamount 
    FROM clientdebtchange 
    WHERE billid = '. $row["executorssellid"]. ' 
    and tablename = "clientPayedDeptSellBillsController.php"');

3. Execution Costs

$executionCosts = $row["expensesValue"];

4. Outstanding Amount (Collection Gap)

$outstandingAmount = $salesAmount - $clientPayments;

5. Gross Profit (Sales Margin)

$grossProfit = $salesAmount - $executionCosts;

6. Net Profit (Realized Profit)

$netProfit = $clientPayments - $executionCosts;

---

๐Ÿ”„ Workflows

Workflow 1: Financial Analysis Report Generation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Request Financial Analysis
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Apply Filters
- Set date range for analysis period
- Select specific client (optional)
- Choose specific sales bill (optional)
- Define analysis scope
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Query Execution Data
- Load executors with sales bill information
- JOIN with client data for context
- Include execution cost data
- Apply date and entity filters
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Calculate Financial Metrics
FOR EACH Execution Record:
โ”‚
โ†’ Get Sales Amount from bill
โ†’ Calculate Client Payments Received
โ†’ Determine Execution Costs
โ†’ Calculate Outstanding Amount
โ†’ Compute Gross Profit
โ”‚ โ””โ”€โ†’ Compute Net Profit โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Generate Report Display
- Format data for DataTables display
- Include all financial KPIs
- Provide sorting and filtering capabilities
- Enable export functionality
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

Workflow 2: Profitability Analysis Process

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Analyze Execution Profitability
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Load Sales Bill Base Data
- Sales Amount: sellbill.sellbillaftertotalbill
- Bill Date: sellbill.datestarting
- Client Information: client.clientname
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Calculate Client Payment Collection
- Query clientdebtchange for this bill
- Filter by tablename = "clientPayedDeptSellBills..."
- SUM all payment amounts
- Determine collection rate
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Determine Execution Costs
- Load expenses.expensesValue for bill
- Include all execution-related costs
- Calculate cost percentage of sales
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Compute Profitability Metrics
- Outstanding Amount = Sales - Payments
- Gross Profit = Sales - Costs
- Net Profit = Payments - Costs
- Collection Rate = Payments / Sales * 100
- Cost Rate = Costs / Sales * 100
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Provide Analysis Insights
- Identify most profitable executions
- Highlight collection issues
- Track cost efficiency
- Support business decision making
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescriptionAjax
(empty)Default displayShow financial report interfaceNo
`do=select2client``select2client()`Ajax client search for filteringYes
`do=select2sellbill``select2sellbill()`Ajax sales bill searchYes
`do=showajax``showajax()`DataTables financial data providerYes
### Required Parameters by Action

Client Search (do=select2client):

Sales Bill Search (do=select2sellbill):

Financial Report (do=showajax):

---

๐Ÿ“Š Financial KPIs and Metrics

Core Financial Metrics

1. Sales Amount: Base sales value after discounts

2. Client Payments: Actual cash collected from client

3. Execution Costs: Direct costs of order fulfillment

4. Outstanding Amount: Uncollected receivables

5. Gross Profit: Sales margin before collection issues

6. Net Profit: Realized profit after collections

Derived Analytics

// Collection Efficiency Rate
$collectionRate = ($clientPayments / $salesAmount) * 100;

// Cost Efficiency Rate  
$costRate = ($executionCosts / $salesAmount) * 100;

// Profit Margin (Gross)
$grossMargin = (($salesAmount - $executionCosts) / $salesAmount) * 100;

// Profit Margin (Net) 
$netMargin = (($clientPayments - $executionCosts) / $clientPayments) * 100;

// Return on Execution Investment
$roi = (($clientPayments - $executionCosts) / $executionCosts) * 100;

Business Intelligence Insights

---

๐Ÿ”’ Security & Permissions

Session Management

include("../public/impOpreation.php");

Input Validation

$searchTerm = $_POST['searchTerm'];
$clientid = $_POST['clientid'];
$start_date = $_POST['fromdate'];
$end_date = $_POST['todate'];

Security Features:

---

๐Ÿ› Common Issues & Troubleshooting

1. Missing Payment Data

Issue: NULL values in client payment calculations

Cause: No payment records in clientdebtchange table

Debug Query:

-- Check payment records for bills
SELECT cdc.billid, SUM(cdc.clientdebtchangeamount) as payments
FROM clientdebtchange cdc 
WHERE cdc.tablename = 'clientPayedDeptSellBillsController.php'
  AND cdc.billid IN (SELECT executorssellid FROM executors)
GROUP BY cdc.billid;

Fix: Handle NULL payments in calculation:

$clientdebtchangeamount = R::getCell('...') ?? 0;

2. Missing Expense Data

Issue: NULL values in execution costs

Cause: No expense records linked to sales bills

Solution: Default to zero costs:

$expensesValue = $row["expensesValue"] ?? 0;

3. Date Range Issues

Issue: No data returned for valid date ranges

Cause: Date format mismatch or timezone issues

Debug: Check date formats in sellbill.datestarting vs filter dates

4. Performance Issues

Issue: Slow loading with large datasets

Solutions:

---

๐Ÿ“ˆ Performance Considerations

Database Optimization

1. Critical Indexes:

   -- Execution queries
   CREATE INDEX idx_executors_sellbill_del ON executors(executorssellid, del);
   CREATE INDEX idx_executors_client_del ON executors(executorsclientid, del);
   
   -- Financial data queries
   CREATE INDEX idx_sellbill_client_date ON sellbill(sellbillclientid, datestarting, conditions);
   CREATE INDEX idx_expenses_sellbill ON expenses(sellbillid, conditions);
   CREATE INDEX idx_clientdebtchange_bill_table ON clientdebtchange(billid, tablename);
   ```

2. **Query Optimization**:
   - Use appropriate date ranges to limit result sets
   - Optimize JOIN conditions in main query
   - Consider materialized views for complex aggregations

### Memory Management
- Efficient processing of financial calculations
- Proper variable cleanup in loops
- Optimized JSON response generation

### Report Performance
- Implement caching for frequently accessed data
- Use pagination for large result sets
- Optimize DataTables server-side processing

---

## ๐Ÿงช Testing Scenarios

### Test Case 1: Basic Financial Calculations

1. Create execution with known sales amount, costs, and payments

2. Verify all financial metrics calculated correctly

3. Check outstanding amount = sales - payments

4. Confirm gross profit = sales - costs

5. Validate net profit = payments - costs

### Test Case 2: Edge Cases

1. Test with zero payments (all outstanding)

2. Test with costs exceeding sales (negative gross profit)

3. Test with zero costs (100% gross profit)

4. Test with payments exceeding sales (overpayment)

5. Verify NULL handling in all calculations

### Test Case 3: Date Range Filtering

1. Generate report for specific month

2. Test with custom date ranges

3. Verify date filtering accuracy

4. Check edge cases (single day, year ranges)

5. Test with no data in range

### Test Case 4: Search and Filtering

1. Test client search functionality

2. Test sales bill search with client context

3. Verify filtering by client affects results appropriately

4. Test DataTables search across columns

5. Validate sorting functionality

```

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur

โ†‘