๐Ÿ“š ERP Documentation Viewer

Beautiful, colorful documentation for your ERP system

Production Equation Controller Documentation

File: /controllers/productionEquationController.php

Purpose: Manages production equations/formulas defining raw material requirements for manufacturing finished products

Last Updated: December 20, 2024

Total Functions: 9

Lines of Code: ~613

---

๐Ÿ“‹ Overview

The Production Equation Controller manages manufacturing formulas that define the precise raw material composition needed to produce finished goods. This is essential for:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**productionrate**Production equation headersproductionRateId, name, finalName, sizeid, colorid, conditions, thedate, userId
**productionrateproduct**Raw material componentsproductionRateProductId, productionRateId, productId, sizeid, colorid, quantity, rate, unitId
### Referenced Tables

Table NamePurposeKey Columns
**product**Product master dataproductId, productName, productCatId, productBuyPrice
**productcat**Product categoriesproductCatId, productCatName
**productunit**Product units and conversionsproductunitid, productid, unitname, productnumber
**programsettings**System configurationprogramsettingsid, settingkey, settingvalue
**youtubelink**Tutorial linksyoutubelinkid, title, url
---

๐Ÿ”‘ Key Functions

1. Default Action - Add Production Equation Form

Location: Line 113

Purpose: Display form for creating new production equations

Function Signature:

// Triggered when: do=showadd or empty $do

Process Flow:

1. Check user authentication

2. Load program settings for display

3. Assign template variables

4. Display add form template

Features:

---

2. add() - Create Production Equation

Location: Line 376

Purpose: Validate and save new production equations with raw material components

Function Signature:

function add()

Process Flow:

1. Extract final product information (handle size/color variants)

2. Validate that total percentages equal 100%

3. Create production rate header record

4. Call addproductionrateproducts() for components

5. Return percentage total for validation

Validation Logic:

$totalpercentage = 0;
for ($i = 1; $i <= $rowitr; $i++) {
    if (isset($_POST["input" . $i . ""]) && !empty($_POST["input" . $i . ""])) {
        $Productionrateproduct->rate = $_POST["input" . $i . ""];
    } else {
        $Productionrateproduct->rate = 0;
    }
    $totalpercentage += $Productionrateproduct->rate;
}

if ($totalpercentage == 100) {
    // Process equation creation
}

Size/Color Handling:

if (strpos($productId, "hasSizeColor") !== false) {
    $productIdComplex = explode('-', str_replace("hasSizeColor", "", $productId));
    $productId = $productIdComplex[0];
    $sizeId = $productIdComplex[1];
    $colorId = $productIdComplex[2];
}

---

3. addproductionrateproducts() - Add Raw Material Components

Location: Line 318

Purpose: Save individual raw material entries for a production equation

Function Signature:

function addproductionrateproducts($productionrateid)

Process Flow:

1. Loop through posted row data

2. Extract product ID (handle size/color encoding)

3. Validate product and unit selections

4. Insert or update production rate product records

5. Handle quantity and percentage data

Component Validation:

if (isset($_POST["product" . $i . ""]) && !empty($_POST["product" . $i . ""]) && $_POST["product" . $i . ""] != -1) {
    if (isset($_POST["unitId" . $i . ""]) && !empty($_POST["unitId" . $i . ""]) && $_POST["unitId" . $i . ""] != -1) {
        // Process component
    }
}

---

4. edit() - Edit Production Equation

Location: Line 149

Purpose: Load existing equation for modification

Process Flow:

1. Load production rate and associated products

2. Build display names with category/size/color info

3. Load product unit data for dropdowns

4. Assign data to edit template

5. Display edit form

Display Name Building:

$oldfinalproductname = $product->productName . '/ ' . $productCat->productCatName;
if (!empty($product->sizeName))
    $oldfinalproductname .= '/ ' . $product->sizeName;
if (!empty($product->colorName))
    $oldfinalproductname .= '/ ' . $product->colorName;

---

5. update() - Update Production Equation

Location: Line 520

Purpose: Modify existing production equations with validation

Process Flow:

1. Re-validate percentage totals

2. Update header record

3. Delete existing component records

4. Re-insert updated components

5. Handle size/color variants

Update Logic:

//delete old ones in data base
$ProductionrateproductDAO->deleteByProductionRateId($productionrate->productionRateId);

//insert
addproductionrateproducts($productionrate->productionRateId);

---

6. addFromExcel() - Excel Import

Location: Line 428

Purpose: Bulk import production equations from Excel files

Function Signature:

function addFromExcel()

Process Flow:

1. Upload and process Excel file

2. Read worksheet starting from row 4

3. Extract equation name and final product ID

4. Validate product existence by barcode

5. Process up to 6 raw material components per equation

6. Insert valid equations with transaction control

Excel Format Expected:

Validation Logic:

if (!empty($name) && !empty($finalProductId)) {
    $checkName = $productionrateDAO->queryByName($name);
    $checkFinalProduct = $productDAO->queryByParcode($finalProductId);
    if (count($checkName) == 0 && isset($checkFinalProduct[0]->productId) && $checkFinalProduct[0]->productId > 0) {
        // Process equation
    }
}

---

7. details() - View Equation Details

Location: Line 221

Purpose: Display read-only view of production equation

Process Flow:

1. Load equation and component data

2. Load product unit information

3. Build display names

4. Assign to details template

---

8. show() - List All Equations

Location: Line 257

Purpose: Display paginated list of production equations

Features:

---

9. stop_active() - Toggle Equation Status

Location: Line 587

Purpose: Activate or deactivate production equations

Function Signature:

function stop_active()

Toggle Logic:

if ($productionrate->conditions == 0) {
    $productionrate->conditions = 1;
} elseif ($productionrate->conditions == 1) {
    $productionrate->conditions = 0;
}

---

10. delete() - Delete Production Equation

Location: Line 603

Purpose: Remove equation and all associated components

Process Flow:

1. Delete all component records

2. Delete header record

3. Cascading deletion ensures data integrity

---

๐Ÿ”„ Workflows

Workflow 1: Creating Production Equation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Create Production Equation
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Select Final Product
- Choose manufactured product
- Handle size/color variants
- Set equation name
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Define Raw Material Components
FOR EACH raw material:
โ”‚
โ†’ Select product
โ†’ Choose unit of measure
โ†’ Set quantity required
โ”‚ โ””โ”€โ†’ Set percentage in formula โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Validate Formula
- Check total percentages = 100%
- Validate all required fields
- Ensure product/unit selections
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Save Production Equation
- Insert header record
- Insert component records
- Set active status
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

Workflow 2: Excel Import Process

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Excel Import
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Upload Excel File
- Validate file format
- Read worksheet data
- Start from row 4
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Process Each Row
FOR EACH row in Excel:
โ”‚
โ†’ Extract equation name
โ†’ Lookup final product by barcode
โ†’ Validate equation name uniqueness
โ”‚ โ””โ”€โ†’ Process up to 6 raw materials โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Import Valid Equations
- Insert equation header
- Insert raw material components
- Use transaction control
- Skip invalid/duplicate entries
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty) or `do=showadd`Default actionDisplay add equation form
`do=add``add()`Create new production equation
`do=edit&id={id}`Edit displayLoad equation for modification
`do=update``update()`Update existing equation
`do=details&id={id}`Details viewDisplay equation components
`do=show``show()`List all production equations
`do=stop&id={id}``stop_active()`Toggle equation active status
`do=delete&id={id}``delete()`Delete equation and components
`do=addexcel`Excel formShow Excel upload form
`do=addfromexcel``addFromExcel()`Process Excel import
### Required Parameters by Action

Add Equation (do=add):

Excel Import (do=addfromexcel):

---

๐Ÿงฎ Calculation Methods

Percentage Validation

$totalpercentage = 0;
for ($i = 1; $i <= $rowitr; $i++) {
    if (isset($_POST["input" . $i . ""]) && !empty($_POST["input" . $i . ""])) {
        $Productionrateproduct->rate = $_POST["input" . $i . ""];
    } else {
        $Productionrateproduct->rate = 0;
    }
    $totalpercentage += $Productionrateproduct->rate;
}

// Must equal 100%
if ($totalpercentage == 100) {
    // Proceed with equation creation
}

Size/Color Variant Parsing

if (strpos($productId, "hasSizeColor") !== false) {
    $productIdComplex = explode('-', str_replace("hasSizeColor", "", $productId));
    $productId = $productIdComplex[0];
    $sizeId = $productIdComplex[1];
    $colorId = $productIdComplex[2];
}

---

๐Ÿ”’ Security & Permissions

Authentication Requirements

include_once("../public/authentication.php");

Input Validation

Transaction Control (Excel Import)

$mytransactions = new Transaction();
try {
    // Import operations
    $mytransactions->commit();
} catch (Exception $e) {
    $mytransactions->rollback();
}

---

๐Ÿ“Š Performance Considerations

Database Optimization Tips

1. Indexes Required:

- productionrate(finalName, sizeid, colorid)

- productionrateproduct(productionRateId)

- productionrateproduct(productId)

2. Query Optimization:

- Batch deletion for updates

- Efficient Excel processing with validation

- Product lookup optimization

Memory Management

---

๐Ÿ› Common Issues & Troubleshooting

1. Percentage Validation Errors

Issue: "ู„ุงุจุฏ ุงู† ูŠูƒูˆู† ู…ุฌู…ูˆุน ุงู„ู†ุณุจ ูŠุณุงูˆู‰ 100" (Total percentages must equal 100%)

Cause: Raw material percentages don't sum to exactly 100%

Debug:

// Check percentage calculation
$totalpercentage = 0;
for ($i = 1; $i <= $rowitr; $i++) {
    echo "Row $i: " . $_POST["input" . $i . ""] . "<br>";
    $totalpercentage += (float)$_POST["input" . $i . ""];
}
echo "Total: $totalpercentage";

2. Excel Import Failures

Issue: Excel equations not importing correctly

Cause: Format mismatch or invalid product barcodes

Debug:

-- Check product existence by barcode
SELECT productId, productName FROM product WHERE productParcode = 'BARCODE123';

-- Check equation name uniqueness
SELECT COUNT(*) FROM productionrate WHERE name = 'EQUATION_NAME';

3. Size/Color Variant Issues

Issue: Products with variants not saving correctly

Cause: Incorrect hasSizeColor encoding

Fix:

// Proper encoding format: "hasSizeColor{productId}-{sizeId}-{colorId}"
if (strpos($productId, "hasSizeColor") !== false) {
    $productIdComplex = explode('-', str_replace("hasSizeColor", "", $productId));
    if (count($productIdComplex) == 3) {
        $productId = $productIdComplex[0];
        $sizeId = $productIdComplex[1]; 
        $colorId = $productIdComplex[2];
    }
}

---

๐Ÿงช Testing Scenarios

Test Case 1: Basic Equation Creation

1. Create equation with 3 raw materials
2. Set percentages: 60%, 30%, 10%
3. Verify total equals 100%
4. Save and verify database records
5. Check equation appears in listing

Test Case 2: Excel Import Validation

1. Prepare Excel file with valid equations
2. Include duplicate names (should skip)
3. Include invalid product barcodes (should skip)
4. Import and verify only valid equations created
5. Check transaction rollback on errors

Test Case 3: Size/Color Variant Handling

1. Create equation for product with size/color variants
2. Verify hasSizeColor encoding works
3. Check edit form loads variants correctly
4. Update equation and verify variants preserved

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur

โ†‘