# 🗺️ ZONE-BASED PRICING SYSTEM

## Overview

The new pricing system allows admin to set different prices for each vehicle based on:
- **Journey Type**: Airport↔Address or Address↔Address
- **Service Zone**: 4 zones covering Greater Manchester, Chester, and Liverpool
- **Vehicle Type**: Different prices for each Mercedes model

---

## 🗺️ SERVICE ZONES

### **Zone A: Manchester City Center**
- **Postcodes**: M1, M2, M3, M4, M15
- **Coverage**: Manchester city center

### **Zone B: Greater Manchester**
- **Postcodes**: M5-M99, BL, OL, SK, WA, WN
- **Coverage**: Bolton, Oldham, Stockport, Warrington, Wigan

### **Zone C: Chester**
- **Postcodes**: CH (all CH postcodes)
- **Coverage**: Chester and surrounding areas

### **Zone D: Liverpool**
- **Postcodes**: L (all L postcodes)
- **Coverage**: Liverpool and Merseyside

---

## 💰 DEFAULT PRICING

### **Airport to Address**
| Zone | E-Class | S-Class | V-Class | Sprinter |
|------|---------|---------|---------|----------|
| Zone A (City Center) | £45 | £65 | £95 | £120 |
| Zone B (Greater Manchester) | £50 | £70 | £100 | £130 |
| Zone C (Chester) | £75 | £95 | £135 | £165 |
| Zone D (Liverpool) | £60 | £80 | £115 | £145 |

### **Address to Airport**
Same prices as Airport to Address (symmetric pricing).

### **Address to Address**
| From → To | E-Class | S-Class | V-Class | Sprinter |
|-----------|---------|---------|---------|----------|
| Zone A → Zone B | £30 | £40 | £55 | £70 |
| Zone A → Zone C | £55 | £70 | £95 | £120 |
| Zone A → Zone D | £45 | £60 | £80 | £100 |
| Zone B → Zone C | £60 | £75 | £100 | £125 |
| Zone B → Zone D | £50 | £65 | £85 | £105 |
| Zone C → Zone D | £70 | £85 | £110 | £140 |

*All reverse routes (e.g., Zone B → Zone A) have the same price as forward routes.*

---

## 🎛️ ADMIN MANAGEMENT

### **Admin Page URL**
https://3000-ibnwdfw71auepqpe8f3p3-b9b802c4.sandbox.novita.ai/admin-pricing-zones.html

### **Login Credentials**
- Email: `admin@ashtransfer.com`
- Password: `Global2026@`

### **Features**
1. **Zone Map**: Visual display of all 4 service zones with postcodes
2. **Three Tabs**:
   - Airport to Address pricing
   - Address to Airport pricing
   - Address to Address pricing
3. **Live Editing**: Change prices and they save automatically
4. **Real-time Updates**: Prices update instantly across all tabs

### **How to Change Prices**
1. Log in to admin panel
2. Go to "Pricing Zones Management" (new link on dashboard)
3. Select journey type tab (Airport→Address, Address→Airport, or Address→Address)
4. Find the zone card
5. Change price in the input field
6. Price saves automatically when you type!

---

## 📊 DATABASE

### **Table: `pricing_zones`**
```sql
CREATE TABLE pricing_zones (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  journey_type TEXT NOT NULL,      -- 'airport-to-address', 'address-to-airport', 'address-to-address'
  from_zone TEXT NOT NULL,          -- 'zone-a', 'zone-b', 'zone-c', 'zone-d', 'any-airport'
  to_zone TEXT NOT NULL,            -- 'zone-a', 'zone-b', 'zone-c', 'zone-d', 'any-airport'
  vehicle_id INTEGER NOT NULL,      -- 1, 2, 3, 4 (E-Class, S-Class, V-Class, Sprinter)
  price DECIMAL(10,2) NOT NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
```

### **Sample Data**
80 pricing rules covering all combinations:
- 16 Airport↔Address rules (4 zones × 4 vehicles)
- 16 Address↔Airport rules (4 zones × 4 vehicles)
- 48 Address↔Address rules (12 zone combinations × 4 vehicles)

---

## 🔌 API ENDPOINTS

### **1. Get All Pricing Rules (Admin)**
```http
GET /api/pricing-zones
Authorization: Bearer <admin_token>
```

**Response:**
```json
{
  "success": true,
  "rules": [
    {
      "id": 1,
      "journey_type": "airport-to-address",
      "from_zone": "any-airport",
      "to_zone": "zone-a",
      "vehicle_id": 1,
      "price": 45.00,
      "created_at": "2026-02-17 13:00:00",
      "updated_at": "2026-02-17 13:00:00"
    },
    ...
  ]
}
```

### **2. Create/Update Pricing Rule (Admin)**
```http
POST /api/pricing-zones
Authorization: Bearer <admin_token>
Content-Type: application/json

{
  "journey_type": "airport-to-address",
  "from_zone": "any-airport",
  "to_zone": "zone-a",
  "vehicle_id": 1,
  "price": 50.00
}
```

**Response:**
```json
{
  "success": true,
  "message": "Price updated successfully",
  "id": 1
}
```

### **3. Calculate Price for Journey (Public)**
```http
POST /api/pricing-zones/calculate
Content-Type: application/json

{
  "journey_type": "airport-to-address",
  "pickup_postcode": "M90 1QX",
  "dropoff_postcode": "M14 5PL",
  "vehicle_id": 1
}
```

**Response:**
```json
{
  "success": true,
  "price": 50.00,
  "from_zone": "any-airport",
  "to_zone": "zone-b"
}
```

---

## 🔄 HOW IT WORKS

### **Postcode Detection**
When customer enters a postcode, the system automatically determines the zone:

