ProductionRate Documentation
Production Rate Controller Documentation
File: /controllers/productionRateController.php
Purpose: Manages production rate formulas defining percentage-based raw material requirements for manufacturing
Last Updated: December 20, 2024
Total Functions: 8
Lines of Code: ~467
---
๐ Overview
The Production Rate Controller manages percentage-based manufacturing formulas that define the exact proportional requirements of raw materials needed to produce finished goods. Unlike the Production Equation Controller which uses fixed quantities, this controller focuses on:
- โข Percentage-based material allocation (must total 100%)
- โข Production rate formulas for manufacturing
- โข Raw material ratio management
- โข Manufacturing recipe percentage validation
- โข Size/color variant support in formulas
- โข Production planning based on rates
Primary Functions
- โ Create production rate formulas with percentage allocations
- โ Validate that material percentages sum to exactly 100%
- โ Manage raw material to finished product ratios
- โ Product size/color variant support in formulas
- โ Edit and update existing rate formulas
- โ Activate/deactivate production rate formulas
- โ Delete rate records with cascading rules
- โ Production rate listing and management
Related Controllers
- โข productionEquationController.php - Fixed quantity equations
- โข productionOutController.php - Simple manufacturing
- โข productionExecutionController.php - Production execution
- โข productController.php - Product master data
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **productionrate** | Production rate headers | productionRateId, name, finalName, sizeid, colorid, conditions, thedate, userId | |
| **productionrateproduct** | Raw material components with percentages | productionRateProductId, productionRateId, productId, sizeid, colorid, quantity, rate, unitId, thedate, userId |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **product** | Product master data | productId, productName, productCatId | |
| **productcat** | Product categories | productCatId, productCatName | |
| **productunit** | Product units and conversions | productunitid, productid, unitname, productnumber | |
| **programsettings** | System configuration | programsettingsid, settingkey, settingvalue | |
| **youtubelink** | Tutorial links | youtubelinkid, title, url |
๐ Key Functions
1. Default Action - Add Production Rate Form
Location: Line 108
Purpose: Display form for creating new production rate formulas
Function Signature:
// Triggered when: do=showadd or empty $do
Process Flow:
1. Check user authentication
2. Load program settings for display
3. Set production rate template flag
4. Display add form template
Features:
- โข Program settings integration
- โข Custom production rate flag
- โข Simple form interface for percentage-based formulas
---
2. add() - Create Production Rate Formula
Location: Line 330
Purpose: Validate percentage totals and save new production rate formulas
Function Signature:
function add()
Process Flow:
1. Extract final product information (handle size/color variants)
2. Calculate total percentages from all components
3. Validate that total equals exactly 100%
4. Create production rate header record
5. Call addproductionrateproducts() for components
6. Return percentage total for validation
Percentage 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 rate formula creation
$productionrateid = $productionrateDAO->insert($productionrate);
addproductionrateproducts($productionrateid);
}
return $totalpercentage;
Size/Color Variant 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 272
Purpose: Save individual raw material percentage entries for a production rate formula
Function Signature:
function addproductionrateproducts($productionrateid)
Process Flow:
1. Loop through posted component data
2. Validate component has percentage value
3. Extract product ID (handle size/color encoding)
4. Validate product and unit selections
5. Insert or update production rate product records
6. Handle quantity and percentage data
Component Processing Logic:
for ($i = 1; $i <= $rowitr; $i++) {
if (strlen($_POST["input" . $i . ""]) <= 0) {
// Skip empty percentage entries
} else {
if (isset($_POST["product" . $i . ""]) && $_POST["product" . $i . ""] != -1) {
if (isset($_POST["unitId" . $i . ""]) && !empty($_POST["unitId" . $i . ""])) {
// Process valid component
$Productionrateproduct->rate = $_POST["input" . $i . ""];
$Productionrateproduct->quantity = $_POST["inputt" . $i . ""] ?? 0;
if (isset($productionRateProductId) && $productionRateProductId > 0) {
$ProductionrateproductEX->insertwithid($Productionrateproduct);
} else {
$ProductionrateproductDAO->insert($Productionrateproduct);
}
}
}
}
}
---
4. edit() - Edit Production Rate Formula
Location: Line 143
Purpose: Load existing rate formula for modification
Process Flow:
1. Load production rate and associated product components
2. Build display names with category/size/color information
3. Load product unit data for each component
4. Handle size/color variant display encoding
5. Assign data to edit template
Product Unit Loading:
$i = 1;
foreach ($Productionrateproduct as $p) {
$productUnitData = $productUnitExt->queryWithProductId($p->productId);
$smarty->assign("productUnitData" . $i, $productUnitData);
$product = $productExt->loadSizeColor($p->productId, $p->sizeid, $p->colorid);
$productCat = $productCatDAO->load($product->productCatId);
$p->productName = $product->productName . '/' . $productCat->productCatName;
if (!is_null($product->sizeName) && !empty($product->sizeName) &&
!is_null($product->colorName) && !empty($product->colorName)) {
$p->productId = "hasSizeColor" . $product->productId . "-" . $product->sizeid . "-" . $product->colorid;
$p->productName .= '/' . $product->sizeName . '/' . $product->colorName;
}
$i++;
}
Final Product Display:
$product = $productExt->loadSizeColor($productionrate->finalName, $productionrate->sizeid, $productionrate->colorid);
$productCat = $productCatDAO->load($product->productCatId);
$product->productName = $product->productName . '/' . $productCat->productCatName;
if (!is_null($product->sizeName) && !empty($product->sizeName) &&
!is_null($product->colorName) && !empty($product->colorName)) {
$productionrate->finalName = "hasSizeColor" . $product->productId . "-" . $product->sizeid . "-" . $product->colorid;
$product->productName .= '/' . $product->sizeName . '/' . $product->colorName;
}
---
5. update() - Update Production Rate Formula
Location: Line 209
Purpose: Modify existing production rate formulas with percentage validation
Function Signature:
function update()
Process Flow:
1. Re-validate percentage totals (must equal 100%)
2. Update header record with new data
3. Delete existing component records
4. Re-insert updated components
5. Handle size/color variants
Update Process:
// Re-validate percentages
$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) {
// Update header
$productionrateDAO->update($productionrate);
// Delete and recreate components
$ProductionrateproductDAO->deleteByProductionRateId($productionrate->productionRateId);
addproductionrateproducts($productionrate->productionRateId);
}
---
6. show() - List All Production Rates
Location: Line 227
Purpose: Display list of all production rate formulas
Process Flow:
1. Load all production rate records
2. Assign to template for display
3. Display listing template
Simple listing functionality without filtering or search capabilities.
---
7. stop_active() - Toggle Rate Formula Status
Location: Line 441
Purpose: Activate or deactivate production rate formulas
Function Signature:
function stop_active()
Toggle Logic:
$id = $_GET["id"];
$productionrate = $productionrateDAO->load($id);
if ($productionrate->conditions == 0) {
$productionrate->conditions = 1; // Deactivate
} elseif ($productionrate->conditions == 1) {
$productionrate->conditions = 0; // Activate
}
$productionrateDAO->update($productionrate);
---
8. delete() - Delete Production Rate Formula
Location: Line 457
Purpose: Remove rate formula and all associated components
Function Signature:
function delete()
Process Flow:
1. Delete all component records by production rate ID
2. Delete header record
3. Cascading deletion ensures data integrity
Deletion Logic:
$id = $_GET["id"];
print_r($id); // Debug output
$ProductionrateproductDAO->deleteByProductionRateId($id);
$productionrateDAO->delete($id);
---
๐ Workflows
Workflow 1: Creating Percentage-Based Production Rate
---
Workflow 2: Editing Production Rate Formula
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) or `do=showadd` | Default action | Display add rate formula form | |
| `do=add` | `add()` | Create new production rate formula | |
| `do=edit&id={id}` | Edit display | Load rate formula for modification | |
| `do=update` | `update()` | Update existing rate formula | |
| `do=show` | `show()` | List all production rate formulas | |
| `do=stop&id={id}` | `stop_active()` | Toggle rate formula active status | |
| `do=delete&id={id}` | `delete()` | Delete rate formula and components |
Add Rate Formula (do=add):
- โข
name- Formula name - โข
finalProductId- Final product (may include hasSizeColor encoding) - โข
rowitr- Number of raw material components - โข
product{i}- Raw material product IDs - โข
unitId{i}- Unit of measure IDs - โข
inputt{i}- Optional quantities - โข
input{i}- Percentage rates (must sum to 100%)
Update Formula (do=update):
- โข
productionrateid- ID of formula to update - โข Same parameters as add operation
---
๐งฎ 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;
}
// Validation: Must equal exactly 100%
if ($totalpercentage == 100) {
// Proceed with formula creation/update
} else {
// Display error: "ูุงุจุฏ ุงู ูููู ู
ุฌู
ูุน ุงููุณุจ ูุณุงูู 100"
// (Total percentages must equal 100)
}
Size/Color Variant Processing
// Encoding: "hasSizeColor{productId}-{sizeId}-{colorId}"
if (strpos($productId, "hasSizeColor") !== false) {
$productIdComplex = explode('-', str_replace("hasSizeColor", "", $productId));
$productId = $productIdComplex[0];
$sizeId = $productIdComplex[1];
$colorId = $productIdComplex[2];
}
// Display name building
$product = $productExt->loadSizeColor($productId, $sizeId, $colorId);
$productCat = $productCatDAO->load($product->productCatId);
$displayName = $product->productName . '/' . $productCat->productCatName;
if (!is_null($product->sizeName) && !empty($product->sizeName) &&
!is_null($product->colorName) && !empty($product->colorName)) {
$displayName .= '/' . $product->sizeName . '/' . $product->colorName;
}
---
๐ Security & Permissions
Authentication Requirements
include_once("../public/authentication.php");
Input Validation
- โข Percentage validation (must equal 100%)
- โข Product IDs validated against database
- โข Unit IDs must be valid for selected products
- โข Numeric values properly cast and validated
- โข Required field validation
Data Integrity
- โข Cascading deletion for components
- โข Percentage total enforcement
- โข Product-unit relationship validation
- โข Size/color variant consistency
---
๐ Performance Considerations
Database Optimization Tips
1. Indexes Required:
- productionrate(finalName, sizeid, colorid)
- productionrateproduct(productionRateId)
- productionrateproduct(productId)
2. Query Optimization:
- Batch deletion for updates
- Efficient product/unit data loading
- Minimal database round trips
Memory Management
- โข Component arrays processed incrementally
- โข Product unit data loaded per component
- โข Proper variable cleanup
---
๐ Common Issues & Troubleshooting
1. Percentage Validation Errors
Issue: Error message "ูุงุจุฏ ุงู ูููู ู ุฌู ูุน ุงููุณุจ ูุณุงูู 100" appears
Cause: Component percentages don't sum to exactly 100%
Debug:
// Check percentage calculation
$totalpercentage = 0;
for ($i = 1; $i <= $rowitr; $i++) {
$rate = (float)$_POST["input" . $i . ""];
echo "Component $i: $rate%<br>";
$totalpercentage += $rate;
}
echo "Total: $totalpercentage%<br>";
// Exact match check
if ($totalpercentage == 100) {
echo "Valid total";
} else {
echo "Invalid total - must be exactly 100%";
}
2. Size/Color Variant Issues
Issue: Products with variants not displaying correctly in edit form
Cause: Incorrect hasSizeColor encoding or null checks
Fix:
// Proper null checking
if (!is_null($product->sizeName) && !empty($product->sizeName) &&
!is_null($product->colorName) && !empty($product->colorName)) {
$productId = "hasSizeColor" . $product->productId . "-" . $product->sizeid . "-" . $product->colorid;
$displayName .= '/' . $product->sizeName . '/' . $product->colorName;
}
3. Component Update Issues
Issue: Components not updating correctly
Cause: Delete-and-recreate pattern not working properly
Debug:
-- Check component deletion
SELECT COUNT(*) FROM productionrateproduct WHERE productionRateId = {id};
-- Verify re-insertion
SELECT * FROM productionrateproduct WHERE productionRateId = {id} ORDER BY productionRateProductId;
---
๐งช Testing Scenarios
Test Case 1: Basic Rate Formula Creation
1. Create rate formula with 3 components:
- Material A: 60%
- Material B: 30%
- Material C: 10%
2. Verify total equals 100%
3. Save and check database records
4. Verify formula appears in listing
Test Case 2: Percentage Validation
1. Create formula with invalid percentages:
- Material A: 60%
- Material B: 30%
- Material C: 5%
2. Verify error message appears
3. Adjust to valid percentages (60%, 30%, 10%)
4. Verify successful save
Test Case 3: Size/Color Variant Formula
1. Create formula for "Red Large Shirt"
2. Add components with variants
3. Verify hasSizeColor encoding
4. Edit formula and verify variants preserved
5. Check display names include variant info
Test Case 4: Edit and Update Operations
1. Create rate formula
2. Edit with different percentages
3. Verify update process:
- Old components deleted
- New components inserted
- Percentage validation applied
- Header record updated
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข productionEquationController.md - Fixed quantity equations
- โข productionOutController.md - Simple manufacturing
- โข productController.md - Product master data
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur