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:
- โข Category-wise sales vs returns comparison
- โข Return rate calculation and analysis
- โข Simple AJAX-based data retrieval
- โข Category selection with autocomplete search
- โข Minimal footprint with focused functionality
- โข Direct SQL queries for performance
Primary Functions
- โ Generate category-based return rate reports
- โ Compare sales volumes to return volumes
- โ AJAX data loading for responsive UI
- โ Category search with autocomplete
- โ Date range filtering
- โ Simple aggregated metrics
Related Controllers
- โข salesreportcatstore.php - Category sales reports
- โข returnsellbillController.php - Return bill management
- โข sellbillController.php - Sales operations
- โข productController.php - Product management
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **sellbilldetail** | Sales line items | sellbilldetailid, sellbillid, sellbilldetailcatid, sellbilldetailtotalprice, sellbilldetailquantity | |
| **sellbill** | Sales bills master | sellbillid, sellbilldate, conditions | |
| **returnsellbilldetail** | Return line items | returnsellbilldetailid, returnsellbillid, returnsellbilldetailcatid, returnsellbilldetailtotalprice, returnsellbilldetailquantity | |
| **returnsellbill** | Return bills master | returnsellbillid, returnsellbilldate, conditions |
| Table Name | Purpose | Key Columns |
|---|---|---|
| **productcat** | Product categories | productCatId, 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:
- โข Minimal setup - just template rendering
- โข No complex initialization required
---
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
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) | Default action | Display report interface | |
| `do=getdata` | Data generation | Generate return rate data via AJAX | |
| `do=select2category` | `select2category()` | Category autocomplete search |
Data Generation (do=getdata):
- โข
start_date- Report start date (YYYY-MM-DD) - โข
end_date- Report end date (YYYY-MM-DD) - โข
categoryid- Category ID (optional, all categories if empty)
Category Search (do=select2category):
- โข
searchTerm- Category name search string
---
๐งฎ 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
- โข Uses RedBeanPHP R::getAll() and R::getRow() for safe queries
- โข Direct variable concatenation present - should be parameterized
- โข Category search uses LIKE with user input - potential vulnerability
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
- โข CLAUDE.md - PHP 8.2 migration guide
- โข salesreportcatstore.md - Category sales reports
- โข sellbillController.md - Sales operations
- โข Database Schema Documentation - Table relationships
---
Documented By: AI Assistant
Review Status: โ Complete
Security Review: โ ๏ธ SQL injection vulnerability in category search
Next Review: When major changes occur