# Business Registration Feature

## Overview
This feature allows any Google authenticated user to automatically register and create their own business/branch in the POS system. Users who don't have a branch assigned are guided through a business registration process.

## How It Works

### 1. User Authentication Flow
1. **Google Login**: Any user with a Gmail account can sign in
2. **User Creation**: If user doesn't exist, they're automatically added to the `users` table with `branch_id = NULL`
3. **Branch Check**: System checks if user has a branch assigned
4. **Registration Required**: Users without a branch are redirected to business registration

### 2. Business Registration Process
Users are presented with a form containing:
- **Business Type Selection**: Retail, Wholesale, Restaurant, Trader, Fast Food, Service, Other
- **Business Name**: Custom business name or brand
- **Phone Number**: 10-digit phone number (Philippines +63 format)

### 3. Branch Creation
Upon successful registration:
- New branch is created with auto-generated branch code (e.g., `a123`, `b456`)
- User is assigned as admin of their branch
- Branch tables are created with sample data
- Business info is stored in branch-specific `business_info` table

### 4. Cancellation Handling
If user cancels registration:
- Their temporary user record is deleted
- Session is cleared
- Redirected back to login page

## Files Modified/Created

### New Files
- `business_registration.php` - Registration form interface
- `business_registration_handler.php` - Form processing logic
- `scripts/update_user_registration_system.php` - Migration script
- `sql/migrations/allow_null_branch_users.sql` - Database migration
- `BUSINESS_REGISTRATION_FEATURE.md` - This documentation

### Modified Files
- `callback.php` - Updated to allow any Google user and handle branch assignment
- `includes/auth_check.php` - Updated to redirect pending users to registration
- `index.php` - Added welcome message for new registrations
- `sql/create_users_table.sql` - Allow NULL branch_id and add restrictions column
- `sql/branch_tables_template.sql` - Added business_info table template

## Database Changes

### Users Table
```sql
-- branch_id now allows NULL for pending users
`branch_id` INT NULL DEFAULT NULL

-- New restrictions column for user permissions
`restrictions` TEXT NULL
```

### New Branch Table
Each branch now gets a `business_info` table:
```sql
CREATE TABLE `{branch_code}_business_info` (
    `info_id` int(11) NOT NULL AUTO_INCREMENT,
    `business_type` enum('retail','wholesale','restaurant','trader','fast_food','service','other') NOT NULL,
    `business_name` varchar(255) NOT NULL,
    `phone` varchar(20) NOT NULL,
    `owner_name` varchar(255) NOT NULL,
    `owner_email` varchar(255) NOT NULL,
    `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`info_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```

## Session Management

### Temporary Session Variables
During registration process:
- `$_SESSION['temp_user_id']` - User ID being registered
- `$_SESSION['temp_user_name']` - User's name from Google
- `$_SESSION['temp_user_email']` - User's email from Google

### Final Session Variables
After successful registration:
- `$_SESSION['user_id']` - Confirmed user ID
- `$_SESSION['user_role']` - Set to 'admin'
- `$_SESSION['branch_id']` - New branch ID
- `$_SESSION['branch_code']` - Auto-generated branch code

## Branch Code Generation
- Format: One lowercase letter + 3 digits (e.g., `a123`, `z999`)
- Automatically checks for uniqueness
- Maximum 100 attempts to find unique code

## Business Types Available
1. **Retail** - General retail stores
2. **Wholesale** - Bulk/wholesale businesses
3. **Restaurant** - Full-service restaurants
4. **Trader** - Trading businesses
5. **Fast Food** - Quick service restaurants
6. **Service** - Service-based businesses
7. **Other** - Any other business type

## Phone Number Handling
- Format: 10 digits (Philippine mobile format)
- Stored with +63 prefix in database
- Client-side validation ensures proper format

## Security Features
- CSRF protection through proper session management
- Input validation and sanitization
- Automatic cleanup of abandoned registrations
- Branch isolation through proper table prefixing

## Installation/Migration

### For New Installations
1. Use the updated SQL files which include the new structure

### For Existing Installations
1. Run the migration script:
   ```bash
   php scripts/update_user_registration_system.php
   ```

2. This will:
   - Update users table structure
   - Add business_info table to all existing branches
   - Preserve existing user data

## Usage Examples

### New User Flow
1. User visits login page
2. Clicks "Sign in with Google"
3. Google authentication completes
4. System creates user record with NULL branch_id
5. User redirected to business registration
6. User fills form and submits
7. New branch created, user assigned as admin
8. User redirected to dashboard with welcome message

### Existing User Flow
1. User visits login page
2. Clicks "Sign in with Google"
3. System finds existing user with assigned branch
4. User redirected to dashboard normally

### Cancellation Flow
1. User starts registration process
2. User clicks "Cancel" link
3. System deletes temporary user record
4. User redirected to login page

## Error Handling
- Invalid business types rejected
- Duplicate email addresses handled
- Branch creation failures trigger cleanup
- Database errors properly logged
- User-friendly error messages displayed

## Future Enhancements
- Email verification before branch creation
- Business logo upload during registration
- Multi-language support for business types
- Business address collection
- Tax ID/Business license integration
- Business hours configuration

## Notes
- All new users are automatically assigned 'manager' role for their branch
- Sample products and categories are created for each new branch
- Business registration details are stored for future reference
- System maintains backward compatibility with existing installations 