DamagedProduct Documentation
Damaged Product Controller Documentation
File: /controllers/damagedProductController.php
Purpose: Manages damaged product tracking in manufacturing operations with automatic inventory adjustments
Last Updated: December 20, 2024
Total Functions: 12+
Lines of Code: ~569
---
๐ Overview
The Damaged Product Controller handles tracking of damaged products during manufacturing processes. It manages:
- โข Damaged product registration and tracking
- โข Automatic raw material deduction from inventory
- โข Production order integration
- โข Store quantity management
- โข Cost tracking and reporting
- โข Manufacturing rate calculations
- โข Store movement reporting
- โข Multi-unit product handling
Primary Functions
- โ Register damaged products during production
- โ Automatically deduct raw materials from inventory
- โ Track production order relationships
- โ Manage store quantity adjustments
- โ Calculate production rates and costs
- โ Generate inventory movement reports
- โ Handle multi-unit product conversions
- โ Maintain audit trails for all changes
- โ Edit and update damaged product records
- โ Delete damaged product entries
Related Controllers
- โข productionOrderController.php - Manufacturing orders
- โข storeController.php - Inventory management
- โข productController.php - Product management
- โข storereportController.php - Inventory reporting
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **damagedproduct** | Damaged product records | damagedProductId, productId, measurUnitId, productQuantity, storeId, cost, operationDate, userID, productionOrderId | |
| **storedetail** | Store inventory levels | storedetailid, storeid, productid, productquantity, userid, storedetaildate | |
| **storereport** | Inventory movement log | productid, storeid, productquantity, productbefore, productafter, storereporttype, storereportmodelid, processname, tablename |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **productionorder** | Manufacturing orders | productionOrderId, productId, quantity, status | |
| **productionrate** | Production recipes/rates | productionRateId, finalProductId, rateName | |
| **productionrateproduct** | Recipe components | productId, productionRateId, quantity, unitId |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **store** | Warehouse locations | storeid, storename, description | |
| **product** | Product master data | productid, productname, productcode | |
| **unit** | Measurement units | unitid, unitname, unitcode | |
| **productunit** | Product-unit conversions | productid, unitid, productnumber | |
| **programsettings** | System configuration | programsettingsid, settingkey, settingvalue |
๐ Key Functions
1. Default Action (empty $do) - Add Damaged Product Form
Location: Line 154
Purpose: Display form for registering new damaged products
Process Flow:
1. Load program settings and stores
2. Get production order data by ID
3. Load production order details with product names
4. Display add form template
Template Variables:
- โข
$allStores- Available warehouses - โข
$productionOrderData- Current production order - โข
$orderName- Final product being manufactured
---
2. add() - Register Damaged Product
Location: Line 302
Purpose: Create damaged product record and adjust inventory automatically
Function Signature:
function add()
Process Flow:
1. Extract form data (product, unit, quantity, store, cost)
2. Create damaged product record
3. Critical: Load production rate for the product
4. For each raw material component:
- Calculate required raw material quantity
- Get current store inventory levels
- Deduct materials from inventory
- Create store movement reports
Key Calculations:
// Convert damaged quantity to base units
$productunitData = loadProductUnitWithProductAndUnit($productId, $unitId);
$myproductNumber = $productunitData->productnumber;
// Calculate raw material deduction
$storeValue = $quantity * $productQuantity * $myproductNumber;
// Update inventory
$newQt = $productquantityBefore - $storeValue;
updateQuantityOfProduct($store, $productId, $newQt);
Automatic Inventory Adjustments:
- โข Loads production rate/recipe for damaged product
- โข Calculates raw material requirements
- โข Deducts proportional raw materials from inventory
- โข Creates audit trail in store reports
---
3. show - Display Damaged Products
Location: Line 198
Purpose: List all damaged products with store information
Features:
- โข Join with store names for readable display
- โข Historical damaged product tracking
- โข Cost calculations and totals
---
4. edit() - Load Product for Editing
Location: Line 515
Purpose: Retrieve damaged product data for modification
Function Signature:
function edit()
Returns: Damaged product object with all fields
---
5. update() - Update Damaged Product
Location: Line 529
Purpose: Modify existing damaged product record
Process Flow:
1. Extract updated form data
2. Update damaged product properties
3. Save changes to database
Note: Does not re-calculate inventory adjustments
---
6. delete() - Remove Damaged Product
Location: Line 560
Purpose: Delete damaged product record
Warning: Physical deletion without inventory reversal
---
๐ Core Business Logic Functions
7. decreaseProductQuantity() - Inventory Reduction
Location: Line 413
Purpose: Reduce product quantity in store inventory
Function Signature:
function decreaseProductQuantity($storedetailId, $productquantityBefore, $productChangeAmount)
---
8. increaseProductQuantity() - Inventory Addition
Location: Line 481
Purpose: Increase product quantity (for reversals)
---
9. updateQuantityOfProduct() - Direct Store Update
Location: Line 436
Purpose: Update store quantity directly via Extended DAO
---
10. insertStorereport() - Movement Tracking
Location: Line 444
Purpose: Create audit trail for inventory movements
Function Signature:
function insertStorereport($productid, $storeid, $productChangeAmount, $productbefore, $productafter, $storereporttype, $storereportmodelid, $processname, $tablename)
Parameters:
- โข
$storereporttype: 1 = Decrease, 0 = Increase - โข
$storereportmodelid: Reference to damaged product ID - โข
$processname: Description in Arabic - โข
$tablename: Source controller name
---
11. getStoredetailData() - Inventory Query
Location: Line 464
Purpose: Retrieve current inventory levels for product/store combination
Returns: Array with store detail object, ID, and current quantity
---
12. loadProductUnitWithProductAndUnit() - Unit Conversion
Location: Line 503
Purpose: Get conversion factor between different product units
---
๐ Workflows
Workflow 1: Register Damaged Product
---
Workflow 2: Inventory Impact Calculation
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) | Default action | Show add damaged product form | |
| `do=add` | `add()` | Register new damaged product | |
| `do=show` | - | Display all damaged products | |
| `do=edit` | `edit()` | Load product for editing | |
| `do=update` | `update()` | Save product changes | |
| `do=delete` | `delete()` | Remove damaged product |
Add Form: id - Production order ID
Edit: id - Damaged product ID
Add Action: productId, unitId, productQuantity, store, cost, id
---
๐งฎ Critical Calculations
Raw Material Deduction Formula
// For each raw material in production rate:
$quantity = $Productionrateproduct[$i]->quantity; // Base quantity per unit
$productQuantity = $_POST['productQuantity']; // Damaged quantity
$myproductNumber = $productunitData->productnumber; // Unit conversion
$storeValue = $quantity * $productQuantity * $myproductNumber;
// Update inventory
$newQt = $productquantityBefore - $storeValue;
Unit Conversion Logic
$productunitData = loadProductUnitWithProductAndUnit($productId, $unitId);
$productnumber = $productunitData->productnumber;
$finalquantity = $productQuantity * $productnumber;
---
๐ Security & Business Rules
Data Integrity
- โข Automatic inventory adjustments prevent manual errors
- โข Audit trails track all inventory movements
- โข Production order linkage maintains traceability
Validation Points
- โข Production order must exist
- โข Sufficient inventory must be available
- โข Valid product-unit combinations required
- โข Store locations must be valid
---
๐ Performance Considerations
Database Impact
- โข Multiple inventory updates per damaged product
- โข Extensive DAO operations for complex calculations
- โข Production rate queries for each operation
Optimization Opportunities
1. Batch inventory updates for multiple materials
2. Cache production rates during session
3. Pre-calculate unit conversions
---
๐ Common Issues & Troubleshooting
1. Inventory Goes Negative
Issue: Raw material quantities become negative after damaged product entry
Cause: Insufficient raw materials in inventory
Solution: Validate inventory availability before processing
2. Missing Production Rates
Issue: Error when no production rate exists for product
Cause: Production rate not configured for manufactured item
Solution: Create production rate or handle gracefully
3. Unit Conversion Errors
Issue: Incorrect quantity calculations
Cause: Missing or incorrect productunit records
Debug: Verify productunit table has conversion factors
---
๐งช Testing Scenarios
Test Case 1: Basic Damaged Product Entry
1. Create production order for known product
2. Enter damaged product with valid quantity
3. Verify damaged product record created
4. Check raw material deductions occurred
5. Validate store report entries created
Test Case 2: Unit Conversion Testing
1. Use product with multiple units (kg, g, piece)
2. Enter damaged quantity in non-base unit
3. Verify correct conversion applied
4. Check inventory deducted correctly
Test Case 3: Production Rate Integration
1. Configure production rate with multiple materials
2. Enter damaged product
3. Verify each raw material deducted proportionally
4. Check all materials have store reports
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข productionOrderController.php - Manufacturing operations
- โข storeController.php - Inventory management
- โข storereportController.php - Inventory reporting
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur