# Truck Payment System - Complete Workflow Documentation

## Overview
This document explains the complete truck payment workflow with support for returns, expenses, and proper accounting treatment.

## Business Workflow

### 1. Goods Given to Driver (Credit Sale)
**Example:** Store gives driver goods worth GH₵120,000 on credit

**Accounting Entry (Auto-posted when truck sales confirmed):**
```
DR Accounts Receivable - Truck    120,000
DR COGS                             80,000
    CR Revenue                               110,000
    CR VAT Payable                             9,174
    CR NHIL Payable                            2,294
    CR GETFund Payable                           458
    CR COVID Levy Payable                         74
    CR Inventory                              80,000
```

### 2. Driver Returns with Payment
Driver comes back and reports:
- **Total collections from customers:** GH₵90,000
  - Cash: GH₵50,000
  - MoMo: GH₵20,000
  - Bank: GH₵13,000
  - Fuel paid: GH₵3,000
  - Repairs paid: GH₵4,000

- **Goods returned (unsold):** GH₵30,000 worth

**Payment Calculation:**
```
Collections Breakdown:
  Cash:         50,000
  MoMo:         20,000
  Bank:         13,000
  Fuel:          3,000 (expense)
  Repairs:       4,000 (expense)
  ─────────────────────
  Net Payment:  90,000

Outstanding Balance Calculation:
  Goods Given:      120,000
  Less: Returned:   (30,000)
  ─────────────────────────
  Actual Sold:       90,000
  Less: Payment:    (90,000)
  ─────────────────────────
  Outstanding:           0
```

**Accounting Entry (Posted when payment is posted):**
```
Collections & Expenses (reduce AR):
DR Cash                             50,000
DR MoMo Account                     20,000
DR Bank Account                     13,000
DR Fuel Expense                      3,000
DR Vehicle Maintenance               4,000
    CR Accounts Receivable - Truck          90,000

Goods Returned (reverse sale):
DR Inventory                        20,000  (cost of returned goods)
DR Sales Returns                    30,000  (contra revenue)
    CR COGS                                 20,000  (contra expense)
    CR Accounts Receivable - Truck          30,000
```

### 3. Partial Payment Scenario
Driver returns with:
- Collections: GH₵60,000 (Cash: 35k, MoMo: 15k, Bank: 10k)
- Expenses: GH₵5,000 (Fuel: 3k, Repairs: 2k)
- No returns
- Net payment: GH₵65,000

**Result:**
```
Goods Given:       120,000
Net Payment:       (65,000)
─────────────────────────
Outstanding:        55,000  (Driver still owes this amount)
```

**Accounting Entry:**
```
DR Cash                             35,000
DR MoMo Account                     15,000
DR Bank Account                     10,000
DR Fuel Expense                      3,000
DR Vehicle Maintenance               2,000
    CR Accounts Receivable - Truck          65,000
```

### 4. Adding New Stock to Existing Invoice
**Scenario:** Driver still has GH₵55,000 outstanding, but needs more goods worth GH₵40,000

**Process:**
1. Create new truck sales for additional GH₵40,000 (use same invoice or new invoice)
2. System updates AR automatically when invoice is confirmed

**New Outstanding:**
```
Previous Outstanding:  55,000
New Goods Added:      40,000
─────────────────────────
Total Outstanding:    95,000
```

## Payment Form Fields Explained

### Goods Tracking
- **Goods Given to Driver:** Total value of goods from truck_sales invoice (auto-filled, readonly)
- **Goods Returned (Value):** Value of unsold goods physically returned to store (user input)
- **Actual Goods Sold:** Goods Given - Goods Returned (auto-calculated)

### Collections
- **Cash Payment:** Physical cash received from driver
- **MoMo Payment:** Mobile money received
- **Bank Payment:** Bank transfer/deposit received

### Expenses (Store Expenses - Distribution Costs)
- **Fuel Expense:** Fuel cost paid by driver during trip
- **Maintenance:** Vehicle repairs/maintenance paid by driver
- **Other Expenses:** Any other trip-related expenses

### Summary Calculations
- **Total Collections:** Cash + MoMo + Bank (money physically collected)
- **Total Expenses:** Fuel + Maintenance + Other (costs incurred)
- **Net Payment:** Total Collections + Total Expenses (what driver is accounting for)
- **Outstanding Balance:** Actual Goods Sold - Net Payment (what driver still owes)

## Accounting Treatment Summary

### Why Expenses Count as Payment
Fuel and maintenance expenses are **STORE EXPENSES** because:
1. They are distribution costs (cost of getting goods to customers)
2. The store owns the truck and goods
3. Driver is just a sales agent/distributor
4. These expenses reduce what the driver owes (they paid on behalf of the store)

### Key Accounting Principles
1. **When goods are given:** AR increases, Inventory decreases, Revenue recognized
2. **When driver pays:** Cash/Bank increases, AR decreases
3. **When expenses paid:** Expense accounts increase, AR decreases (same effect as payment)
4. **When goods returned:** Inventory increases, AR decreases, Sales Returns recorded

## Database Structure

### truck_payments Table
```sql
- invoice (string): Reference to truck_sales invoice
- truck_id (int): Reference to truck_details
- payment_date (date): Date of payment recording
- cash (decimal): Cash amount collected
- momo (decimal): Mobile money collected
- bank (decimal): Bank amount collected
- total_payment (decimal): Sum of cash+momo+bank
- fuel_expense (decimal): Fuel costs during trip
- maintenance_expense (decimal): Vehicle maintenance
- other_expense (decimal): Other trip expenses
- total_expenses (decimal): Sum of all expenses
- goods_sold_amount (decimal): Actual goods sold (given - returned)
- goods_returned_value (decimal): Value of returned goods
- net_payment (decimal): total_payment + total_expenses
- outstanding_balance (decimal): goods_sold_amount - net_payment
- posting_status (enum): 'unposted', 'posted', 'reversed'
```

## Common Scenarios

### Scenario A: Perfect Settlement
- Goods Given: 100,000
- Collections: 70,000
- Expenses: 5,000
- Returns: 25,000
- Net Payment: 75,000
- Actual Sold: 75,000
- **Outstanding: 0** ✓

### Scenario B: Partial Payment
- Goods Given: 100,000
- Collections: 50,000
- Expenses: 3,000
- Returns: 0
- Net Payment: 53,000
- Actual Sold: 100,000
- **Outstanding: 47,000** ⚠️

### Scenario C: Over-collection (Driver has change to return)
- Goods Given: 100,000
- Collections: 85,000
- Expenses: 5,000
- Returns: 5,000
- Net Payment: 90,000
- Actual Sold: 95,000
- **Outstanding: -5,000** (Store owes driver 5,000) ⚠️

## Best Practices

### For Store Managers
1. Always verify returned goods physically before accepting return value
2. Cross-check expense receipts when possible
3. Monitor outstanding balances regularly
4. Post to accounting only after verification

### For System Users
1. Record payment as soon as driver returns
2. Enter exact amounts for each payment method
3. Always enter goods returned if applicable
4. Add remarks for any unusual transactions
5. Post to accounting only once all amounts are verified

### For Accountants
1. Review journal entries before finalizing
2. Verify AR balances match payment records
3. Check that Sales Returns are properly offset against revenue
4. Ensure COGS adjustments match inventory changes

## Troubleshooting

### Q: Outstanding balance is negative, what does it mean?
**A:** Driver collected/paid more than the goods sold. Store owes the driver. Issue refund or credit to next sale.

### Q: Can I edit payment after posting?
**A:** No. Once posted, you must reverse the journal entry and create a new payment record.

### Q: What if driver partially returns goods?
**A:** Enter the exact value of returned goods. System will calculate outstanding correctly.

### Q: How do I handle damaged goods?
**A:** Use the expense "Other Expenses" field or create a separate stock reduction entry.

### Q: Can one driver have multiple outstanding invoices?
**A:** Yes. Each invoice is tracked separately. Total AR for the driver is sum of all outstanding balances.

## Migration Steps

### For Existing Systems
1. Run: `php artisan migrate` (if not run yet)
2. Execute SQL: `add_goods_returned_to_truck_payments.sql`
3. Refresh browser cache
4. Test with a dummy payment entry

### For New Installations
1. Run all migrations
2. Seed accounts with `php artisan db:seed --class=ChartOfAccountsSeeder`
3. Configure user privileges for truck_payment access
4. Create test truck and record sample payment

## Support & Contact
For issues or questions, refer to:
- TRUCK_SALES_ACCOUNTING_EXPLAINED.md
- TRUCK_PAYMENT_QUICK_REFERENCE.md
- ACCOUNTING_SYSTEM_DOCUMENTATION.md
