# Quick Start Guide

## System Overview

Your Factur-X Order System is now complete! Here's what you have:

### ✅ Core Components
- **Order Management**: Complete order lifecycle (draft → paid → invoiced)
- **Factur-X Generation**: French-compliant invoice XML generation
- **Admin Panel**: Web interface for order and invoice management
- **Security**: Immutable validated orders with audit trails
- **Multi-tenant**: Per-customer database isolation

### 📁 Project Structure
```
factur-x/
├── src/                    # Core library
│   ├── OrderManager.php   # Order handling
│   ├── InvoiceGenerator.php # Factur-X generation
│   ├── Database.php        # Database abstraction
│   ├── Config.php         # Configuration
│   ├── Security.php       # Security & validation
│   └── Validator.php     # Input validation
├── admin/                 # Web admin panel
│   ├── views/            # Admin interface views
│   ├── css/              # Stylesheets
│   └── js/               # JavaScript
├── config/               # Configuration templates
├── sql/                  # Database schema
├── examples/             # Usage examples
└── docs/                 # Documentation
```

## 🚀 Getting Started

### 1. Install Dependencies
```bash
composer install
```

### 2. Setup Database
```bash
# Create database
mysql -u root -p
CREATE DATABASE facturx_orders;
CREATE USER 'facturx_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON facturx_orders.* TO 'facturx_user'@'localhost';

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

### 3. Configure System
```bash
cp config/config.example.php config/config.php
# Edit config.php with your database and company details
```

### 4. Test the System
```bash
php test_system.php
```

### 5. Access Admin Panel
Point your web server to the `admin/` directory and visit:
```
http://yourdomain.com/admin/
```

## 🎯 Key Features

### Order Management
- Create orders with multiple items
- Automatic reference generation (ORD-YYYY-NNNNNN)
- Customer information management
- Order validation and locking

### Invoice Generation
- Automatic Factur-X XML generation
- French standard compliance
- Downloadable invoice files
- Invoice status tracking

### Security Features
- Validated orders cannot be edited/deleted
- Status transition validation
- Input sanitization and validation
- Comprehensive audit logging
- Rate limiting and CSRF protection

### Multi-Customer Support
- Per-customer database isolation
- Individual configuration files
- Secure data separation

## 📋 Usage Examples

### Basic Order Creation
```php
use FacturX\Config;
use FacturX\OrderManager;

$config = Config::load(include 'config/config.php');
$orderManager = new OrderManager($config);

$orderId = $orderManager->createOrder([
    'customer_name' => 'John Doe',
    'customer_email' => 'john@example.com',
    'items' => [
        ['name' => 'Product', 'quantity' => 1, 'unit_price' => 99.99, 'tax_rate' => 20.0]
    ]
]);

$orderManager->validateOrder($orderId);
```

### Invoice Generation
```php
use FacturX\InvoiceGenerator;

$invoiceGenerator = new InvoiceGenerator($config);
$invoice = $invoiceGenerator->generateInvoice($orderId);

// Download Factur-X file
$filePath = $invoiceGenerator->downloadFacturXFile($invoice['id']);
```

## 🔧 Configuration Options

### Database Settings
```php
'database' => [
    'host' => 'localhost',
    'name' => 'your_database',
    'user' => 'your_username',
    'password' => 'your_password'
]
```

### Company Information
```php
'company' => [
    'name' => 'Your Company',
    'address' => 'Your Address',
    'siret' => '12345678901234',
    'email' => 'contact@yourcompany.com'
]
```

### Security Settings
```php
'security' => [
    'encryption_key' => '32_character_key',
    'session_timeout' => 3600,
    'max_login_attempts' => 5
]
```

## 🌐 Web Integration

### Customer Website Integration
```php
// Include in customer website
require_once 'factur-x/vendor/autoload.php';

// Handle order form submission
$orderManager = new OrderManager($config);
$orderId = $orderManager->createOrder($_POST);

// Validate and generate invoice
$orderManager->validateOrder($orderId);
$invoice = $invoiceGenerator->generateInvoice($orderId);
```

### Admin Panel Features
- Dashboard with statistics
- Order filtering and search
- Invoice management
- Factur-X file downloads
- Customer information display

## 🔒 Security Best Practices

1. **Database Security**: Use strong, unique passwords
2. **File Permissions**: Restrict access to sensitive files
3. **HTTPS**: Enable SSL in production
4. **Access Control**: Restrict admin panel by IP if needed
5. **Regular Updates**: Keep dependencies updated
6. **Monitoring**: Check security logs regularly

## 📊 Multi-Customer Deployment

### Database per Customer
```bash
# Customer 1
CREATE DATABASE customer1_orders;
CREATE USER 'customer1_user'@'localhost' IDENTIFIED BY 'password1';

# Customer 2
CREATE DATABASE customer2_orders;
CREATE USER 'customer2_user'@'localhost' IDENTIFIED BY 'password2';
```

### Configuration per Customer
```php
// /customers/customer1/config/config.php
return [
    'database' => ['name' => 'customer1_orders', ...],
    'company' => ['name' => 'Customer 1 Company', ...]
];
```

## 🛠️ Troubleshooting

### Common Issues
1. **Database Connection**: Check credentials in config.php
2. **Permissions**: Ensure web server can write to logs/ and invoices/ directories
3. **Factur-X Generation**: Verify invoices/ directory is writable
4. **Admin Panel**: Check web server configuration

### Debug Mode
```php
// Enable debug mode in config.php
'debug' => true
```

## 📞 Support

For issues and questions:
1. Check the installation guide (INSTALL.md)
2. Review deployment documentation (DEPLOYMENT.md)
3. Test with the provided examples
4. Check logs in the logs/ directory

## 🎉 Next Steps

1. **Configure your database** and test the system
2. **Set up your web server** for the admin panel
3. **Create customer configurations** for multi-tenant deployment
4. **Integrate with customer websites** using the library
5. **Monitor and maintain** the system regularly

Your Factur-X Order System is ready for production use! 🚀
