Client Controller Documentation
File: /controllers/clientController.php
Purpose: Complete customer lifecycle management including creation, editing, deletion, and Excel import functionality
Last Updated: December 20, 2024
Total Functions: 12
Lines of Code: ~1,822
---
๐ Overview
The Client Controller is the primary customer management module that handles all customer-related operations in the ERP system. It provides comprehensive functionality for customer creation, modification, deletion, and bulk import operations. The controller integrates with:
- โข Customer debt management
- โข Daily entry accounting system
- โข Store and product associations
- โข Government and area classifications
- โข Customer type categorization
- โข Online store integration
- โข Excel import/export capabilities
- โข Supplier linkage for special operations
Primary Functions
- โ Customer creation with full profile details
- โ Customer editing and updates
- โ Customer deletion (soft delete)
- โ Excel bulk import functionality
- โ Customer listing and search
- โ Debt balance initialization
- โ Accounting integration (daily entries)
- โ Geographic classification (government/area)
- โ Customer type categorization
- โ Special customer features (RFID, discounts, credit limits)
- โ Online store synchronization
- โ Supplier linking operations
Related Controllers
- โข clientdebt.php - Customer debt tracking
- โข clientReportsController.php - Customer reports
- โข clientPayedDeptController.php - Payment processing
- โข sellbillController.php - Sales operations
- โข dailyentry.php - Accounting entries
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **client** | Customer master data | clientid, clientname, clientaddress, clientphone, clientmobile, clientdebt, clientareaid, delegateid, typeClientid, conditions | |
| **clientdebtchange** | Customer debt transaction log | clientdebtchangeid, clientid, clientdebtchangeamount, clientdebtchangetype, clientdebtchangedate, tablename | |
| **clientdetails** | Extended customer information | clientdetailsid, noOfPersonsTamween, noOfPersonsDa3m, cardNum, cardPassword, specialDiscount |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **dailyentry** | Journal entries | dailyentryid, dailyentryserial, dailyentrydate, dailyentrytotalamount, dailyentrydescription | |
| **dailyentrycreditor** | Credit side entries | dailyentrycreditorid, dailyentryid, accountstreeid, dailyentrycreditoramount | |
| **dailyentrydebtor** | Debit side entries | dailyentrydebtoryid, dailyentryid, accountstreeid, dailyentrydebtorramount | |
| **accountstree** | Chart of accounts | accountstreeid, accountstreecode, accountstreename, accountstreetype |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **clientarea** | Customer areas/regions | id, name, description | |
| **government** | Government/state data | governmentid, governmentname | |
| **goverarea** | Government sub-areas | goverareaid, governmentid, goverareaname | |
| **typeclient** | Customer types | typeclientid, typeclientname, typeclientdiscount |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **store** | Store locations | storeid, storename, storecode | |
| **user** | System users/delegates | userid, username, employeename, usergroupid | |
| **associatedtags** | Customer tags | associatedtagid, associatedtagname, conditions | |
| **youtubelink** | Tutorial videos | youtubelinkid, title, url | |
| **programsettings** | System settings | programsettingsid, settingkey, settingvalue |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **eclientsetting** | E-commerce customer settings | eclientsettingid, emailnotifications, smsnotifications, callingcustomer | |
| **esaudiclientsetting** | Saudi-specific e-commerce settings | esaudiclientsettingid, identityNumber, companyNumber, vatNumber | |
| **onlinestoremainsetting** | Main online store configuration | onlinestoremainsettingid, isactive, domain | |
| **tamweenclientdetail** | Social support program details | tamweenclientdetailid, clientid, cardNum, cardPassword |
๐ Key Functions
1. Default Action - Add Customer Form
Location: Line 190-237
Purpose: Display customer creation form with all required dropdown data
Process Flow:
1. Check user authentication
2. Load reference data:
- All government locations
- All store locations
- Customer types
- Customer areas
- User delegates
- Associated tags
3. Calculate next customer ID
4. Display add form template
Template Variables:
- โข
$allGovernment- Government dropdown options - โข
$stores- Store assignment options - โข
$typeClient- Customer type options - โข
$allDelegate- Delegate assignment options - โข
$allclientarea- Area classification options - โข
$allTags- Available customer tags - โข
$nextId- Auto-generated next customer ID
---
2. add() - Create New Customer
Location: Line 632-941
Purpose: Process customer creation with full validation and accounting integration
Function Signature:
function add()
Input Processing:
$clientname = $_POST["txtName"];
$clientaddress = $_POST["txtAddress"];
$clientphone = $_POST["txtPhone"];
$clientmobile = $_POST["txtMobile"];
$clientdebt = (float) $_POST["txtDebt"];
$clientareaid = (int) $_POST["clientareaid"];
$clientcode = $_POST["clientcode"];
$debtLimit = $_POST["debtLimit"];
Validation Checks:
1. Name Uniqueness: Prevents duplicate customer names
2. Required Fields: Validates essential customer information
3. Debt Limit: Ensures proper credit limit settings
4. Type Classification: Validates customer type assignments
Process Flow:
1. Extract and validate POST data
2. Check for existing customer with same name
3. Create new client record
4. Process extended customer details
5. Handle online store settings (if enabled)
6. Create initial debt balance entry (if non-zero)
7. Generate accounting entries for debt
8. Process store assignments
9. Handle customer type assignments
10. Process special features (RFID, discounts)
Accounting Integration:
// Create debt balance entry
if ($clientdebt != 0) {
$clientDeptChange->clientid = $newClientid;
$clientDeptChange->clientdebtchangeamount = $clientdebt;
$clientDeptChange->clientdebtchangetype = 0; // Debt increase
$clientDeptChange->tablename = 'clientController.php';
$clientDeptChangeDAO->save($clientDeptChange);
}
Return Values:
- โข
Positive Integer- Success (new client ID) - โข
-1- Error: Duplicate client name - โข
-2- Error: Missing required fields
---
3. addFromExcel() - Bulk Import from Excel
Location: Line 942-1187
Purpose: Import multiple customers from Excel spreadsheet with validation
Function Signature:
function addFromExcel()
Supported File Formats:
- โข Excel 2007+ (.xlsx)
- โข Excel 97-2003 (.xls)
- โข CSV files
Excel Column Mapping:
| Column | Field | Validation | |
|---|---|---|---|
| A | Customer Name | Required, unique check | |
| B | Phone | Optional | |
| C | Mobile | Optional | |
| D | Address | Optional | |
| E | Customer Area ID | Must exist in clientarea table | |
| F | Initial Debt | Numeric, default 0 | |
| G | Customer Code | Optional, unique |
1. Validate uploaded file format
2. Load Excel data using PHPExcel
3. For each row:
- Extract customer data
- Validate required fields
- Check name uniqueness
- Create customer record
- Handle debt initialization
- Generate accounting entries
4. Track success/failure counts
5. Generate import summary
Error Handling:
try {
$objPHPExcel = PHPExcel_IOFactory::load($uploadPath);
$worksheet = $objPHPExcel->getActiveSheet();
$highestRow = $worksheet->getHighestRow();
for ($row = 2; $row <= $highestRow; ++$row) {
// Process each customer row
}
} catch (Exception $e) {
// Handle file processing errors
}
---
4. show() - Customer Listing
Location: Line 1188-1240
Purpose: Display paginated list of customers with search capabilities
Function Signature:
function show()
Features:
- โข Pagination support
- โข Search by name, phone, address
- โข Sort by various columns
- โข Customer type filtering
- โข Area-based filtering
- โข Active/inactive status filtering
---
5. edit() - Customer Modification
Location: Line 1401+
Purpose: Load customer data for editing and process updates
Function Signature:
function edit()
Process Flow:
1. Load existing customer data
2. Load related information (areas, types, etc.)
3. Pre-populate edit form
4. Process update submission
5. Handle related record updates
6. Update accounting entries if debt changed
---
6. tempdelete() - Soft Delete Customer
Location: Line 1357-1378
Purpose: Deactivate customer without removing historical data
Function Signature:
function tempdelete($clientid)
Process:
$client = $clientDAO->load($clientid);
$client->conditions = 1; // Mark as deleted
$clientDAO->update($client);
---
7. returndelete() - Reactivate Customer
Location: Line 1379-1400
Purpose: Restore previously soft-deleted customer
Function Signature:
function returndelete($clientid)
Process:
$client = $clientDAO->load($clientid);
$client->conditions = 0; // Mark as active
$clientDAO->update($client);
---
๐ Workflows
Workflow 1: Customer Creation
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) | Default action | Customer creation form | |
| `do=add` | `add()` | Process new customer creation | |
| `do=addSimpleReturn` | Special AJAX | Simplified customer creation for supplier linking | |
| `do=addFromExcel` | `addFromExcel()` | Bulk import from Excel file | |
| `do=show` | `show()` | Customer listing with pagination | |
| `do=showByType` | `showByType()` | Filter customers by type | |
| `do=search` | `search()` | Search customers by criteria | |
| `do=edit` | `edit()` | Customer editing form | |
| `do=executeOperation` | `executeOperation()` | Process customer updates | |
| `do=tempdelete` | `tempdelete()` | Soft delete customer | |
| `do=returndelete` | `returndelete()` | Reactivate customer |
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข clientdebt.php - Customer debt tracking
- โข clientReportsController.php - Customer reporting
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur