xNightR00T File Manager

Loading...
Current Directory:
Name Size Permission Modified Actions
Loading...
$ Waiting for command...
HEX
HEX
Server: LiteSpeed
System: Linux server701.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
User: artiksbn (3537)
PHP: 8.1.34
Disabled: NONE
Upload Files
File: //home/artiksbn/fidelity.artigianodellapizza.com/customer_functions.php
<?php
// customer_functions.php - Customer registration and data management

require_once 'config.php';

// Register new customer
// Modified to accept address
function registerCustomer($phone, $firstName = null, $lastName = null, $address = null) {
    $pdo = getDB();
    
    // Validate phone number
    $phone = validatePhoneNumber($phone);
    if (!$phone) {
        return ['success' => false, 'message' => 'Format de numéro de téléphone invalide'];
    }
    
    // Check if customer already exists
    $stmt = $pdo->prepare("SELECT id FROM customers WHERE phone_number = ?");
    $stmt->execute([$phone]);
    if ($stmt->fetch()) {
        return ['success' => false, 'message' => 'Numéro de téléphone déjà enregistré'];
    }
    
    // Generate customer code
    $customerCode = getNextCustomerCode();
    
    try {
        // Modified query to include address
        $stmt = $pdo->prepare("
            INSERT INTO customers (phone_number, customer_code, first_name, last_name, address) 
            VALUES (?, ?, ?, ?, ?)
        ");
        // Modified execute to include address
        $stmt->execute([$phone, $customerCode, $firstName, $lastName, $address]);
        
        return [
            'success' => true, 
            'message' => 'Inscription réussie',
            'customer_code' => $customerCode
        ];
    } catch (PDOException $e) {
        return ['success' => false, 'message' => 'Échec de l’inscription'];
    }
}

// Function to get the next available customer code
function getNextCustomerCode() {
    $stmt = getDB()->query("SELECT MAX(CAST(customer_code AS UNSIGNED)) as max_code FROM customers");
    $result = $stmt->fetch();
    return $result['max_code'] ? $result['max_code'] + 1 : 1001;
}

// Get customer by phone number
function getCustomerByPhone($phone) {
    $pdo = getDB();
    $phone = validatePhoneNumber($phone);
    
    if (!$phone) {
        return null;
    }
    
    $stmt = $pdo->prepare("SELECT * FROM customers WHERE phone_number = ?");
    $stmt->execute([$phone]);
    return $stmt->fetch();
}

// Get customer by code
function getCustomerByCode($code) {
    $pdo = getDB();
    $stmt = $pdo->prepare("SELECT * FROM customers WHERE customer_code = ?");
    $stmt->execute([$code]);
    return $stmt->fetch();
}

// Get customer transactions
function getCustomerTransactions($customerId, $limit = 5) {
    $pdo = getDB();
    $stmt = $pdo->prepare("
        SELECT t.*, m.username as manager_name
        FROM transactions t
        LEFT JOIN managers m ON t.manager_id = m.id
        WHERE t.customer_id = ?
        ORDER BY t.created_at DESC
        LIMIT ?
    ");
    $stmt->execute([$customerId, $limit]);
    return $stmt->fetchAll();
}

// Add point to customer
function addPointToCustomer($customerId, $managerId, $pointsToAdd = 1) {
    $pdo = getDB();

    if (!is_numeric($pointsToAdd) || $pointsToAdd <= 0) {
        return ['success' => false, 'message' => 'Nombre de points invalide.'];
    }
    
    try {
        $pdo->beginTransaction();
        
        // Get current customer data
        $stmt = $pdo->prepare("SELECT current_points, available_free_pizzas FROM customers WHERE id = ? FOR UPDATE");
        $stmt->execute([$customerId]);
        $customer = $stmt->fetch();
        
        if (!$customer) {
            throw new Exception("Client non trouvé");
        }
        
        $totalPoints = $customer['current_points'] + $pointsToAdd;
        $pizzasEarned = floor($totalPoints / POINTS_FOR_FREE_PIZZA);
        
        $newPoints = $totalPoints % POINTS_FOR_FREE_PIZZA;
        $newFreePizzas = $customer['available_free_pizzas'] + $pizzasEarned;
        
        // Update customer
        $stmt = $pdo->prepare("
            UPDATE customers 
            SET current_points = ?, available_free_pizzas = ? 
            WHERE id = ?
        ");
        $stmt->execute([$newPoints, $newFreePizzas, $customerId]);
        
        // Add transaction record
        $details = "+{$pointsToAdd} point" . ($pointsToAdd > 1 ? 's' : '');
        $stmt = $pdo->prepare("
            INSERT INTO transactions (customer_id, manager_id, type, details) 
            VALUES (?, ?, 'POINT_ADDITION', ?)
        ");
        $stmt->execute([$customerId, $managerId, $details]);
        
        $pdo->commit();
        
        $message = "{$pointsToAdd} point" . ($pointsToAdd > 1 ? 's' : '') . " ajouté" . ($pointsToAdd > 1 ? 's' : '') . " avec succès";
        
        return [
            'success' => true, 
            'message' => $message,
            'earned_free_pizza' => $pizzasEarned > 0,
            'pizzas_earned' => $pizzasEarned
        ];
        
    } catch (Exception $e) {
        $pdo->rollBack();
        return ['success' => false, 'message' => 'Échec de l’ajout des points: ' . $e->getMessage()];
    }
}

// Redeem free pizza
function redeemFreePizza($customerId, $managerId) {
    $pdo = getDB();
    
    try {
        $pdo->beginTransaction();
        
        // Check if customer has free pizzas
        $stmt = $pdo->prepare("SELECT available_free_pizzas FROM customers WHERE id = ?");
        $stmt->execute([$customerId]);
        $customer = $stmt->fetch();
        
        if (!$customer || $customer['available_free_pizzas'] < 1) {
            throw new Exception("Aucune pizza gratuite disponible");
        }
        
        // Update customer
        $stmt = $pdo->prepare("
            UPDATE customers 
            SET available_free_pizzas = available_free_pizzas - 1 
            WHERE id = ?
        ");
        $stmt->execute([$customerId]);
        
        // Add transaction record
        $stmt = $pdo->prepare("
            INSERT INTO transactions (customer_id, manager_id, type, details) 
            VALUES (?, ?, 'PIZZA_REDEMPTION', 'Pizza gratuite échangée')
        ");
        $stmt->execute([$customerId, $managerId]);
        
        $pdo->commit();
        
        return ['success' => true, 'message' => 'Pizza gratuite échangée avec succès'];
        
    } catch (Exception $e) {
        $pdo->rollBack();
        return ['success' => false, 'message' => 'Échec de l’échange de la pizza gratuite'];
    }
}

// Search customers (for admin)
function searchCustomers($query) {
    $pdo = getDB();

    $searchTerm = "%{$query}%";

    $stmt = $pdo->prepare("
        SELECT * FROM customers 
        WHERE customer_code = ?
        OR phone_number LIKE ?
        OR first_name LIKE ?
        OR last_name LIKE ?
        LIMIT 10
    ");
    
    $stmt->execute([$query, $searchTerm, $searchTerm, $searchTerm]);
    
    return $stmt->fetchAll();
}

// *** NEW FUNCTION ***
// Update customer details (first name, last name, address)
function updateCustomerDetails($customerId, $firstName, $lastName, $address) {
    $pdo = getDB();
    
    try {
        $stmt = $pdo->prepare("
            UPDATE customers 
            SET first_name = ?, last_name = ?, address = ?, updated_at = ?
            WHERE id = ?
        ");
        
        $stmt->execute([
            empty($firstName) ? null : $firstName,
            empty($lastName) ? null : $lastName,
            empty($address) ? null : $address,
            getCurrentParisDateTimeString(),
            $customerId
        ]);
        
        return ['success' => true, 'message' => 'Vos informations ont été mises à jour avec succès.'];
    } catch (PDOException $e) {
        return ['success' => false, 'message' => 'Erreur lors de la mise à jour de vos informations.'];
    }
}