DailyReport Documentation
Daily Report Controller Documentation
File: /controllers/dailyReportController.php
Purpose: Generates comprehensive daily business summary reports with sales, purchases, debts, and inventory metrics
Last Updated: December 20, 2024
Total Functions: 1
Lines of Code: ~134
---
๐ Overview
The Daily Report Controller provides a comprehensive daily dashboard that summarizes key business metrics including sales revenue, purchase costs, customer payments, supplier payments, debt totals, inventory valuation, and expense breakdowns. It automatically calculates time-adjusted reports based on configured business hours.
Primary Functions
- โ Daily sales and payment summary
- โ Purchase and supplier payment tracking
- โ Customer and supplier debt totals
- โ Inventory valuation by configured method
- โ Daily expense breakdown by category
- โ Cash register totals
- โ Time-zone adjusted reporting periods
- โ Configurable report timing
Related Controllers
- โข sellbillController.php - Sales transactions
- โข buyBillController.php - Purchase transactions
- โข clientPayedDeptController.php - Customer payments
- โข supplierPayedDeptController.php - Supplier payments
- โข expensesController.php - Expense management
---
๐๏ธ Database Tables
Sales Tables
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **sellbill** | Sales transactions | sellbillid, sellbilltotalpayed, sellbillfinalbill, sellbilldate, conditions | |
| **buybill** | Purchase transactions | buybillid, buybillaftertotalbill, buybillsupplierid, buybilldate | |
| **returnsellbill** | Sales returns | returnsellbillid, returnsellbilltotalpayed, returnsellbilldate |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **clientdebtchange** | Customer debt changes | clientdebtchangeid, clientid, clientdebtchangeamount, clientdebtchangedate, tablename | |
| **supplierdebtchange** | Supplier debt changes | supplierdebtchangeid, supplierid, supplierdebtchangeamount, supplierdebtchangedate | |
| **client** | Customer master data | clientid, clientname, clientdebt, conditions | |
| **supplier** | Supplier master data | supplierid, suppliername, suppliercurrentDebt, conditions |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **storedetail** | Inventory quantities | storedetailid, productid, productquantity | |
| **product** | Product master data | productId, productBuyPrice, lastbuyprice, meanbuyprice, overAllAveragePrice |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **expenses** | Daily expenses | expensesid, expensesValue, expensesdate, expensestypeid, conditions | |
| **expensestype** | Expense categories | expensestypeid, expensestypename | |
| **save** | Cash registers/safes | saveid, savename, savecurrentvalue, conditions |
| Table Name | Purpose | Key Columns |
|---|---|---|
| **programsettings** | System settings | programsettingsid, Inventoryevaluation, reportsPlusHours |
๐ Key Functions
1. show() / Default Action - Daily Report Generation
Location: Line 31
Purpose: Generate comprehensive daily business summary
Function Signature:
// Triggered when: do=show or empty $do
$today = filter_input(INPUT_POST, 'date') ?: date('Y-m-d');
Process Flow:
1. Load program settings for report configuration
2. Calculate time-adjusted reporting period
3. Query sales revenue and debt data
4. Query purchase and supplier payment data
5. Calculate customer and supplier debt totals
6. Evaluate inventory value by configured method
7. Summarize daily expenses by category
8. Calculate total cash register values
9. Display via show.html template
Time Adjustment Logic:
if (isset($Programsettingdata->reportsPlusHours) && !empty($Programsettingdata->reportsPlusHours)) {
$reportsPlusHours = $Programsettingdata->reportsPlusHours + 24;
$endToday = date('Y-m-d H:i:s', strtotime('+' . $reportsPlusHours . ' hour', strtotime($today)));
$startToday = date('Y-m-d H:i:s', strtotime('+' . $Programsettingdata->reportsPlusHours . ' hour', strtotime($today)));
}
Dynamic Date Range Selection:
if (date('H') < $Programsettingdata->reportsPlusHours) {
$startDate = $startYesterday; // Use previous day's data
$endDate = $endYesterday;
} else {
$startDate = $startToday; // Use current day's data
$endDate = $endToday;
}
---
๐ Workflows
Workflow 1: Daily Report Generation
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description |
|---|---|---|
| `do=` (empty) or `do=show` | Default action | Generate daily report |
Daily Report:
- โข
date(POST, optional) - Specific date for report (defaults to today)
---
๐งฎ Calculation Methods
Sales Revenue Query
SELECT SUM(sellbilltotalpayed) FROM sellbill
WHERE conditions = 0
AND sellbilldate >= ? AND sellbilldate <= ?
Outstanding Invoices
SELECT SUM(sellbillfinalbill) FROM sellbill
WHERE conditions = 0
AND sellbilldate >= ? AND sellbilldate <= ?
Purchase Totals by Supplier
SELECT suppliername, SUM(buybillaftertotalbill) AS total
FROM buybill
JOIN supplier ON buybill.buybillsupplierid = supplier.supplierid
WHERE buybill.conditions = 0
AND buybilldate >= ? AND buybilldate <= ?
GROUP BY buybill.buybillsupplierid
Customer Payments Received
SELECT clientname, SUM(clientdebtchangeamount) total
FROM clientdebtchange
JOIN client ON clientdebtchange.clientid = client.clientid
WHERE del = 0
AND clientdebtchangedate >= ? AND clientdebtchangedate <= ?
AND tablename = 'clientPayedDeptController.php'
GROUP BY clientdebtchange.clientid
Inventory Valuation Logic
switch ($Programsettingdata->Inventoryevaluation) {
case "first":
$pro_price = 'productBuyPrice';
break;
case "last":
$pro_price = 'lastbuyprice';
break;
case "mean":
$pro_price = 'meanbuyprice';
break;
case "last_discount":
$pro_price = 'lastbuyprice_withDiscount';
break;
case "mean_discount":
$pro_price = 'meanbuyprice_withDiscount';
break;
case "tax":
$pro_price = 'lastbuyprice_withTax';
break;
case "mean_tax":
$pro_price = 'meanbuyprice_withTax';
break;
default:
$pro_price = 'overAllAveragePrice';
break;
}
$storeEvaluation = R::getCell('
SELECT ROUND(SUM(productquantity * ' . $pro_price . '),2) AS total
FROM storedetail
JOIN product ON product.productId = storedetail.productid
WHERE product.conditions = 0
');
---
๐ Security & Permissions
Authentication Required
- โข All actions require authentication via
include_once("../public/authentication.php") - โข Session-based access control
Input Sanitization
$today = filter_input(INPUT_POST, 'date');
if (!$today) $today = date('Y-m-d');
SQL Injection Prevention
- โข Uses parameterized queries through RedBean ORM
- โข Date validation and formatting
- โข Secure variable binding
---
๐ Performance Considerations
Database Optimization Tips
1. Indexes Required:
- sellbill(sellbilldate, conditions)
- buybill(buybilldate, conditions, buybillsupplierid)
- clientdebtchange(clientdebtchangedate, del, tablename)
- supplierdebtchange(supplierdebtchangedate, del, tablename)
- expenses(expensesdate, conditions)
2. Query Optimization:
- Single date range for all queries
- Efficient aggregation functions
- Minimal data retrieval
- Proper JOIN usage
3. Memory Management:
- Process large datasets in chunks
- Clear variables after use
- Optimize template variable assignment
Known Performance Issues
-- This inventory query can be slow with many products
SELECT ROUND(SUM(productquantity * productBuyPrice),2) AS total
FROM storedetail
JOIN product ON product.productId = storedetail.productid
WHERE product.conditions = 0
-- Solution: Add composite index
CREATE INDEX idx_storedetail_product ON storedetail(productid);
CREATE INDEX idx_product_conditions ON product(conditions, productId);
---
๐ Common Issues & Troubleshooting
1. Incorrect Time Periods
Issue: Report shows wrong day's data
Cause: reportsPlusHours configuration mismatch
Debug:
echo "Current Hour: " . date('H') . "<br>";
echo "Report Plus Hours: " . $Programsettingdata->reportsPlusHours . "<br>";
echo "Start Date: " . $startDate . "<br>";
echo "End Date: " . $endDate . "<br>";
2. Missing Sales Data
Issue: Sales totals show zero
Cause: Incorrect date range or cancelled bills
Debug:
SELECT COUNT(*) as total_bills,
COUNT(CASE WHEN conditions = 0 THEN 1 END) as active_bills
FROM sellbill
WHERE sellbilldate = 'YYYY-MM-DD';
3. Inventory Valuation Errors
Issue: Negative or unrealistic inventory values
Cause: Missing price data or incorrect evaluation method
Debug:
SELECT COUNT(*) as products_with_no_price
FROM storedetail sd
JOIN product p ON sd.productid = p.productId
WHERE p.productBuyPrice IS NULL OR p.productBuyPrice = 0;
4. Expense Category Missing
Issue: Expenses not categorized properly
Cause: Missing expensestype records
Fix:
SELECT e.expensesValue, e.expensestypeid, et.expensestypename
FROM expenses e
LEFT JOIN expensestype et ON e.expensestypeid = et.expensestypeid
WHERE et.expensestypeid IS NULL;
---
๐งช Testing Scenarios
Test Case 1: Normal Daily Report
1. Set specific test date
2. Create test sales, purchases, payments
3. Run daily report
4. Verify all totals match expected values
5. Check time period accuracy
Test Case 2: Cross-Midnight Operations
1. Set reportsPlusHours to offset time
2. Test before and after midnight
3. Verify correct date period selection
4. Check data consistency
Test Case 3: Different Inventory Methods
1. Change Inventoryevaluation setting
2. Run report with each method
3. Verify price field usage
4. Compare valuation differences
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข sellbillController.md - Sales operations
- โข buyBillController.md - Purchase operations
- โข expensesController.php - Expense management
- โข Database Schema Documentation - Table relationships
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur