<?php
/**
 * Gestion de la Caisse - StayManager
 */

// Inclure la configuration
require_once __DIR__ . '/config/config.php';

// Inclure le contrôleur d'authentification
require_once __DIR__ . '/controllers/AuthController.php';
require_once __DIR__ . '/controllers/CashController.php';

// Initialiser le contrôleur d'authentification
$authController = new AuthController();

// Vérifier l'authentification
$authController->checkAuth();

// Vérifier les permissions
if (!hasPermission('cash_register')) {
    redirect(getBaseUrl() . '/error.php?code=403');
}

// Initialiser la connexion à la base de données
$db = new Database();
$pdo = $db->getConnection();

// Initialiser le contrôleur de la caisse
$cashController = new CashController($pdo);

// Déterminer l'action à effectuer
$action = isset($_GET['action']) ? $_GET['action'] : 'index';

// Exécuter l'action appropriée
switch ($action) {
    case 'open_session':
        $cashController->openSession();
        break;
    case 'close_session':
        $cashController->closeSession();
        break;
    case 'suspend_session':
        $cashController->suspendSession();
        break;
    case 'resume_session':
        $cashController->resumeSession();
        break;
    case 'create_transaction':
        $cashController->createTransaction();
        break;
    case 'cancel_transaction':
        $cashController->cancelTransaction();
        break;
    case 'show_session':
        $cashController->showSession();
        break;
    case 'show_transaction':
        $cashController->showTransaction();
        break;
    case 'session_status':
        $cashController->getSessionStatus();
        break;
    case 'session_transactions':
        $cashController->getSessionTransactions();
        break;
    case 'session_summary':
        $cashController->getSessionSummary();
        break;
    case 'statistics':
        $cashController->getRegisterStatistics();
        break;
    case 'transaction_statistics':
        $cashController->getTransactionStatistics();
        break;
    case 'search_transactions':
        $cashController->searchTransactions();
        break;
    case 'available_registers':
        $cashController->getAvailableRegisters();
        break;
    case 'get_client_invoices':
        $cashController->getClientInvoices();
        break;
    case 'check_register_availability':
        $cashController->checkRegisterAvailability();
        break;
    case 'check_user_session':
        $cashController->checkUserSession();
        break;
    case 'session_status':
        $cashController->getSessionStatus();
        break;
    case 'session_transactions':
        $cashController->getSessionTransactions();
        break;
    case 'session_summary':
        $cashController->getSessionSummary();
        break;
    case 'transaction_statistics':
        $cashController->getTransactionStatistics();
        break;
    case 'search_transactions':
        $cashController->searchTransactions();
        break;
    case 'reports':
        $cashController->reports();
        break;
    case 'export_cancelled_transactions':
        $cashController->exportCancelledTransactions();
        break;
    case 'get_transaction_details':
        $cashController->getTransactionDetails();
        break;
    default:
        $cashController->index();
        break;
}
?>
