AllReportsOnOne Documentation

All Reports On One Controller Documentation

File: /controllers/allReportsOnOne.php

Purpose: Unified reporting interface that consolidates sales and purchase reports in a single controller

Last Updated: December 20, 2024

Total Functions: 14

Lines of Code: ~2,400+ (Large multi-purpose controller)

---

๐Ÿ“‹ Overview

The All Reports On One Controller is a comprehensive reporting module that serves as a unified interface for multiple types of business reports. It consolidates functionality from several specialized report controllers into a single interface, providing:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Sales Tables (Direct Operations)

Table NamePurposeKey Columns
**sellbill**Sales billssellbillid, sellbillclientid, sellbillaftertotalbill, sellQuantity, sellbilldate, conditions, sellbillstoreid
**sellbilldetail**Sales bill line itemssellbilldetailid, sellbillid, sellbilldetailproductid, sellbilldetailquantity, discountvalue, proSellTrackingSerial
**sellbillandrutern**Combined sales/return billssellbillid, sellbillclientid, sellbillaftertotalbill, sellQuantity, returnsellQuantity, sellbilldate
**sellandruternbilldetail**Combined bill detailssellandruternbilldetailid, sellbillid, selltype, sellbilldetailquantity, discountvalue
**returnsellbill**Sales return billsreturnsellbillid, returnsellbillclientid, returnsellbillaftertotalbill, returnsellQuantity, returnsellbilldate
**returnsellbilldetail**Return bill line itemsreturnsellbilldetailid, returnsellbillid, returnsellbilldetailquantity, proSellTrackingSerial
### Purchase Tables (Referenced)

Table NamePurposeKey Columns
**buybill**Purchase billsbuybillid, buybillsupplierid, buybillaftertotalbill, buyQuantity, buybilldate, conditions
**buybilldetail**Purchase bill line itemsbuybilldetailid, buybillid, buybilldetailproductid, buybilldetailquantity
**returnbuybill**Purchase return billsreturnbuybillid, returnbuybillsupplierid, returnbuybillaftertotalbill, returnbuyQuantity, returnbuybilldate
**buyandruternbill**Combined purchase/return billsbuybillid, buybillsupplierid, buybillaftertotalbill, buyQuantity, returnbuyQuantity
### Reference Tables

Table NamePurposeKey Columns
**client**Customer informationclientid, clientname
**supplier**Supplier informationsupplierid, suppliername
**store**Store/warehouse informationstoreid, storename
**user**System usersuserid, employeename, username
**billname**Bill format definitionsbillnameid, billname, billtype
**billsettings**Bill formatting settingsbillsettingid, billnameid
**programsettings**System configurationprogramsettingsid, reportsPlusHours
**youtubelink**Tutorial linksyoutubelinkid, title, url
---

๐Ÿ”‘ Key Functions

1. show() / Default Action - Unified Report Interface

Location: Line 381-525

Purpose: Main interface that handles multiple report types based on checkbox selections

Function Signature:

// Multiple report types can be selected via checkboxes
$rep1 = $_REQUEST['rep1']; // Sales reports
$rep2 = $_REQUEST['rep2']; // Sales return reports  
$rep3 = $_REQUEST['rep3']; // Combined sales/return reports
$rep4 = $_REQUEST['rep4']; // Purchase reports
$rep5 = $_REQUEST['rep5']; // Purchase return reports
$rep6 = $_REQUEST['rep6']; // Combined purchase/return reports

Process Flow:

1. Load dropdown data (stores, suppliers, clients)

2. Parse report type selections (rep1-rep6 checkboxes)

3. Route to appropriate report functions based on selections

4. Handle client-specific vs supplier-specific vs general reports

5. Display via appropriate templates

Report Type Routing:

if ($clientId > 0) {
    if ($rep1 == '1') showAll(); // Sales reports
    if ($rep2 == '1') returnshowAll(); // Return reports
    if ($rep3 == '1') { showAll(); returnshowAll(); } // Combined
}

if ($supplierId > 0) {
    if ($rep4 == '1') showBuyBill(); // Purchase reports
    if ($rep5 == '1') showBuyReturn(); // Purchase returns
    if ($rep6 == '1') showBuyReturnAndBuy(); // Combined purchase
}

---

2. showAll() - Sales Bill Report Generator

Location: Line 672-1022

Purpose: Comprehensive sales bill reporting with advanced filtering and calculations

Function Signature:

function showAll()

Process Flow:

1. Parse Search Parameters
   โ”œโ”€ Date range (from/to with timezone support)
   โ”œโ”€ Client ID filter
   โ”œโ”€ Store ID filter  
   โ”œโ”€ Bill serial/ID filters
   โ”œโ”€ Bill type filter (obgyBillType)
   โ””โ”€ Product serial tracking filter

2. Build Dynamic Queries
   โ”œโ”€ Construct WHERE clauses for multiple tables
   โ”œโ”€ Handle serial number filtering via detail tables
   โ”œโ”€ Apply date range with timezone adjustments
   โ””โ”€ Default to today's date if no criteria

3. Execute Optimized Queries
   โ”œโ”€ Query sellbill table with filters
   โ”œโ”€ Query sellbillandrutern table with filters
   โ”œโ”€ Batch load related data (users, clients, discounts)
   โ””โ”€ Use array indexing for performance

4. Process Sales Data
   โ”œโ”€ Calculate totals and quantities
   โ”œโ”€ Apply discount calculations (fixed vs percentage)
   โ”œโ”€ Calculate tax amounts
   โ”œโ”€ Convert arrays to objects for template use
   โ””โ”€ Merge regular and combined bill data

5. Generate Report Output
   โ”œโ”€ Assign processed data to templates
   โ”œโ”€ Calculate summary statistics
   โ””โ”€ Display via appropriate template

Performance Optimization:

// Batch load related data to avoid N+1 queries
$userDataArr = R::getAll('select userid,employeename from user where userid in(' . implode(',', $userIDs) . ') ');
$clientDataArr = R::getAll('select clientid,clientname from client where clientid in(' . implode(',', $clientIDs) . ') ');
$discountDataArr = R::getAll('SELECT sellbillid,sum(discountvalue) as sumdiscountvalue FROM sellbilldetail where sellbillid in(' . implode(',', $billIDs) . ') group by sellbillid');

// Index arrays for fast lookups
$userDataArr = customArrayIndexOne($userDataArr, 'userid');
$clientDataArr = customArrayIndexOne($clientDataArr, 'clientid');
$discountDataArr = customArrayIndexOne($discountDataArr, 'sellbillid');

---

3. returnshowAll() - Sales Return Report Generator

Location: Line 1024-1393

Purpose: Generate comprehensive sales return reports with similar filtering capabilities

Function Signature:

function returnshowAll()

Process Flow:

1. Parse same search parameters as showAll()

2. Build queries for returnsellbill and sellbillandrutern tables

3. Focus on return portions of combined bills

4. Calculate return totals, quantities, and discounts

5. Generate return-specific report output

Key Differences from Sales:

---

4. showBuyBill() - Purchase Bill Report Generator

Location: Line 2183+

Purpose: Generate purchase bill reports with supplier and date filtering

Features:

---

5. showBuyReturn() - Purchase Return Report Generator

Location: Line 1559-1867

Purpose: Generate purchase return reports

Features:

---

6. showDetail() - Sales Bill Detail View

Location: Line 629-645

Purpose: Load detailed information for a specific sales bill

Function Signature:

function showDetail($sellbillid)

Returns:

return array(
    $sellbillData,        // Bill header information
    $sellbilldetailData,  // Line item details
    $quantity             // Total quantity
);

---

7. showsellAndReturnDetail() - Combined Bill Detail View

Location: Line 647-670

Purpose: Load detailed information for combined sales/return bills

Function Signature:

function showsellAndReturnDetail($sellbillid)

Returns:

return array(
    $sellbillandruternData, // Combined bill header
    $sellbilldetailData,    // Sales line items
    $ruternbilldetailData,  // Return line items  
    $sellQuantity,          // Sales quantity
    $returnQuantity         // Return quantity
);

---

8. Filter Functions - Search and Filter Utilities

Locations: Lines 1394-1558

Purpose: Specialized filtering functions for different search criteria

Available Functions:

---

9. Data Loading Functions - Reference Data Loaders

Locations: Lines 591-627

Purpose: Load reference data for dropdowns and lookups

Available Functions:

---

๐Ÿ”„ Workflows

