# Vendor Payment Module - Comprehensive Test Plan

**Module:** Vendor Payments (Accounts Payable)  
**Version:** 1.0  
**Date:** 2025-11-15  
**Status:** Ready for Testing

---

## 📋 Pre-Test Setup

### Database Preparation
1. **Run Migration**
   ```bash
   mysql -u rpmbbu -p brickwal_m1_ds < database/migrations/030_vendor_payment_processing.sql
   ```
   ✅ **Expected:** All tables created without errors

2. **Verify Tables Created**
   - `vendor_payments`
   - `vendor_payment_applications`
   - `vendor_credits`
   - `vendor_credit_applications`
   - `check_templates`
   - `payment_batches`

3. **Verify Permissions**
   ```sql
   SELECT * FROM permissions WHERE name LIKE 'vendor_payments.%';
   ```
   ✅ **Expected:** 7 permissions exist

### Test Data Requirements
- At least 2 **active suppliers** in the system
- At least 3 **unpaid purchase bills** (approved status)
- At least 1 **active bank account**
- **User with permissions:** vendor_payments.create, vendor_payments.view, vendor_payments.edit, vendor_payments.void

---

## 🧪 Test Scenarios

### Test Suite 1: List View & Navigation

#### TC1.1 - Access Vendor Payments List
**Steps:**
1. Navigate to `/vendor-payments`
2. Login if required

**Expected Results:**
- ✅ Page loads with "Vendor Payments" header
- ✅ Statistics cards display (4 cards with totals)
- ✅ Search and filter section visible
- ✅ "New Payment" button visible (if has permission)
- ✅ Empty state message if no payments exist

#### TC1.2 - Statistics Cards Display
**Steps:**
1. View statistics cards

**Expected Results:**
- ✅ Card 1: Total Payments count and amount
- ✅ Card 2: Check count and printed count
- ✅ Card 3: ACH/Wire transfer count
- ✅ Card 4: Cleared payments count

#### TC1.3 - Search Functionality
**Steps:**
1. Enter payment number in search box
2. Click Filter button

**Expected Results:**
- ✅ Results filtered by search term
- ✅ Pagination updates if needed
- ✅ Clear button resets search

#### TC1.4 - Status Filter
**Steps:**
1. Select "Draft" from status dropdown
2. Click Filter

**Expected Results:**
- ✅ Only draft payments displayed
- ✅ Status badge matches filter

#### TC1.5 - Pagination
**Steps:**
1. Create 30+ payments (if needed)
2. Navigate between pages

**Expected Results:**
- ✅ 25 payments per page
- ✅ Page numbers display correctly
- ✅ Previous/Next buttons work
- ✅ Ellipsis (...) for skipped pages

---

### Test Suite 2: Create Payment Flow

#### TC2.1 - Access Create Form
**Steps:**
1. Click "New Payment" button
2. Or navigate to `/vendor-payments/create`

**Expected Results:**
- ✅ Form loads with all fields
- ✅ Payment number auto-generated
- ✅ Payment date defaults to today
- ✅ Vendor dropdown populated
- ✅ Bank account dropdown populated
- ✅ Payment method defaults to "Check"
- ✅ Info message: "Select a vendor above to see their unpaid bills"

#### TC2.2 - Vendor Selection Loads Bills (AJAX)
**Steps:**
1. Select a vendor from dropdown
2. Wait for AJAX response

**Expected Results:**
- ✅ Loading spinner appears
- ✅ Bills table loads with unpaid bills
- ✅ Bill details display: number, date, due date, amount, balance
- ✅ Overdue bills show red badge with days overdue
- ✅ Amount input fields disabled until checked
- ✅ If no unpaid bills: warning message displayed

#### TC2.3 - Payment Method Change
**Steps:**
1. Change payment method from "Check" to "ACH"

**Expected Results:**
- ✅ Check number field hides
- ✅ Change back to "Check"
- ✅ Check number field reappears

#### TC2.4 - Bank Account Selection Auto-fills Check Number
**Steps:**
1. Select payment method: "Check"
2. Select a bank account

**Expected Results:**
- ✅ Check number field auto-populates with next number
- ✅ Format: 4 digits (e.g., "1001")
- ✅ Can manually override

#### TC2.5 - Bill Selection - Individual
**Steps:**
1. Check one bill checkbox

**Expected Results:**
- ✅ Amount input enables
- ✅ Amount auto-fills to bill balance
- ✅ Total updates in footer
- ✅ Sidebar "Bills Selected" increments
- ✅ Sidebar "Total Amount" updates
- ✅ Badge "X selected" updates

#### TC2.6 - Bill Selection - Select All
**Steps:**
1. Click "Select All" checkbox

**Expected Results:**
- ✅ All bill checkboxes checked
- ✅ All amount inputs enabled and filled
- ✅ Total calculates correctly
- ✅ Sidebar updates with count and total

#### TC2.7 - Partial Payment Entry
**Steps:**
1. Select a bill
2. Change amount to less than balance (e.g., $500 on $1000 bill)

**Expected Results:**
- ✅ Amount accepts decimal input
- ✅ Total updates in real-time
- ✅ Cannot exceed bill balance (max validation)
- ✅ Sidebar updates

#### TC2.8 - Form Validation - No Bills Selected
**Steps:**
1. Fill payment details
2. Don't select any bills
3. Click "Create Payment"

**Expected Results:**
- ✅ JavaScript alert: "Please select at least one bill..."
- ✅ Form does not submit

#### TC2.9 - Form Validation - Missing Required Fields
**Steps:**
1. Leave vendor blank
2. Click "Create Payment"

**Expected Results:**
- ✅ HTML5 validation triggers
- ✅ Field highlights as required

#### TC2.10 - Successful Payment Creation
**Steps:**
1. Select vendor
2. Select bills to pay
3. Fill optional fields (memo, reference)
4. Click "Create Payment"

**Expected Results:**
- ✅ Success message appears
- ✅ Redirects to payment detail view
- ✅ Payment number displayed
- ✅ Status: "Draft"
- ✅ All details saved correctly

#### TC2.11 - Create and Post Immediately
**Steps:**
1. Complete payment form
2. Check "Post payment immediately"
3. Click "Create Payment"

**Expected Results:**
- ✅ Payment created
- ✅ Journal entry created automatically
- ✅ Status: "Printed" (not draft)
- ✅ Journal entry ID linked
- ✅ Success message: "Payment created and posted successfully"

---

### Test Suite 3: Payment Detail View

#### TC3.1 - View Payment Details
**Steps:**
1. Click on a payment from list
2. Or navigate to `/vendor-payments/{id}`

**Expected Results:**
- ✅ Payment information card displays all details
- ✅ Status badge displayed correctly
- ✅ Vendor name shown
- ✅ Payment method with icon
- ✅ Bank account displayed
- ✅ Check number (if applicable)
- ✅ Total amount prominent
- ✅ Memo and reference (if entered)

#### TC3.2 - Bills Paid Table
**Steps:**
1. View "Bills Paid" section

**Expected Results:**
- ✅ All applied bills listed
- ✅ Bill numbers link to bill details
- ✅ Amounts displayed correctly
- ✅ Discounts shown (if any)
- ✅ Total row calculates correctly

#### TC3.3 - Actions Sidebar - Draft Payment
**Steps:**
1. View draft payment

**Expected Results:**
- ✅ "Post Payment" button visible
- ✅ "Delete Payment" button visible
- ✅ "Edit" button in header (if has permission)
- ✅ "Print Check" button (if check payment)

#### TC3.4 - Actions Sidebar - Posted Payment
**Steps:**
1. View posted (non-draft) payment

**Expected Results:**
- ✅ "Void Payment" button visible
- ✅ NO "Post" or "Delete" buttons
- ✅ NO "Edit" button

#### TC3.5 - Audit Trail Display
**Steps:**
1. View audit trail card

**Expected Results:**
- ✅ Created date and user shown
- ✅ Updated date shown (if modified)
- ✅ Printed date shown (if printed)
- ✅ Journal entry link (if posted)

---

### Test Suite 4: Edit Payment

#### TC4.1 - Access Edit Form
**Steps:**
1. From draft payment detail, click "Edit"
2. Or navigate to `/vendor-payments/{id}/edit`

**Expected Results:**
- ✅ Form pre-populated with payment data
- ✅ Payment number readonly
- ✅ Vendor field readonly (cannot change)
- ✅ Bills loaded with current applications checked
- ✅ Amounts pre-filled
- ✅ Totals calculated on load

#### TC4.2 - Cannot Edit Non-Draft Payment
**Steps:**
1. Try to access edit for posted payment

**Expected Results:**
- ✅ Error message: "Only draft payments can be edited"
- ✅ Redirects to detail view

#### TC4.3 - Modify Bill Selection
**Steps:**
1. Uncheck one bill
2. Check a different bill
3. Update amounts
4. Click "Update Payment"

**Expected Results:**
- ✅ Payment updated
- ✅ Old bill applications removed
- ✅ New bill applications created
- ✅ Purchase bills updated correctly
- ✅ Total recalculated

#### TC4.4 - Change Payment Date
**Steps:**
1. Change payment date
2. Update payment

**Expected Results:**
- ✅ Payment date saved
- ✅ Success message displayed

---

### Test Suite 5: Post Payment

#### TC5.1 - Post Draft Payment
**Steps:**
1. View draft payment
2. Click "Post Payment"
3. Confirm in dialog

**Expected Results:**
- ✅ Confirmation prompt appears
- ✅ After confirm:
  - Status changes to "Printed"
  - Journal entry created
  - Journal entry ID displayed in audit trail
  - Success message: "Payment posted successfully"

#### TC5.2 - Verify Journal Entry Created
**Steps:**
1. Post payment
2. Click journal entry link in audit trail
3. Navigate to `/accounting/journal/{id}`

**Expected Results:**
- ✅ Journal entry exists
- ✅ Entry date = payment date
- ✅ Reference: "Vendor Payment: PAY-XXXXX"
- ✅ Line items:
  - Debit: Accounts Payable (for each bill)
  - Credit: Cash/Bank account
- ✅ Debits = Credits (balanced)

#### TC5.3 - Cannot Post Already-Posted Payment
**Steps:**
1. Try to post already-posted payment

**Expected Results:**
- ✅ "Post Payment" button not visible
- ✅ Or error if accessed directly

---

### Test Suite 6: Void Payment

#### TC6.1 - Void Posted Payment
**Steps:**
1. View posted (non-draft) payment
2. Click "Void Payment"
3. Enter void reason in modal
4. Click "Void Payment" button

**Expected Results:**
- ✅ Modal appears with warning
- ✅ Reason field required
- ✅ After void:
  - Status = "Voided"
  - Void date set to today
  - Void reason saved
  - Red alert shows void details
  - Bill applications reversed (paid_amount reduced)
  - Bill statuses updated
  - Journal entry status = "Voided"
  - Success message displayed

#### TC6.2 - Void Without Reason
**Steps:**
1. Click "Void Payment"
2. Leave reason blank
3. Submit

**Expected Results:**
- ✅ Error: "Void reason is required"
- ✅ Payment not voided

#### TC6.3 - Cannot Void Already-Voided Payment
**Steps:**
1. Try to void already-voided payment

**Expected Results:**
- ✅ "Void" button not visible
- ✅ Or error: "Payment is already voided"

#### TC6.4 - Cannot Void Draft Payment
**Steps:**
1. Try to void draft payment

**Expected Results:**
- ✅ "Void" button not visible for drafts
- ✅ Use "Delete" instead

---

### Test Suite 7: Delete Payment

#### TC7.1 - Delete Draft Payment
**Steps:**
1. View draft payment
2. Click "Delete Payment"
3. Confirm in browser alert

**Expected Results:**
- ✅ Confirmation prompt
- ✅ After confirm:
  - Payment deleted from database
  - Payment applications deleted
  - Redirects to list
  - Success message displayed

#### TC7.2 - Cannot Delete Non-Draft Payment
**Steps:**
1. Try to delete posted payment

**Expected Results:**
- ✅ Error: "Only draft payments can be deleted. Use void for posted payments."

---

### Test Suite 8: Check Printing

#### TC8.1 - Print Check
**Steps:**
1. View check payment
2. Click "Print Check"

**Expected Results:**
- ✅ Opens in new window/tab
- ✅ Check displays:
  - Company info (top left)
  - Check number (top right)
  - Date (formatted)
  - "Pay to the Order of" with vendor name
  - Amount in number box
  - Amount in words
  - Memo
  - Signature line
  - MICR line (if configured)
- ✅ Detachable stub shows:
  - Payment details
  - Bill list with amounts
  - Total
- ✅ Print buttons visible
- ✅ "Print Check" button triggers print dialog

#### TC8.2 - Check Status After Print
**Steps:**
1. Print check for draft payment

**Expected Results:**
- ✅ Status changes to "Printed"
- ✅ Printed timestamp recorded
- ✅ Printed by user recorded

#### TC8.3 - Amount to Words Conversion
**Test Cases:**
- $123.45 → "One Hundred Twenty Three Dollars and 45/100"
- $1,000.00 → "One Thousand  Dollars and 00/100"
- $15,250.75 → "Fifteen Thousand Two Hundred Fifty  Dollars and 75/100"

**Expected Results:**
- ✅ All amounts convert correctly

#### TC8.4 - Cannot Print Non-Check Payment
**Steps:**
1. View ACH payment
2. Check for print button

**Expected Results:**
- ✅ Print button not visible
- ✅ Or error message

---

### Test Suite 9: Bill Application Logic

#### TC9.1 - Bill Balance Updates
**Steps:**
1. Create bill with $1,000 amount
2. Create payment applying $600 to bill
3. Check bill

**Expected Results:**
- ✅ Bill `paid_amount` = $600
- ✅ Bill `balance` = $400 (generated column)
- ✅ Bill `status` = "Partial"

#### TC9.2 - Bill Fully Paid
**Steps:**
1. Apply payment for full bill balance

**Expected Results:**
- ✅ Bill `paid_amount` = `total_amount`
- ✅ Bill `balance` = $0
- ✅ Bill `status` = "Paid"

#### TC9.3 - Multiple Payments to Same Bill
**Steps:**
1. Create payment 1: $400 on $1,000 bill
2. Create payment 2: $600 on same bill

**Expected Results:**
- ✅ Bill `paid_amount` = $1,000
- ✅ Two application records exist
- ✅ Bill status = "Paid"

#### TC9.4 - Void Reverses Bill Application
**Steps:**
1. Create payment applying $600 to bill
2. Void the payment

**Expected Results:**
- ✅ Bill `paid_amount` reduced by $600
- ✅ Bill `balance` increases by $600
- ✅ Bill status reverts (e.g., "Paid" → "Partial" or "Approved")

---

### Test Suite 10: Permission Testing

#### TC10.1 - View Permission
**Steps:**
1. Login as user WITHOUT `vendor_payments.view`
2. Try to access `/vendor-payments`

**Expected Results:**
- ✅ Access denied / redirect
- ✅ Error message

#### TC10.2 - Create Permission
**Steps:**
1. Login as user WITHOUT `vendor_payments.create`
2. View payment list

**Expected Results:**
- ✅ "New Payment" button hidden
- ✅ Cannot access `/vendor-payments/create`

#### TC10.3 - Edit Permission
**Steps:**
1. Login as user WITHOUT `vendor_payments.edit`
2. View draft payment

**Expected Results:**
- ✅ "Edit" button hidden
- ✅ Cannot access edit URL

#### TC10.4 - Delete Permission
**Steps:**
1. Login as user WITHOUT `vendor_payments.delete`

**Expected Results:**
- ✅ "Delete" button hidden

#### TC10.5 - Void Permission
**Steps:**
1. Login as user WITHOUT `vendor_payments.void`

**Expected Results:**
- ✅ "Void Payment" button hidden

#### TC10.6 - Print Permission
**Steps:**
1. Login as user WITHOUT `vendor_payments.print_checks`

**Expected Results:**
- ✅ "Print Check" button hidden

---

### Test Suite 11: Data Integrity & Edge Cases

#### TC11.1 - CSRF Protection
**Steps:**
1. Try to submit form without CSRF token

**Expected Results:**
- ✅ Request rejected
- ✅ Security error

#### TC11.2 - SQL Injection Prevention
**Steps:**
1. Try entering SQL in search: `' OR '1'='1`

**Expected Results:**
- ✅ Treated as literal string
- ✅ No database error
- ✅ No unauthorized data access

#### TC11.3 - XSS Prevention
**Steps:**
1. Enter `<script>alert('XSS')</script>` in memo field

**Expected Results:**
- ✅ Saved as text (escaped)
- ✅ Displays as text, not executed
- ✅ No alert popup

#### TC11.4 - Decimal Precision
**Steps:**
1. Enter amount: $123.456 (3 decimals)

**Expected Results:**
- ✅ Rounds or truncates to $123.46
- ✅ Consistent handling

#### TC11.5 - Zero Amount Payment
**Steps:**
1. Select bills but enter $0.00 amounts

**Expected Results:**
- ✅ Validation prevents submission
- ✅ Error message

#### TC11.6 - Negative Amount
**Steps:**
1. Try to enter negative amount

**Expected Results:**
- ✅ HTML5 min="0" prevents it
- ✅ Or server validation rejects

#### TC11.7 - Amount Exceeds Bill Balance
**Steps:**
1. Enter $1,500 on $1,000 bill

**Expected Results:**
- ✅ HTML5 max validation prevents it
- ✅ Or server validation rejects

#### TC11.8 - Concurrent Edit Conflict
**Steps:**
1. User A opens payment for edit
2. User B edits and saves same payment
3. User A tries to save

**Expected Results:**
- ✅ Last write wins (or)
- ✅ Conflict detection with error

#### TC11.9 - Deleted Vendor
**Steps:**
1. Create payment for vendor
2. Delete vendor (if allowed)
3. View payment

**Expected Results:**
- ✅ Payment still displays
- ✅ Vendor name preserved (or FK prevents deletion)

#### TC11.10 - Unicode/Special Characters
**Steps:**
1. Enter memo: "Payment für Händler – €100"

**Expected Results:**
- ✅ Saves correctly
- ✅ Displays correctly
- ✅ No encoding issues

---

### Test Suite 12: Performance & Usability

#### TC12.1 - Large Bill List Performance
**Steps:**
1. Vendor with 100+ unpaid bills
2. Select vendor in create form

**Expected Results:**
- ✅ AJAX loads in < 2 seconds
- ✅ Table renders smoothly
- ✅ Checkboxes responsive

#### TC12.2 - Real-time Calculation Speed
**Steps:**
1. Select 10 bills rapidly
2. Change amounts quickly

**Expected Results:**
- ✅ Totals update instantly
- ✅ No lag or delay

#### TC12.3 - Mobile Responsiveness
**Steps:**
1. Access on mobile device (or resize browser)

**Expected Results:**
- ✅ Table scrolls horizontally
- ✅ Cards stack vertically
- ✅ Forms usable
- ✅ Buttons accessible

#### TC12.4 - Browser Compatibility
**Test on:**
- Chrome
- Firefox
- Safari
- Edge

**Expected Results:**
- ✅ All features work
- ✅ Consistent appearance

---

## 🐛 Defect Reporting Template

```markdown
**Defect ID:** VPM-XXX
**Title:** [Brief description]
**Severity:** Critical / High / Medium / Low
**Test Case:** TC#.#
**Steps to Reproduce:**
1. 
2. 
3. 

**Expected Result:**

**Actual Result:**

**Screenshots:** [Attach if applicable]
**Browser/Environment:**
**Notes:**
```

---

## ✅ Test Completion Checklist

- [ ] All 90+ test cases executed
- [ ] Critical path tests passed (Create → View → Edit → Post → Void)
- [ ] Permission tests passed for all roles
- [ ] Data integrity tests passed
- [ ] No critical or high-severity defects open
- [ ] Performance acceptable (page loads < 3s)
- [ ] Check printing produces valid output
- [ ] Journal entries balance correctly
- [ ] Bill balances calculate correctly
- [ ] AJAX endpoints respond correctly
- [ ] Search and filters work as expected
- [ ] Pagination handles edge cases
- [ ] Mobile responsive on common devices
- [ ] Cross-browser testing complete

---

## 📊 Test Metrics to Track

- **Total Tests:** 90+
- **Pass Rate Target:** 95%+
- **Critical Defects:** 0
- **High Defects:** ≤ 2
- **Medium Defects:** ≤ 5
- **Performance:** All pages < 3s load time
- **Code Coverage:** Backend 80%+ (if testing)

---

## 🚀 Sign-Off Criteria

**Ready for Production when:**
1. ✅ All critical path tests pass
2. ✅ All critical/high defects resolved
3. ✅ Permission system verified
4. ✅ Data integrity confirmed
5. ✅ Check printing validated
6. ✅ Journal entries balance
7. ✅ User acceptance testing complete

---

**Test Plan Version:** 1.0  
**Last Updated:** 2025-11-15  
**Next Review:** After initial test cycle
