# Booking Type and Category Implementation
## Overview
Successfully implemented `booking_type` and `booking_category` fields throughout the ZapaziMe booking system to enable proper classification and filtering of bookings, with a specialized export module for accommodation guest registries.
---
## 1. Database Changes
### Migration: `2025_10_20_141600_add_booking_type_and_category_to_bookings.php`
**Added Fields:**
- `booking_type` - ENUM('spots', 'rentals', 'services') - Required field with default 'spots'
- `booking_category` - VARCHAR - Nullable field for specific category (e.g., 'apartment', 'hotel', 'vehicle')
**Indexes Added:**
- `booking_type` - Single column index
- `booking_category` - Single column index
- `[booking_type, booking_category]` - Composite index for combined queries
**To Apply:**
```bash
php artisan migrate
```
---
## 2. Model Updates
### `app/Models/Booking.php`
**Added to $fillable:**
```php
'booking_type', // spots, rentals, services
'booking_category', // apartment, hotel, vehicle, medical, etc.
```
**New Scopes:**
- `scopeByType($query, string $type)` - Filter by booking type
- `scopeByCategory($query, string $category)` - Filter by category
- `scopeRentalAccommodation($query)` - Get rental accommodation bookings (apartments, hotels, guesthouses, houses)
**New Methods:**
- `isRentalAccommodation(): bool` - Check if booking is rental accommodation type
---
## 3. Admin Panel Updates
### `app/Filament/Resources/BookingResource/Forms/BookingForm.php`
**Added Fields in "Booking Details" Section:**
1. **Booking Type Select:**
- Options: Spots, Rentals, Services
- Required field with emoji icons
- Live/reactive field
- Default: 'spots'
2. **Booking Category Select:**
- Dynamic options based on selected booking type
- Categories per type:
- **Spots:** beach, parking, camping, park, sports, events, restaurant, bar
- **Rentals:** apartment, house, hotel, guesthouse, vehicle, boat, motorcycle, equipment
- **Services:** medical, legal, beauty, consulting, home, education
- Searchable dropdown
- Nullable field
**Grid Layout:** Changed from 2 columns to 3 columns to accommodate new fields
---
## 4. Export Module for Accommodation Guests
### `app/Filament/Resources/BookingResource/Pages/ExportAccommodationGuests.php`
**Purpose:** Export guest registry for rental accommodation bookings (required for police reports and accommodation registries)
**Features:**
- Date range filtering (from/to dates)
- Category filtering (all, apartment, hotel, guesthouse, house)
- Status filtering (all, confirmed, checked_in, checked_out, completed)
- CSV export with comprehensive guest data
**Export Fields:**
- Booking Number
- Booking Date
- Check-in / Check-out dates and times
- Venue name
- Category
- Status
- Guest First Name / Last Name
- Guest Email / Phone
- ID Document Type / Number
- Nationality
- Date of Birth
- Gender
- Total Adults / Children
- Total Amount
**Statistics Dashboard:**
- Active accommodation bookings count
- Current month bookings count
- Total guests registered
**Access:** `/admin/bookings/export-guests`
### `resources/views/filament/resources/booking-resource/pages/export-accommodation-guests.blade.php`
Professional Blade view with:
- Information card explaining the export purpose
- Filter form with date pickers and selects
- Statistics cards showing real-time counts
- Export fields reference list
- Download CSV button
---
## 5. Web Booking Integration
### `app/Http/Controllers/BookingController.php`
**Updated `store()` Method:**
1. **Added Validation Rule:**
```php
'booking_category' => 'nullable|string|max:50',
```
2. **Booking Data Creation:**
```php
'booking_type' => $bookingType,
'booking_category' => $request->input('booking_category') ?? $this->inferCategoryFromVenue($venue),
```
3. **New Helper Method: `inferCategoryFromVenue()`**
- Automatically determines category from venue data
- Checks venue category relationship first
- Falls back to name/type pattern matching
- Supports all major categories (accommodation, vehicles, spots, services)
**Pattern Matching Logic:**
- Hotel: 'hotel' in name/type
- Apartment: 'apartment' in name/type
- Guest House: 'guest house' or 'guesthouse' in name
- House/Villa: 'villa' or 'house' in name
- Vehicle: 'car' or 'vehicle' in name/type
- Boat: 'boat' or 'yacht' in name
- Beach: 'beach' in name/type
- Parking: 'parking' in name/type
- Medical: 'medical' or 'doctor' in name
- Legal: 'legal' or 'lawyer' in name
---
## 6. Category Definitions
### Spots Categories:
- `beach` - Beach Spots
- `parking` - Parking Lots
- `camping` - Camper Lots
- `park` - Park Areas
- `sports` - Sports Courts
- `events` - Event Spaces
- `restaurant` - Restaurant
- `bar` - Bar
### Rentals Categories:
- `apartment` - Apartments ⭐ **Accommodation**
- `house` - Houses & Villas ⭐ **Accommodation**
- `hotel` - Hotel ⭐ **Accommodation**
- `guesthouse` - Guest House ⭐ **Accommodation**
- `vehicle` - Cars & Vehicles
- `boat` - Boats & Yachts
- `motorcycle` - Motorcycles
- `equipment` - Equipment
### Services Categories:
- `medical` - Medical Services
- `legal` - Legal Services
- `beauty` - Beauty & Wellness
- `consulting` - Business Consulting
- `home` - Home Services
- `education` - Education & Training
---
## 7. Business Logic
### Accommodation Filtering
Bookings are considered "accommodation" if:
- `booking_type` = 'rentals' **AND**
- `booking_category` IN ('apartment', 'hotel', 'guesthouse', 'house')
This distinction is critical for:
- Guest registry exports (legal requirement)
- Police reports
- Tourism statistics
- Tax reporting
- Different business rules and regulations
### Non-Accommodation Rentals
Vehicle rentals, boat rentals, equipment rentals are excluded from accommodation exports as they don't require guest registration.
---
## 8. Usage Examples
### Query Accommodation Bookings:
```php
// Using scope
$accommodationBookings = Booking::rentalAccommodation()->get();
// Manual query
$bookings = Booking::where('booking_type', 'rentals')
->whereIn('booking_category', ['apartment', 'hotel', 'guesthouse', 'house'])
->get();
// Check if booking is accommodation
if ($booking->isRentalAccommodation()) {
// Handle accommodation-specific logic
}
```
### Filter by Type and Category:
```php
// Get all hotel bookings
$hotelBookings = Booking::byType('rentals')
->byCategory('hotel')
->get();
// Get all medical service bookings
$medicalBookings = Booking::byType('services')
->byCategory('medical')
->get();
```
---
## 9. Testing Checklist
- [ ] Run migration successfully
- [ ] Create new booking in admin panel with booking_type and booking_category
- [ ] Edit existing booking and update booking_type/booking_category
- [ ] Create booking from web interface (verify auto-inference works)
- [ ] Export accommodation guests CSV
- [ ] Filter export by date range
- [ ] Filter export by category
- [ ] Filter export by status
- [ ] Verify statistics on export page are accurate
- [ ] Test with bookings that have multiple guests
- [ ] Test with bookings that have no guests (should use user data)
---
## 10. Future Enhancements
1. **Automatic Category Detection:**
- Improve `inferCategoryFromVenue()` with ML/AI
- Add category suggestions based on venue amenities
2. **Category-Specific Validations:**
- Different required fields per category
- Category-specific pricing rules
3. **Reporting:**
- Category-based revenue reports
- Occupancy rates per accommodation type
- Service booking analytics
4. **Integration:**
- Automatic submission to tourism authorities
- Police registry API integration
- Tax reporting per category
---
## 11. Important Notes
⚠️ **Critical for Compliance:**
- The accommodation guest export is required by law in many jurisdictions
- Guest data must be reported to police/tourism authorities
- Proper categorization ensures correct reporting
⚠️ **Data Integrity:**
- Always set `booking_type` when creating bookings
- `booking_category` should match the venue's actual category
- Use the provided scopes for consistent filtering
⚠️ **Performance:**
- Composite index on `[booking_type, booking_category]` ensures fast queries
- Export module uses eager loading to prevent N+1 queries
---
## Files Modified/Created
### Created:
1. `database/migrations/2025_10_20_141600_add_booking_type_and_category_to_bookings.php`
2. `app/Filament/Resources/BookingResource/Pages/ExportAccommodationGuests.php`
3. `resources/views/filament/resources/booking-resource/pages/export-accommodation-guests.blade.php`
4. `BOOKING_TYPE_CATEGORY_IMPLEMENTATION.md` (this file)
### Modified:
1. `app/Models/Booking.php` - Added fillable fields, scopes, and helper methods
2. `app/Filament/Resources/BookingResource/Forms/BookingForm.php` - Added booking_type and booking_category fields
3. `app/Filament/Resources/BookingResource.php` - Registered export page route
4. `app/Http/Controllers/BookingController.php` - Added booking_type/category to web bookings
5. `app/Filament/Resources/BookingResource/Pages/CreateBooking.php` - Handle new fields on creation
6. `app/Filament/Resources/BookingResource/Pages/EditBooking.php` - Already handles via form
---
## Support
For questions or issues related to this implementation, refer to:
- Laravel Documentation: https://laravel.com/docs
- Filament Documentation: https://filamentphp.com/docs
- Project memories about polymorphic booking system