Clientarea Documentation

Client Area Controller Documentation

File: /controllers/clientareaController.php

Purpose: Manages customer geographical areas and regional settings

Last Updated: December 20, 2024

Total Functions: 8

Lines of Code: ~331


๐Ÿ“‹ Overview

The Client Area Controller manages geographical customer areas and regional divisions for customer organization and reporting. It provides functionality for:

Primary Functions

Related Controllers


๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**clientarea**Customer area definitionsid, name, name_en, comment, webApiId, shipping_cost, associatedtag_id, sys_date, user_id, is_active, is_del
**client**Customer records linked to areasclientid, clientname, clientarea (FK to clientarea.id)
### Reference Tables

Table NamePurposeKey Columns
**associatedtags**Area classification tagsid, tagname, conditions
**user**System usersuserid, username
---

๐Ÿ”‘ Key Functions

1. Default Action - Area Creation Form

Location: Line 83

Purpose: Display area creation form

Function Signature:

// Triggered when: empty $do
include_once("../public/authentication.php");

Features:

2. add() - Create New Area

Location: Line 217

Purpose: Insert new customer area record

Function Signature:

function add() {
    global $Clientarea, $ClientareaDAO;
}

Process Flow:

1. Extract Form Data:

   $name = $_POST['name'];
   $name_en = $_POST['name_en'];
   $associatedtag_id = $_POST['associatedtag_id'];
   $comment = $_POST['comment'];
   $webApiId = $_POST['webApiId'];
   $shipping_cost = (int)$_POST['shipping_cost'];
   ```

2. **Set Area Properties**:
   ```php
   $Clientarea->name = $name;
   $Clientarea->name_en = $name_en;
   $Clientarea->associatedtag_id = $associatedtag_id;
   $Clientarea->shipping_cost = (int)$shipping_cost;
   $Clientarea->sys_date = date("Y-m-d H:i:s");
   $Clientarea->user_id = $_SESSION['userid'];
   $Clientarea->is_active = 1;
   $Clientarea->is_del = 0;
   ```

3. **Insert Record**:
   ```php
   $ClientareaDAO->insert($Clientarea);
   ```

### 3. **show()** - List All Areas
**Location**: Line 267  
**Purpose**: Display all customer areas with associated tag names

**Function Signature**:
php

function show() {

global $ClientareaDAO;

return $ClientareaDAO->queryAll();

}

**Post-Processing**:
php

foreach($clientareaData as $Data) {

$associatedtag = R::load('associatedtags', $Data->associatedtag_id);

$Data->associatedtag = $associatedtag->tagname;

}

### 4. **delete()** - Safe Area Deletion
**Location**: Line 247  
**Purpose**: Delete area with customer relationship validation

**Function Signature**:
php

function delete($id) {

global $clientExt;

}

**Safety Check**:
php

$allclientdata = $clientExt->queryAllbyarea($id);

if (count($allclientdata) > 0) {

return 1; // Cannot delete - has customers

} else {

R::exec("UPDATE clientarea SET is_del= 1 WHERE id = $id");

return 0; // Successfully marked as deleted

}

### 5. **edit() / update()** - Area Modification
**Location**: Lines 279, 295  
**Purpose**: Load and update area information

**Edit Process**:
php

function edit($id) {

global $ClientareaDAO;

return $ClientareaDAO->load($id);

}

**Update Process**:
php

function update() {

global $Clientarea, $ClientareaDAO;

$Clientarea->id = $_POST['id'];

$Clientarea->name = $_POST['name'];

$Clientarea->name_en = $_POST['name_en'];

$Clientarea->is_active = (int)$_POST['is_active'];

$ClientareaDAO->update($Clientarea);

}

### 6. **updateshipping** - Update Shipping Cost
**Location**: Line 184  
**Purpose**: Ajax endpoint for shipping cost updates

**Function Signature**:
php

// Triggered when: $do == "updateshipping"

$id = (int)$_POST['id'];

$shipping_cost = (int)$_POST['shipping_cost'];

R::exec("UPDATE clientarea SET shipping_cost= $shipping_cost WHERE id = $id");

---

## ๐Ÿ”„ Workflows

### Workflow 1: Area Creation Process

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

โ”‚ START: Create Area โ”‚

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

โ”‚

โ–ผ

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

โ”‚ 1. Display Area Creation Form โ”‚

โ”‚ - Load associated tags for classification โ”‚

โ”‚ - Show area input fields โ”‚

โ”‚ - Include shipping cost settings โ”‚

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

โ”‚

โ–ผ

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

โ”‚ 2. Validate Input Data โ”‚

โ”‚ - Check area name uniqueness โ”‚

โ”‚ - Validate associated tag selection โ”‚

โ”‚ - Verify shipping cost is numeric โ”‚

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

โ”‚

โ–ผ

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

โ”‚ 3. Create Area Record โ”‚

โ”‚ - Set Arabic and English names โ”‚

โ”‚ - Assign associated tag โ”‚

โ”‚ - Set shipping cost โ”‚

โ”‚ - Add creation timestamp โ”‚

โ”‚ - Link to creating user โ”‚

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

โ”‚

โ–ผ

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

โ”‚ 4. Insert into Database โ”‚

โ”‚ - Execute clientarea table insert โ”‚

โ”‚ - Generate new area ID โ”‚

โ”‚ - Confirm successful creation โ”‚

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

โ”‚

โ–ผ

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

โ”‚ 5. Redirect to Success โ”‚

โ”‚ - Show success confirmation โ”‚

โ”‚ - Area now available for customer assignment โ”‚

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

---

## ๐ŸŒ URL Routes & Actions

| URL Parameter | Function Called | Description |
|---------------|----------------|-------------|
| `do=` (empty) | Default action | Display area creation form |
| `do=add` | `add()` | Create new customer area |
| `do=show` | `show()` | List all customer areas |
| `do=delete` | `delete()` | Delete area (with validation) |
| `do=edit` | `edit()` | Load area for editing |
| `do=update` | `update()` | Update area information |
| `do=updateshipping` | Ajax update | Update shipping cost only |
| `do=sucess` | N/A | Success confirmation |
| `do=error` | N/A | Error page |

### Required Parameters by Action

**Create Area** (`do=add`):
- `name` - Area name in Arabic
- `name_en` - Area name in English
- `associatedtag_id` - Classification tag ID
- `comment` - Area description
- `webApiId` - Web API identifier (optional)
- `shipping_cost` - Shipping cost for area

**Delete Area** (`do=delete`):
- `id` - Area ID to delete

**Edit/Update Area** (`do=edit`, `do=update`):
- `id` - Area ID
- `name`, `name_en`, `comment`, `webApiId`, `shipping_cost` (for update)
- `is_active` - Active status (for update)

**Update Shipping** (`do=updateshipping`):
- `id` - Area ID
- `shipping_cost` - New shipping cost

---

## ๐Ÿ”’ Security & Data Validation

### Safety Checks
php

// Prevent deletion if customers assigned

$allclientdata = $clientExt->queryAllbyarea($id);

if (count($allclientdata) > 0) {

return 1; // Show warning, prevent deletion

}

```

Data Integrity

Input Validation


๐Ÿ“‰ Area Management Features

Multi-Language Support

Financial Integration

Classification System

Web API Support


๐Ÿ” Common Use Cases

1. Regional Customer Organization

2. Shipping Cost Management

3. Sales Territory Management


๐Ÿ› Common Issues & Troubleshooting

1. Cannot Delete Area

Issue: "ู„ุง ูŠู…ูƒู† ุญุฐู ู‡ุฐู‡ ุงู„ุดุฑูƒุฉ ู„ูˆุฌูˆุฏ ุนู…ู„ุงุก ู…ุฑุชุจุทูŠู† ุจู‡ุง"

Cause: Customers are assigned to this area

Solution: Reassign customers to different areas first

2. Shipping Cost Not Updating

Issue: Shipping cost changes not reflected

Cause: Ajax request failing or validation error

Solution: Check numeric format and user permissions

3. Associated Tag Not Displaying

Issue: Tag names not showing in area list

Cause: Missing or deleted associated tag

Solution: Verify tag exists and is not marked as deleted


๐Ÿ“† Performance Considerations

Database Optimization

Memory Management


๐Ÿ“„ Related Documentation


Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur