# Mobile Apps for M1 ERP

Two Progressive Web Apps (PWAs) for mobile data collection that integrate with the ERP system.

## 📱 Apps Overview

### 1. Visitor Log App (Tablet)
**URL:** `http://your-domain.com/apps/visitor-log/`

**Features:**
- Camera-based photo capture of visitors
- Contact information collection (name, email, phone)
- Offline capability with automatic sync
- Optimized for tablet devices

**Use Case:** Reception desk visitor check-in system

### 2. Inventory Scanner App (Mobile)
**URL:** `http://your-domain.com/apps/inventory-scanner/`

**Features:**
- Barcode scanning using device camera
- Photo capture of inventory items
- Bin location and quantity entry
- Batch upload to ERP
- Offline mode with local storage

**Use Case:** Warehouse inventory counting and bin location tracking

## 🚀 Installation

### Step 1: Run Database Migrations

```bash
# Connect to database
mysql -u rpmbbu -p brickwal_m1_ds

# Run migrations in order
mysql -u rpmbbu -p brickwal_m1_ds < database/migrations/030_visitor_log.sql
mysql -u rpmbbu -p brickwal_m1_ds < database/migrations/031_inventory_scanner.sql
```

### Step 2: Set Folder Permissions

```bash
# Create upload directories
mkdir -p uploads/visitors
mkdir -p uploads/inventory_scans

# Set permissions
chmod -R 777 uploads/visitors
chmod -R 777 uploads/inventory_scans
```

### Step 3: Install Apps on Devices

#### For iOS (iPad/iPhone):
1. Open Safari and navigate to the app URL
2. Tap the Share button (square with arrow)
3. Scroll and tap "Add to Home Screen"
4. Name the app and tap "Add"
5. The app will appear on your home screen like a native app

#### For Android (Tablet/Phone):
1. Open Chrome and navigate to the app URL
2. Tap the three-dot menu
3. Select "Add to Home screen" or "Install app"
4. Confirm installation
5. The app will appear in your app drawer

## 📖 Usage Guide

### Visitor Log App

**For Reception Staff:**

1. **Open the app** from your tablet home screen
2. **Take a photo**: Tap "📸 Take Photo" when the visitor is ready
3. **Fill in details**:
   - First Name (required)
   - Last Name (required)
   - Email (optional)
   - Phone (optional)
4. **Check In**: Tap "✓ Check In"
5. **Confirmation**: Success message appears, form resets for next visitor

**Offline Mode:**
- If internet is unavailable, visitor data is saved locally
- When connection is restored, data syncs automatically
- Pending visitors are shown in the app

**Tips:**
- Ensure good lighting for photos
- Keep the tablet charged throughout the day
- Review synced data in ERP admin panel

### Inventory Scanner App

**For Warehouse Staff:**

1. **Open the app** from your phone home screen
2. **Scan or Enter**:
   - Tap "📷 Scan Barcode" to use camera
   - Or manually type barcode
3. **Optional Photo**: Tap "📸 Take Photo" to capture item image
4. **Enter Details**:
   - Bin Location (optional)
   - Quantity (default: 1)
5. **Add to List**: Tap "➕ Add to List"
6. **Repeat** for all items in your count
7. **Sync**: Tap "☁️ Sync to Server" when done

**Batch Mode:**
- Scan multiple items before syncing
- All scans are stored locally until synced
- Can clear list if mistakes are made

**Offline Mode:**
- Continue scanning even without internet
- Scans saved in device storage
- Sync when back in Wi-Fi range

**Tips:**
- Use rear camera for better barcode scanning
- Ensure barcode is in focus and well-lit
- Sync frequently to avoid losing data

## 🔧 Admin Management

### View Visitor Logs

Access visitor data through ERP:
```sql
SELECT * FROM visitors 
WHERE DATE(check_in_time) = CURDATE()
ORDER BY check_in_time DESC;
```

Or via API:
```bash
GET /api/visitor-log?date=2026-01-21
```

### View Inventory Scans

Access scan data:
```sql
SELECT s.*, p.name as product_name 
FROM inventory_scans s
LEFT JOIN products p ON s.product_id = p.id
WHERE s.status = 'pending'
ORDER BY s.scanned_at DESC;
```

