Salesreturnsratingsreport Documentation

Sales Returns Ratings Report Controller Documentation

File: /controllers/salesreturnsratingsreport.php

Purpose: Generates return rate analysis reports by product category with sales vs returns comparison

Last Updated: December 21, 2024

Total Functions: 2

Lines of Code: ~82

---

๐Ÿ“‹ Overview

The Sales Returns Ratings Report Controller is a lightweight reporting module that analyzes return patterns by product category. It provides:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**sellbilldetail**Sales line itemssellbilldetailid, sellbillid, sellbilldetailcatid, sellbilldetailtotalprice, sellbilldetailquantity
**sellbill**Sales bills mastersellbillid, sellbilldate, conditions
**returnsellbilldetail**Return line itemsreturnsellbilldetailid, returnsellbillid, returnsellbilldetailcatid, returnsellbilldetailtotalprice, returnsellbilldetailquantity
**returnsellbill**Return bills masterreturnsellbillid, returnsellbilldate, conditions
### Reference Tables

Table NamePurposeKey Columns
**productcat**Product categoriesproductCatId, productCatName, conditions
---

๐Ÿ”‘ Key Functions

1. Default Action - Display Report Interface

Location: Line 6

Purpose: Shows the main report interface template

Process Flow:

1. Display header template

2. Load main report template (salesreturnsratingsreportview/show.html)

3. Display footer template

Features:

---

2. getdata Action - Generate Report Data

Location: Line 11

Purpose: Main report data generation with AJAX response

Process Flow:

1. Accept POST parameters (start_date, end_date, categoryid)

2. Build date filter conditions for sales and returns

3. Query specific category or all categories

4. Aggregate sales totals and return totals

5. Return results via HTML template

Function Signature:

$start_date = filter_input(INPUT_POST, 'start_date');
$end_date = filter_input(INPUT_POST, 'end_date');  
$categoryid = filter_input(INPUT_POST, 'categoryid');

3. select2category Action - Category Autocomplete

Location: Line 69

Purpose: Provides category search functionality for UI

Function Signature:

function select2category() {
    $name = $_POST['searchTerm'];
    // Returns JSON array of matching categories
}

Process Flow:

1. Accept search term from POST

2. Query productcat with LIKE search on category name

3. Limit results to 50 matches

4. Format as Select2-compatible JSON array

5. Return JSON response

---

๐Ÿ”„ Workflows

Workflow 1: Return Rate Analysis Report

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Select Category & Date Range
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1User Input Processing
- Start date and end date selection
- Category selection (specific or all)
- Form submission via AJAX
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Build Date Filter Conditions
- Add time stamps (00:00:00 to 23:59:55)
- Create sellQuery for sales bills
- Create returnQuery for return bills
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Category-Specific Processing
IF category selected:
โ†’ Load category details
โ†’ Query sales for this category
โ†’ Query returns for this category
โ”‚ โ””โ”€โ†’ Display category-specific template โ”‚
ELSE:
โ†’ Load all categories
โ†’ Loop through each category
โ†’ Aggregate totals per category
โ”‚ โ””โ”€โ†’ Display summary template โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Data Aggregation
FOR EACH category:
โ”‚
โ†’ Sum sales quantities and values
โ”‚ SELECT sum(sellbilldetailtotalprice),
โ”‚ sum(sellbilldetailquantity)
โ”‚
โ†’ Sum return quantities and values
โ”‚ SELECT sum(returnsellbilldetailtotalprice),
โ”‚ sum(returnsellbilldetailquantity)
โ”‚
โ”‚ โ””โ”€โ†’ Calculate return ratios โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Template Output
- Assign aggregated data to Smarty
- Display appropriate template:
โ”œโ”€ getcategory.html (specific category)
โ”‚ โ””โ”€ getdata.html (all categories summary) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default actionDisplay report interface
`do=getdata`Data generationGenerate return rate data via AJAX
`do=select2category``select2category()`Category autocomplete search
### Required Parameters by Action

Data Generation (do=getdata):

Category Search (do=select2category):

---

๐Ÿงฎ Calculation Methods

Sales Aggregation Query

SELECT sum(sellbilldetailtotalprice) as total, 
       sum(sellbilldetailquantity) as qunt 
FROM sellbilldetail 
JOIN sellbill ON sellbilldetail.sellbillid = sellbill.sellbillid
WHERE sellbill.conditions = 0 
  AND sellbilldetail.sellbilldetailcatid = [CATEGORY_ID]
  AND sellbill.sellbilldate >= '[START_DATE] 00:00:00' 
  AND sellbill.sellbilldate <= '[END_DATE] 23:59:55'

Returns Aggregation Query

SELECT sum(returnsellbilldetailtotalprice) as total, 
       sum(returnsellbilldetailquantity) as qunt 
FROM returnsellbilldetail  
JOIN returnsellbill ON returnsellbilldetail.returnsellbillid = returnsellbill.returnsellbillid
WHERE returnsellbill.conditions = 0  
  AND returnsellbilldetail.returnsellbilldetailcatid = [CATEGORY_ID]
  AND returnsellbill.returnsellbilldate >= '[START_DATE] 00:00:00' 
  AND returnsellbill.returnsellbilldate <= '[END_DATE] 23:59:55'

Data Structure for Templates

// For all categories summary
$productcatsdatas[] = [
    'productCatName' => $productcat['productCatName'],
    'squnt' => $sellbill['qunt'],        // Sales quantity
    'stotal' => $sellbill['total'],      // Sales total
    'rqunt' => $returnsellbill['qunt'],  // Returns quantity  
    'rtotal' => $returnsellbill['total'] // Returns total
];

---

๐Ÿ”’ Security & Permissions

Input Sanitization

// All inputs filtered through filter_input()
$start_date = filter_input(INPUT_POST, 'start_date');
$end_date = filter_input(INPUT_POST, 'end_date');
$categoryid = filter_input(INPUT_POST, 'categoryid');

SQL Injection Prevention

Security Issue:

-- Current implementation vulnerable to SQL injection
WHERE productCatName LIKE '%$name%'

-- Should be:
WHERE productCatName LIKE :searchTerm

---

๐Ÿ“Š Performance Considerations

Database Optimization

1. Required Indexes:

- sellbilldetail(sellbilldetailcatid, sellbillid)

- sellbill(sellbilldate, conditions)

- returnsellbilldetail(returnsellbilldetailcatid, returnsellbillid)

- returnsellbill(returnsellbilldate, conditions)

2. Query Efficiency:

- Uses SUM() aggregation for efficient totaling

- Proper JOIN relationships reduce data transfer

- Date filtering with appropriate indexes

3. Scalability Notes:

- All categories query may be slow with many categories

- Consider pagination for large category lists

- Date range filtering prevents runaway queries

---

๐Ÿ› Common Issues & Troubleshooting

1. Missing Category Data

Issue: Categories showing zero sales/returns when data exists

Cause: Category ID mismatches between bills and products

Debug:

-- Check category assignments in bill details
SELECT DISTINCT sellbilldetailcatid FROM sellbilldetail 
WHERE sellbilldetailcatid IS NOT NULL;

-- Verify category exists in productcat table
SELECT * FROM productcat WHERE productCatId = [MISSING_ID];

2. Date Range Issues

Issue: No data returned for valid date ranges

Cause: Date format or time stamp handling

Fix:

// Ensure proper date format in queries
$start_date . ' 00:00:00'  // Start of day
$end_date . ' 23:59:55'    // End of day (almost midnight)

3. AJAX Loading Problems

Issue: Category autocomplete not working

Cause: JSON format issues or server response problems

Debug:

// Check browser console for AJAX errors
console.log(response);

// Verify JSON format
{
    "id": "123",
    "text": "Category Name"
}

---

๐Ÿงช Testing Scenarios

Test Case 1: Category Return Rate Accuracy

1. Create test category with known products
2. Generate sales bills for test period
3. Create return bills for some of the sales
4. Run return rate report
5. Verify percentages match manual calculations

Test Case 2: Date Range Filtering

1. Create sales/returns on specific dates
2. Test various date ranges including:
   - Single day ranges
   - Month-long ranges  
   - Cross-month boundaries
3. Verify only appropriate data appears

Test Case 3: All Categories Summary

1. Create multiple categories with different return patterns
2. Run summary report (no category filter)
3. Verify each category's totals are correct
4. Check that sum of categories equals overall totals

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Security Review: โš ๏ธ SQL injection vulnerability in category search

Next Review: When major changes occur