Workflow 1: Multi-Report Generation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Report Selection Interface
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Display Report Selection Form
โ”œโ”€ Show checkboxes for 6 report types
โ”œโ”€ Load client and supplier dropdowns
โ”œโ”€ Load store and date range selectors
โ”‚ โ””โ”€ Display unified search interface โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Parse User Selections
โ”œโ”€ Check which report types selected (rep1-rep6)
โ”œโ”€ Parse search criteria (client, supplier, dates)
โ”œโ”€ Validate input parameters
โ”‚ โ””โ”€ Determine report execution order โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Execute Selected Reports
FOR EACH selected report type:
โ”œโ”€ Call appropriate report function
โ”œโ”€ Apply common search filters
โ”œโ”€ Generate report data
โ”œโ”€ Calculate totals and summaries
โ”‚ โ””โ”€ Assign to template variables โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Display Unified Results
โ”œโ”€ Show all selected reports on single page
โ”œโ”€ Display comparative totals
โ”œโ”€ Provide drill-down links to details
โ”‚ โ””โ”€ Format for printing/export โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

Workflow 2: Advanced Sales Report Generation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Sales Report Request
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Parse Complex Filter Criteria
โ”œโ”€ Date range with timezone adjustments
โ”œโ”€ Client/store/bill type filters
โ”œโ”€ Serial number and product tracking
โ”‚ โ””โ”€ Bill status and condition filters โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Build Optimized Database Queries
โ”œโ”€ Construct main table queries
โ”œโ”€ Build detail table serial filters
โ”œโ”€ Prepare batch loading queries
โ”‚ โ””โ”€ Optimize for performance โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Execute Batch Data Loading
โ”œโ”€ Load main bill data
โ”œโ”€ Load related reference data in batches
โ”œโ”€ Calculate discount totals efficiently
โ”‚ โ””โ”€ Index arrays for fast lookups โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Process Business Calculations
FOR EACH bill:
โ”œโ”€ Apply discount calculations (fixed/percentage)
โ”œโ”€ Calculate tax amounts
โ”œโ”€ Sum quantities and totals
โ”œโ”€ Join with reference data
โ”‚ โ””โ”€ Convert to template objects โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Generate Comprehensive Report
โ”œโ”€ Merge regular and combined bill data
โ”œโ”€ Calculate summary statistics
โ”œโ”€ Apply sorting and grouping
โ”‚ โ””โ”€ Display formatted results โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty) or `do=show`Multiple functionsUnified report interface
`do=sellandreturnreport``showAll()` + `returnshowAll()`Combined sales/return report
`do=sellDetail``showDetail()`Individual sales bill detail
`do=sellAndReturnDetail``showsellAndReturnDetail()`Combined bill detail
### Required Parameters by Action

Unified Report Interface (do=show):

Sales Detail (do=sellDetail):

Combined Detail (do=sellAndReturnDetail):

---

๐Ÿงฎ Calculation Methods

Discount Calculation (Fixed vs Percentage)

// Fixed amount discount (type = 1)
if ($sellbilldiscounttype == 1) {
    $totalDiscount = $sellbilldiscount + $detailDiscount;
    $taxValue = $sellbillaftertotalbill - ($sellbilltotalbill - $sellbilldiscount);
}

// Percentage discount (type != 1)
else {
    $discountValue = ($sellbilltotalbill / 100) * $sellbilldiscount;
    $totalDiscount = $discountValue + $detailDiscount;
    $taxValue = $sellbillaftertotalbill - ($sellbilltotalbill - $discountValue);
}

Performance Optimization Techniques

// Batch loading to prevent N+1 queries
$billIDs = array_unique(array_column($sellbillData, 'sellbillid'));
$userDataArr = R::getAll('select userid,employeename from user where userid in(' . implode(',', $userIDs) . ')');

// Array indexing for fast lookups
$userDataArr = customArrayIndexOne($userDataArr, 'userid');

// Join data efficiently
foreach ($sellbillData as &$value) {
    $value['username'] = $userDataArr[$value['userid']]['employeename'];
    $value['clientname'] = $clientDataArr[$value['sellbillclientid']]['clientname'];
}

Serial Number Filtering

if (!empty($proSellTrackingSerial)) {
    // Find bills containing specific product serial
    $detailsData = R::getAll('SELECT distinct sellbillid FROM sellbilldetail where proSellTrackingSerial="' . $proSellTrackingSerial . '"');
    
    // Build IN clause for main query
    $sellbillIds = array_column($detailsData, 'sellbillid');
    $queryStringDetailIds = " and sellbillid in (" . implode(',', $sellbillIds) . ")";
}

---

๐Ÿ”’ Security & Permissions

Input Validation

Data Access Control

Query Security

// Safe parameter binding
$queryString .= ' sellbilldate >= "' . $startDate . '" AND sellbilldate <= "' . $endDate . '" AND';

// Condition filtering for data integrity
$queryString .= ' AND sellbill.conditions=0';

// Array indexing prevents injection
$billIDs = array_unique($billIDs);
$queryString .= ' AND sellbillid IN (' . implode(',', $billIDs) . ')';

---

๐Ÿ“Š Performance Considerations

Database Optimization Tips

1. Critical Indexes Required:

- sellbill(sellbillclientid, sellbilldate, conditions, sellbillstoreid)

- sellbilldetail(sellbillid, proSellTrackingSerial)

- sellbillandrutern(sellbillclientid, sellbilldate, conditions)

- returnsellbill(returnsellbillclientid, returnsellbilldate, conditions)

2. Query Optimization Techniques:

- Batch loading of related data to prevent N+1 queries

- Array indexing for fast data joins

- Efficient use of IN clauses for filtering

- Proper date range filtering with indexes

3. Memory Management:

- Large datasets may require pagination

- Array indexing reduces memory overhead

- Object conversion only when necessary

- Cleanup of temporary variables

Performance Monitoring

-- Monitor query performance
EXPLAIN SELECT * FROM sellbill 
WHERE sellbilldate >= '2024-01-01' AND sellbilldate <= '2024-12-31' 
AND conditions = 0 
AND sellbillclientid = 123;

-- Check index usage
SHOW INDEX FROM sellbill;
SHOW INDEX FROM sellbilldetail;

---

๐Ÿ› Common Issues & Troubleshooting

1. Large Dataset Performance

Issue: Reports timeout or run slowly with large date ranges

Cause: Missing indexes or inefficient queries

Solutions:

-- Add required indexes
CREATE INDEX idx_sellbill_date_client ON sellbill(sellbilldate, sellbillclientid, conditions);
CREATE INDEX idx_detail_serial ON sellbilldetail(proSellTrackingSerial, sellbillid);

-- Limit date ranges or add pagination
WHERE sellbilldate >= '2024-12-01' AND sellbilldate <= '2024-12-31';

2. Memory Exhaustion

Issue: PHP memory limit exceeded with large reports

Cause: Loading too much data into memory at once

Fix:

// Increase memory limit temporarily
ini_set('memory_limit', '512M');

// Or implement pagination
$limit = 1000;
$offset = $page * $limit;
$queryString .= " LIMIT $limit OFFSET $offset";

3. Incorrect Discount Calculations

Issue: Discount totals don't match expected values

Cause: Mixed discount types or missing detail discounts

Debug:

// Debug discount calculation
echo "Bill Discount: " . $sellbilldiscount . "<br>";
echo "Discount Type: " . $sellbilldiscounttype . "<br>";
echo "Detail Discount: " . $detaildiscount . "<br>";
echo "Total Before: " . $sellbilltotalbill . "<br>";
echo "Total After: " . $sellbillaftertotalbill . "<br>";

4. Serial Number Filtering Issues

Issue: Serial number search returns no results

Cause: Case sensitivity or formatting issues

Fix:

-- Use case-insensitive search
WHERE UPPER(proSellTrackingSerial) = UPPER('SERIAL123');

-- Check for leading/trailing spaces
WHERE TRIM(proSellTrackingSerial) = 'SERIAL123';

---

๐Ÿงช Testing Scenarios

Test Case 1: Multi-Report Generation

1. Select multiple report checkboxes (rep1, rep2, rep3)
2. Set date range and client filter
3. Submit form and verify all selected reports generate
4. Check that totals are calculated correctly across reports
5. Verify performance with large datasets

Test Case 2: Serial Number Tracking

1. Create sales bills with specific product serial numbers
2. Search by serial number in advanced filter
3. Verify only bills containing that serial appear
4. Test with partial serial matches
5. Confirm case sensitivity handling

Test Case 3: Performance Testing

1. Generate large dataset (1000+ bills)
2. Run reports with various filter combinations
3. Monitor query execution times
4. Test memory usage with large result sets
5. Verify batch loading optimization works

Debug Mode Enable

// Add at top of functions for debugging
echo "Memory Usage: " . memory_get_usage(true) / 1024 / 1024 . " MB<br>";
echo "Query String: " . $queryString . "<br>";
echo "Bill Count: " . count($sellbillData) . "<br>";

// Debug data processing
foreach ($sellbillData as $bill) {
    echo "Bill: " . $bill->sellbillid . " Total: " . $bill->sellbillaftertotalbill . "<br>";
}

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When report consolidation changes or performance issues arise