Billsreturn Documentation

Bills Return Controller Documentation

File: /controllers/billsreturn.php

Purpose: Manages product returns and refund processing for optical sales

Last Updated: December 20, 2024

Total Functions: 15+

Lines of Code: 734

---

๐Ÿ“‹ Overview

The Bills Return Controller handles the complete return process for optical products sold through the ERP system. It provides:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Return Tables

Table NamePurposeKey Columns
**billsreturn**Return bill masterid, billid, returnedprice, clientreceivevalue, date, userid, deleted, dailyentryid
**billsreturnproducts**Return line itemsid, returnbillid, productid, productno, productprice, producttotalprice, billproductid, deleted
**bills**Original sales billsid, clientid, billno, finalnetbillvalue, clientdebt, deleted
**billsproducts**Original sales line itemsid, billid, productid, productno, productprice, producttotalprice, deleted
### Supporting Tables

Table NamePurposeKey Columns
**client**Customer master dataclientid, clientname, clientdebt, treeId
**product**Product catalogproductid, productName, productBuyPrice, lastbuyprice, meanbuyprice
**storedetail**Current inventorystoredetailid, productid, storeid, productquantity
**storereport**Inventory movement logid, productid, storeid, productbefore, productafter, storereporttype, tablename
### Financial Tables

Table NamePurposeKey Columns
**dailyentry**Accounting journal entriesdailyentryid, dDateTime, entryComment, fromFlag
**dailyentrycreditor**Credit entriesid, dailyentryid, accountstreeid, value, pluginControllerName
**dailyentrydebtor**Debit entriesid, dailyentryid, accountstreeid, value, pluginControllerName
**save**Cash registerssaveid, savename, savecurrentvalue, treeId
---

๐Ÿ”‘ Key Functions

1. Default Action / Return Form - Return Processing Interface

Location: Line 166

Purpose: Display return creation form with store selection

Process Flow:

1. Load all available stores for dropdown

2. Display return form template

3. Enable bill selection and return processing

---

2. add() - Process Complete Return

Location: Line 395

Purpose: Process complete return transaction with all components

Function Signature:

function add()

Process Flow:

1. Save return bill details via saveBillDetails()

2. Process returned products via saveBillProducts()

3. Create financial entries via saveDailyEntry()

4. Handle transaction completion

Features:

---

3. saveBillDetails() - Return Bill Creation

Location: Line 401

Purpose: Create return bill master record

Function Signature:

function saveBillDetails()
// Returns: Return bill ID

Process Flow:

1. Extract return parameters (bill ID, amounts)

2. Load original bill information

3. Create return bill record

4. Set system metadata (date, user, branch)

Key Data Captured:

$billId = filter_input(INPUT_POST, "billId");                    // Original bill
$discValue = filter_input(INPUT_POST, "discValue");              // Total return value
$clientReceiveValue = filter_input(INPUT_POST, "clientReceiveValue"); // Cash refund

---

4. saveBillProducts() - Process Returned Products

Location: Line 427

Purpose: Process individual returned products with inventory restoration

Function Signature:

function saveBillProducts($billsReturnId)

Process Flow:

1. Process each returned product line

2. Create return product records

3. Update inventory quantities (restore stock)

4. Log inventory movements

5. Update product cost tracking

Inventory Restoration Logic:

if ($billProductId && $billProductId != -1 && $productNum > 0) {
    // Get current inventory
    $storeDetail = $storeDetailEX->getProductQuantity($billsProducts->productid, $storeId);
    $productbefore = $storeDetail->productquantity;
    $productafter = $productbefore + $productNum; // Add back to inventory
    
    // Update inventory
    $storeDetail->productquantity = $productafter;
    $storeDetailDAO->update($storeDetail);
    
    // Log movement
    insertStorereportupdate($billsProducts->productid, $storeId, $productNum, 
                           $productbefore, $productafter, 0, $billsReturnId, 
                           "ู…ุฑุชุฌุน ูุงุชูˆุฑุฉ ู…ุจูŠุนุงุช", "billsreturn.php", date('Y-m-d'));
}

---

5. saveDailyEntry() - Return Financial Processing

Location: Line 524

Purpose: Create accounting entries for return transactions

Function Signature:

function saveDailyEntry($billsReturnId)

Accounting Logic:

1. Debit: Returns & Allowances account (expense)

2. Credits:

- Cash account (if cash refund given)

- Customer receivable (if account credit)

Journal Entry Structure:

// Debit: Returns account
$dailyEntryDebtor->value = $discValue;           // Total return amount
$dailyEntryDebtor->accountstreeid = 16;          // Returns account

// Credit: Cash account (if cash refund)
if ($clientReceiveValue > 0) {
    $dailyEntryCreditor->value = $clientReceiveValue;
    $dailyEntryCreditor->accountstreeid = $save->treeId; // Cash register
}

// Credit: Customer account (if account credit)
if ($clientDeptChangeValue > 0) {
    $dailyEntryCreditor2->value = $clientDeptChangeValue;
    $dailyEntryCreditor2->accountstreeid = $client->treeId; // Customer receivable
}

---

6. show() - Return Listing

Location: Line 192

Purpose: Display all return transactions with filtering options

Process Flow:

1. Load customer data for filtering

2. Load bill numbers for reference

3. Display return listing template

4. Enable return management operations

---

7. details() - Return Detail View

Location: Line 275

Purpose: Display comprehensive return transaction details

Function Signature:

// Called via: GET parameter 'id' (return bill ID)
$billsReturnId = filter_input(INPUT_GET, "id");

Process Flow:

1. Load return bill record

2. Calculate other returns for same bill

3. Display bill details via getBillDetails()

4. Show return-specific information

---

8. edit() / update() - Return Modification

Location: Line 240 (edit), Line 699 (update)

Purpose: Modify existing return transactions

Edit Process:

1. Load return record and related data

2. Calculate return totals and balances

3. Display edit form with current values

Update Process:

1. Update return bill details via updateBillDetails()

2. Update returned products via updateBillProducts()

3. Reverse and recreate financial entries

4. Handle transaction completion

---

9. delete() - Return Deletion

Location: Line 210

Purpose: Soft delete return with inventory reversal

Process Flow:

1. Mark return as deleted

2. Reverse financial entries

3. Reverse inventory changes (remove returned quantities)

4. Log inventory movements

Inventory Reversal Logic:

foreach ($billsReturnProducts as $productR) {
    $productNum = $productR->productno;
    $storeDetail = $storeDetailEX->getProductQuantity($productR->productid, $storeId);
    $productbefore = $storeDetail->productquantity;
    $productafter = $productbefore - $productNum; // Remove returned quantity
    $storeDetail->productquantity = $productafter;
    $storeDetailDAO->update($storeDetail);
}

---

10. getBillDetails() / getBillProducts() - Return Data Assembly

Location: Line 302 (details), Line 336 (products)

Purpose: Assemble complete return transaction information

Data Assembly Components:

1. Original Bill: Load source sales bill

2. Customer Info: Client details and history

3. Return Calculations: Net amounts after returns

4. Product Details: Line-by-line return information

5. Return History: Previous returns for same bill

---

๐Ÿ”„ Workflows

Workflow 1: Complete Product Return Processing

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Process Product Return
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Return Authorization
- Select original sales bill
- Validate return eligibility
- Check return timeframe and policies
- Verify product condition
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Return Details Specification
FOR EACH product to return:
โ”‚
โ†’ Select product from original bill
โ†’ Specify return quantity (โ‰ค original quantity)
โ†’ Calculate return value (qty ร— original price)
โ”‚ โ””โ”€โ†’ Specify return reason/notes โ”‚
Return Value Calculation:
โ†’ Total return value = Sum of product returns
โ†’ Cash refund amount (customer choice)
โ”‚ โ””โ”€โ†’ Account credit = Total return - Cash refund โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Inventory Restoration
FOR EACH returned product:
โ”‚
โ†’ Get current inventory quantity
โ”‚
โ†’ Add returned quantity back to inventory:
โ”‚ New_Qty = Current_Qty + Returned_Qty
โ”‚
โ†’ Update product cost tracking:
โ”‚ โ”œโ”€ Restore last buy price
โ”‚ โ”‚ โ””โ”€ Maintain mean buy price โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ Log inventory movement: โ”‚
โ”œโ”€ Movement type: Return (increase)
โ”œโ”€ Quantity before/after
โ”‚ โ””โ”€ Reference return bill โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Financial Processing
Create return journal entries:
โ”‚
โ†’ Debit: Returns & Allowances Account
โ”‚ Amount: Total return value
โ”‚ Purpose: Record return expense
โ”‚
โ†’ Credit: Cash Account (if cash refund):
โ”‚ Amount: Cash refund amount
โ”‚ Purpose: Cash paid to customer
โ”‚
โ”‚ โ””โ”€โ†’ Credit: Customer Receivable (if account credit): โ”‚
Amount: Total return - Cash refund
Purpose: Reduce customer debt
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Return Documentation
- Create return bill record
- Link to original sales bill
- Generate return receipt/documentation
- Update original bill net values
- Complete transaction and commit changes
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

Workflow 2: Return Validation and Business Rules

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Validate Return Request
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Original Bill Validation
- Verify bill exists and is not deleted
- Check bill status (paid/unpaid)
- Validate customer information
- Ensure bill is within return period
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Product Return Eligibility
FOR EACH product on return request:
โ”‚
โ†’ Verify product was on original bill
โ”‚
โ†’ Check returnable quantity:
โ”‚ Original_Qty - Previously_Returned_Qty
โ”‚
โ†’ Validate requested return quantity:
โ”‚ Return_Qty โ‰ค Available_For_Return
โ”‚
โ”‚ โ””โ”€โ†’ Check product condition and policy: โ”‚
โ”œโ”€ Physical condition acceptable
โ”œโ”€ Custom products (may not be returnable)
โ”‚ โ””โ”€ Service items (special handling) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Financial Impact Calculation
- Calculate total return value impact
- Determine refund vs account credit split
- Validate against customer account limits
- Check for outstanding customer debt
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Authorization and Processing
IF all validations pass:
โ”‚ โ””โ”€โ†’ Proceed with return processing โ”‚
ELSE:
โ†’ Display validation errors
โ†’ Suggest corrective actions
โ”‚ โ””โ”€โ†’ Require manager override if applicable โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐Ÿงฎ Return Calculations

Return Value Calculation

// Calculate total return amount
$totalReturnValue = 0;
for ($i = 1; $i <= $itr; $i++) {
    $returnQty = filter_input(INPUT_POST, 'retnum' . $i);
    $productPrice = filter_input(INPUT_POST, 'proprice' . $i);
    $lineReturnValue = $returnQty * $productPrice;
    $totalReturnValue += $lineReturnValue;
}

Refund Distribution

$discValue = filter_input(INPUT_POST, "discValue");              // Total return value
$clientReceiveValue = filter_input(INPUT_POST, "clientReceiveValue"); // Cash refund
$clientDeptChangeValue = $discValue - $clientReceiveValue;       // Account credit

Inventory Impact

// For each returned product
$storeDetail = $storeDetailEX->getProductQuantity($productid, $storeId);
$productbefore = $storeDetail->productquantity;
$productafter = $productbefore + $returnedQuantity; // Add back to stock

// Log the movement
insertStorereportupdate($productid, $storeId, $returnedQuantity, 
                       $productbefore, $productafter, 0, $returnBillId,
                       "ู…ุฑุชุฌุน ูุงุชูˆุฑุฉ ู…ุจูŠุนุงุช", "billsreturn.php", date('Y-m-d'));

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default actionReturn creation form
`do=add``add()`Process new return
`do=show``show()`List all returns
`do=edit``edit()`Edit return form
`do=update``update()`Update return
`do=delete``delete()`Delete return (soft)
`do=details``details()`View return details
`do=sucess`Success pageOperation completed
`do=error`Error pageOperation failed
### Required Parameters by Action

Add Return (do=add):

Edit Return (do=edit):

Delete Return (do=delete):

View Details (do=details):

---

๐Ÿ”’ Security & Permissions

Input Validation

$billId = filter_input(INPUT_POST, "billId");
$discValue = filter_input(INPUT_POST, "discValue");
$clientReceiveValue = filter_input(INPUT_POST, "clientReceiveValue");

Business Rule Validation

Transaction Integrity

---

๐Ÿ“Š Performance Considerations

Database Optimization

1. Critical Indexes:

- billsreturn(billid, deleted)

- billsreturnproducts(returnbillid, billproductid)

- billsproducts(billid, productid)

- storereport(productid, storeid, tablename)

2. Query Optimization:

- Efficient return history queries

- Fast product availability checks

- Optimized inventory updates

Return Processing Performance

---

๐Ÿ› Common Issues & Troubleshooting

1. Inventory Discrepancies After Returns

Issue: Inventory levels incorrect after processing returns

Cause: Missing or incorrect inventory restoration

Debug:

-- Check inventory movement logs
SELECT * FROM storereport 
WHERE tablename = 'billsreturn.php' 
AND productid = [PRODUCT_ID] 
ORDER BY storereportdate DESC;

-- Verify current vs calculated inventory
SELECT 
    p.productName,
    sd.productquantity as current_qty,
    -- Calculate expected based on movements
    (SELECT SUM(CASE WHEN storereporttype = 1 THEN productquantity 
                    ELSE -productquantity END)
     FROM storereport sr 
     WHERE sr.productid = p.productid 
     AND sr.storeid = sd.storeid) as calculated_qty
FROM storedetail sd
JOIN product p ON p.productid = sd.productid
WHERE sd.productid = [PRODUCT_ID];

2. Return Validation Failures

Issue: Cannot return products that should be returnable

Cause: Incorrect return quantity calculations

Debug:

-- Check return eligibility
SELECT 
    bp.productid,
    bp.productno as original_qty,
    COALESCE(SUM(brp.productno), 0) as returned_qty,
    bp.productno - COALESCE(SUM(brp.productno), 0) as available_for_return
FROM billsproducts bp
LEFT JOIN billsreturnproducts brp ON brp.billproductid = bp.id AND brp.deleted = 0
WHERE bp.billid = [BILL_ID] AND bp.deleted = 0
GROUP BY bp.id, bp.productid, bp.productno;

3. Financial Entry Mismatches

Issue: Return accounting entries don't balance

Cause: Incorrect credit/debit calculations

Debug:

// Verify return entry totals
$totalReturn = $discValue;
$cashRefund = $clientReceiveValue;
$accountCredit = $totalReturn - $cashRefund;

// Check that debits = credits
$totalDebits = $totalReturn;
$totalCredits = $cashRefund + $accountCredit;
if ($totalDebits != $totalCredits) {
    throw new Exception("Return entry doesn't balance");
}

---

๐Ÿงช Testing Scenarios

Test Case 1: Partial Product Return

1. Create sales bill with multiple products
2. Return only some products with partial quantities
3. Verify inventory restoration is correct
4. Check financial entries are accurate
5. Validate original bill net amounts updated

Test Case 2: Full Bill Return

1. Create sales bill with multiple products
2. Return all products in full quantities
3. Verify complete inventory restoration
4. Check full refund processing (cash + credit)
5. Validate bill status after full return

Test Case 3: Multiple Partial Returns

1. Create sales bill
2. Process first partial return
3. Process second partial return
4. Verify cumulative return tracking
5. Check inventory levels throughout process

Test Case 4: Return Validation

1. Attempt return with quantities > original
2. Attempt return for non-existent products
3. Test return period validation
4. Verify authorization requirements

Test Case 5: Cash vs Credit Returns

1. Test full cash refund return
2. Test full account credit return  
3. Test mixed cash + credit return
4. Verify accounting entries for each scenario

---

๐Ÿš€ Future Enhancement Opportunities

1. Advanced Return Management

2. Customer Experience

3. Inventory Optimization

4. Financial Integration

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When return management features are enhanced