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:
- โข Customer area creation and management
- โข Regional customer grouping
- โข Shipping cost management by area
- โข Area-based customer filtering
- โข Associated tag management
- โข Area deletion with customer validation
- โข Multi-language support (English names)
- โข Web API integration support
Primary Functions
- โ Customer area creation and editing
- โ Area deletion with safety checks
- โ Shipping cost management by area
- โ Associated tag integration
- โ Multi-language area names
- โ Web API ID management
- โ Customer relationship validation
- โ CRUD operations with audit trails
Related Controllers
- โข clientController.php - Customer management
- โข clientReportsController.php - Area-based reports
- โข clientReportsbyareaController.php - Regional reporting
- โข shippingFilesController.php - Shipping management
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns |
|---|---|---|
| **clientarea** | Customer area definitions | id, name, name_en, comment, webApiId, shipping_cost, associatedtag_id, sys_date, user_id, is_active, is_del |
| **client** | Customer records linked to areas | clientid, clientname, clientarea (FK to clientarea.id) |
| Table Name | Purpose | Key Columns |
|---|---|---|
| **associatedtags** | Area classification tags | id, tagname, conditions |
| **user** | System users | userid, 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:
- โข Load associated tags for area classification
- โข Display area creation form
- โข User permission validation
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 Checksphp
// Prevent deletion if customers assigned
$allclientdata = $clientExt->queryAllbyarea($id);
if (count($allclientdata) > 0) {
return 1; // Show warning, prevent deletion
}
```
Data Integrity
- โข Soft deletion (is_del flag) instead of hard delete
- โข User tracking for all area modifications
- โข Timestamp tracking for audit trails
- โข Active status management
Input Validation
- โข Numeric validation for shipping costs
- โข Associated tag existence checking
- โข Required field validation
- โข User permission enforcement
๐ Area Management Features
Multi-Language Support
- โข Arabic name (primary)
- โข English name (secondary)
- โข Supports international customers
Financial Integration
- โข Shipping cost per area
- โข Cost calculation for delivery
- โข Integration with order processing
Classification System
- โข Associated tags for area grouping
- โข Custom area categorization
- โข Flexible organization structure
Web API Support
- โข External system integration
- โข API identifier tracking
- โข Third-party service connectivity
๐ Common Use Cases
1. Regional Customer Organization
- โข Group customers by geographical area
- โข Separate reporting by region
- โข Targeted marketing campaigns
2. Shipping Cost Management
- โข Different shipping rates per area
- โข Automatic cost calculation
- โข Delivery route optimization
3. Sales Territory Management
- โข Assign salespeople to areas
- โข Track performance by region
- โข Commission calculations
๐ 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
- โข Index on clientarea(is_del) for active areas
- โข Index on client(clientarea) for customer queries
- โข Efficient joins with associated tags
Memory Management
- โข Minimal data loading in list views
- โข Efficient tag name resolution
- โข Quick update operations for shipping costs
๐ Related Documentation
- โข clientController.md - Customer management
- โข clientReportsController.md - Customer reports
- โข clientReportsbyareaController.md - Area-based reports
- โข shippingFilesController.php - Shipping management
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur