๐Ÿ“š ERP Documentation Viewer

Beautiful, colorful documentation for your ERP system

Element Management Controller Documentation (elemam.php)

File: /controllers/elemam.php

Purpose: Restaurant operations and bill reporting system with AJAX data tables

Last Updated: December 20, 2024

Total Functions: 2 (main actions)

Lines of Code: ~543

---

๐Ÿ“‹ Overview

The Element Management Controller (elemam.php) is a specialized restaurant operations module that provides comprehensive bill reporting and data aggregation. It handles:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**sellbill**Sales billssellbillid, sellbillclientname, sellbilldate, sellbilltotalpayed, sellbillaftertotalbill
**returnsellbill**Sales returnsreturnsellbillid, sellbillclientname, sellbilldate, sellbilltotalpayed, sellbillaftertotalbill
**sellbillandrutern**Combined billssellbillid, returnsellbillclientname, returnsellbilldate, returnsellbilltotalpayed, returnsellbillaftertotalbill
### Financial Calculation Fields

FieldPurposeType
**sellbilltotalpayed**Amount paid by customerDecimal
**sellbillaftertotalbill**Total bill after discounts/taxesDecimal
**sellbilldate**Transaction dateDateTime
**sellbillclientname**Customer nameString
---

๐Ÿ”‘ Key Functions

1. Default Action (empty $do) - AJAX Data Table Response

Location: Line 90

Purpose: Return JSON data for DataTables AJAX requests with bill aggregation

Process Flow:

