SupplierReportsnew Documentation

Supplier Reports New Controller Documentation

File: /controllers/supplierReportsnewController.php

Purpose: Simplified supplier reporting module for debt tracking and transaction history

Last Updated: December 20, 2024

Total Functions: 7

Lines of Code: ~1,062

---

๐Ÿ“‹ Overview

The Supplier Reports New Controller is a streamlined reporting module that provides basic supplier account analysis and debt tracking capabilities. It handles:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**supplierdebtchange**Supplier debt transaction logsupplierdebtchangeid, supplierid, supplierdebtchangeamount, supplierdebtchangetype, supplierdebtchangedate, tablename
**supplier**Supplier master datasupplierid, suppliername, suppliercurrentDebt, userid
### Purchase Tables (Referenced)

Table NamePurposeKey Columns
**buybill**Purchase billsbuybillid, buybillsupplierid, buybilltotalbill, buybillaftertotalbill, buybilltotalpayed
**buybilldetail**Purchase bill line itemsbuybilldetailid, buybillid, buybilldetailproductid, buybilldetailquantity, buybilldetailtotalprice
**returnbuybill**Purchase return billsreturnbuybillid, returnbuybillsupplierid, returnbuybilltotalbill, returnbuybillaftertotalbill
**returnbuybilldetail**Return bill detailsreturnbuybilldetailid, returnbuybillid, returnbuybilldetailproductid, returnbuybilldetailquantity
**buyandruternbill**Combined buy & returnbuybillid, buybillsupplierid, buybillprice, returnbuybillprice, buybilldate
**buyandruternbilldetail**Combined bill detailsbuyandruternbilldetailid, buybillid, buybilldetailproductid, buybilldetailquantity, buytype
### Reference Tables

Table NamePurposeKey Columns
**youtubelink**Tutorial linksyoutubelinkid, title, url
**product**Product informationproductid, productName
**productcat**Product categoriesproductCatId, productCatName
**unit**Measurement unitsunitid, unitName
---

๐Ÿ”‘ Key Functions

1. Default Action - Basic Supplier Report Form

Location: Line 129

Purpose: Display supplier report form with basic filtering options

Function Signature:

// Triggered when: do is empty or do=supplierShow2
$startDate = $_REQUEST['from'];
$endDate = $_REQUEST['to'];  
$supplierId = $_POST['supplierId'];

Process Flow:

1. Load supplier dropdown data

2. Validate input parameters

3. Call supplierShow2() if supplier selected

4. Load YouTube tutorial links

5. Display via supplierShow2.html template

---

2. supplierShow2() - Core Supplier Report Logic

Location: Line 217

Purpose: Build supplier debt history with transaction linking

Function Signature:

function supplierShow2($supplierid, $startDate, $endDate)

Process Flow:

1. Build dynamic SQL query string based on filters

2. Query supplierdebtchange table with supplier/date filters

3. Process each transaction record:

- Link to source documents (bills, payments, checks)

- Calculate operational costs and payments

- Load related data (bill totals, discounts)

4. Handle "bure" (net) view calculation

5. Assign data to Smarty template

Transaction Types Handled:

---

3. supplierShowDetail() - Detailed Supplier Report

Location: Line 866

Purpose: Enhanced supplier report with product-level details

Function Signature:

function supplierShowDetail($supplierid, $startDate, $endDate)

Process Flow:

1. Similar to supplierShow2() but with enhanced detail

2. Load product details for each bill type:

- $productsArr - Regular purchase bill details

- $productsReArr - Return bill details

- $productsBuyAndReArr - Combined bill details

3. Query bill detail tables for product information

4. Assign enhanced data arrays to template

Enhanced Features:

---

4. showBuyBillsBySupplierAndDate() - Purchase Bills Query

Location: Line 663

Purpose: Retrieve and process purchase bills for a supplier/date range

Function Signature:

function showBuyBillsBySupplierAndDate($supplierid, $startDate, $endDate)

Process Flow:

1. Query buybill table with supplier/date filters

2. Query buyandruternbill table for combined bills (buy portion)

3. For each bill:

- Count product quantities via detail tables

- Apply discount calculations

- Calculate final bill amounts

4. Merge datasets and return combined array

Discount Processing:

// Fixed amount discount
if ($buybilldiscountrype == 0) {
    $myfialtotal = $mytotal - $buybilldiscount;
} 
// Percentage discount  
else {
    $myfialtotal = $mytotal - (($mytotal * $buybilldiscount) / 100);
}

---

5. showReturnBuyBillsBySupplierAndDate() - Returns Query

Location: Line 734

Purpose: Retrieve and process return bills for analysis

Function Signature:

function showReturnBuyBillsBySupplierAndDate($supplierid, $startDate, $endDate)

Process Flow:

1. Query returnbuybill table

2. Query buyandruternbill table for return portions

3. Handle negative amounts in returns (convert to positive)

4. Count returned quantities via detail tables

5. Return array with both datasets

Return Value:

$returnbuybillData = array_merge($returnbuybillData, $buybillandruternData);
// Combined return bills from both sources

---

6. getSupplierData() - Supplier Dropdown Data

Location: Line 207

Purpose: Load supplier list for report selection dropdowns

Function Signature:

function getSupplierData()

