/home/awneajlw/www/codestechvista.com/includes/currency_helper.php
<?php
/**
 * Currency Helper Functions
 * This file contains functions to get and display user's selected currency
 */

/**
 * Get user's selected currency from shop settings
 * @param PDO $db Database connection
 * @param int $user_id User ID
 * @return string Currency code (e.g., 'USD', 'PKR', 'EUR')
 */
function getUserCurrency($db, $user_id) {
    try {
        $query = "SELECT currency FROM shops WHERE user_id = ?";
        $stmt = $db->prepare($query);
        $stmt->execute([$user_id]);
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        
        return $result['currency'] ?? 'USD'; // Default to USD if not found
    } catch (Exception $e) {
        error_log("Currency fetch error: " . $e->getMessage());
        return 'USD'; // Default fallback
    }
}

/**
 * Get currency symbol for display
 * @param string $currency_code Currency code (e.g., 'USD', 'PKR', 'EUR')
 * @return string Currency symbol
 */
function getCurrencySymbol($currency_code) {
    $symbols = [
        'USD' => '$',
        'EUR' => '€',
        'GBP' => '£',
        'PKR' => '₨',
        'INR' => '₹',
        'AED' => 'د.إ',
        'SAR' => '﷼',
        'CAD' => '$',
        'AUD' => '$',
        'JPY' => '¥',
        'CNY' => '¥',
        'CHF' => 'Fr',
        'SGD' => '$',
        'MYR' => 'RM',
        'BDT' => '৳',
        'THB' => '฿',
        'KRW' => '₩',
        'ZAR' => 'R',
        'TRY' => '₺',
        'BRL' => 'R$',
        'MXN' => '$',
        'NZD' => '$',
        'RUB' => '₽'
    ];
    
    return $symbols[$currency_code] ?? $currency_code;
}

/**
 * Format amount with currency
 * @param float $amount Amount to format
 * @param string $currency_code Currency code
 * @param int $decimals Number of decimal places
 * @return string Formatted amount with currency
 */
function formatCurrency($amount, $currency_code, $decimals = 0) {
    $symbol = getCurrencySymbol($currency_code);
    
    // Check if amount is already formatted (contains commas)
    if (is_string($amount) && strpos($amount, ',') !== false) {
        // Amount is already formatted, use as is
        $formatted_amount = $amount;
    } else {
        // Convert to float and format
        $numeric_amount = (float) $amount;
        $formatted_amount = number_format($numeric_amount, $decimals);
    }
    
    // Always show symbol first for all currencies
     return $symbol . ' ' . $formatted_amount;
}

/**
 * Get currency display name
 * @param string $currency_code Currency code
 * @return string Full currency name
 */
function getCurrencyName($currency_code) {
    $names = [
        'USD' => 'US Dollar',
        'EUR' => 'Euro',
        'GBP' => 'British Pound',
        'PKR' => 'Pakistani Rupee',
        'INR' => 'Indian Rupee',
        'AED' => 'UAE Dirham',
        'SAR' => 'Saudi Riyal',
        'CAD' => 'Canadian Dollar',
        'AUD' => 'Australian Dollar',
        'JPY' => 'Japanese Yen',
        'CNY' => 'Chinese Yuan',
        'CHF' => 'Swiss Franc',
        'SGD' => 'Singapore Dollar',
        'MYR' => 'Malaysian Ringgit',
        'BDT' => 'Bangladeshi Taka',
        'THB' => 'Thai Baht',
        'KRW' => 'South Korean Won',
        'ZAR' => 'South African Rand',
        'TRY' => 'Turkish Lira',
        'BRL' => 'Brazilian Real',
        'MXN' => 'Mexican Peso',
        'NZD' => 'New Zealand Dollar',
        'RUB' => 'Russian Ruble'
    ];
    
    return $names[$currency_code] ?? $currency_code;
}
?>