```javascript
function getZoneFromPostcode(postcode) {
  const prefix = postcode.match(/^[A-Z]+/)[0];  // Extract letters
  const number = postcode.match(/^[A-Z]+(\d+)/)[1];  // Extract number
  
  // Zone A: M1-M4, M15
  if (prefix === 'M' && (number >= 1 && number <= 4 || number === 15)) {
    return 'zone-a';
  }
  
  // Zone B: M5-M99, BL, OL, SK, WA, WN
  if (prefix === 'M' && number >= 5) return 'zone-b';
  if (['BL', 'OL', 'SK', 'WA', 'WN'].includes(prefix)) return 'zone-b';
  
  // Zone C: CH
  if (prefix === 'CH') return 'zone-c';
  
  // Zone D: L
  if (prefix === 'L') return 'zone-d';
  
  return null;  // Out of service area
}
```

### **Price Lookup**
1. Customer selects journey type and enters postcodes
2. System determines zones from postcodes
3. System queries database for matching price rule
4. If no rule found, falls back to vehicle base_price
5. Price displayed to customer

---

## 📱 EXAMPLE USE CASES

### **Example 1: Airport to City Center**
- **Customer**: Books from Manchester Airport (MAN) to M3 2AB
- **System**: Detects `M3` → Zone A (City Center)
- **Price**: Mercedes E-Class = £45

### **Example 2: Bolton to Chester**
- **Customer**: Books from BL1 (Bolton) to CH1 (Chester)
- **System**: Detects `BL` → Zone B, `CH` → Zone C
- **Price**: Mercedes S-Class = £75

### **Example 3: Liverpool to Airport**
- **Customer**: Books from L1 8JQ (Liverpool) to Manchester Airport
- **System**: Detects `L1` → Zone D
- **Price**: Mercedes V-Class = £115

---

## 🎨 ADMIN INTERFACE FEATURES

### **Visual Zone Map**
Color-coded zones with clear postcode indicators:
- 🔵 **Zone A (Blue)**: City Center - M1, M2, M3, M4, M15
- 🟣 **Zone B (Purple)**: Greater Manchester - M5-M99, BL, OL, SK, WA, WN
- 🟠 **Zone C (Orange)**: Chester - CH
- 🟢 **Zone D (Green)**: Liverpool - L

### **Price Management Cards**
Each zone has a card showing:
- Zone name and description
- Postcode coverage
- All 4 vehicle prices with edit inputs
- Auto-save on price change

### **Tab Navigation**
- **Airport to Address**: Set prices from airports to zones
- **Address to Airport**: Set prices from zones to airports
- **Address to Address**: Set prices between zones (matrix view)

---

## ⚙️ CONFIGURATION OPTIONS

### **Adding New Zones**
To add a new zone, edit `server/routes/pricing-zones.js`:

```javascript
// Add to getZoneFromPostcode function
if (prefix === 'NEW') return 'zone-e';

// Add to zones array in admin page
{ 
  id: 'zone-e', 
  name: 'Zone E: New Area', 
  postcodes: ['NEW'] 
}
```

### **Changing Zone Rules**
Admin can change:
- ✅ Individual vehicle prices
- ✅ All prices for a zone
- ✅ Different prices per journey type

Admin CANNOT change (requires code update):
- ❌ Zone boundaries
- ❌ Postcode assignments
- ❌ Number of zones

---

## 🚀 TESTING

### **Test Admin Panel**
```bash
# 1. Access admin page
https://3000-ibnwdfw71auepqpe8f3p3-b9b802c4.sandbox.novita.ai/admin-pricing-zones.html

# 2. Login
Email: admin@ashtransfer.com
Password: Global2026@

# 3. Change a price
- Select "Airport to Address" tab
- Find "Zone A: City Center" card
- Change Mercedes E-Class price from £45 to £50
- Watch it save automatically!

# 4. Test calculation
POST http://localhost:3000/api/pricing-zones/calculate
{
  "journey_type": "airport-to-address",
  "pickup_postcode": "M90 1QX",
  "dropoff_postcode": "M3 1AA",
  "vehicle_id": 1
}

# Expected: £50 (updated price)
```

---

## 📈 BENEFITS

### **For Admin**
- ✅ Easy price management
- ✅ Different prices for different areas
- ✅ Visual zone map
- ✅ Instant updates
- ✅ No technical knowledge needed

### **For Customers**
- ✅ Accurate pricing
- ✅ Fair zone-based prices
- ✅ Transparent calculations
- ✅ Instant quotes

### **For Business**
- ✅ Flexible pricing strategy
- ✅ Zone-based revenue optimization
- ✅ Easy seasonal adjustments
- ✅ Competitive pricing per area

---

## 🔧 TROUBLESHOOTING

### **Price not updating?**
- Check you're logged in as admin
- Verify browser isn't caching old page (hard refresh: Ctrl+Shift+R)
- Check browser console for errors

### **Wrong price calculated?**
- Verify postcode is in service area
- Check zone detection is correct
- Confirm pricing rule exists in database

### **Can't access admin page?**
- Verify server is running
- Check admin token is valid
- Clear browser cache and try again

---

## 📊 STATISTICS

- **Total Zones**: 4
- **Total Pricing Rules**: 80
- **Journey Types**: 3
- **Vehicles**: 4 (E-Class, S-Class, V-Class, Sprinter)
- **Admin Pages**: 16 (including new pricing zones page)
- **API Endpoints**: 3 new pricing zone endpoints

---

## 🎉 CONCLUSION

The zone-based pricing system provides:
- Flexible pricing management
- Clear zone boundaries
- Easy admin interface
- Automatic zone detection
- Fair and transparent pricing

**Ready to use!** 🚀
