Buyreport Documentation

Buy Report Controller Documentation

File: /controllers/buyreport.php

Purpose: Comprehensive purchase transaction reporting with supplier analysis and financial tracking

Last Updated: December 20, 2024

Total Functions: 8

Lines of Code: ~1,000+

---

๐Ÿ“‹ Overview

The Buy Report Controller is a comprehensive reporting module that provides detailed analysis of purchase transactions, supplier relationships, and financial tracking. It handles:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Purchase Tables

Table NamePurposeKey Columns
**buybill**Purchase billsbuybillid, buybillsupplierid, buybilldate, buybillfinalbill
**buybilldetail**Purchase bill itemsbuybilldetailid, buybillid, buybilldetailproductid, buybilldetailquantity
**returnbuybill**Purchase returnsreturnbuybillid, returnbuybillsupplierid, returnbuybilldate
**returnbuybilldetail**Return bill itemsreturnbuybilldetailid, returnbuybillid, returnbuybilldetailproductid
**buyandruternbill**Combined billsbuybillid, buybillsupplierid, buybilldate
**buyandruternbilldetail**Combined bill itemsbuybilldetailid, buybillid, buybilldetailproductid
### Service Bill Tables

Table NamePurposeKey Columns
**billsbuy**Service purchase billsbillsbuyid, clientid, billdate, finalnetbillvalue
**billsproductsbuy**Service bill itemsbillsproductsbuyid, billsbuyid, productid, quantity
### Supporting Tables

Table NamePurposeKey Columns
**supplier**Supplier master datasupplierid, suppliername, suppliercurrentDebt
**supplierdebtchange**Supplier debt historysupplierdebtchangeid, supplierid, supplierdebtchangeamount
**product**Product catalogproductid, productname, productCatId
**productcat**Product categoriesproductCatId, productCatName, parentExt
**productunit**Product unitsproductunitid, productid, unitid
**storedetail**Inventory levelsstoredetailid, productid, storeid, productquantity
---

๐Ÿ”‘ Key Functions

1. Default Action (empty $do) - Main Report Display

Location: Line 188

Purpose: Generate comprehensive purchase report with filtering capabilities

Search Parameters:

$datefrom = filter_input(INPUT_POST, 'datefrom');
$dateto = filter_input(INPUT_POST, 'dateto');
$supplierid = filter_input(INPUT_POST, 'supplierid');
$clientid = filter_input(INPUT_POST, 'clientid');
$order = filter_input(INPUT_POST, 'order');

Query Building Logic:

$queryString = " where 1 ";   // Service bills
$queryString1 = " where 1 ";  // Purchase bills  
$queryString1R = " where 1 "; // Return bills
$queryString1SR = " where 1 "; // Combined bills

// Supplier filtering
if (isset($supplierid) && !empty($supplierid) && $supplierid != -1) {
    $queryString .= 'and billsbuy.clientid = ' . $supplierid . ' ';
    $queryString1 .= 'and buybill.buybillsupplierid = ' . $supplierid . ' ';
    $queryString1R .= 'and returnbuybill.returnbuybillsupplierid = ' . $supplierid . ' ';
    $queryString1SR .= 'and buyandruternbill.buybillsupplierid = ' . $supplierid . ' ';
}

Date Processing with Timezone Support:

if (isset($Programsetting->reportsPlusHours) && !empty($Programsetting->reportsPlusHours)) {
    $reportsPlusHours = $Programsetting->reportsPlusHours + 24;
    $datefrom = date('Y-m-d H:i:s', strtotime('+' . $Programsetting->reportsPlusHours . ' hour +0 minutes', strtotime($datefrom)));
} else {
    $datefrom = $datefrom . " 00:00:00";
}

---

2. getDataNew() - Enhanced Data Aggregation

Purpose: Aggregate data from multiple bill types with advanced processing

Function Signature:

function getDataNew($queryString, $queryString1, $queryString1R, $queryString1SR, $order)

Multi-Source Data Collection:

1. Service bills (billsbuy)

2. Standard purchase bills (buybill)

3. Purchase return bills (returnbuybill)

4. Combined bills (buyandruternbill)

Data Processing Flow:

---

3. getData() - Standard Data Aggregation

Purpose: Basic data aggregation without enhanced features

Differences from getDataNew():

---

4. sortByTotal() - Data Sorting Algorithm

Purpose: Sort aggregated data by various criteria

Function Signature:

function sortByTotal($type, $allDataArrTemp, $productTotalArrTemp)

Sorting Options:

---

5. getAllSubCat() - Category Hierarchy Navigation

Purpose: Recursively traverse product category hierarchy

Function Signature:

function getAllSubCat($catid, $mode)

Mode Options:

Category Processing:

$result = $productCatExt->queryByParentExt($catid);
if (count($result) > 0) {
    foreach ($result as $data) {
        if ($mode == 1) {
            $catsIDS .= "," . $data->productCatId;
            getAllSubCat($data->productCatId, $mode);
        } elseif ($mode == 2) {
            $childData = $productCatExt->queryByParentExt($data->productCatId);
            if (count($childData) == 0) {
                array_push($lastLevelCatIDS, $data->productCatId);
            }
        }
    }
}

---

6. getSupplierDept() - Supplier Debt Calculation

Purpose: Calculate current supplier debt balance

Function Signature:

function getSupplierDept($supplierId)

Debt Calculation Logic:

---

7. getCLientPayedDeptData() - Payment History Analysis

Purpose: Analyze supplier payment history within date range

Function Signature:

function getCLientPayedDeptData($supplierid, $datefrom, $dateto)

Payment Analysis:

---

8. getDataNewWithClient() - Client-Integrated Reporting

Purpose: Enhanced reporting with client relationship data

Function Signature:

function getDataNewWithClient($queryString, $queryString1, $queryString1R, $queryString1SR, $order, $queryStringClient, $queryStringClient1R, $queryStringClient1SR)

Client Integration Features:

---

๐Ÿ”„ Workflows

Workflow 1: Comprehensive Purchase Report Generation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Purchase Report Request
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Initialize Report Parameters
- Load user permissions and settings
- Load suppliers and clients for filters
- Load YouTube tutorial links
- Initialize query builders
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Process Search Filters
- Supplier selection and validation
- Date range with timezone adjustments
- Client filtering (if applicable)
- Sort order specification
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Build Dynamic Query Strings
FOR EACH bill type:
โ”‚
โ†’ Service bills (billsbuy)
โ”‚ โ”‚ โ””โ”€ Apply supplier and date filters โ”‚
โ”‚
โ†’ Purchase bills (buybill)
โ”‚ โ”‚ โ””โ”€ Apply supplier and date filters โ”‚
โ”‚
โ†’ Return bills (returnbuybill)
โ”‚ โ”‚ โ””โ”€ Apply supplier and date filters โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ Combined bills (buyandruternbill) โ”‚
โ”‚ โ””โ”€ Apply supplier and date filters โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Execute Data Collection
- Query service bills and details
- Query purchase bills and details
- Query return bills and details
- Query combined bills and details
- Aggregate data by product and supplier
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Process and Enhance Data
- Calculate quantities and totals
- Apply currency conversions
- Generate financial summaries
- Sort by specified criteria
- Add supplier debt information
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Generate Report Output
- Assign data to template variables
- Include search parameters for display
- Add summary totals and statistics
- Display via reporting template
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default actionMain purchase report with full filtering
### Required/Optional Parameters

Date Filtering:

Entity Filtering:

Category Filtering (via global variables):

---

๐Ÿงฎ Calculation Methods

Date Range Processing with Timezone

// Timezone adjustment calculation
if (isset($Programsetting->reportsPlusHours) && !empty($Programsetting->reportsPlusHours)) {
    $reportsPlusHours = $Programsetting->reportsPlusHours + 24; // +24 for end of day
    $adjustedDate = date('Y-m-d H:i:s', strtotime('+' . $Programsetting->reportsPlusHours . ' hour +0 minutes', strtotime($inputDate)));
}

Multi-Bill Type Aggregation

// Data aggregation across bill types
$totalQuantity = 0;
$totalAmount = 0;

// Service bills
foreach ($serviceBillData as $bill) {
    $totalQuantity += $bill->quantity;
    $totalAmount += $bill->finalnetbillvalue;
}

// Purchase bills  
foreach ($purchaseBillData as $bill) {
    $totalQuantity += $bill->buybilldetailquantity;
    $totalAmount += $bill->buybillfinalbill;
}

// Return bills (subtract)
foreach ($returnBillData as $bill) {
    $totalQuantity -= $bill->returnbuybilldetailquantity;
    $totalAmount -= $bill->returnbuybillfinalbill;
}

Supplier Debt Calculation

function getSupplierDept($supplierId) {
    global $supplierDAO;
    
    if ($supplierId > 0) {
        $supplierData = $supplierDAO->load($supplierId);
        return $supplierData->suppliercurrentDebt;
    }
    return 0;
}

---

๐Ÿ”’ Security & Permissions

User Authentication

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

User Group Permissions

$userGroupId = $_SESSION['usergroupid'];
$loadUserGroupData = $myUserGroupRecord->load($userGroupId);
$smarty->assign("loadUserGroupData", $loadUserGroupData);

Input Validation

// Secure input filtering
$datefrom = filter_input(INPUT_POST, 'datefrom');
$dateto = filter_input(INPUT_POST, 'dateto');
$supplierid = filter_input(INPUT_POST, 'supplierid');

// Order validation
if ($order != "asc" && $order != "desc") {
    $order = "no";
}

---

๐Ÿ“Š Performance Considerations

Database Optimization

1. Required Indexes:

   CREATE INDEX idx_buybill_supplier_date ON buybill(buybillsupplierid, buybilldate);
   CREATE INDEX idx_returnbuybill_supplier_date ON returnbuybill(returnbuybillsupplierid, returnbuybilldate);
   CREATE INDEX idx_billsbuy_client_date ON billsbuy(clientid, billdate);
   ```

2. **Query Optimization**:
   - Use appropriate date range queries
   - Limit result sets with proper WHERE clauses
   - Efficient JOIN operations for detail data

3. **Memory Management**:
   - Large date ranges may consume significant memory
   - Consider pagination for very large datasets
   - Optimize data aggregation algorithms

### Performance Monitoring
php

// Add performance tracking

$startTime = microtime(true);

// Report generation code...

$endTime = microtime(true);

$executionTime = ($endTime - $startTime);

error_log("Report generation time: " . $executionTime . " seconds");

---

## ๐Ÿ› Common Issues & Troubleshooting

### 1. **Incorrect Date Range Results**
**Issue**: Reports show wrong data for date ranges  
**Cause**: Timezone adjustment errors or date format issues

**Debug**:
php

echo "Original date: " . $datefrom . "
";

echo "Adjusted date: " . $adjustedDate . "
";

echo "Timezone hours: " . $Programsetting->reportsPlusHours . "
";

### 2. **Supplier Debt Calculation Errors**
**Issue**: Debt amounts don't match expected values  
**Cause**: Currency conversion or debt change tracking issues

**Debug**:
php

function debugSupplierDebt($supplierId) {

global $supplierDAO, $supplierDeptChangeExt;

$supplier = $supplierDAO->load($supplierId);

$debtChanges = $supplierDeptChangeExt->queryBySupplierId($supplierId);

echo "Current debt: " . $supplier->suppliercurrentDebt . "
";

echo "Debt changes count: " . count($debtChanges) . "
";

foreach ($debtChanges as $change) {

echo "Change: " . $change->supplierdebtchangeamount . " Type: " . $change->supplierdebtchangetype . "
";

}

}

### 3. **Data Aggregation Inconsistencies**
**Issue**: Totals don't match when filtering by different criteria  
**Cause**: Logic errors in data collection or aggregation

**Debug**:
php

// Log aggregation steps

foreach ($billTypes as $type => $data) {

echo "$type bills count: " . count($data) . "
";

$typeTotal = array_sum(array_column($data, 'amount'));

echo "$type total: $typeTotal
";

}

---

## ๐Ÿงช Testing Scenarios

### Test Case 1: Basic Purchase Report

1. Select date range (last 30 days)

2. Select specific supplier

3. Generate report

4. Verify all bill types included

5. Check financial totals accuracy

### Test Case 2: Multi-Supplier Analysis

1. Run report for all suppliers

2. Verify supplier-specific data separation

3. Check debt calculations per supplier

4. Validate payment history accuracy

### Test Case 3: Date Range with Timezone

1. Set specific timezone in program settings

2. Run report with date range

3. Verify timezone adjustments applied

4. Check edge cases (date boundaries)

```

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When purchase reporting requirements change