InsuranceReturn Documentation

Insurance Return Controller Documentation

File: /controllers/insuranceReturn.php

Purpose: Manages incoming warranty returns from repair/maintenance services with repair outcome processing

Last Updated: December 20, 2024

Total Functions: 10

Lines of Code: ~384

---

๐Ÿ“‹ Overview

The Insurance Return Controller handles the complete process of receiving products back from warranty service providers. It manages repair outcomes (replacement, repair, write-off), tracks return shipping costs, processes insurance costs and discounts, and maintains comprehensive financial tracking of warranty-related expenses. This controller complements the insuranceGo.php controller by completing the warranty service cycle.

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**minsurance**Insurance/warranty trackinginsuranceid, ourserial, stageidbefore, currentstage, shipnamereturn, shipcompanyreturn, shippersonalreturn, shipcostreturn, insurancedate, type, oldsn, newsn, insurancecost, insurancediscount
**mproducthistory**Product stage change historyproducthistoryid, ourserial, stagefrom, stageto, cost, comment, status, userid, producthistorydate, branchid
**mcomreceiptdetail**Customer receipt product detailsid, ourserial, currentstage, clientid
**mcomreceipt**Customer receipt masterid, clientid, totalcost
### Financial Tables

Table NamePurposeKey Columns
**save**Cash registers/safessaveid, savename, savecurrentvalue, userid
**savedaily**Daily cash transactionssavedailyid, savedailydate, userid, savedailysavebefore, savedailychangeamount, savedailychangetype, saveid, processname, savedailymodelid, savedailysaveafter, tablename
### Reference Tables

Table NamePurposeKey Columns
**shippercompanies**Shipping company informationid, companyname, contactinfo
**representativecompany**Shipping representativesid, representativename, companyid, contactinfo
---

๐Ÿ”‘ Key Functions

1. show() / Default Action - Products Available for Return

Location: Lines 120-133

Purpose: Display suppliers with products currently shipped for warranty (stage 10) that can be returned

Function Signature:

// Triggered when: do=all or empty $do
$data = $MComReceiptDetailEX->getSupplierVSNoOfProducts2($_SESSION['branchId']);

Process Flow:

1. Query products in stage 10 (shipped for warranty)

2. Group by supplier for organized return processing

3. Display supplier summary with product counts

4. Present via insuranceReturnPrepare.html template

Features:

---

2. return() - Product Return Processing Form

Location: Lines 134-144

Purpose: Display detailed product list for selected supplier with return processing options

Function Signature:

// Triggered when: do=return&supplierid=X
$supplierid = $_GET["supplierid"];
$productsData = $MComReceiptDetailEX->getProductsToGo3($supplierid, $_SESSION['branchId']);

Process Flow:

1. Load products in stage 10 for specific supplier

2. Query available shipping companies and representatives

3. Display return processing form with repair outcome options

4. Present via insuranceReturn.html template

Template Variables:

---

3. add() - Process Warranty Return

Location: Lines 196-269

Purpose: Execute warranty return with complex outcome processing and cost calculations

Function Signature:

function add()

Process Flow:

1. Process each selected product with return details:

   for ($i = 1; $i <= $itr; $i++) {
       $ourserial = $_POST["ourserial" . $i];
       $cost = $_POST["cost" . $i];
       $type = $_POST["type" . $i];
       $newSN = $_POST["newSN" . $i];
       $insuranceCost = $_POST["insuranceCost" . $i];
       $insuranceDiscount = $_POST["insuranceDiscount" . $i];
   }
   ```
2. Handle repair outcome types (replacement, repair, write-off)
3. Calculate complex cost structure:
   ```php
   $finalCost = $cost + $insuranceCost - $insuranceDiscount;
   ```
4. Update cash register with total costs
5. Update insurance record with return details
6. Insert product history with outcome status
7. Update product current stage (10 โ†’ 9)
8. Increase customer receipt total cost

**Repair Outcome Types**:
- `type = 0` - Product replacement (requires new serial number)
- `type = 1` - Product repaired (keep original serial)
- `type = 2` - Product written off/discarded
- `type = 3` - Product accepted/approved (sets isAccepted = 1)

**Cost Calculation**:
php

$finalCost = $cost + $insuranceCost - $insuranceDiscount;

// cost = return shipping cost

// insuranceCost = insurance service cost

// insuranceDiscount = insurance discount applied

---

### 4. **prepareReturn()** - Prepare for Return (Unused Function)
**Location**: Lines 182-194  
**Purpose**: Transition products from stage 10 to stage 8 (appears to be an intermediate preparation step)

**Note**: This function exists but is not called by any route - may be legacy code for a two-step return process.

---

### 5. **updateInsuranceReturn()** - Update Return Details
**Location**: Lines 286-291  
**Purpose**: Update insurance table with comprehensive return information

**Function Signature**:
php

function updateInsuranceReturn($ourSerial, $stageIdBefore, $currentStage, $shipNameReturn, $shipCompanyReturn, $shipPersonalReturn, $shipCostReturn, $insuranceDate, $type, $oldSN, $newSN, $insuranceCost, $insuranceDiscount)

**Fields Updated**:
- Return shipping details (company, representative, cost)
- Repair outcome information
- Serial number changes (for replacements)
- Insurance cost adjustments
- Return processing date

---

### 6. **insertProductHistory()** - Comprehensive Audit Trail
**Location**: Lines 300-317  
**Purpose**: Create detailed audit record of warranty return with repair outcome

**Function Signature**:
php

function insertProductHistory($ourSerial, $stageFrom, $stageTo, $cost, $comment, $status)

**Status Field Usage**:
- `status = 0` - Standard repair/return
- `status = 1` - Product accepted/approved (when type = 3)

**Audit Information**:
- Stage transition (10 โ†’ 9)
- Total cost impact (shipping + insurance - discount)
- User and timestamp
- Branch context
- Repair outcome status

---

### 7. **getSaveValueAndMins()** - Financial Impact Calculation
**Location**: Lines 338-349  
**Purpose**: Calculate cash register balance after warranty return costs

**Function Signature**:
php

function getSaveValueAndMins($saveid, $savevaluechanged)

**Cost Structure**:
php

$finalCost = $cost + $insuranceCost - $insuranceDiscount;

$saveValueafter = $saveValuebefore - $finalCost;

---

### 8. **insertSavedaily()** - Financial Transaction Recording
**Location**: Lines 365-382  
**Purpose**: Record warranty return costs in daily cash log

**Function Signature**:
php

function insertSavedaily($savedailysavebefore, $savedailychangeamount, $savedailychangetype, $saveid, $processname, $savedailymodelid, $savedailysaveafter, $tablename)

**Transaction Details**:
- `processname = "ุดุญู† ุนูˆุฏุฉ"` - "Return shipping"
- `tablename = "insuranceReturn.php"` - Source reference
- `savedailychangetype = 1` - Cash outflow (expense)

---

## ๐Ÿ”„ Workflows

### Workflow 1: Warranty Return Processing

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ START: Process Warranty Returns โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 1. View Suppliers with Returning Products โ”‚

โ”‚ - Query products in stage 10 (shipped for warranty) โ”‚

โ”‚ - Group by supplier for organized processing โ”‚

โ”‚ - Display supplier summary with return counts โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 2. Select Supplier and View Returning Products โ”‚

โ”‚ - Load detailed product list for supplier โ”‚

โ”‚ - Query available return shipping companies โ”‚

โ”‚ - Query available shipping representatives โ”‚

โ”‚ - Display return processing form โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 3. Enter Return and Repair Details โ”‚

โ”‚ - Select products to process return โ”‚

โ”‚ - Choose repair outcome type: โ”‚

โ”‚ โ”œโ”€ 0: Replacement (enter new serial number) โ”‚

โ”‚ โ”œโ”€ 1: Repaired (keep original serial) โ”‚

โ”‚ โ”œโ”€ 2: Write-off/discarded โ”‚

โ”‚ โ””โ”€ 3: Accepted/approved โ”‚

โ”‚ - Enter return shipping cost โ”‚

โ”‚ - Enter insurance service cost โ”‚

โ”‚ - Enter insurance discount (if any) โ”‚

โ”‚ - Select return shipping company โ”‚

โ”‚ - Select return representative โ”‚

โ”‚ - Set return date โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 4. Process Each Returning Product โ”‚

โ”‚ FOR EACH valid product: โ”‚

โ”‚ โ”‚ โ”‚

โ”‚ โ”œโ”€โ†’ Handle Repair Outcome โ”‚

โ”‚ โ”‚ โ”œโ”€ If replacement: validate new serial number โ”‚

โ”‚ โ”‚ โ”œโ”€ If repair: keep original serial โ”‚

โ”‚ โ”‚ โ””โ”€ Set acceptance status for approved items โ”‚

โ”‚ โ”‚ โ”‚

โ”‚ โ”œโ”€โ†’ Calculate Total Costs โ”‚

โ”‚ โ”‚ โ”œโ”€ Final Cost = Shipping + Insurance - Discount โ”‚

โ”‚ โ”‚ โ””โ”€ Validate cost calculations โ”‚

โ”‚ โ”‚ โ”‚

โ”‚ โ”œโ”€โ†’ Update Cash Register โ”‚

โ”‚ โ”‚ โ”œโ”€ Get current balance โ”‚

โ”‚ โ”‚ โ”œโ”€ Subtract final cost โ”‚

โ”‚ โ”‚ โ””โ”€ Update register balance โ”‚

โ”‚ โ”‚ โ”‚

โ”‚ โ”œโ”€โ†’ Record Cash Transaction โ”‚

โ”‚ โ”‚ โ”œโ”€ Insert into savedaily table โ”‚

โ”‚ โ”‚ โ”œโ”€ Record total cost as expense โ”‚

โ”‚ โ”‚ โ””โ”€ Link to insurance ID โ”‚

โ”‚ โ”‚ โ”‚

โ”‚ โ”œโ”€โ†’ Update Insurance Record โ”‚

โ”‚ โ”‚ โ”œโ”€ Set return shipping details โ”‚

โ”‚ โ”‚ โ”œโ”€ Set repair outcome type โ”‚

โ”‚ โ”‚ โ”œโ”€ Set serial number changes โ”‚

โ”‚ โ”‚ โ”œโ”€ Set insurance costs and discounts โ”‚

โ”‚ โ”‚ โ””โ”€ Set return processing date โ”‚

โ”‚ โ”‚ โ”‚

โ”‚ โ”œโ”€โ†’ Update Product Stage โ”‚

โ”‚ โ”‚ โ”œโ”€ Change from stage 10 to 9 โ”‚

โ”‚ โ”‚ โ””โ”€ Update current stage โ”‚

โ”‚ โ”‚ โ”‚

โ”‚ โ”œโ”€โ†’ Insert Product History โ”‚

โ”‚ โ”‚ โ”œโ”€ Record stage transition (10โ†’9) โ”‚

โ”‚ โ”‚ โ”œโ”€ Record final cost impact โ”‚

โ”‚ โ”‚ โ”œโ”€ Record repair outcome status โ”‚

โ”‚ โ”‚ โ””โ”€ Record user and timestamp โ”‚

โ”‚ โ”‚ โ”‚

โ”‚ โ””โ”€โ†’ Update Customer Receipt Total โ”‚

โ”‚ โ”œโ”€ Get customer receipt ID โ”‚

โ”‚ โ”œโ”€ Add final cost to receipt total โ”‚

โ”‚ โ””โ”€ Update receipt total โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”‚

โ–ผ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ 5. Complete Return Transaction โ”‚

โ”‚ - Display success or error message โ”‚

โ”‚ - Return to supplier selection screen โ”‚

โ”‚ - Update all related systems consistently โ”‚

โ”‚ - Generate audit trail for compliance โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

## ๐ŸŒ URL Routes & Actions

| URL Parameter | Function Called | Description |
|---------------|----------------|-------------|
| `do=` (empty) or `do=all` | Default action | View suppliers with products returning from warranty |
| `do=return&supplierid=X` | return() | Product return processing for specific supplier |
| `do=add` | add() | Process warranty return with repair outcomes |
| `do=sucess` | Default | Success page display |
| `do=error` | Default | Error page display |

### Required Parameters by Action

**Supplier Return View** (`do=all`):
- No additional parameters required
- Uses `$_SESSION['branchId']` for filtering

**Product Return Processing** (`do=return`):
- `supplierid` - Supplier ID to filter returning products

**Process Return** (`do=add`):
- `hidden_itr` - Number of products being processed
- `mydate` - Return processing date (optional, defaults to today)
- `companyId` - Return shipping company ID
- `representativeId` - Return representative ID
- `Policyholder` - Policyholder name (optional)
- `ourserial{N}` - Product serial numbers (N = 1 to itr)
- `cost{N}` - Return shipping costs per product
- `go{N}` - Processing flag per product
- `type{N}` - Repair outcome type (0=replacement, 1=repair, 2=writeoff, 3=accepted)
- `newSN{N}` - New serial numbers for replacements (optional)
- `insuranceCost{N}` - Insurance service costs per product (optional)
- `insuranceDiscount{N}` - Insurance discounts per product (optional)

---

## ๐Ÿงฎ Calculation Methods

### Complex Cost Structure
php

// Multi-component cost calculation

$finalCost = $cost + $insuranceCost - $insuranceDiscount;

// Where:

// $cost = return shipping cost

// $insuranceCost = insurance service/repair cost

// $insuranceDiscount = discount applied by insurance company

### Cash Register Balance Update
php

// Calculate impact on cash register

$saveData = getSaveValueAndMins($_SESSION['saveid'], $finalCost);

$saveValueafter = $saveValuebefore - $finalCost;

### Serial Number Handling
php

// Handle product replacement scenarios

if ($type != 0) {

$newSN = $ourserial; // Keep original serial for non-replacement outcomes

}

// For type = 0 (replacement), use provided new serial number

### Repair Outcome Status
php

// Set acceptance status for approved repairs

$isAcccepted = 0;

if ($type == 3) { // Accepted/approved outcome

$isAcccepted = 1;

}

### Customer Receipt Impact
php

// Add all warranty-related costs to customer receipt

$totalCost = $MComreceiptEX->getTotalCostComRecIDTotalCost($comRecID);

$totalCostNew = $totalCost + $finalCost;

---

## ๐Ÿ”’ Security & Permissions

### Session Requirements
php

// Required session variables

$_SESSION['userid'] - User performing the operation

$_SESSION['branchId'] - Branch context for operations

$_SESSION['saveid'] - Default cash register for costs

### Input Validation
php

// Comprehensive validation for return processing

if (isset($ourserial) && !empty($ourserial) &&

isset($go) && !empty($go) &&

isset($cost) && !empty($cost) &&

isset($type) && $type != -1 &&

isset($companyId) && $companyId != -1 &&

isset($representativeId) && $representativeId != -1)

### Default Value Handling
php

// Safe handling of optional fields

if (!isset($cost) || empty($cost)) {

$cost = 0;

}

if (!isset($insuranceCost) || empty($insuranceCost)) {

$insuranceCost = 0;

}

if (!isset($insuranceDiscount) || empty($insuranceDiscount)) {

$insuranceDiscount = 0;

}

### Data Integrity
- All database operations use DAO layer with parameterized queries
- No direct SQL concatenation in controller
- Financial transactions maintain audit trails
- Stage transitions are atomic operations

---

## ๐Ÿ“Š Performance Considerations

### Database Optimization
1. **Required Indexes**:
   - `mcomreceiptdetail(ourserial)`
   - `mcomreceiptdetail(currentstage, branchid)`
   - `minsurance(ourserial)`
   - `save(saveid)`
   - `mproducthistory(ourserial, producthistorydate)`

2. **Query Efficiency**:
   - Stage-specific filtering (stage 10 only)
   - Branch-specific filtering reduces data set
   - Supplier-based grouping improves UI performance
   - Single product lookup per serial number

3. **Transaction Management**:
   - Multiple table updates should be wrapped in database transactions
   - Financial calculations are critical and need consistency
   - Product history maintains complete audit trail

### Memory Considerations
- Products filtered by stage and branch reduce memory usage
- Iterator pattern for bulk processing
- Complex cost calculations performed per product

---

## ๐Ÿ› Common Issues & Troubleshooting

### 1. **Incorrect Final Cost Calculations**
**Issue**: Final costs don't match expected values  
**Cause**: Missing or invalid cost components

**Debug**:
php

echo "Shipping cost: " . $cost . "
";

echo "Insurance cost: " . $insuranceCost . "
";

echo "Insurance discount: " . $insuranceDiscount . "
";

echo "Final cost: " . ($cost + $insuranceCost - $insuranceDiscount) . "
";

### 2. **Product Serial Number Issues**
**Issue**: Replacement products show wrong serial numbers  
**Cause**: New serial number not properly assigned for type=0

**Debug**:
sql

-- Check insurance record for serial number changes

SELECT ourserial, type, oldsn, newsn FROM minsurance WHERE ourserial = ?;

-- Check if new serial number conflicts exist

SELECT COUNT(*) FROM mcomreceiptdetail WHERE ourserial = ?;

### 3. **Products Stuck in Stage 10**
**Issue**: Products not transitioning to stage 9 after return  
**Cause**: Failed stage update or validation errors

**Debug**:
sql

-- Check current stage

SELECT ourserial, currentstage FROM mcomreceiptdetail WHERE ourserial = ?;

-- Check product history for stage transitions

SELECT stagefrom, stageto, producthistorydate FROM mproducthistory

WHERE ourserial = ? ORDER BY producthistorydate DESC;

### 4. **Cash Register Balance Issues**
**Issue**: Cash register shows incorrect balance after returns  
**Cause**: Complex cost calculations or concurrent transactions

**Debug**:
sql

-- Check recent cash transactions

SELECT * FROM savedaily WHERE processname = 'ุดุญู† ุนูˆุฏุฉ'

ORDER BY savedailydate DESC LIMIT 10;

-- Verify cash register balance

SELECT saveid, savecurrentvalue FROM save WHERE saveid = ?;

### 5. **Missing Repair Outcome Types**
**Issue**: Repair outcome type validation fails  
**Cause**: Invalid type values or missing options

**Debug**:
php

// Validate repair outcome types

$validTypes = [0, 1, 2, 3];

if (!in_array($type, $validTypes)) {

echo "Invalid repair outcome type: " . $type;

}

---

## ๐Ÿงช Testing Scenarios

### Test Case 1: Simple Product Return

1. Login and navigate to warranty return management

2. Select supplier with products in stage 10

3. Choose product and enter basic return details

4. Set repair outcome to "repaired" (type=1)

5. Enter return shipping cost only

6. Process return and verify stage transition

### Test Case 2: Product Replacement Return

1. Select product for return processing

2. Set repair outcome to "replacement" (type=0)

3. Enter new serial number for replacement

4. Include insurance costs and discounts

5. Process return and verify new serial number

6. Check original and new serial in insurance record

### Test Case 3: Complex Cost Return

1. Select product for return

2. Enter all cost components:

- Return shipping cost

- Insurance service cost

- Insurance discount

3. Verify final cost calculation

4. Process return and check cash register impact

5. Verify customer receipt total update

### Test Case 4: Validation Error Handling

1. Try return without selecting repair outcome type

2. Try replacement without entering new serial number

3. Enter invalid cost values (negative numbers)

4. Verify appropriate error messages

5. Confirm no partial updates occur

### Debug Mode Enable
php

// Add at top of controller for debugging

error_reporting(E_ALL);

ini_set('display_errors', 1);

// Debug return processing

echo "Processing return - Serial: " . $ourserial . ", Type: " . $type . "
";

echo "Costs - Shipping: " . $cost . ", Insurance: " . $insuranceCost . ", Discount: " . $insuranceDiscount . "
";

echo "Final cost: " . $finalCost . "
";

```

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When warranty return processes change