Productrequest Documentation

Product Request Controller Documentation

File: /controllers/productrequest.php

Purpose: Manages inter-store product transfer requests and inventory redistribution workflows

Last Updated: December 19, 2024

Total Functions: 5

Lines of Code: 367

---

๐Ÿ“‹ Overview

The Product Request Controller manages the complex workflow of product requests between different stores or warehouses within the ERP system. It handles request creation, approval workflows, status tracking, and facilitates inventory redistribution across multiple locations.

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
productrequestMain requests tableproductrequestid, productid, requeststoreid, storeid, status, deleted
### Related Tables (Referenced)

Table NamePurposeRelationship
productProduct informationForeign Key: productrequest.productid
productcatProduct categoriesForeign Key: product.productCatId
storeStore/warehouse infoForeign Key: productrequest.storeid, requeststoreid
userUser store assignmentsForeign Key: user.userstoreid
storedetailInventory quantitiesRelated: store inventory tracking
---

๐Ÿ”ง Key Functions

1. Main Display Logic (do = "" - Default)

Purpose: Displays the product request creation form

Called By: GET request with no 'do' parameter

Business Logic:

1. Get user's assigned store ID from user data

2. If user has no specific store (storeId = 0), show all stores for selection

3. Display add form with appropriate store options

Template Integration:

2. Show Requests (do = "show")

Purpose: Displays all product requests with complex filtering based on user store access

Called By: GET request with do=show

Business Logic Flow:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Get user store ID
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Store ID = 0?
(Admin access)
โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Show ALL
requests:
โ€ข New
โ€ข Accepted
โ€ข Refused
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Request Categories:

- New: p.status = 0

- Refused: p.status = 1

- Accepted: p.status in (2, 3)

Data Enrichment:

For each request, the system adds:

3. Delete Request (do = "delete")

Purpose: Soft deletes a product request

Called By: GET request with do=delete&id={requestId}

Parameters:

Database Operations:

Business Logic:

1. Load existing request record

2. Set deleted flag to 1 (soft delete)

3. Update database record

4. Redirect to success or error page

4. add() Function (Legacy/Placeholder)

Purpose: Originally intended for adding insurance companies (appears to be legacy code)

Status: Contains obsolete code not related to product requests

Note: Function contains insurance company logic that should be cleaned up

5. getProductPath_recursive($parentid, $categories, $level)

Purpose: Builds hierarchical category path for products (e.g., "Electronics/Computers/Laptops")

Parameters:

Returns: string - Complete category path separated by "/"

Business Logic Flow:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Get category data
for parentid
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Category exists AND
level < 2?
โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Add category
name to path
Recurse with
parent ID
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Recursion Safety: Limited to 2 levels deep to prevent infinite loops

Example Output:

getProductPath_recursive(123, "", 0);
// Returns: "Electronics/Computers"

---

๐Ÿ”„ Business Logic Flow

Request Creation Workflow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
User accesses form
โ€ข Store auto-selected
OR store dropdown
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
User selects:
โ€ข Target store
โ€ข Product(s) needed
โ€ข Quantity required
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
System creates request
โ€ข Status = 0 (new)
โ€ข Links stores
โ€ข Records product info
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Multi-Store Request Management

โ””โ”€โ”€ Refused requests (all)
โ”‚ โ””โ”€โ”€ Action: View status, cancel
โ””โ”€โ”€ INCOMING Requests (storeid = X)
โ””โ”€โ”€ Action: Approve, refuse, fulfill

Request Status Workflow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” Approve โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” Fulfill โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
0 = New
(Pending)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1 = Refused
(Rejected)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

โš ๏ธ Common Issues

Legacy Code Cleanup Needed

Issue: add() function contains insurance company code

Impact: Confusion and potential conflicts

Recommendation: Remove or move insurance logic to appropriate controller

Missing CRUD Operations

Issue: No functions for creating, updating, or approving requests

Current State: Only display and delete functionality

Missing Functions:

Category Path Performance

Issue: Recursive function called for each request in loops

Impact: N+1 query problem with large request lists

Recommendation: Cache category paths or use joins

Error Handling

