ClientNew Documentation

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:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**client**Customer master dataclientid, clientname, clientaddress, clientphone, clientmobile, clientdebt, clientareaid, delegateid, typeClientid, conditions
**clientdebtchange**Customer debt transaction logclientdebtchangeid, clientid, clientdebtchangeamount, clientdebtchangetype, clientdebtchangedate, tablename
**clientdetails**Extended customer informationclientdetailsid, noOfPersonsTamween, noOfPersonsDa3m, cardNum, cardPassword, specialDiscount
### Accounting Tables

Table NamePurposeKey Columns
**dailyentry**Journal entriesdailyentryid, dailyentryserial, dailyentrydate, dailyentrytotalamount, dailyentrydescription
**dailyentrycreditor**Credit side entriesdailyentrycreditorid, dailyentryid, accountstreeid, dailyentrycreditoramount
**dailyentrydebtor**Debit side entriesdailyentrydebtoryid, dailyentryid, accountstreeid, dailyentrydebtorramount
**accountstree**Chart of accountsaccountstreeid, accountstreecode, accountstreename, accountstreetype
### Classification Tables

Table NamePurposeKey Columns
**clientarea**Customer areas/regionsid, name, description
**government**Government/state datagovernmentid, governmentname
**goverarea**Government sub-areasgoverareaid, governmentid, goverareaname
**typeclient**Customer typestypeclientid, typeclientname, typeclientdiscount
### Configuration Tables

Table NamePurposeKey Columns
**store**Store locationsstoreid, storename, storecode
**user**System users/delegatesuserid, username, employeename, usergroupid
**associatedtags**Customer tagsassociatedtagid, associatedtagname, conditions
**youtubelink**Tutorial videosyoutubelinkid, title, url
**programsettings**System settingsprogramsettingsid, settingkey, settingvalue
### Online Store Tables

Table NamePurposeKey Columns
**eclientsetting**E-commerce customer settingseclientsettingid, emailnotifications, smsnotifications, callingcustomer
**esaudiclientsetting**Saudi-specific e-commerce settingsesaudiclientsettingid, identityNumber, companyNumber, vatNumber
**onlinestoremainsetting**Main online store configurationonlinestoremainsettingid, isactive, domain
**tamweenclientdetail**Social support program detailstamweenclientdetailid, 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:

---

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:

---

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 Column Mapping:

ColumnFieldValidation
ACustomer NameRequired, unique check
BPhoneOptional
CMobileOptional
DAddressOptional
ECustomer Area IDMust exist in clientarea table
FInitial DebtNumeric, default 0
GCustomer CodeOptional, unique
Process Flow:

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:

---

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

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: New Customer Creation
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Load Form Dependencies
- Government locations
- Store locations
- Customer types
- Customer areas
- Available delegates
- System tags
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Display Customer Form
- Pre-populate dropdowns
- Generate next customer ID
- Show validation requirements
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Process Form Submission
- Validate required fields
- Check name uniqueness
- Validate business rules
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Create Customer Record
- Insert main client record
- Create extended details
- Set up online store profile (if enabled)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Initialize Financial Records
IF initial debt > 0:
โ”‚
โ†’ Create debt change record
โ”‚
โ†’ Generate daily entry
โ”‚ โ”œโ”€ Debit: Customer account
โ”‚ โ”‚ โ””โ”€ Credit: Opening balance account โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ Update customer debt balance โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Finalize Creation
- Return success status
- Redirect to success page
- Or return customer ID for API calls
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default actionCustomer creation form
`do=add``add()`Process new customer creation
`do=addSimpleReturn`Special AJAXSimplified 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

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur