# Installation Guide

## Prerequisites

- PHP 8.0 or higher
- MySQL 5.7 or higher / MariaDB 10.2 or higher
- Composer
- Web server (Apache, Nginx, etc.)

## Installation Steps

### 1. Clone or Download

```bash
git clone https://github.com/your-repo/factur-x.git
cd factur-x
```

### 2. Install Dependencies

```bash
composer install
```

### 3. Database Setup

Create a database and user:

```sql
CREATE DATABASE facturx_orders CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'facturx_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON facturx_orders.* TO 'facturx_user'@'localhost';
FLUSH PRIVILEGES;
```

Import the database schema:

```bash
mysql -u facturx_user -p facturx_orders < sql/schema.sql
```

### 4. Configuration

Copy the example configuration file:

```bash
cp config/config.example.php config/config.php
```

Edit `config/config.php` with your database and company details:

```php
return [
    'database' => [
        'host' => 'localhost',
        'name' => 'facturx_orders',
        'user' => 'facturx_user',
        'password' => 'your_secure_password_here'
    ],
    'company' => [
        'name' => 'Your Company Name',
        'address' => '123 Business Street, 75001 Paris, France',
        'siret' => '12345678901234'
    ]
];
```

### 5. Web Server Configuration

#### Apache

Create a virtual host configuration:

```apache
<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /path/to/factur-x/admin
    
    <Directory /path/to/factur-x/admin>
        AllowOverride All
        Require all granted
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/facturx_error.log
    CustomLog ${APACHE_LOG_DIR}/facturx_access.log combined
</VirtualHost>
```

#### Nginx

```nginx
server {
    listen 80;
    server_name yourdomain.com;
    root /path/to/factur-x/admin;
    index index.php;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
```

### 6. Permissions

Set appropriate permissions:

```bash
chmod -R 755 admin/
chmod -R 755 examples/
mkdir -p logs invoices
chmod 755 logs invoices
```

### 7. Test Installation

Visit `http://yourdomain.com/admin/` in your browser to access the admin panel.

## Multi-Customer Deployment

For per-customer deployment:

1. Create a separate database for each customer
2. Copy `config/config.example.php` to each customer's directory
3. Configure each customer's database connection
4. Ensure proper file permissions and isolation

## Security Considerations

- Use strong database passwords
- Enable HTTPS in production
- Restrict access to admin panel by IP address
- Regularly update dependencies
- Monitor security logs in `logs/security.log`

## Troubleshooting

### Database Connection Issues

Check database credentials in `config/config.php` and ensure MySQL is running.

### Permission Denied Errors

Verify file permissions and web server user has access to required directories.

### Factur-X Generation Issues

Ensure the `invoices/` directory is writable by the web server.
