Salesreportcatstore Documentation

Sales Report Category Store Controller Documentation

File: /controllers/salesreportcatstore.php

Purpose: Generates sales reports grouped by product categories and stores with profit analysis

Last Updated: December 21, 2024

Total Functions: 4

Lines of Code: ~1,281

---

๐Ÿ“‹ Overview

The Sales Report Category Store Controller is a specialized reporting module that provides comprehensive sales analysis by product categories and stores. It handles:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**sellbill**Sales bills mastersellbillid, sellbillclientid, sellbilltotalbill, sellbillaftertotalbill, sellbilldate, sellbillstoreid, sellerid, userid
**sellbilldetail**Sales line itemssellbilldetailid, sellbillid, sellbilldetailproductid, sellbilldetailquantity, sellbilldetailtotalprice, productunitid, storeid
**returnsellbill**Return bills masterreturnsellbillid, returnsellbillclientid, returnsellbilltotalbill, returnsellbilldate, returnsellbillstoreid
**returnsellbilldetail**Return line itemsreturnsellbilldetailid, returnsellbillid, returnsellbilldetailproductid, returnsellbilldetailquantity
**sellbillandrutern**Combined sell & returnsellbillid, sellbillclientid, sellbilldate, sellbillstoreid, sellerid
**sellandruternbilldetail**Combined bill detailssellandruternbilldetailid, sellbillid, sellbilldetailproductid, sellbilldetailquantity, selltype
### Product Tables

Table NamePurposeKey Columns
**product**Product master dataproductId, productName, productCatId, overAllAveragePrice, productBuyPrice, isService
**productcat**Product categoriesproductCatId, productCatName, productCatParent
**productunit**Product unitsproductunitid, productid, productnumber, buyprice, lastbuyprice, meanbuyprice
### Configuration Tables

Table NamePurposeKey Columns
**programsettings**System settingsprogramsettingsid, vatValue, valueadded, reportsPlusHours
**store**Store informationstoreId, storeName
**client**Customer dataclientid, clientname
**user**System usersuserid, employeename, userstoreid, usergroupid
---

๐Ÿ”‘ Key Functions

1. getDataNew() - Main Report Generation Function

Location: Line 370

Purpose: Core function that processes all sales data and generates comprehensive reports

Function Signature:

function getDataNew($queryString, $queryString1, $queryStringR, $queryString1R, $queryString1SR, 
                   $chosenProductPrice, $searchtype, $productCatId, $theStore, $IDSOfProducts, 
                   $typeId, $servicesAreIncluded)

Process Flow:

1. Initialize productData class for result storage

2. Calculate VAT value for profit calculations

3. Process regular sales bills with R::getAll() queries

4. Process return bills and combined bills

5. Apply category/store filtering based on searchtype

6. Calculate real costs using chosen pricing method

7. Generate summary totals and category aggregations

Key Variables:

---

2. getAllSubCat() - Category Hierarchy Processing

Location: Line 1229

Purpose: Recursively processes product category hierarchies to include all subcategories

Function Signature:

function getAllSubCat($catid, $mode)

Parameters:

Process Flow:

1. Query subcategories of given parent

2. Recursively process each child category

3. Build comma-separated ID string for SQL IN clauses

4. Handle both full tree and leaf-only modes

---

3. isCatChildToParent() - Category Parent Checking

Location: Line 1267

Purpose: Determines if a category is a child of a specified parent category

Function Signature:

function isCatChildToParent($catId, $parent)

Process Flow:

1. Query parent ID of given category

2. Check if matches target parent

3. Recursively check grandparent relationships

4. Set global flag when relationship found

---

4. Default Action - Main Controller Logic

Location: Line 199

Purpose: Handles URL routing, parameter processing, and report orchestration

Process Flow:

1. Load program settings and user permissions

2. Process POST parameters and session data

3. Handle "today's bills" quick filter

4. Build dynamic query strings for each bill type

5. Apply store, date, seller, and category filters

6. Call getDataNew() with constructed parameters

7. Assign results to Smarty template

---

๐Ÿ”„ Workflows

Workflow 1: Category-Based Sales Report Generation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Select Category & Date Range
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Process Input Parameters
- Category ID and search type
- Date range (with hour adjustments)
- Store and seller filters
- Pricing method selection
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Expand Category Hierarchy
- Call getAllSubCat() to get all child categories
- Build product list for category tree
- Create SQL IN clause for product filtering
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Query All Bill Types
- Regular sales bills (sellbill + sellbilldetail)
- Return bills (returnsellbill + returnsellbilldetail)
- Combined bills (sellbillandrutern + details)
- Apply filters: date, store, seller, category
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Process Each Transaction
FOR EACH bill detail record:
โ”‚
โ†’ Load product and category information
โ”‚
โ†’ Calculate quantities (handle product units)
โ”‚
โ†’ Process discounts and VAT
โ”‚
โ†’ Calculate real cost using chosen method
โ”‚ โ”œโ”€ Buy price from bill
โ”‚ โ”œโ”€ Last buy price
โ”‚ โ”œโ”€ Mean buy price
โ”‚ โ”œโ”€ Discounted prices
โ”‚ โ”‚ โ””โ”€ Tax-inclusive prices โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ Accumulate totals by category/store โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Generate Category Summations
- Group results by product categories
- Calculate net quantities (sold - returned)
- Calculate net values and profit margins
- Generate parent category rollups
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Output Report
- Assign data to Smarty template
- Include summary totals
- Display via salesreportcatstoreviews/show.html
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default actionCategory/Store sales report based on searchtype
### Required Parameters

Optional Parameters