Issue: Limited error handling in delete operation

Risk: Potential data inconsistency

Recommendation: Add transaction support and better validation

---

๐Ÿ”— Dependencies

Required Files

include("../public/impOpreation.php");           // Core operations
include("../library/uploadImages.php");         // Image upload (legacy?)
include("../public/config.php");                // Configuration
include("../public/authentication.php");        // User authentication
include("../public/include_dao.php");           // DAO includes

DAO Classes

// Product-related DAOs
ProductDAO.class.php                            // Product operations
ProductcatDAO.class.php                         // Category operations

// Request-related DAOs  
ProductrequestsMySqlDAO.class.php               // Request operations
ProductrequestsMySqlExtDAO.class.php            // Extended request queries

// Store-related DAOs
StoreDAO.class.php                              // Store operations
StoreMySqlExtDAO.class.php                      // Extended store operations

// User management
UserDAO.class.php                               // User operations
UserMySqlExtDAO.class.php                       // Extended user operations

Views

Global Objects

$productRequest                                 // Request DTO
$productRequestDAO                              // Request DAO
$productRequestExt                              // Extended request operations
$product, $productDAO, $productExt             // Product objects
$productCatDAO, $productCatExt                 // Category objects  
$store, $storeDAO, $storeEX                    // Store objects
$user, $userDAO, $userEX                       // User objects

---

๐Ÿ“Š Performance Considerations

Database Queries

Memory Usage

Optimization Opportunities

1. Join Queries: Get product/category names in single query

2. Category Path Caching: Pre-compute and cache category hierarchies

3. Pagination: Add pagination to request listing

4. Index Optimization: Ensure proper indexes on request status and store IDs

---

๐ŸŽฏ Integration Points

Store Management Integration

// User assigned to specific store
$storeId = $userdata->userstoreid;

// Request between stores  
requeststoreid = source store
storeid = target store

Inventory System Integration

User Permission System

Approval Workflow Integration

---

๐Ÿ” Troubleshooting

No Requests Showing

Symptom: Empty request lists

Causes:

Solution: Check user store assignment and database logs

Category Paths Not Displaying

Symptom: Empty or broken category paths

Causes:

Solution: Validate category hierarchy integrity

Permission Issues

Symptom: Users seeing wrong requests

Causes:

Solution: Verify user.userstoreid values

---

๐Ÿ’ก Future Enhancements

Missing Core Functionality

1. Request Creation: Complete form and backend processing

2. Approval Workflow: Approve/reject functionality with notifications

3. Fulfillment: Mark requests as completed with inventory movement

4. Status Tracking: Timeline view of request progress

5. Bulk Operations: Handle multiple requests simultaneously

Advanced Features

1. Auto-Approval: Rules-based automatic approval for certain conditions

2. Inventory Integration: Real-time availability checking

3. Notification System: Email/SMS alerts for request status changes

4. Reporting: Analytics on request patterns and fulfillment times

5. Mobile Support: Mobile-friendly interface for warehouse staff

Performance Improvements

1. Caching Layer: Cache category hierarchies and frequent queries

2. Real-time Updates: WebSocket integration for live status updates

3. API Integration: RESTful API for mobile apps and external systems

4. Batch Processing: Optimize database operations for large datasets

---

๐Ÿ“‹ Data Flow Example

Typical Request Scenario

Store A needs Product X (ID: 100)
โ””โ”€ Store A user creates request
   โ”œโ”€ requeststoreid = A (requesting store)  
   โ”œโ”€ storeid = B (target store that has inventory)
   โ”œโ”€ productid = 100
   โ”œโ”€ status = 0 (new)
   โ””โ”€ deleted = 0

Store B user sees incoming request
โ””โ”€ Reviews inventory availability  
   โ”œโ”€ Approves: status = 2
   โ”œโ”€ Prepares shipment: status = 3  
   โ””โ”€ OR Rejects: status = 1

Store A user sees status update
โ””โ”€ Receives notification of approval/rejection
   โ””โ”€ If approved, coordinates pickup/delivery

This controller serves as a foundation for inter-store inventory management but requires significant development to reach full functionality.