# Deploying Dezen B2B to cPanel

No SSH needed. Everything is done in cPanel and in the browser:

- **First install:** upload the zip, extract it, open `/instalacija` and follow the steps.
- **Updates:** upload the new zip and extract it over the old one. The app clears its caches and updates the database by itself on the next page load.

There are no long-running workers. A cron job runs the Laravel scheduler every minute, and the scheduler sends the queued e-mails.

## 1. Before you start

### Hosting (cPanel)

- **PHP 8.3 or newer.** cPanel → *MultiPHP Manager* → set the domain to PHP 8.3 or 8.4.
- **PHP extensions** (cPanel → *Select PHP Version* → Extensions): `pdo_mysql`, `mbstring`, `intl`, `gd`, `zip`, `fileinfo`, `bcmath`, `openssl`, `ctype`, `tokenizer`, `xml`, `curl`.
- **PHP limits** (cPanel → *MultiPHP INI Editor*). Price lists with pictures are 15 MB or more:
  `upload_max_filesize = 100M`, `post_max_size = 110M`, `memory_limit = 512M`, `max_execution_time = 300`.
- **MySQL 8 or MariaDB 10.6+.**

The installer's first step checks all of this and says what is missing.

### E-mail (Google Workspace)

The portal sends e-mail through Google as the user **b2b@studio.rs**, with sender **b2b@dezen.rs** (dezen.rs is a domain alias of studio.rs).

1. **Create the user.** Google Admin → *Users* → add `b2b@studio.rs`. Because of the domain alias, it also receives mail for `b2b@dezen.rs`.
2. **Turn on 2-Step Verification** for that user. Sign in as b2b@studio.rs → *Google Account* → *Security* → *2-Step Verification*. App passwords exist only once this is on.
3. **Create an app password.** Same account → *Security* → *App passwords* (or myaccount.google.com/apppasswords). Name it "Dezen B2B" and copy the 16 characters. The installer accepts it with or without the spaces. It is shown only once; if you lose it, create a new one.
4. **Send mail as b2b@dezen.rs.** In Gmail, signed in as b2b@studio.rs → *Settings* → *Accounts* → *Send mail as* → *Add another email address* → `b2b@dezen.rs`, and leave "Treat as an alias" checked. Without this, Google replaces the sender with b2b@studio.rs.
5. **DKIM for dezen.rs.** Google Admin → *Apps* → *Google Workspace* → *Gmail* → *Authenticate email*:
   - Choose `dezen.rs` and generate the key.
   - Add the TXT record (`google._domainkey`) in the DNS for dezen.rs.
   - Wait for DNS, then click *Start authentication*.

   SPF for dezen.rs should already include `include:_spf.google.com`.

Gmail allows about 2,000 messages a day per user, which is far more than the portal needs.

### The zip

On the development machine:

```bash
powershell -ExecutionPolicy Bypass -File scripts\package.ps1
```

This packs the last commit with production dependencies and built assets into `dist/dezen-b2b-<date>-<commit>.zip`. The zip never contains `.env`.

## 2. First install

1. **Database.** cPanel → *MySQL Databases*:
   - Create a database, for example `nalog_b2b`.
   - Create a user with a strong password.
   - Add the user to the database with **ALL PRIVILEGES**.
   - Keep the password; the installer asks for it.
2. **Folder and subdomain.**
   - cPanel → *File Manager*: create the folder `b2b` in the home folder, next to `public_html` (not inside it).
   - cPanel → *Domains*: create `b2b.dezen.rs` with the document root `b2b/public`.
   - Turn on SSL (*SSL/TLS Status* → AutoSSL) and *Force HTTPS Redirect*.
3. **Upload.** In *File Manager*, open `b2b`, *Upload* the zip, then right-click it → *Extract*. Folders such as `app`, `public` and `vendor` must end up directly in `b2b`, not in a subfolder. Delete the zip afterwards.
4. **Run the installer.** Open `https://b2b.dezen.rs`. It redirects to `/instalacija`.

   | Step | What happens |
   |---|---|
   | Provera servera | PHP version, extensions, folder permissions, limits. Red must be fixed in cPanel; yellow is a recommendation. |
   | Baza | Database name, user and password from step 1. The connection is tested before moving on. |
   | E-mail | SMTP `smtp.gmail.com`, port 587, login `b2b@studio.rs`, the app password, sender `b2b@dezen.rs`, and the addresses that get notifications (new customers, orders). *Pošalji probni e-mail* sends a test right away. |
   | Instalacija | Creates the tables, saves the settings and links the images folder. |
   | Administrator | Your login e-mail and password (at least 10 characters). |
   | Cron | Shows the exact cron line for this server. In cPanel → *Cron Jobs*, choose "Once Per Minute" (`* * * * *`) and paste the command. The page turns green when the first run arrives (within a minute or two). |

   After *Završi*, the installer locks itself: `/instalacija` returns "not found" from then on.

   If the browser closes partway through, opening the site again asks for the database password and then continues.
5. **Kontrolna tabla.** Log in at `https://b2b.dezen.rs/admin`.
   - *Provera sistema* should be all green.
   - In *Podešavanja*, check the minimum order, the VAT rate and the notification addresses.
   - Add brands in *Brendovi*, then upload the price list in *Cenovnik*.
6. **Smoke test.**
   - Register a test company at `/registracija` and check that the notification arrives within a minute or two.
   - Approve it in *Kupci*, check the customer's e-mail, and place a test order.
   - Then cancel the order and disable the test company.

Never run `db:seed` on the server; it creates demo accounts.

## 3. Updates

1. Optional: do it outside working hours, since a page may show an error for the few seconds the extraction takes.
2. Upload the new zip to `b2b` and *Extract* it over the existing files, overwriting them. `.env`, uploaded images (`storage/`) and the database are not touched.
3. Delete the zip and open the site.

The new zip has a new `VERSION` file. On the first page load the app clears its caches and applies any database changes.

If *Provera sistema* still shows "Baza podataka: N izmena čeka", click **Ažuriraj bazu** there.

## 4. Changing settings later

- **E-mail account or app password:** *Kontrolna tabla* → *Provera sistema* → **E-mail podešavanja**. A test message goes to your address first, and nothing is saved if it fails.
- **Test e-mail:** *Provera sistema* → **Pošalji probni e-mail**.
- **Notification addresses, minimum order, VAT:** *Podešavanja*.
- **Anything else in `.env`:** *File Manager* → `b2b/.env` → *Edit*. Turn on *Show Hidden Files* in File Manager's *Settings* to see it. Keep a copy before editing.

## 5. Troubleshooting

*Provera sistema* on Kontrolna tabla shows most problems in red, with a hint.

- **Cron "nikad nije pokrenut" / e-mails waiting in the queue.**
  - Check the cron line in *Cron Jobs*. It must be the one the installer showed, with the full PHP path (for example `/opt/cpanel/ea-php83/root/usr/bin/php`). On many servers the plain `php` is an older version.
  - cPanel e-mails the cron output if *Cron Email* is set; this helps find the error.
- **E-mails fail.**
  - Use *Pošalji probni e-mail*; the error is shown on screen.
  - "Username and Password not accepted" means the app password is wrong or 2-Step Verification was turned off. Create a new app password and save it in *E-mail podešavanja*.
  - Recipients see "via studio.rs" or the wrong sender: finish "Send mail as" and DKIM (section 1).
- **"Instalacija ne može da počne" or "Fajl .env nije moguće sačuvati".** In *File Manager*, set the `b2b` folder, `b2b/storage` and `b2b/bootstrap/cache` to 755, and `b2b/.env` to 644 or 600.
- **500 error.** Look at the newest file in `b2b/storage/logs/` (*File Manager* → *View*). After an update, deleting the files in `b2b/bootstrap/cache/` except `.gitignore` clears the caches by hand.
- **Upload stops or "file too large".** Raise the PHP limits in section 1.
- **Product images don't show.** *Provera sistema* → *Folder za slike* must be green. Hosts that don't allow links get a normal `public/storage` folder instead, and the app handles this by itself.
- **Starting the install again** (only before real data exists):
  1. In `b2b/.env`, set `B2B_INSTALLED=false` and remove the `B2B_INSTALL_TOKEN` line.
  2. In phpMyAdmin, empty the database (drop all tables).
  3. Open `/instalacija`.