Or via API:
```bash
GET /api/inventory-scan?status=pending
```

### Processing Scans

Inventory scans have statuses:
- **pending**: Newly scanned, awaiting review
- **processed**: Reviewed and accepted
- **verified**: Physically verified
- **rejected**: Rejected with reason

Update status in database:
```sql
UPDATE inventory_scans 
SET status = 'processed', 
    processed_by = USER_ID,
    processed_at = NOW()
WHERE id = SCAN_ID;
```

## 🛠️ Technical Details

### Architecture
- **Frontend**: Vanilla JavaScript PWAs
- **Backend**: PHP API controllers
- **Storage**: MariaDB database
- **Offline**: IndexedDB (Visitor Log), LocalStorage (Inventory Scanner)
- **Images**: Base64 encoding, stored in uploads folder

### API Endpoints

**Visitor Log:**
- `POST /api/visitor-log` - Create visitor entry
- `GET /api/visitor-log` - List visitors (with filters)

**Inventory Scanner:**
- `POST /api/inventory-scan` - Create single scan
- `POST /api/inventory-scan/batch` - Create multiple scans
- `GET /api/inventory-scan` - List scans (with filters)

### Database Tables

**visitors:**
- id, first_name, last_name, email, phone
- photo_path, check_in_time, location_id
- notes, created_at, updated_at

**inventory_scans:**
- id, product_id, barcode, bin_location, quantity
- photo_path, scanned_by, scanned_at, location_id
- status, processed_at, processed_by
- notes, created_at, updated_at

### Security
- Sessions are used for API authentication
- File uploads are validated and stored securely
- SQL injection prevented via prepared statements
- XSS protection on all outputs

## 🔍 Troubleshooting

### Camera Not Working
- **iOS**: Check Settings > Safari > Camera access
- **Android**: Check Chrome > Settings > Site Settings > Camera
- Ensure HTTPS or localhost for camera access

### App Not Syncing
- Check internet connection
- Verify API endpoints are accessible
- Check browser console for errors
- Ensure database tables exist

### Photos Not Saving
- Verify upload folder permissions (777)
- Check disk space
- Review PHP error logs
- Confirm BASE_PATH constant is set

### Barcode Scanning Issues
- Ensure good lighting
- Hold camera steady and close to barcode
- Try manual entry if scanning fails
- Check if barcode format is supported

## 📊 Reporting

### Visitor Analytics
```sql
-- Daily visitor count
SELECT DATE(check_in_time) as date, COUNT(*) as visitors
FROM visitors
GROUP BY DATE(check_in_time)
ORDER BY date DESC
LIMIT 30;

-- Visitor trends by hour
SELECT HOUR(check_in_time) as hour, COUNT(*) as count
FROM visitors
WHERE DATE(check_in_time) = CURDATE()
GROUP BY HOUR(check_in_time);
```

### Inventory Scan Analytics
```sql
-- Scans by user
SELECT u.username, COUNT(*) as scans
FROM inventory_scans s
JOIN users u ON s.scanned_by = u.id
GROUP BY u.username
ORDER BY scans DESC;

-- Products scanned today
SELECT p.name, COUNT(*) as times_scanned
FROM inventory_scans s
JOIN products p ON s.product_id = p.id
WHERE DATE(s.scanned_at) = CURDATE()
GROUP BY p.id;
```

## 🔄 Updates & Maintenance

### Updating Apps
1. Edit files in `public/apps/visitor-log/` or `public/apps/inventory-scanner/`
2. Update version in manifest.json
3. Users will get updates automatically on next launch

### Backup
```bash
# Backup visitor data
mysqldump -u rpmbbu -p brickwal_m1_ds visitors > visitors_backup.sql

# Backup inventory scans
mysqldump -u rpmbbu -p brickwal_m1_ds inventory_scans > scans_backup.sql

# Backup photos
tar -czf uploads_backup.tar.gz uploads/visitors uploads/inventory_scans
```

## 📞 Support

For issues or questions:
1. Check browser console for error messages
2. Review PHP error logs in debug/ folder
3. Test API endpoints directly
4. Verify database connectivity

---

**Version:** 1.0  
**Created:** 2026-01-21  
**Compatibility:** iOS 14+, Android 8+, Modern browsers