---

๐Ÿงฎ Calculation Methods

Profit Calculation by Pricing Method

switch ($chosenProductPrice) {
    case 0: // Buy price from bill
        $myproduct->realCost += $finalquantity * $value->buyprice * $vatValue;
        break;
    case 1: // Mean buy price
        $myproduct->realCost += $finalquantity * $value->meanbuyprice * $vatValue;
        break;
    case 2: // Last buy price  
        $myproduct->realCost += $finalquantity * $value->lastbuyprice * $vatValue;
        break;
    case 3: // Last buy price with discount
        $myproduct->realCost += $finalquantity * $value->lastbuyprice_withDiscount * $vatValue;
        break;
    case 4: // Mean buy price with discount
        $myproduct->realCost += $finalquantity * $value->meanbuyprice_withDiscount * $vatValue;
        break;
    case 5: // General/average price
        $myproduct->realCost += $finalquantity * $value->generalPrice * $vatValue;
        break;
    case 6: // Last buy price with tax
        $myproduct->realCost += $finalquantity * $value->lastbuyprice_withTax * $vatValue;
        break;
    case 7: // Mean buy price with tax
        $myproduct->realCost += $finalquantity * $value->meanbuyprice_withTax * $vatValue;
        break;
}

Discount Processing

// Get bill-level discount
$dicount = $value->sellbilldiscount;
if ($value->sellbilldiscounttype == 2) {
    // Percentage discount
    $dicount = ($value->sellbilldiscount / 100) * $value->parcode;
}

// Distribute discount proportionally to line item
if ($dicount != 0) {
    $theDiscount = ($value->sellbilldetailtotalprice / $value->parcode) * $dicount;
    $theDiscount = round($theDiscount, 2);
}

VAT Calculation

// Calculate VAT multiplier for cost calculations
$vatValue = 1 + ($Programsetting->vatValue / 100);

// Apply to cost calculation
$realCost = $finalquantity * $buyPrice * $vatValue;

---

๐Ÿ”’ Security & Permissions

User Store Access Control

if ($user->userstoreid == 0) {
    // Admin user - can select any store
    $theStore = $storeId;
    $queryString1 .= 'and sellbill.sellbillstoreid = ' . $storeId . ' ';
} else if ($_SESSION['searchinonestore'] == 0) {
    // Multi-store user
    if ($_SESSION['storeids'] != 0) {
        $queryString1 .= 'and sellbill.sellbillstoreid in (' . $_SESSION['storeids'] . ') ';
    }
} else {
    // Single store user
    $theStore = $_SESSION['storeid'];
    $queryString1 .= 'and sellbill.sellbillstoreid = ' . $_SESSION['storeid'] . ' ';
}

Authentication Requirements

---

๐Ÿ“Š Performance Considerations

Database Optimization Tips

1. Critical Indexes:

- sellbill(sellbilldate, sellbillstoreid, sellerid)

- sellbilldetail(sellbillid, sellbilldetailproductid)

- productcat(productCatParent) for hierarchy queries

- product(productCatId) for category filtering

2. Query Optimization:

- Uses R::getAll() for efficient data retrieval

- Implements proper JOIN relationships

- Date filtering with time adjustment logic

- IN clauses for category hierarchy filtering

3. Memory Considerations:

- Large date ranges may process thousands of records

- Category hierarchy recursion depth should be monitored

- Product arrays built in memory for fast lookups

Known Performance Issues

-- Category hierarchy expansion can be expensive for deep trees
-- Solution: Consider materialized path or nested set model

-- Multiple bill type queries could be combined with UNION
-- Current separate queries allow for better debugging but impact performance

---

๐Ÿ› Common Issues & Troubleshooting

1. Missing Category Data

Issue: Products don't appear in category reports

Cause: Product category assignments missing or hierarchy broken

Debug:

-- Check products without categories
SELECT productId, productName FROM product WHERE productCatId IS NULL OR productCatId = 0;

-- Verify category hierarchy
SELECT productCatId, productCatName, productCatParent FROM productcat WHERE conditions = 0;

2. Incorrect Profit Calculations

Issue: Profit margins don't match expectations

Cause: VAT settings or pricing method selection issues

Debug:

// Check VAT configuration
$Programsetting = $ProgramsettingDAO->load(1);
echo "VAT Value: " . $Programsetting->vatValue . "%<br>";
echo "VAT Calculation: " . (1 + ($Programsetting->vatValue / 100));

// Verify pricing method data
echo "Chosen Price Method: " . $chosenProductPrice;

3. Store Filtering Not Working

Issue: All stores showing when specific store selected

Cause: User permission conflicts or session data issues

Fix:

// Debug user store permissions
echo "User Store ID: " . $user->userstoreid . "<br>";
echo "Search In One Store: " . $_SESSION['searchinonestore'] . "<br>";
echo "User Store IDs: " . $_SESSION['storeids'];

---

๐Ÿงช Testing Scenarios

Test Case 1: Category Report Accuracy

1. Create test products in specific category
2. Generate test sales bills with known quantities/prices
3. Run category report for test period
4. Verify quantities and amounts match source bills
5. Check profit calculations against manual calculations

Test Case 2: Store Comparison Report

1. Create sales in multiple stores
2. Use searchtype=1 for store-based grouping
3. Verify each store's totals are isolated correctly
4. Check that combined totals match overall sales

Test Case 3: Pricing Method Validation

1. Set up product with different cost prices
2. Create sales bill using known pricing method
3. Run report with each pricing method (0-7)
4. Verify cost calculations use correct price source
5. Confirm profit margins calculated properly

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur