# 🚀 FakturaFlow — Deployment Guide
## boktech.se | cPanel Shared Hosting | PHP 8.2+

---

## YOUR SETUP SUMMARY

| Setting | Value |
|---------|-------|
| **Domain** | https://boktech.se |
| **PHP** | 8.2 or higher (8.2 / 8.3 / 8.4 all work) |
| **Webroot** | `/public_html/public` |
| **DB name** | `boktech_fakturaflow` |
| **DB user** | `boktech_fakturaflow_admin` |

---

## STEP 1 — Set PHP Version

1. Log in to **cPanel**
2. Search for **"MultiPHP Manager"**
3. Find **boktech.se** → select **PHP 8.2** (or 8.3 / 8.4 — all work)
4. Click **Apply**

---

## STEP 2 — Enable PHP Extensions

1. cPanel → **"Select PHP Version"** (or "PHP Extensions & Applications")
2. Click the **Extensions** tab
3. Enable these (tick each box):

   ```
   ✓ pdo_mysql      ✓ mbstring     ✓ openssl
   ✓ tokenizer      ✓ xml          ✓ json
   ✓ bcmath         ✓ curl         ✓ gd
   ✓ zip            ✓ fileinfo     ✓ imap
   ```

4. Click **Save**

---

## STEP 3 — Create MySQL Database

1. cPanel → **MySQL Databases**

2. **Create New Database:**
   ```
   Name: fakturaflow
   Full name (cPanel adds prefix): boktech_fakturaflow  ✓
   ```
   → Click **Create Database**

3. **Create New User:**
   ```
   Username: fakturaflow_admin
   Full name: boktech_fakturaflow_admin  ✓
   Password:  [create a strong password — SAVE IT!]
   ```
   → Click **Create User**

4. **Add User to Database:**
   - User: `boktech_fakturaflow_admin`
   - Database: `boktech_fakturaflow`
   → Click **Add**
   - On next screen: check ✓ **ALL PRIVILEGES**
   → Click **Make Changes**

---

## STEP 4 — Upload Files

### The folder structure on your server must be:

```
/home/yourusername/
└── public_html/          ← Upload ALL files here
    ├── app/
    ├── bootstrap/
    ├── config/
    ├── database/
    ├── resources/
    ├── routes/
    ├── storage/
    ├── artisan
    ├── composer.json
    ├── deploy.sh
    ├── .env.example
    └── public/           ← This IS your webroot (/public_html/public)
        ├── index.php
        ├── .htaccess
        ├── css/
        └── js/
```

### How to upload:

**Via cPanel File Manager:**
1. Open **File Manager**
2. Navigate to `/home/yourusername/public_html/`
3. Click **Upload** → upload `fakturaflow_boktech.zip`
4. Select the zip → click **Extract**
5. This creates `public_html/fakturaflow/` — you need to move files UP one level:
   - Select all files inside `fakturaflow/`
   - Move them to `public_html/`
   - Delete the now-empty `fakturaflow/` folder

**OR via FTP/SFTP** (FileZilla):
```
Host:     boktech.se  (or your server IP)
Username: your-cpanel-username
Password: your-cpanel-password
Port:     21 (FTP) or 22 (SFTP)

Upload contents of fakturaflow/ → to → public_html/
```

**OR via SSH:**
```bash
scp -r fakturaflow/* username@boktech.se:~/public_html/
```

---

## STEP 5 — Verify Document Root

Your cPanel should already have `public_html/public` as the document root.

**To check / confirm:**
1. cPanel → **Domains** → click **Manage** on boktech.se
2. Document root should show: `/home/yourusername/public_html/public`
3. If it shows `/home/yourusername/public_html` → change it to include `/public`

**If you cannot change document root:**
Create this file at `/home/yourusername/public_html/.htaccess`:
```apache
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
```

---

## STEP 6 — Configure .env File

1. In **File Manager**, go to `/home/yourusername/public_html/`
2. Find `.env.example`
3. **Right-click** → **Copy** → name it `.env`
4. **Right-click `.env`** → **Edit**
5. Fill in ONLY these values (everything else is pre-configured):

```env
DB_PASSWORD=YOUR_DATABASE_PASSWORD_FROM_STEP_3

MAIL_PASSWORD=your_email_password

STRIPE_KEY=pk_live_...
STRIPE_SECRET=sk_live_...

ANTHROPIC_API_KEY=sk-ant-...
```

6. Click **Save Changes**

---

## STEP 7 — Run the Deployment Script

### Via cPanel Terminal:
1. cPanel → search **"Terminal"**
2. Run:
   ```bash
   cd ~/public_html
   bash deploy.sh
   ```

### Via SSH:
```bash
ssh username@boktech.se
cd ~/public_html
bash deploy.sh
```

The script will (automatically):
- ✓ Install all PHP packages via Composer (~2-3 mins)
- ✓ Generate your `APP_KEY`
- ✓ Create all 15 database tables
- ✓ Insert demo data
- ✓ Create storage symlink
- ✓ Build production caches
- ✓ Set file permissions

---

## STEP 8 — Add Cron Job

1. cPanel → **Cron Jobs**
2. Set to run **Every Minute** (`* * * * *`)
3. Command (replace `yourusername`):
   ```bash
   /usr/local/bin/php /home/yourusername/public_html/artisan schedule:run >> /dev/null 2>&1
   ```
   *If `/usr/local/bin/php` doesn't work, try just `php`:*
   ```bash
   php /home/yourusername/public_html/artisan schedule:run >> /dev/null 2>&1
   ```
4. Click **Add New Cron Job**

This enables:
- Auto overdue invoice detection (hourly)
- Email sync (every 10 min)
- Automatic payment reminders (daily 9:00)

---

## STEP 9 — Configure Stripe Webhook

1. Go to **https://dashboard.stripe.com/webhooks**
2. Click **+ Add endpoint**
3. URL: `https://boktech.se/webhook/stripe`
4. Select events:
   - `checkout.session.completed`
   - `invoice.payment_succeeded`
   - `customer.subscription.created`
   - `customer.subscription.updated`
   - `customer.subscription.deleted`
5. Click **Add endpoint**
6. Copy the **Signing secret** (`whsec_...`)
7. Add it to `.env`:
   ```
   STRIPE_WEBHOOK_SECRET=whsec_xxxxxxxxxx
   ```
8. Then run in Terminal:
   ```bash
   cd ~/public_html && php artisan config:cache
   ```

---

## STEP 10 — Test & Go Live

| URL | Should show |
|-----|-------------|
| `https://boktech.se` | FakturaFlow landing page |
| `https://boktech.se/logga-in` | Login page |
| `https://boktech.se/registrera` | Registration page |

**Demo login:**
- Email: `anna@demo.se`
- Password: `password`

---

## TROUBLESHOOTING

### 500 Error
```bash
cd ~/public_html
tail -50 storage/logs/laravel.log
```

### White/blank page
```bash
# In .env temporarily set:
APP_DEBUG=true
# Refresh browser — you'll see the real error
# Then set back to false!
```

### "Class not found" / autoload error
```bash
cd ~/public_html
composer dump-autoload
php artisan config:clear
php artisan cache:clear
```

### Storage not writable
```bash
chmod -R 755 ~/public_html/storage
chmod -R 755 ~/public_html/bootstrap/cache
```

### Routes not working (404 on all pages)
Make sure `mod_rewrite` is enabled and the `.htaccess` file exists in `public_html/public/`

### Cannot run Composer (command not found)
```bash
# Install Composer in your home dir
php -r "copy('https://getcomposer.org/installer', 'cs.php');"
php cs.php --install-dir=$HOME/bin --filename=composer
rm cs.php
export PATH="$HOME/bin:$PATH"
# Now try:
composer install
```

### Database error "Access denied"
- Confirm DB_HOST is `localhost`
- Confirm username is exactly `boktech_fakturaflow_admin`
- In cPanel MySQL Databases, verify user is added to DB with ALL PRIVILEGES

---

## POST-SETUP: Configure Your Company

1. Log in → click **Inställningar** (Settings)
2. **Företagsinformation**: Add your company name, org.nr, address, logo
3. **Fakturainställningar**: Set invoice prefix (e.g. `BT`), Bankgiro number
4. **E-postkonto**: Add SMTP/IMAP settings for your email
5. **Integrationer**: Connect Fortnox or Visma if needed

---

## CREATE YOUR REAL ADMIN ACCOUNT

After testing with demo data:
1. Go to `https://boktech.se/registrera`
2. Sign up with your real company details
3. You get a **14-day free trial** automatically

To make yourself Super Admin (manage all accounts):
```bash
cd ~/public_html
php artisan tinker --execute="
    \App\Models\User::where('email','YOUR@EMAIL.SE')
        ->update(['is_super_admin' => true]);
    echo 'Done';
"
```