Returns: Array of active supplier objects for dropdown population

---

7. Net View Processing - Transaction Cleanup

Location: Lines 404-473

Purpose: Clean up offsetting entries for simpler reporting view

Process Flow:

if ($bure == "1") {
    foreach ($shownData as $datax) {
        if ($datax->supplierdebtchangeid > 0) {
            $finalstring = $datax->finalstring;
            $type = $datax->supplierdebtchangetype;
            
            if ($type == 1) { // Payment entry
                $otherfinal = substr_replace($finalstring, '0', -1);
                // Hide offsetting entries
                foreach ($shownData as $xx) {
                    if ($xx->finalstring == $otherfinal) {
                        $xx->supplierdebtchangeid = -1;
                        $datax->supplierdebtchangeid = -1;
                    }
                }
            }
        }
    }
}

---

๐Ÿ”„ Workflows

Workflow 1: Basic Supplier Report Generation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Select Supplier & Date Range
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Validate Input Parameters
- Check supplierid
- Parse date range
- Load supplier data
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Build SQL Query String
- Add supplier filter
- Add date range filter
- Add deletion filter (del = 0)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Query supplierdebtchange Table
- Get all transactions for supplier/date
- Order by specified criteria
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Process Each Transaction
FOR EACH debt change record:
โ”‚
โ†’ Identify source table (buybill, payment, etc.)
โ”‚
โ†’ Load source document details
โ”‚ โ”œโ”€ Bill totals and discounts
โ”‚ โ”œโ”€ Payment amounts
โ”‚ โ”‚ โ””โ”€ Operational costs โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ Generate link to source document โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Apply Net View Processing (optional)
- Remove offsetting entries
- Recalculate running balances
- Clean up display
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Generate Report Output
- Assign data to Smarty template
- Include totals and summaries
- Display via appropriate template
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty) or `do=supplierShow2`Default actionBasic supplier report form
`do=supplierShowDetail``supplierShowDetail()`Detailed supplier report with products
### Required Parameters by Action

Basic Supplier Report (do=supplierShow2):

Detailed Supplier Report (do=supplierShowDetail):

---

๐Ÿงฎ Calculation Methods

Operational Cost Calculation

// For purchase bills
$data->totalOperationCost = $buybilldata->buybilltotalbill + $data->supplierdebtchangebefore;

// For returns
$data->totalOperationCost = 0 + $data->supplierdebtchangebefore;

// For payments
$data->totalOperationCost = $data->supplierdebtchangebefore;

Discount Processing

// Calculate bill discount
$data->discount = ($buybilldata->buybilltotalbill - $buybilldata->buybillaftertotalbill);

// Fixed vs percentage discount in combined bills
if ($buybilldiscountrype == 0) {
    $myfialtotal = $mytotal - $buybilldiscount; // Fixed
} else {
    $myfialtotal = $mytotal - (($mytotal * $buybilldiscount) / 100); // Percentage
}

Running Balance Calculation

foreach ($shownData as $mov) {
    if ($mov->supplierdebtchangetype == "0") { // Debt increase
        $mov->supplierdebtchangeafter = $startafter + $startvalue;
        $startafter = $startafter + $startvalue;
    } else { // Debt decrease (payment)
        $mov->supplierdebtchangeafter = $startafter - $startvalue;
        $startafter = $startafter - $startvalue;
    }
}

---

๐Ÿงช Testing Scenarios

Test Case 1: Basic Supplier Report

1. Select supplier with recent transactions
2. Set date range covering known purchases
3. Verify transaction count matches database
4. Check operational cost calculations
5. Confirm links to source documents work

Test Case 2: Detailed Product Breakdown

1. Select supplier with detailed purchase history
2. Run detailed report
3. Verify product arrays are populated
4. Check product quantities and prices
5. Validate template displays product tables

Test Case 3: Net View Processing

1. Select supplier with offsetting transactions
2. Enable net view processing
3. Verify offsetting entries are hidden
4. Check running balance accuracy
5. Confirm cleaner display

---

๐Ÿ”’ Security & Permissions

Input Sanitization

Access Control

---

๐Ÿ“Š Performance Considerations

Database Optimization Tips

1. Indexes Required:

- supplierdebtchange(supplierid, supplierdebtchangedate)

- buybill(buybillsupplierid, buybilldate)

- returnbuybill(returnbuybillsupplierid, returnbuybilldate)

2. Query Optimization:

- Date filtering with proper format: YYYY-MM-DD HH:MM:SS

- Efficient WHERE clauses

- Batch loading of related data

3. Memory Management:

- Product arrays can grow large for active suppliers

- Consider pagination for very long date ranges

---

๐Ÿ› Common Issues & Troubleshooting

1. Missing Transaction Links

Issue: Transaction shows "#" link instead of proper document link

Cause: supplierdebtchangemodelid = -1 or unrecognized tablename

2. Incorrect Operational Costs

Issue: Cost calculations don't match expected values

Cause: Bill loading issues or discount calculation errors

3. Empty Product Arrays

Issue: Detailed reports don't show products

Cause: Bill detail queries failing or empty detail tables

4. Net View Not Working

Issue: Offsetting entries still visible

Cause: finalstring matching logic issues or incorrect transaction types

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur