Stage Documentation

Stage Controller Documentation

File: /controllers/stageController.php

Purpose: Manages production stages/workflows with operation sequencing and timing

Last Updated: December 21, 2024

Total Functions: 6

Lines of Code: ~421

---

๐Ÿ“‹ Overview

The Stage Controller is a production management module designed for manufacturing workflow control. It manages production stages that define sequences of operations required to manufacture products. The system handles:

This controller is essential for:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**stage**Production stage definitionsstageId, stagename, productId, totalhour, conditions, date, userid
**stagestep**Operation sequences within stagesstagestepId, stageId, operationId, stepno
**settingoperation**Available operationssettingoperationId, operationname, realTime
**product**Products requiring production stagesproductid, productname
### Supporting Tables

Table NamePurposeKey Columns
**youtubelink**Tutorial video linksyoutubelinkid, title, url
---

๐Ÿ”‘ Key Functions

1. Default Action - Add Stage Form

Location: Line 109

Purpose: Display stage creation form with product and operation data

Process Flow:

1. Include authentication check

2. Load all products for selection dropdown

3. Load all available operations for stage building

4. Display stage creation template

Data Loading:

$allproduct = loadproduct();      // Available products
$alloperation = loadopration();   // Available operations
$smarty->assign("allproduct", $allproduct);
$smarty->assign("alloperation", $alloperation);

Template: stageview/add.html

---

2. add() - Create New Production Stage

Location: Line 239

Purpose: Create production stage with multiple operation steps

Function Signature:

function add() {
    global $StagestepDAO, $Stagestep;
    global $StageDAO, $Stage;
}

Process Flow:

1. Extract stage details from POST data

2. Create stage master record

3. Process dynamic operation sequence from form

4. Insert each operation step with sequence numbers

5. Calculate total production hours

Stage Creation:

$Stage->conditions = 0;                    // Active status
$Stage->date = date("Y-m-d");
$Stage->userid = $_SESSION['userid'];
$Stage->productId = $productid;
$Stage->stagename = $stagename;
$Stage->totalhour = $stagehour;

$stageno = $StageDAO->insert($Stage);      // Returns new stage ID

Dynamic Operation Processing:

$operationitr = $_POST["operationitr"];    // Number of operations

for ($h = 1; $h <= $operationitr; $h++) {
    $operationname = $_POST['oprationname' . $h . ''];
    $no = $_POST['no' . $h . ''];          // Step sequence number
    
    if (isset($operationname) && $operationname != -1) {
        $Stagestep->operationId = $operationname;
        $Stagestep->stageId = $stageno;
        $Stagestep->stepno = $no;
        
        $StagestepDAO->insert($Stagestep);
    }
}

---

3. show - Display Stages with Operation Counts

Location: Line 116

Purpose: Show all stages with operation count summaries

Process Flow:

1. Load YouTube tutorial links

2. Query all stages from database

3. For each stage, count associated operations

4. Assign enhanced stage data to template

Operation Count Enhancement:

$allstage = $StageDAO->queryAll();
foreach ($allstage as $newstage) {
    $totaloperation = $StagestepDAO->queryByStageId($newstage->stageId);
    $newstage->countoperation = count($totaloperation);  // Add operation count
}
$smarty->assign("allstage", $allstage);

Template: stageview/show.html

---

4. update() - Edit Existing Stage

Location: Line 278

Purpose: Update stage details and reorganize operation sequence

Process Flow:

1. Update stage master record with new details

2. Delete all existing stage steps for clean rebuild

3. Process new operation sequence from form

4. Re-insert operations with updated sequence

5. Handle both new and existing step IDs

Stage Update:

$Stage->conditions = 0;
$Stage->date = date("Y-m-d");
$Stage->userid = $_SESSION['userid'];
$Stage->productId = $productid;
$Stage->stagename = $stagename;
$Stage->totalhour = $stagehour;
$Stage->stageId = $stageid;

$StageDAO->update($Stage);

Operation Sequence Rebuild:

$StagestepDAO->deleteByStageId($stageid);  // Clean slate

$operationitr = $_POST["operationitr"];
for ($h = 1; $h <= $operationitr; $h++) {
    $operationname = $_POST['oprationname' . $h . ''];
    $no = $_POST['no' . $h . ''];
    $stagestepid = $_POST['stagestep' . $h . ''];    // Existing step ID
    
    if (!empty($operationname) && $operationname != '-1' && !empty($no)) {
        $Stagestep->operationId = $operationname;
        $Stagestep->stageId = $stageid;
        $Stagestep->stepno = $no;
        $Stagestep->stagestepId = $stagestepid;
        
        if (isset($stagestepid) && $stagestepid > 0) {
            $StagestepEX->insertwithid($Stagestep);    // Preserve ID
        } else {
            $StagestepDAO->insert($Stagestep);         // New step
        }
    }
}

---

5. delete() - Permanent Deletion with Validation

Location: Line 389

Purpose: Permanently delete stage after dependency validation

Function Signature:

function delete($stageId) {
    global $StagestepDAO, $StageDAO, $StageEX;
}

Process Flow:

1. Check for associated operations/dependencies

2. If no dependencies exist, allow deletion

3. If dependencies found, prevent deletion

4. Return status code for UI handling

Dependency Validation:

$alloperation = $StagestepDAO->queryByStageId($stageId);
if (count($alloperation) <= 0) {
    $operationdeleteValid = 0;          // Safe to delete
    $StageDAO->delete($stageId);
    $note = "success";
} else {
    $operationdeleteValid = 1;          // Has dependencies
    $note = "ู„ุง ูŠู…ูƒู† ุญุฐู ู‡ุฐู‡ ุงู„ู…ุฑุญู„ู‡";    // Arabic: Cannot delete this stage
}

return array($note, $operationdeleteValid);

---

6. Soft Delete Functions - Hide/Restore Operations

Locations: Lines 331, 360

Purpose: Temporarily hide stages without losing data

deletetemp() - Hide Stage:

function deletetemp($stageId) {
    global $Stage, $StageEX;
    
    $Stage->conditions = 1;             // Mark as hidden
    $Stage->date = date("Y-m-d");
    $Stage->userid = $_SESSION["userid"];
    $Stage->stageId = $stageId;
    
    $StageEX->updateConditions($Stage);
}

returndelete() - Restore Stage:

function returndelete($stageId) {
    global $Stage, $StageEX;
    
    $Stage->conditions = 0;             // Mark as active
    $Stage->date = date("Y-m-d");
    $Stage->userid = $_SESSION["userid"];
    $Stage->stageId = $stageId;
    
    $StageEX->updateConditions($Stage);
}

---

๐Ÿ”„ Workflows

Workflow 1: Production Stage Creation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Create Production Stage
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Display Stage Creation Form
- Load available products
- Load available operations
- Set up dynamic operation sequence builder
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Configure Stage Details
- Select target product
- Enter stage name/description
- Add operations in sequence
- Set step numbers for ordering
- Calculate total production hours
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Execute add() Function
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ A. Create Stage Master Record
โ”‚ - Insert into stage table
โ”‚ - Get new stage ID
โ”‚ - Set metadata (user, date)
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ B. Process Operation Sequence
โ”‚ - Loop through dynamic form fields
โ”‚ - Validate operation selections
โ”‚ - Insert each step with sequence number
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Validate and Finalize
- Verify all operations added successfully
- Calculate total stage time
- Redirect to success page
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Workflow 2: Stage Update and Operation Management

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Edit Production Stage
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Load Existing Stage Data
- Query stage master record
- Load current operation sequence
- Calculate operation counts and times
- Populate edit form with existing data
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Modify Stage Configuration
- Update stage name/product association
- Add/remove/reorder operations
- Adjust step sequence numbers
- Recalculate production timing
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Execute update() Function
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ A. Update Master Record
โ”‚ - Modify stage table entry
โ”‚ - Update metadata fields
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ B. Rebuild Operation Sequence
โ”‚ - Delete all existing steps
โ”‚ - Process new operation configuration
โ”‚ - Re-insert steps with new sequence
โ”‚ - Preserve step IDs where possible
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Validate Changes and Update Display
- Verify operation sequence integrity
- Update stage listing with new data
- Display success confirmation
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)DefaultDisplay stage creation form
`do=add``add()`Process new stage creation
`do=show`Display listingShow all stages with operation counts
`do=edit`Edit formDisplay stage edit form with existing data
`do=update``update()`Process stage modifications
`do=delete``delete()`Permanently delete stage (with validation)
`do=deletetemp``deletetemp()`Soft delete (hide) stage
`do=returndelete``returndelete()`Restore soft deleted stage
`do=sucess`Success pageDisplay operation success message
`do=error`Error pageDisplay operation error message
### Required Parameters by Action

Add Stage (do=add):

Edit Stage (do=edit):

Update Stage (do=update):

Delete Stage (do=delete, do=deletetemp, do=returndelete):

---

๐Ÿงฎ Calculation Methods

Operation Count Calculation

// Count operations per stage for display
foreach ($allstage as $newstage) {
    $totaloperation = $StagestepDAO->queryByStageId($newstage->stageId);
    $newstage->countoperation = count($totaloperation);
}

Dynamic Form Processing

// Process variable number of operations
$operationitr = $_POST["operationitr"];    // Total operations to process

for ($h = 1; $h <= $operationitr; $h++) {
    $operationname = $_POST['oprationname' . $h . ''];   // Dynamic field name
    $no = $_POST['no' . $h . ''];                        // Step sequence
    
    if (isset($operationname) && $operationname != -1) {
        // Process this operation step
    }
}

Total Hour Calculation

// Stage total hours (from form input)
$stagehour = $_POST['totaloperationhour1'];
$Stage->totalhour = $stagehour;

// Individual operation times loaded from settingoperation table
$steprow = $SettingoperationEX->loadByopertaionid($new->operationId);
$new->newstage = $steprow->realTime;  // Individual operation time

Dependency Validation

// Check if stage can be safely deleted
$alloperation = $StagestepDAO->queryByStageId($stageId);
if (count($alloperation) <= 0) {
    $operationdeleteValid = 0;      // No dependencies
    $StageDAO->delete($stageId);    // Safe to delete
} else {
    $operationdeleteValid = 1;      // Has dependencies
    // Prevent deletion
}

---

๐Ÿ”’ Security & Permissions

Authentication Requirements

Input Validation

// Direct POST access - framework-level sanitization expected
$stagename = $_POST['setoperation'];
$productid = $_POST['productname'];
$stagehour = $_POST['totaloperationhour1'];

// Dynamic field processing with loop validation
for ($h = 1; $h <= $operationitr; $h++) {
    if (isset($operationname) && $operationname != -1) {
        // Only process valid operations
    }
}

Data Integrity

// Audit trail with user tracking
$Stage->userid = $_SESSION['userid'];
$Stage->date = date("Y-m-d");

// Soft delete preserves data
$Stage->conditions = 1;  // Hide instead of hard delete

// Dependency checking prevents data corruption
if (count($alloperation) <= 0) {
    // Safe to delete
} else {
    // Prevent deletion
}

---

๐Ÿ“Š Performance Considerations

Database Optimization Tips

1. Indexes Required:

- stage(productId) for product-based filtering

- stage(conditions) for active/deleted filtering

- stagestep(stageId) for operation lookups

- stagestep(operationId) for reverse lookups

- stage(userid) for user-based queries

2. Query Optimization:

- Operation counts calculated at display time

- Efficient DAO-based operations

- Minimal recursive queries

3. Update Strategy:

- Delete/rebuild approach for operation sequences

- May be inefficient for large operation lists

- Consider differential update for production systems

Known Performance Issues

// Delete/rebuild approach in update()
$StagestepDAO->deleteByStageId($stageid);  // Delete all steps
// Then rebuild all steps - inefficient for minor changes

// Operation count queries in loop
foreach ($allstage as $newstage) {
    $totaloperation = $StagestepDAO->queryByStageId($newstage->stageId);
    // Separate query for each stage - N+1 problem
}

---

๐Ÿ› Common Issues & Troubleshooting

1. Operation Sequence Inconsistencies

Issue: Step numbers don't reflect actual sequence

Cause: Manual step numbering without validation

Debug:

-- Check for duplicate step numbers
SELECT stageId, stepno, COUNT(*) 
FROM stagestep 
GROUP BY stageId, stepno 
HAVING COUNT(*) > 1;

-- Check for gaps in sequence
SELECT stageId, stepno FROM stagestep 
WHERE stageId = ? ORDER BY stepno;

2. Edit Form Data Loading Issues

Issue: Existing operations not loading in edit form

Cause: Complex data preparation for dynamic forms

Debug:

// Verify stage step loading
$stagerow = $StagestepDAO->queryByStageId($stageId);
foreach ($stagerow as $new) {
    echo "Step: " . $new->stepno . " Operation: " . $new->operationId . "<br>";
}

3. Dynamic Form Processing Failures

Issue: Some operations not saving during add/update

Cause: JavaScript form generation vs PHP processing mismatch

Fix:

// Debug dynamic form processing
$operationitr = $_POST["operationitr"];
echo "Processing $operationitr operations:<br>";

for ($h = 1; $h <= $operationitr; $h++) {
    $operationname = $_POST['oprationname' . $h . ''];
    echo "Operation $h: $operationname<br>";
    
    if (!isset($operationname) || $operationname == -1) {
        echo "Skipping invalid operation $h<br>";
    }
}

4. Delete Validation Logic

Issue: Deletion prevention not working correctly

Cause: Complex dependency checking logic

Enhancement:

// Improve delete validation
function canDeleteStage($stageId) {
    global $StagestepDAO;
    
    $dependencies = array();
    
    // Check stage steps
    $steps = $StagestepDAO->queryByStageId($stageId);
    if (count($steps) > 0) {
        $dependencies[] = "Has " . count($steps) . " operation steps";
    }
    
    // Check production schedules, work orders, etc.
    // Add other dependency checks here
    
    return $dependencies;
}

---

๐Ÿงช Testing Scenarios

Test Case 1: Basic Stage Creation

1. Access stage creation form
2. Select product "Widget ABC"
3. Enter stage name "Assembly Process"
4. Add 3 operations in sequence:
   - Step 1: Preparation (10 min)
   - Step 2: Assembly (30 min) 
   - Step 3: Quality Check (15 min)
5. Set total hours to 55 minutes
6. Submit and verify database records

Test Case 2: Complex Stage Update

1. Edit existing 3-operation stage
2. Remove middle operation
3. Add new operation at end
4. Reorder remaining operations
5. Verify sequence integrity
6. Check operation count updates

Test Case 3: Delete Validation

1. Create stage with operations
2. Attempt permanent deletion
3. Verify prevention due to dependencies
4. Test soft delete functionality
5. Verify restore capability
6. Remove all operations, then test permanent delete

Test Case 4: Dynamic Form Processing

1. Create stage with maximum operations (test limits)
2. Use JavaScript to add/remove operations dynamically
3. Verify form submission handles variable operation count
4. Test with gaps in operation sequence
5. Validate step numbering consistency

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When production workflow requirements change