Index Documentation

Index Controller Documentation

File: /controllers/index.php

Purpose: Main dashboard and system entry point with comprehensive business intelligence

Last Updated: December 20, 2024

Total Functions: 35+

Lines of Code: ~3,500+


๐Ÿ“‹ Overview

The Index Controller serves as the main dashboard and central hub of the ERP19 system. It provides:

Primary Functions

Related Controllers


๐Ÿ—„๏ธ Database Tables

Primary Analytics Tables

Table NamePurposeKey Columns
**sellbill**Sales transactionssellbillid, sellbillclientid, sellbilltotalbill, sellbilldate
**buybill**Purchase transactionsbuybillid, buybillsupplierid, buyibilltotalbill, buybilldate
**client**Customer accountsclientid, clientname, clientdebt, clientarea
**supplier**Supplier accountssupplierid, suppliername, suppliercurrentDebt
**save**Cash registers/safessaveid, savename, savevalue
**store**Inventory locationsstoreid, storename, storebalance
### Financial Tables

Table NamePurposeKey Columns
**clientdebtchange**Customer debt historyclientid, clientdebtchangeamount, clientdebtchangedate
**supplierdebtchange**Supplier debt historysupplierid, supplierdebtchangeamount, supplierdebtchangedate
**dailyentry**Journal entriesdailyentryid, entryComment, entryDate
**capital**Company capitalcapitalid, capitalvalue, capitaldate
### System Tables

Table NamePurposeKey Columns
**programsettings**System configurationprogramsettingsid, companyname, roundnumbers
**user**System usersuserid, username, usergroupid, saveid
**usergroup**User rolesusergroupid, usergroupname, permissions
**newdbbackup**Backup trackingbackupid, backupname, backupdate
---

๐Ÿ”‘ Key Functions

1. Default Dashboard - Main Analytics Display

Location: Line 763

Purpose: Display comprehensive business dashboard with KPIs

Process Flow:

1. Load user permissions and settings

2. Calculate financial summaries

3. Generate sales and purchase analytics

4. Process client and supplier debt summaries

5. Display inventory status

6. Show system health metrics

Dashboard Components:


2. search() - Financial Search and Analytics

Location: Line 1933

Purpose: Advanced financial search with date range filtering

Function Signature:

function search($saveid, $processid, $startDate, $endDate) {
    // Comprehensive financial analysis
}

Process Flow:

1. Apply date range filters

2. Filter by cash register (save)

3. Process transaction types

4. Calculate running totals

5. Generate detailed reports

Analytics Generated:


3. showAll() - Sales Transaction Summary

Location: Line 2342

Purpose: Comprehensive sales transaction analysis

Function Signature:

function showAll($startDate, $endDate) {
    // Process all sales transactions
}

Metrics Calculated:


4. showBuyBill() - Purchase Analysis

Location: Line 2774

Purpose: Purchase transaction analytics and supplier performance

Function Signature:

function showBuyBill($from, $to) {
    // Analyze purchase transactions
}

Supplier Analytics:


5. clientdebt() - Customer Debt Analysis

Location: Line 3338

Purpose: Comprehensive customer debt tracking and analysis

Debt Metrics:


6. supplierdebt() - Supplier Payment Analysis

Location: Line 3350

Purpose: Track supplier payments and outstanding balances

Payment Analytics:


7. Network Payment Integration

Location: Line 1207

Purpose: Process network payment transactions

Functions:


8. System Initialization Functions

firstlogin()

Location: Line 1329

Purpose: First-time system setup and initialization

insertUserGroup(), insertUser(), insertSave()

Location: Lines 1532-1614

Purpose: Create default system data

insertInitialCapitalValue()

Location: Line 1648

Purpose: Setup initial company capital


9. Database Management Functions

backup_tables()

Location: Line 1777

Purpose: Automated database backup with retention policies

CHECKdatabase(), repardatabase()

Location: Lines 1734-1776

Purpose: Database health monitoring and repair


๐Ÿ”„ Workflows

Workflow 1: Dashboard Load Process

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: User Dashboard Request
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Load User Permissions and Settings
- Check user group permissions
- Load store access rights
- Set language preferences
- Configure display options
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Calculate Financial KPIs
- Total sales revenue (today/week/month)
- Gross and net profit margins
- Cash flow indicators
- Outstanding receivables
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Generate Sales and Purchase Analytics
- Sales performance by period
- Top-selling products
- Customer purchase patterns
- Supplier performance metrics
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Process Debt and Payment Summaries
- Customer debt aging analysis
- Supplier payment schedules
- Cash flow forecasting
- Credit risk assessment
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Display Dashboard Interface
- Render KPI widgets
- Show charts and graphs
- Display alerts and notifications
- Load user-specific views
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜


๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default dashboardMain business intelligence dashboard
`do=bill`Test modeTesting and development mode
`do=compo`Component testComponent testing mode
`do=godo`Go modeSpecial operational mode
`do=change_lang`Language switchChange system language
`do=run`Run analyticsExecute specific analytics
`do=show3`Show by dayDaily breakdown view
### Dashboard Parameters


๐Ÿ”’ Security & Permissions

User-Based Access Control

// User permission checks
$userData = $myUserRecord->load($_SESSION['userid']);
if ($userData->viewbills == 0) {
    // Restrict to user's own transactions
}
if ($userData->viewclients == 0) {
    // Restrict to user's own customers
}

Store-Level Security

// Store access restrictions
$userStores = explode(',', $_SESSION['storeids']);
// Filter data by accessible stores

Financial Data Protection


๐Ÿ“Š Performance Considerations

Dashboard Optimization

1. Caching Strategy: Dashboard metrics cached for 5-15 minutes

2. Lazy Loading: Load charts and heavy analytics on demand

3. Query Optimization: Indexed date range queries

4. Data Aggregation: Pre-calculated summary tables

Database Performance

// Optimized query patterns
$query = "SELECT SUM(sellbilltotalbill) as total_sales 
          FROM sellbill 
          WHERE sellbilldate >= ? AND sellbilldate <= ?
          AND conditions = 0";

Memory Management


๐Ÿ› Common Issues & Troubleshooting

1. Dashboard Loading Slowly

Issue: Dashboard takes 10+ seconds to load

Cause: Heavy analytics queries without optimization

Debug:

// Check query performance
$start = microtime(true);
$result = $dao->complexAnalyticsQuery();
$time = microtime(true) - $start;
echo "Query time: {$time} seconds";

Fix:

-- Add indexes for date range queries
CREATE INDEX idx_sellbill_date ON sellbill(sellbilldate);
CREATE INDEX idx_buybill_date ON buybill(buybilldate);

2. Incorrect Financial Calculations

Issue: KPI numbers don't match detailed reports

Cause: Different query conditions or date handling

Debug:

// Verify query conditions
echo "Sales Query: " . $salesQuery;
echo "Profit Query: " . $profitQuery;

// Check date format consistency
echo "Date format: " . date('Y-m-d', strtotime($inputDate));

3. Permission Errors

Issue: Users see data they shouldn't access

Cause: Missing permission checks or incorrect filtering

Fix:

// Always check user permissions
if (!hasPermission('view_financial_data')) {
    throw new Exception('Access denied');
}

// Apply store filters
$storeFilter = getUserStoreFilter($_SESSION['userid']);
$query .= " AND storeid IN ({$storeFilter})";

4. Memory Issues with Large Datasets

Issue: Script timeout or memory exhaustion

Cause: Loading too much data at once

Fix:

// Use pagination and limits
$limit = 1000;
$offset = 0;
while (true) {
    $batch = getDataBatch($offset, $limit);
    if (empty($batch)) break;
    processBatch($batch);
    $offset += $limit;
    unset($batch); // Free memory
}


๐Ÿงช Testing Scenarios

Test Case 1: Dashboard Performance

1. Login as different user types
2. Access dashboard with large date ranges
3. Verify load times under 3 seconds
4. Check KPI accuracy against manual calculations
5. Test with different store permissions

Test Case 2: Financial Calculations

1. Create test transactions
2. Check dashboard calculations
3. Compare with detailed reports
4. Verify profit margins
5. Test edge cases (refunds, discounts)

Test Case 3: Multi-User Scenarios

1. Test concurrent dashboard access
2. Verify user-specific data filtering
3. Check permission enforcement
4. Test store access restrictions

Test Case 4: Data Integrity

1. Create backdated transactions
2. Check historical KPI accuracy
3. Verify date range filtering
4. Test timezone handling


๐Ÿ“ˆ Business Intelligence Features

Sales Analytics

Financial Analytics

Operational Analytics


๐Ÿ“š Related Documentation


Documented By: AI Assistant

Review Status: โœ… Complete

Performance Review: โš ๏ธ Optimization recommended for large datasets

Next Review: When dashboard performance improvements are implemented