# Cash Flow Integration for Voided/Deleted Sales

This document explains how the audit trail system integrates with the cash flow management system when sales are voided or deleted.

## Overview

When voiding or deleting sales, the system now automatically records cash flow transactions to maintain accurate cash tracking throughout the day.

## What Gets Recorded

### For Cash Sales Only
- **Void Sales**: Records a negative cash movement with type `void_sale`
- **Delete Sales**: Records a negative cash movement with type `void_sale`
- **Amount**: The negative amount of the original sale
- **Description**: "VOIDED Sale #[ID] - Reason: [reason]" or "DELETED Sale #[ID] - Reason: [reason]"

### For Credit/GCash Sales
- **No cash flow impact**: Only credit sales or GCash sales do not affect cash flow
- **Credit balances**: Still handled separately for credit customers

## Technical Implementation

### Database Changes
1. **New Movement Type**: Added `void_sale` to the `movement_type` ENUM in `cash_movements` table
2. **Auto Cash Shift**: System automatically ensures a cash shift exists for the current user

### Process Flow

1. **User voids/deletes a sale**
2. **System checks payment method**
3. **For cash sales**:
   - Gets the cashier ID (current user)
   - Ensures a cash shift exists for today
   - Records negative cash movement
   - Updates shift expected balance
4. **Stock and audit trail**: Continues with normal void/delete process

### Example Cash Flow Entry
```
Movement Type: void_sale
Reference Type: sale
Reference ID: 123
Amount: -150.00
Description: VOIDED Sale #123 - Reason: Customer changed mind
Cashier: John Doe
Date: 2024-01-15
```

## Benefits

1. **Accurate Cash Tracking**: Cash drawer balance reflects voided sales
2. **Complete Audit Trail**: Both sales audit and cash flow audit
3. **Shift Reconciliation**: End-of-day counts are accurate
4. **Management Reporting**: Clear visibility of voided sales impact on cash

## Viewing Cash Flow Impact

### In Cash Flow Management (cash_flow.php)
- Voided sales appear as orange "VOID SALE" badges
- Negative amounts show in red
- Running balance is automatically updated
- Descriptions include void reason

### In Receipts View
- Sales show as "Voided" with strikethrough amounts
- Original audit trail system still tracks all changes

## Important Notes

1. **Only Cash Sales**: GCash and Credit sales do not create cash movements
2. **Automatic Shift Creation**: System creates cash shift if none exists
3. **Negative Amounts**: Void/delete always record negative amounts
4. **Same Day Recording**: Cash movements are recorded for the day the void/delete happens (not original sale date)
5. **Cashier Context**: Movements are tied to the user performing the void/delete

## Migration Required

To enable this feature, you must:
1. Run the updated migration script: `php run_audit_migration.php`
2. This will add the `void_sale` movement type to your cash_movements table

## Troubleshooting

### Cash Movement Not Recording
- Check if user has an active cash shift
- Verify payment method is 'cash'
- Check error logs for permission issues

### Wrong Amount/Balance
- Void amounts are always negative
- Check if multiple voids were processed
- Verify shift expected balance updates

This integration ensures complete financial accuracy and audit compliance for your POS system. 