1. Date Filter Setup:

   $start_date = $_POST["start_date"];
   $end_date = $_POST["end_date"];
   $day = $_POST["day"];
   ```

2. **Query String Building**:
   - `$sellbill` - Sales bill WHERE clause
   - `$returnsellbill` - Return bill WHERE clause  
   - `$sellbillandrutern` - Combined bill WHERE clause

3. **Data Aggregation**:
   ```php
   $sellBillData = R::findAll('sellbill', $sellbill);
   $sellBillDataReturn = R::findAll('returnsellbill', $returnsellbill);
   $sellBillDataSellAndReturn = R::findAll('sellbillandrutern', $sellbillandrutern);
   ```

4. **Bill Data Processing**:
   ```php
   foreach ($sellBillData as $value) {
       $myBill = new billData();
       $myBill->name = '<h3 style="color: green;">ูุงุชูˆุฑุฉ ู…ุจูŠุนุงุช</h3>';
       $myBill->id = '<a style="color: green;" target="_blank" href="sellbillController.php?do=showDetail&sellbillid=' . $value->sellbillid . '">ุชูุงุตูŠู„</a>';
       $myBill->client = $value->sellbillclientname;
       $myBill->datetime = $value->sellbilldate;
       $myBill->monetary += $value->sellbilltotalpayed;
       $myBill->totalbill += $value->sellbillaftertotalbill;
       array_push($allDataArr, $myBill);
   }
   ```

**Daily Aggregation Logic**:
php

if ($day == 1) {

$myArr = array();

$existDate = array();

foreach ($allDataArr as $value) {

$dt = strtotime($value->datetime);

if (in_array(date("Y-m-d", $dt), $existDate)) {

$key = array_search(date("Y-m-d", $dt), $existDate);

$myObj = $myArr[$key];

$myObj->totalbill += $value->totalbill;

$myObj->monetary += $value->monetary;

} else {

array_push($existDate, date("Y-m-d", $dt));

array_push($myArr, $value);

}

}

$allDataArr = $myArr;

}

---

### 2. **Advanced Filtering Action (do=add)** - Enhanced Data Processing
**Location**: Line 316  
**Purpose**: Process filtered data with bill type selection and enhanced aggregation

**Additional Features**:
- **Bill Type Filtering**:
  ```php
  $sell = $_POST["sell"];
  // 0 = All bills
  // 1 = Sales only
  // 2 = Returns only  
  // 3 = Combined only
  ```

- **Conditional Query Building**:
  ```php
  if ($sell == 0 || $sell == 1) {
      $sellbill .= '  and  sellbill.sellbilldate  >= "' . $start_date . '00:00:00" and sellbill.sellbilldate <= "' . $end_date . ' 23:59:59"  ';
  }
  if ($sell == 0 || $sell == 2) {
      $returnsellbill .= ' and  returnsellbill.returnsellbilldate >= "' . $start_date . ' 00:00:00" and returnsellbill.returnsellbilldate <= "' . $end_date . ' 23:59:59" ';
  }
  if ($sell == 0 || $sell == 3) {
      $sellbillandrutern .= ' and  sellbillandrutern.sellbilldate >= "' . $start_date . ' 00:00:00" and sellbillandrutern.sellbilldate <= "' . $end_date . ' 23:59:59" ';
  }
  ```

---

### 3. **billData Class** - Data Structure
**Location**: Line 164  
**Purpose**: Standardized data structure for bill aggregation

**Properties**:
php

class billData {

public $datetime; // Transaction date/time

public $name; // Bill type display name

public $id; // Detail link HTML

public $client; // Customer name

public $monetary; // Amount paid

public $totalbill; // Total bill amount

}

---

## ๐Ÿ”„ Workflows

### Workflow 1: Real-time Bill Data Aggregation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ START: AJAX Data Request โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 1. Parse Filter Parameters โ”‚

โ”‚ - start_date / end_date โ”‚

โ”‚ - day aggregation flag โ”‚

โ”‚ - sell bill type filter โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 2. Build Dynamic WHERE Clauses โ”‚

โ”‚ - Sales bill date filter โ”‚

โ”‚ - Return bill date filter โ”‚

โ”‚ - Combined bill date filter โ”‚

โ”‚ - Default to today if no dates โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 3. Execute Database Queries โ”‚

โ”‚ - Query sales bills โ”‚

โ”‚ - Query return bills โ”‚

โ”‚ - Query combined bills โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 4. Process Each Bill Type โ”‚

โ”‚ FOR EACH bill type: โ”‚

โ”‚ โ”‚ โ”‚

โ”‚ โ”œโ”€โ†’ Create billData object โ”‚

โ”‚ โ”‚ โ”‚

โ”‚ โ”œโ”€โ†’ Set display properties โ”‚

โ”‚ โ”‚ โ”œโ”€ Colored bill type name โ”‚

โ”‚ โ”‚ โ”œโ”€ Detail link with bill ID โ”‚

โ”‚ โ”‚ โ””โ”€ Customer name โ”‚

โ”‚ โ”‚ โ”‚

โ”‚ โ”œโ”€โ†’ Set financial data โ”‚

โ”‚ โ”‚ โ”œโ”€ Amount paid โ”‚

โ”‚ โ”‚ โ””โ”€ Total bill amount โ”‚

โ”‚ โ”‚ โ”‚

โ”‚ โ””โ”€โ†’ Add to aggregation array โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 5. Apply Daily Aggregation (if requested) โ”‚

โ”‚ - Group by date (Y-m-d) โ”‚

โ”‚ - Sum monetary amounts โ”‚

โ”‚ - Sum total bill amounts โ”‚

โ”‚ - Maintain one record per date โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 6. Format JSON Response โ”‚

โ”‚ - Format data for DataTables โ”‚

โ”‚ - Add summary totals โ”‚

โ”‚ - Return JSON with counts โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

## ๐ŸŒ URL Routes & Actions

| URL Parameter | Function | Description |
|---------------|----------|-------------|
| `do=` (empty) | Default AJAX handler | Returns JSON data for DataTables |
| `do=add` | Enhanced filtering | Advanced filtering with bill type selection |

### AJAX Request Parameters

**Standard Request**:
- `start_date` - Filter start date (YYYY-MM-DD)
- `end_date` - Filter end date (YYYY-MM-DD)
- `day` - Daily aggregation flag (0/1)

**Enhanced Request** (`do=add`):
- All standard parameters plus:
- `sell` - Bill type filter:
  - `0` - All bill types
  - `1` - Sales bills only
  - `2` - Return bills only
  - `3` - Combined bills only

---

## ๐Ÿงฎ Calculation Methods

### Financial Aggregation
php

// Calculate totals across all bill types

$monetary = 0;

$totalbill = 0;

foreach ($allDataArr as $value) {

$totalbill += $value->totalbill;

$monetary += $value->monetary;

}

### Daily Grouping Algorithm
php

foreach ($allDataArr as $value) {

$dt = strtotime($value->datetime);

$dateKey = date("Y-m-d", $dt);

if (in_array($dateKey, $existDate)) {

$key = array_search($dateKey, $existDate);

$myObj = $myArr[$key];

$myObj->totalbill += $value->totalbill;

$myObj->monetary += $value->monetary;

} else {

array_push($existDate, $dateKey);

array_push($myArr, $value);

}

}

### Bill Type Identification
php

// Sales Bills (Green)

$myBill->name = '

ูุงุชูˆุฑุฉ ู…ุจูŠุนุงุช

';

// Return Bills (Blue)

$myBill->name = '

ูุงุชูˆุฑู‡ ู…ุฑุฏูˆุฏุงุช ู…ุจูŠุนุงุช

';

// Combined Bills (Red)

$myBill->name = '

ูุงุชูˆุฑุฉ ู…ุจูŠุนุงุช ูˆ ู…ุฑุฏูˆุฏุงุช

';

---

## ๐Ÿ”’ Security & Permissions

### Input Validation
- Date parameters sanitized before SQL inclusion
- POST data accessed through framework filters
- SQL injection prevented by RedBeanPHP ORM

### Data Access Control
- No user permission checks implemented
- Direct database access via ORM
- No authentication requirements

**Security Note**: This controller lacks user authentication and permission checks, which should be added for production use.

---

## ๐Ÿ“Š Performance Considerations

### Database Optimization
1. **Required Indexes**:
   ```sql
   CREATE INDEX idx_sellbill_date ON sellbill(sellbilldate);
   CREATE INDEX idx_returnsellbill_date ON returnsellbill(returnsellbilldate);
   CREATE INDEX idx_sellbillandrutern_date ON sellbillandrutern(sellbilldate);
   ```

2. **Query Optimization**:
   - Date range queries with proper BETWEEN clauses
   - Use of RedBeanPHP for ORM efficiency
   - Minimal JOIN operations

3. **Memory Management**:
   - Large date ranges may consume significant memory
   - Daily aggregation reduces result set size
   - Consider pagination for very large datasets

### Performance Issues
php

// Potential memory issue with large datasets

foreach ($sellBillData as $value) {

// Creating many billData objects

$myBill = new billData();

array_push($allDataArr, $myBill);

}

**Solution**: Implement pagination or streaming for large result sets.

---

## ๐Ÿ› Common Issues & Troubleshooting

### 1. **Date Format Issues**
**Issue**: No data returned for valid date ranges  
**Cause**: Incorrect date format in SQL queries

**Debug**:
php

echo "Start: " . $start_date . " 00:00:00
";

echo "End: " . $end_date . " 23:59:59
";

echo "Query: " . $sellbill . "
";

**Fix**: Ensure proper date formatting with time components

### 2. **JSON Response Errors**
**Issue**: DataTable fails to load data  
**Cause**: Invalid JSON structure or PHP errors

**Debug**:
php

// Add before json_encode

error_log(print_r($output, true));

// Check JSON validity

$json = json_encode($output, JSON_UNESCAPED_SLASHES);

if (json_last_error() !== JSON_ERROR_NONE) {

error_log('JSON Error: ' . json_last_error_msg());

}

### 3. **Arabic Text Display Issues**
**Issue**: Arabic text appears garbled in JSON response  
**Cause**: Character encoding problems

**Fix**:
php

// Set proper headers

header('Content-Type: application/json; charset=utf-8');

echo json_encode($output, JSON_UNESCAPED_UNICODE);

### 4. **Daily Aggregation Errors**
**Issue**: Incorrect daily totals  
**Cause**: Date parsing or array merging logic errors

**Debug**:
php

foreach ($allDataArr as $value) {

$dt = strtotime($value->datetime);

echo "Date: " . date("Y-m-d", $dt) . " Total: " . $value->totalbill . "
";

}

---

## ๐Ÿงช Testing Scenarios

### Test Case 1: Basic Bill Aggregation

1. Send AJAX request with date range

2. Verify all bill types included

3. Check financial totals accuracy

4. Confirm proper JSON structure

### Test Case 2: Daily Aggregation

1. Request data with day=1 parameter

2. Verify bills grouped by date

3. Check aggregated totals per day

4. Confirm no duplicate dates

### Test Case 3: Bill Type Filtering

1. Test each sell parameter value (0,1,2,3)

2. Verify correct bill types returned

3. Check query filtering logic

4. Confirm totals match filter

### Debug Mode Setup
php

// Add at top for debugging

error_reporting(E_ALL);

ini_set('display_errors', 1);

// Log all queries

echo "";

echo "";

echo "";

echo "";

```

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When restaurant reporting requirements change

โ†‘