# Promotion Debugging Guide

## Why Promotion #13 is Not Being Applied

A promotion can be skipped for several reasons. Use this guide to diagnose why promotion #13 is not being applied.

## Common Reasons Promotions Are Skipped

### 1. **Promotion Not Active**
- Check: `is_active = true`
- Location: Promotion edit page → Basic Info tab
- Fix: Enable the promotion

### 2. **Outside Date Range**
- Check: Current date is between `start_date` and `end_date`
- Location: Promotion edit page → Basic Info tab
- Fix: Update start/end dates to include current date

### 3. **Usage Limits Exceeded**
- Check: `max_global`, `max_per_customer`, `max_per_order` limits
- Location: Promotion edit page → Limits tab
- Fix: Increase limits or reset usage counts

### 4. **Branch Scope Mismatch**
- Check: If `branch_scope` is set, current location must be in the list
- Location: Promotion edit page → Where tab
- Fix: Add current location to branch scope, or clear branch scope to apply to all branches

### 5. **Customer Scope Mismatch**
- Check: If `customer_scope` is set, current customer must be in the list
- Location: Promotion edit page → Where tab
- Fix: Add current customer to customer scope, or clear customer scope to apply to all customers

### 6. **Voucher Required But Not Provided**
- Check: If promotion has vouchers, a matching voucher code must be provided
- Location: Promotion edit page → Codes tab
- Fix: Either provide the voucher code in the sales page, or remove vouchers from the promotion

### 7. **Conditions Not Met**
- Check: All conditions must be satisfied
- Common conditions:
  - `cart_total >= X` - Cart total must be >= X
  - `product_id = X` - Specific product must be in cart
  - `product_id >= X` - Quantity of specific product must be >= X
  - `category_id = X` - Product from category must be in cart
- Location: Promotion edit page → When tab
- Fix: Ensure cart meets all condition requirements

### 8. **Exclusive Promotion Already Applied**
- Check: If a non-stackable promotion was already applied, other non-stackable promotions are skipped
- Location: Promotion edit page → Basic Info tab → Stackable checkbox
- Fix: Enable "Stackable" if you want multiple promotions to apply

## How to Debug Promotion #13

### Step 1: Check Promotion Status
1. Go to `http://localhost:3000/admin/promotions/13/edit`
2. Check:
   - ✅ Is Active: Should be checked
   - ✅ Start Date: Should be <= today
   - ✅ End Date: Should be >= today

### Step 2: Check Branch/Customer Scope
1. Go to "Where" tab
2. Check:
   - Branch Scope: If set, ensure your current location is included
   - Customer Scope: If set, ensure the customer you're testing with is included

### Step 3: Check Vouchers
1. Go to "Codes" tab
2. If vouchers exist:
   - You MUST enter the voucher code in the sales page
   - Or remove vouchers if you want automatic application

### Step 4: Check Conditions
1. Go to "When" tab
2. Review all conditions:
   - Ensure your cart meets ALL conditions
   - Example: If condition is `cart_total >= 100`, your cart total must be >= 100

### Step 5: Check Limits
1. Go to "Limits" tab
2. Check:
   - Max Global: Not exceeded
   - Max Per Customer: Not exceeded for the customer you're testing with
   - Max Per Order: Not exceeded

### Step 6: Check Actions
1. Go to "What" tab
2. Ensure at least one action is configured (discount, free item, etc.)

## Debugging with Logs

The promotion engine logs detailed information. Check Laravel logs:

```bash
tail -f storage/logs/laravel.log | grep "Promotion\|promotion"
```

Look for these log messages:
- `🟡 Evaluating promotion` - Promotion is being checked
- `🚫 Promotion skipped` - Promotion was skipped (reason will be in log)
- `✅ Promotion conditions met` - Promotion passed all checks
- `❌ Promotion conditions not met` - Conditions failed

## Quick Test Checklist

When testing promotion #13:

- [ ] Promotion is active (`is_active = true`)
- [ ] Current date is within start/end date range
- [ ] No usage limits exceeded
- [ ] Branch scope matches (or is empty)
- [ ] Customer scope matches (or is empty)
- [ ] If vouchers exist, voucher code is provided
- [ ] All conditions are met
- [ ] At least one action is configured
- [ ] No exclusive promotion already applied (if not stackable)

## Common Issues and Solutions

### Issue: "Promotion has vouchers but no voucher code provided"
**Solution**: Either:
1. Enter the voucher code in the sales page promotion panel
2. Or remove vouchers from the promotion if you want automatic application

### Issue: "Promotion not in branch scope"
**Solution**: Either:
1. Add your current location to the branch scope in the "Where" tab
2. Or clear the branch scope to apply to all branches

### Issue: "Customer not in scope"
**Solution**: Either:
1. Add the customer to customer scope in the "Where" tab
2. Or clear the customer scope to apply to all customers

### Issue: "Conditions not met"
**Solution**: 
- Review conditions in the "When" tab
- Ensure your cart meets ALL condition requirements
- Example: If `cart_total >= 100`, add items until cart total >= 100

### Issue: "Usage limits exceeded"
**Solution**:
- Check limits in the "Limits" tab
- Increase limits or reset usage counts in the database

## Still Not Working?

If promotion #13 still doesn't apply after checking all above:

1. **Check Laravel logs** for detailed error messages
2. **Test with a simple promotion**:
   - No conditions
   - No branch/customer scope
   - No vouchers
   - Simple percentage discount
3. **Verify the promotion is in the active promotions list**:
   - Check `getActivePromotions()` returns promotion #13
4. **Check database directly**:
   ```sql
   SELECT * FROM promotions WHERE id = 13;
   SELECT * FROM promotion_conditions WHERE promotion_id = 13;
   SELECT * FROM promotion_actions WHERE promotion_id = 13;
   SELECT * FROM promotion_limits WHERE promotion_id = 13;
   SELECT * FROM vouchers WHERE promotion_id = 13;
   ```

