Unit Documentation

Unit Controller Documentation

File: /controllers/unitController.php

Purpose: Manages units of measurement for products and categories

Last Updated: December 19, 2024

Total Functions: 8

Lines of Code: 311

---

๐Ÿ“‹ Overview

The Unit Controller manages all units of measurement used throughout the ERP system. Units are fundamental building blocks that define how products are measured (pieces, kilograms, liters, etc.) and are essential for inventory management, billing, and reporting.

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
unitMain units tableunitId, unitName, unitSymbol, unitDescription, conditions
### Related Tables (Referenced)

Table NamePurposeRelationship
productunitProduct-unit assignmentsForeign Key: unit.unitId
productcatunitCategory-unit assignmentsForeign Key: unit.unitId
buybilldetailPurchase quantitiesForeign Key: unit.unitId
sellbilldetailSales quantitiesForeign Key: unit.unitId
storedetailInventory quantitiesForeign Key: unit.unitId
---

๐Ÿ”ง Key Functions

1. add()

Purpose: Creates a new unit of measurement

Called By: POST request with do=add

Parameters:

Returns: void (redirects on success/error)

Database Operations:

Business Logic:

1. Collect form data

2. Set default conditions = 0 (active)

3. Set current date

4. Set current user ID

5. Insert unit record

Example:

// POST data: unitName="Kilogram", unitSymbol="kg", unitDescription="Weight unit"
add(); // Creates new unit and redirects

2. show()

Purpose: Displays all units in management interface

Called By: GET request with do=show

Database Operations:

Business Logic:

1. Fetch all unit records

2. Assign to Smarty template variable

3. Display in show.html template

Template Integration:

3. executeOperation()

Purpose: Performs batch operations on multiple selected units

Called By: POST request with do=executeOperation

Parameters:

Returns: void (displays results)

Business Logic Flow:

โ”‚ โ””โ”€ Type "2": Call returndelete()

Output Format:

4. tempdelete($unitid)

Purpose: Safely deletes a unit with dependency validation

Parameters:

Returns: string - "success" or error message

Business Logic Flow:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Receive unit ID
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Check dependencies
Query productunit
table for usage
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Dependencies?
โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Return
error
message
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Validation Rules:

Error Messages:

5. returndelete($unitid)

Purpose: Restores a temporarily deleted unit

Parameters:

Database Operations:

Business Logic:

1. Restore unit by setting conditions back to 0

2. Unit becomes available for use again

6. edit()

Purpose: Loads unit data for editing

Called By: GET request with do=edit&id={unitId}

Returns: object - Unit data for form population

Database Operations:

Template Integration:

7. update()

Purpose: Updates existing unit with new data

Called By: POST request with do=update

Parameters:

Database Operations:

Business Logic:

1. Collect form data

2. Set current date and user ID

3. Update all unit properties

4. Preserve unit ID for update

8. Global Helper Functions

Purpose: Utility functions used throughout the system

getUnits() (referenced in other controllers):

---

๐Ÿ”„ Business Logic Flow

Unit Creation Workflow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
User fills form
โ€ข Name (required)
โ€ข Symbol (optional)
โ€ข Description
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Server validation
โ€ข Check required
fields
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Insert to database
โ€ข Set conditions=0
โ€ข Set current date
โ€ข Set user ID
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Redirect to success
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Unit Deletion Safety Check

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Delete request
for unit ID
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Query productunit
WHERE unitid = ID
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Found records?
โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Block
deletion
Return
error
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

โš ๏ธ Common Issues

Dependency Validation

Issue: Attempting to delete units that are in use

Solution: System checks productunit table before allowing deletion

Error Handling: Returns descriptive Arabic error message

Unicode Support

Issue: Arabic unit names and symbols need proper encoding

Solution: Database and forms configured for UTF-8 support

Symbol Standardization

Issue: Inconsistent unit symbols

Best Practice: Use standard abbreviations (kg, pcs, L, mยฒ, etc.)

---

๐Ÿ”— Dependencies

Required Files

include("../public/impOpreation.php");           // Core operations
include("../public/config.php");                // Configuration
include("../public/authentication.php");        // User authentication

DAO Classes

UnitDAO.class.php                               // Basic unit operations
UnitMySqlExtDAO.class.php                       // Extended unit operations
ProductunitDAO.class.php                        // Product-unit relationships
YoutubeLinkDAO.class.php                        // YouTube integration (help videos)

Views

Global Variables

$unit                                           // Unit DTO object
$unitDAO                                        // Unit DAO object
$unitExt                                        // Extended DAO object
$productUnitDAO                                 // Product-unit relationships
$today                                          // Current date

---

๐Ÿ“Š Performance Considerations

Database Queries

Memory Usage

Caching Opportunities

---

๐ŸŽฏ Integration Points

Product Management

Inventory System

Billing System

Reporting

---

๐Ÿ“‹ Usage Examples

Common Unit Types

// Weight units
Name: "Kilogram", Symbol: "kg"
Name: "Gram", Symbol: "g"
Name: "Ton", Symbol: "t"

// Count units
Name: "Piece", Symbol: "pcs"
Name: "Dozen", Symbol: "dz"
Name: "Box", Symbol: "box"

// Volume units
Name: "Liter", Symbol: "L"
Name: "Milliliter", Symbol: "mL"
Name: "Cubic Meter", Symbol: "mยณ"

// Length units
Name: "Meter", Symbol: "m"
Name: "Centimeter", Symbol: "cm"
Name: "Kilometer", Symbol: "km"

Typical Workflow

1. Administrator creates standard units (kg, pcs, L, etc.)

2. Categories are assigned appropriate units

3. Products inherit units from categories or override

4. Purchase/sales use these units for quantities

5. Reports display quantities with proper unit symbols

---

๐Ÿ” Troubleshooting

Cannot Delete Unit

Symptom: Unit deletion fails with Arabic error message

Cause: Unit is assigned to one or more products

Solution:

1. Remove unit from all products first

2. Or use temporary deletion to hide unit

Missing Units in Dropdowns

Symptom: Units not appearing in product/category forms

Cause: Units marked as deleted (conditions = 1)

Solution: Restore units using returndelete function

Character Encoding Issues

Symptom: Arabic text not displaying correctly

Cause: Database charset or page encoding mismatch

Solution: Ensure UTF-8 encoding throughout the system