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:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**damagedproduct**Damaged product recordsdamagedProductId, productId, measurUnitId, productQuantity, storeId, cost, operationDate, userID, productionOrderId
**storedetail**Store inventory levelsstoredetailid, storeid, productid, productquantity, userid, storedetaildate
**storereport**Inventory movement logproductid, storeid, productquantity, productbefore, productafter, storereporttype, storereportmodelid, processname, tablename
### Production Tables

Table NamePurposeKey Columns
**productionorder**Manufacturing ordersproductionOrderId, productId, quantity, status
**productionrate**Production recipes/ratesproductionRateId, finalProductId, rateName
**productionrateproduct**Recipe componentsproductId, productionRateId, quantity, unitId
### Reference Tables

Table NamePurposeKey Columns
**store**Warehouse locationsstoreid, storename, description
**product**Product master dataproductid, productname, productcode
**unit**Measurement unitsunitid, unitname, unitcode
**productunit**Product-unit conversionsproductid, unitid, productnumber
**programsettings**System configurationprogramsettingsid, 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:

---

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:

---

3. show - Display Damaged Products

Location: Line 198

Purpose: List all damaged products with store information

Features:

---

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:

---

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

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Production Order โ†’ Damaged Product
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Load Production Order Context
- Get production order details
- Load available stores
- Display add form
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2User Enters Damaged Product Data
- Product and unit selection
- Damaged quantity
- Store location
- Cost information
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Create Damaged Product Record
- Insert into damagedproduct table
- Link to production order
- Set audit fields
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Process Raw Material Deductions
FOR EACH raw material in production rate:
โ”‚
โ†’ Calculate required quantity
โ”‚
โ†’ Convert units if needed
โ”‚
โ†’ Get current inventory level
โ”‚
โ†’ Deduct from store inventory
โ”‚
โ”‚ โ””โ”€โ†’ Create inventory movement report โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Return to Production Order
- Redirect to production order receive page
- Update production status if needed
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

Workflow 2: Inventory Impact Calculation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Damaged Product Entry
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Load Production Recipe
- Get production rate by final product
- Load all raw material components
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Calculate Material Requirements
FOR EACH raw material:
โ”‚
โ†’ Base quantity per unit produced
โ”‚
โ†’ ร— Damaged quantity entered
โ”‚
โ†’ ร— Unit conversion factor
โ”‚
โ”‚ โ””โ”€โ†’ = Total raw material to deduct โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Execute Inventory Updates
- Get current store quantities
- Calculate new quantities after deduction
- Update storedetail records
- Create storereport audit entries
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default actionShow 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
### Required Parameters

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

Validation Points

---

๐Ÿ“Š Performance Considerations

Database Impact

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

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur