# Purchase Return Testing Checklist

## Pre-Testing Verification ✅

- [x] Database tables updated with return types
- [x] Backend PHP files modified (save-purchase.php, delete-purchase.php)
- [x] Frontend JavaScript updated (sales.js)
- [x] UI handlers added (index.php)
- [x] No syntax errors in modified files

## Test Scenarios

### 1. Basic Purchase Return Creation

#### Test 1.1: GST Purchase Return
- [ ] Open Purchase module
- [ ] Select "GST Purchase Return" from dropdown
- [ ] Verify dropdown background turns yellow
- [ ] Verify notification appears: "⚠️ Purchase Return mode activated - Stock will be decreased"
- [ ] Select a supplier
- [ ] Add product with code (e.g., ITEM001)
- [ ] Note current stock before save
- [ ] Enter quantity: 5, rate: 100
- [ ] Click "Save & Print Bill"
- [ ] Verify invoice number has PGSTR prefix (e.g., PGSTR/2025-26/0001)
- [ ] Check stock: Should DECREASE by 5
- [ ] Check supplier balance: Should DECREASE
- [ ] Check database: `tblsupplier_accounts` should have DEBIT entry

**Expected Results:**
```sql
-- Stock should decrease
SELECT ItemCode, CurrentStock FROM tblitems WHERE ItemCode = 'ITEM001';
-- Before: 100, After: 95

-- Supplier account should have DEBIT entry
SELECT TransactionType, Amount FROM tblsupplier_accounts 
WHERE InvoiceNumber LIKE 'PGSTR%' ORDER BY ID DESC LIMIT 1;
-- TransactionType: Debit, Amount: 500

-- Supplier balance should decrease
SELECT SupplierName, OutstandingBalance FROM tblsuppliers WHERE ID = [SUPPLIER_ID];
-- Balance reduced by 500
```

#### Test 1.2: Non-GST Purchase Return
- [ ] Repeat above with "Non GST Purchase Return"
- [ ] Verify invoice has PNGSTR prefix
- [ ] Verify stock decreases
- [ ] Verify supplier account has DEBIT entry

#### Test 1.3: Import Purchase Return
- [ ] Repeat above with "Import Purchase Return"
- [ ] Verify invoice has PIMPR prefix
- [ ] Verify stock decreases
- [ ] Verify supplier account has DEBIT entry

### 2. Edit Purchase Return

#### Test 2.1: Edit Return - Increase Quantity
- [ ] Create a return: 10 units @ ₹100
- [ ] Note stock after creation (should be -10)
- [ ] Use Previous/Next to load the return
- [ ] Change quantity to 15 units
- [ ] Save the edit
- [ ] Verify stock: Should decrease by additional 5 units
- [ ] Verify supplier balance: Should decrease by additional ₹500

**Expected Results:**
```
Initial Stock: 100
After Return: 90 (100 - 10)
After Edit: 85 (90 - 5 more)
```

#### Test 2.2: Edit Return - Decrease Quantity
- [ ] Create a return: 10 units @ ₹100
- [ ] Load the return for editing
- [ ] Change quantity to 5 units
- [ ] Save the edit
- [ ] Verify stock: Should increase by 5 units (less return)
- [ ] Verify supplier balance: Should increase by ₹500

**Expected Results:**
```
Initial Stock: 100
After Return: 90 (100 - 10)
After Edit: 95 (90 + 5 restored)
```

#### Test 2.3: Change Type - Purchase to Return
- [ ] Create a regular purchase: 10 units @ ₹100
- [ ] Verify stock increased to 110
- [ ] Load for editing
- [ ] Change type to "GST Purchase Return"
- [ ] Save
- [ ] Verify stock: Should be 90 (reverse +10, apply -10 = -20 total)
- [ ] Verify balance changed from Credit to Debit

### 3. Delete Purchase Return

#### Test 3.1: Delete Simple Return
- [ ] Create a return: 10 units @ ₹100
- [ ] Note stock after creation: Should be -10
- [ ] Note supplier balance: Should show debit
- [ ] Delete the return using Previous/Next + Delete
- [ ] Verify stock: Should INCREASE by 10 (back to original)
- [ ] Verify supplier balance: Debit entry should be removed

**Expected Results:**
```
Initial Stock: 100
After Return: 90 (100 - 10)
After Delete: 100 (90 + 10 restored)

Supplier Balance Before Delete: -₹1000 (Debit)
Supplier Balance After Delete: ₹0 (Debit removed)
```

### 4. Payment Scenarios

#### Test 4.1: Return with Cash Refund
- [ ] Create return: 10 units @ ₹100 = ₹1,000
- [ ] Enter Cash Amount: ₹1,000
- [ ] Save
- [ ] Check `tblsupplier_accounts`: Should have 2 entries
  - DEBIT ₹1,000 (return entry)
  - CREDIT ₹1,000 (refund received entry)
- [ ] Net effect on balance: ₹0

#### Test 4.2: Return with Partial Refund
- [ ] Create return: ₹1,000
- [ ] Enter Cash Amount: ₹500, Credit: ₹500
- [ ] Save
- [ ] Verify balance reduced by net ₹500

#### Test 4.3: Return Against Outstanding Bill
**Setup:**
- Supplier owes us nothing (balance = 0)
- Create purchase: ₹5,000 (we owe supplier)
- Supplier balance should be: ₹5,000

**Test:**
- [ ] Create return: ₹2,000 (no cash refund)
- [ ] Verify balance: ₹3,000 (5,000 - 2,000)
- [ ] Create another return: ₹1,000
- [ ] Verify balance: ₹2,000 (3,000 - 1,000)

### 5. Edge Cases

#### Test 5.1: Multiple Items in One Return
- [ ] Add 3 different products to return
- [ ] Verify all stock quantities decrease
- [ ] Verify total amount in supplier account

#### Test 5.2: Return with Discount
- [ ] Add item with product-level discount
- [ ] Verify calculations are correct
- [ ] Verify supplier account reflects discounted amount

#### Test 5.3: Return with Zero Payment
- [ ] Create return with ₹1,000 total
- [ ] Leave all payment fields as 0 (full credit)
- [ ] Verify only one entry in supplier accounts (DEBIT)
- [ ] Verify supplier balance reduced by ₹1,000

### 6. Database Verification Queries

After each test, run these queries to verify:

#### Check Recent Returns
```sql
SELECT ID, InvoiceNumber, PurchaseType, FinalAmount, PurchaseDate 
FROM tblpurchases 
WHERE PurchaseType LIKE '%RETURN%' 
ORDER BY ID DESC LIMIT 5;
```

#### Check Supplier Account Entries
```sql
SELECT sa.ID, sa.InvoiceNumber, sa.TransactionType, sa.Amount, sa.Description
FROM tblsupplier_accounts sa
WHERE sa.SupplierID = [SUPPLIER_ID]
ORDER BY sa.ID DESC LIMIT 10;
```

#### Verify Supplier Balance
```sql
SELECT 
    s.SupplierName,
    s.OutstandingBalance,
    SUM(CASE WHEN sa.TransactionType = 'Credit' THEN sa.Amount ELSE 0 END) as TotalCredits,
    SUM(CASE WHEN sa.TransactionType = 'Debit' THEN sa.Amount ELSE 0 END) as TotalDebits,
    (SUM(CASE WHEN sa.TransactionType = 'Credit' THEN sa.Amount ELSE 0 END) -
     SUM(CASE WHEN sa.TransactionType = 'Debit' THEN sa.Amount ELSE 0 END)) as CalculatedBalance
FROM tblsuppliers s
LEFT JOIN tblsupplier_accounts sa ON s.ID = sa.SupplierID
WHERE s.ID = [SUPPLIER_ID]
GROUP BY s.ID;
```

#### Check Stock Movements
```sql
SELECT i.ItemCode, i.ItemName, i.CurrentStock
FROM tblitems i
WHERE i.ItemCode IN ('ITEM001', 'ITEM002', 'ITEM003')
ORDER BY i.ItemCode;
```

### 7. UI/UX Verification

- [ ] Yellow background appears on return type selection
- [ ] Notification shows correctly
- [ ] Invoice number displays with correct prefix
- [ ] Print preview works for returns
- [ ] Previous/Next navigation works with returns
- [ ] Delete confirmation works

### 8. Integration Tests

#### Test 8.1: Mixed Purchases and Returns
- [ ] Create purchase: +10 units
- [ ] Create return: -5 units
- [ ] Create purchase: +8 units
- [ ] Create return: -3 units
- [ ] Verify final stock: 100 + 10 - 5 + 8 - 3 = 110

#### Test 8.2: Supplier Ledger Report
- [ ] Generate supplier ledger
- [ ] Verify returns show correctly
- [ ] Verify balance calculations are accurate

## Test Results Template

```
Test Date: _______________
Tester: _______________
Database: uzrs-inventorydb
Environment: WAMP Local

| Test ID | Description | Status | Notes |
|---------|-------------|--------|-------|
| 1.1 | GST Purchase Return | ⬜ Pass / ⬜ Fail | |
| 1.2 | Non-GST Purchase Return | ⬜ Pass / ⬜ Fail | |
| 1.3 | Import Purchase Return | ⬜ Pass / ⬜ Fail | |
| 2.1 | Edit Return - Increase Qty | ⬜ Pass / ⬜ Fail | |
| 2.2 | Edit Return - Decrease Qty | ⬜ Pass / ⬜ Fail | |
| 2.3 | Change Type Purchase→Return | ⬜ Pass / ⬜ Fail | |
| 3.1 | Delete Return | ⬜ Pass / ⬜ Fail | |
| 4.1 | Return with Cash Refund | ⬜ Pass / ⬜ Fail | |
| 4.2 | Return with Partial Refund | ⬜ Pass / ⬜ Fail | |
| 4.3 | Return Against Outstanding | ⬜ Pass / ⬜ Fail | |

Overall Result: ⬜ Pass / ⬜ Fail
```

## Common Issues & Solutions

### Issue 1: Stock Not Updating
**Check:**
- ProductID is valid in tblpurchases_details
- Transaction committed (no rollback)
- Look for error messages in browser console

**Fix:**
- Verify save-purchase.php has return detection logic
- Check stock operation uses correct +/- based on $isReturn

### Issue 2: Wrong Balance Calculation
**Check:**
- TransactionType (Debit vs Credit)
- Sum of all Credits - Debits should equal OutstandingBalance

**Fix:**
- Verify save-purchase.php uses correct transaction types
- Check payment handling section

### Issue 3: Invoice Number Not Showing Prefix
**Check:**
- Purchase type selected correctly
- Invoice generation uses correct prefix from database

**Fix:**
- Verify tblpurchase_type_config has correct prefixes
- Check invoice number generation logic

## Sign-off

- [ ] All basic tests passed
- [ ] All edit tests passed
- [ ] All delete tests passed
- [ ] All payment scenarios passed
- [ ] Database verification passed
- [ ] UI/UX checks passed

**Tested By:** _____________________  
**Date:** _____________________  
**Approved By:** _____________________  
**Date:** _____________________

---

**Version:** 1.0  
**Last Updated:** November 4, 2025
