# Local setup on Windows with XAMPP

These steps assume XAMPP is installed in `C:\xampp` and this repository is cloned to `C:\xampp\htdocs\school`.
If your paths are different, change them everywhere below. Use forward slashes (`C:/xampp/...`) in Apache files.

You need:

- XAMPP with **PHP 8.2 or newer** and MariaDB 10.4 or newer (XAMPP 8.2.x is fine)
- [Composer](https://getcomposer.org/download/) for Windows (point it at `C:\xampp\php\php.exe` when the installer asks)
- Git

Addresses you will use:

| What | URL |
|---|---|
| Platform Owner panel | http://admin.erp.localhost |
| Demo school (ABC Public School) | http://abc.erp.localhost |
| Local-only fallback (any school) | http://erp.localhost/login?school=abc |

---

## 1. PHP extensions

Open `C:\xampp\php\php.ini` and make sure these lines are **not** commented out (no `;` at the start).
`zip` is needed by Composer, and `pdo_sqlite` and `sqlite3` by the test suite.

```ini
extension=fileinfo
extension=mbstring
extension=openssl
extension=pdo_mysql
extension=zip
extension=pdo_sqlite
extension=sqlite3
extension=intl
```

Then check from a new terminal:

```bash
php -v
```

```bash
php -m
```

`php -v` must show 8.2 or newer. If `php` is not found, add `C:\xampp\php` to your Windows `PATH`.

## 2. Apache: virtual host for erp.localhost and every school subdomain

### 2.1 Enable mod_rewrite

Open `C:\xampp\apache\conf\httpd.conf` and make sure these two lines are not commented out:

```apache
LoadModule rewrite_module modules/mod_rewrite.so
Include conf/extra/httpd-vhosts.conf
```

### 2.2 Add the virtual hosts

Open `C:\xampp\apache\conf\extra\httpd-vhosts.conf` and add the following at the end.
The first block keeps `http://localhost` (phpMyAdmin, other projects) working, because Apache uses the first virtual host as the default.

```apache
<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "C:/xampp/htdocs"
</VirtualHost>

<VirtualHost *:80>
    ServerName erp.localhost
    ServerAlias *.erp.localhost
    DocumentRoot "C:/xampp/htdocs/school/public"

    <Directory "C:/xampp/htdocs/school/public">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog "logs/erp.localhost-error.log"
    CustomLog "logs/erp.localhost-access.log" common
</VirtualHost>
```

`ServerAlias *.erp.localhost` sends `admin.erp.localhost`, `abc.erp.localhost` and every future school to the same app.
`AllowOverride All` lets Laravel's `public/.htaccess` do the URL rewriting.

Restart Apache from the XAMPP Control Panel (Stop, then Start).

## 3. Hosts file

Open Notepad **as Administrator**, open `C:\Windows\System32\drivers\etc\hosts` and add:

```
127.0.0.1 erp.localhost
127.0.0.1 admin.erp.localhost
127.0.0.1 abc.erp.localhost
```

The hosts file has no wildcards, so add one line for every new school you create in the panel
(for example `127.0.0.1 greenvalley.erp.localhost`). Chrome, Edge and Firefox already send `*.localhost` to your
own machine, but the hosts lines make it work everywhere, including other tools.

If a page doesn't load, flush the DNS cache:

```bash
ipconfig /flushdns
```

## 4. Database

Start MySQL in the XAMPP Control Panel, open http://localhost/phpmyadmin and create a database:

- Name: `school_erp`
- Collation: `utf8mb4_unicode_ci`

## 5. Install the app

Open a terminal in the project folder:

```bash
cd C:\xampp\htdocs\school
```

Install PHP packages:

```bash
composer install
```

Composer may ask whether to trust `wikimedia/composer-merge-plugin`; it is already allowed in `composer.json`
(nwidart/laravel-modules uses it to load each module's classes).

Create your `.env` from the example:

```bash
copy .env.example .env
```

The example already has the right local values. Check these lines in `.env`:

```ini
APP_NAME="School ERP"
APP_ENV=local
APP_DEBUG=true
APP_URL=http://erp.localhost

APP_BASE_DOMAIN=erp.localhost
APP_PLATFORM_SUBDOMAIN=admin
APP_SCHEME=http

DB_CONNECTION=mariadb
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=school_erp
DB_USERNAME=root
DB_PASSWORD=

SESSION_DRIVER=file
SESSION_DOMAIN=
SESSION_SECURE_COOKIE=false
CACHE_STORE=file
QUEUE_CONNECTION=database
```

- `DB_PASSWORD` stays empty for XAMPP's default `root` user. Fill it in if you set one.
- `SESSION_DOMAIN` must stay empty, so every subdomain gets its own login cookie.
- No Redis is needed.

Generate the app key:

```bash
php artisan key:generate
```

Create the tables (56 from `school_erp_schema.sql` plus Laravel's own):

```bash
php artisan migrate
```

Load the Basic plan, the modules, the permissions, the Platform Owner and the demo school
(with its default ID formats):

```bash
php artisan db:seed
```

Link public storage:

```bash
php artisan storage:link
```

To start again from an empty database at any time:

```bash
php artisan migrate:fresh --seed
```

## 6. Demo logins

| Where | URL | Login | Password |
|---|---|---|---|
| Platform Owner panel | http://admin.erp.localhost/login | `owner@erp.localhost` | `password` |
| ABC Public School (School Super Admin) | http://abc.erp.localhost/login | `admin` or `admin@abc.test` | `password` |

Change these passwords before the app is reachable from any other machine.

**Platform Owner 2FA.** The first Platform Owner login asks you to scan a QR code with an authenticator app
(Google Authenticator, Microsoft Authenticator or Authy) and type the 6-digit code; after that every login asks for a code.
You then get 8 one-time **recovery codes**: save them; each one works once instead of a phone code
(new ones from your name in the top bar > My account). If you lose the phone and the codes, run
`php artisan platform:reset-2fa owner@erp.localhost` and set it up again.
To skip 2FA on your own machine only, set `PLATFORM_2FA_REQUIRED=false` in `.env` (it is always on when `APP_ENV=production`).

School users can turn on 2FA themselves under **My profile**; a user with `users.edit` can turn it off for someone who lost their phone.

### Local-only fallback

When `APP_ENV=local`, a host without a school subdomain (`erp.localhost`, `localhost`) can pick a school with
`?school=<subdomain>`, for example http://erp.localhost/login?school=abc. The choice is kept in the session until you
pass another `?school=`. Switching school logs you out of the previous one. This never works when `APP_ENV` is anything
other than `local`.

## 7. Run the tests

The tests use an in-memory SQLite database (`pdo_sqlite` from step 1), so they never touch `school_erp`:

```bash
php artisan test
```

To run them against MariaDB instead, create an empty database `school_erp_test` in phpMyAdmin and run this in
Command Prompt (`cmd`, not PowerShell):

```bash
set DB_CONNECTION=mariadb&& set DB_DATABASE=school_erp_test&& php artisan test
```

## 8. Queue worker

Student imports of more than 300 rows run on the queue (smaller files are imported straight away). Keep a worker
running in a second terminal while you import large files:

```bash
php artisan queue:work
```

## 9. Scheduler (fines, online payment checks, reminders)

Late fines are calculated every night, pending Razorpay payments are checked every 15 minutes, each branch's DCR (Day Challan Receipt)
is closed at the school's DCR time and email reminders go out daily. On your machine keep this running in another terminal:

```bash
php artisan schedule:work
```

To calculate fines right now (for example after adding a rule):

```bash
php artisan fees:fines
```

## 10. Razorpay (online fee payment)

Each school uses its own Razorpay account. In the school, open Fees > Fee Settings and enter the **test** key id, key secret and
webhook secret from the Razorpay dashboard. The webhook URL is `http://<school>.erp.localhost/fees/razorpay/webhook`, which
Razorpay cannot reach on your own machine: locally, payments are confirmed by the checkout callback (and by
`fees:reconcile-online`). Use test keys only until the site runs on a real domain with HTTPS.

## Troubleshooting

| Problem | Fix |
|---|---|
| `abc.erp.localhost` shows the XAMPP dashboard | The virtual host isn't loaded: check `Include conf/extra/httpd-vhosts.conf` and restart Apache |
| 404 "Not Found" from Apache on every page except `/` | `mod_rewrite` is off or `AllowOverride All` is missing |
| Laravel 404 on `abc.erp.localhost` | The school doesn't exist; run `php artisan db:seed` |
| Laravel 404 on `erp.localhost` | Expected: the bare domain isn't a school. Use `?school=abc` (local only) |
| 403 "school account is suspended" | Activate the school in the Platform Owner panel |
| 403 "module is not enabled" | Switch the module on for the school in the Platform Owner panel |
| `SQLSTATE[HY000] [1045]` | Wrong `DB_USERNAME`/`DB_PASSWORD` in `.env` |
| `could not find driver` in tests | Enable `extension=pdo_sqlite` and `extension=sqlite3` in `php.ini` |
| Page has no styling | Tabler loads from cdn.jsdelivr.net, which needs internet access |
| Changed `.env` but nothing happens | Run `php artisan config:clear` |
