query("SELECT * FROM company_settings LIMIT 1"); $settings = $stmt->fetch(PDO::FETCH_ASSOC); } } catch (Exception $e) { // Log error or ignore if table doesn't exist yet } // Default values if no settings found if (!$settings) { $settings = [ 'company_name' => 'My Restaurant', 'address' => '123 Food Street', 'phone' => '555-0199', 'email' => 'info@restaurant.com', 'vat_rate' => 0.00, 'currency_symbol' => '$', 'currency_decimals' => 2 ]; } } return $settings; } // Function to format currency using settings function format_currency($amount) { $settings = get_company_settings(); return $settings['currency_symbol'] . number_format((float)$amount, (int)$settings['currency_decimals']); } /** * Calculate the current price of a product considering promotions. * * @param array|object $product The product data from DB. * @return float The effective price. */ function get_product_price($product) { $product = (array)$product; $price = (float)$product['price']; $today = date('Y-m-d'); $promo_active = !empty($product['promo_discount_percent']) && !empty($product['promo_date_from']) && !empty($product['promo_date_to']) && $today >= $product['promo_date_from'] && $today <= $product['promo_date_to']; if ($promo_active) { $discount = (float)$product['promo_discount_percent']; $price = $price * (1 - ($discount / 100)); } return $price; } /** * Paginate a query result. * * @param PDO $pdo The PDO connection object. * @param string $query The base SQL query (without LIMIT/OFFSET). * @param array $params Query parameters. * @param int $default_limit Default items per page. * @return array Pagination result with keys: data, total_rows, total_pages, current_page, limit. */ function paginate_query($pdo, $query, $params = [], $default_limit = 20) { // Get current page $page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1; if ($page < 1) $page = 1; // Get limit (allow 20, 50, 100, or -1 for all) $limit = isset($_GET['limit']) ? (int)$_GET['limit'] : $default_limit; // Validate limit, default to 20 if not standard, allow custom if needed but standardizing is safer if ($limit != -1 && !in_array($limit, [20, 50, 100])) { $limit = $default_limit; } // If limit is -1, fetch all if ($limit == -1) { $stmt = $pdo->prepare($query); $stmt->execute($params); $data = $stmt->fetchAll(); return [ 'data' => $data, 'total_rows' => count($data), 'total_pages' => 1, 'current_page' => 1, 'limit' => -1 ]; } // Count total rows using a subquery to handle complex queries safely $count_sql = "SELECT COUNT(*) FROM ($query) as count_table"; $stmt = $pdo->prepare($count_sql); $stmt->execute($params); $total_rows = $stmt->fetchColumn(); $total_pages = ceil($total_rows / $limit); if ($page > $total_pages && $total_pages > 0) $page = $total_pages; // Calculate offset $offset = ($page - 1) * $limit; if ($offset < 0) $offset = 0; // Add LIMIT and OFFSET // Note: PDO parameters for LIMIT/OFFSET can be tricky with some drivers, sticking to direct injection for integers is safe here $query_with_limit = $query . " LIMIT " . (int)$limit . " OFFSET " . (int)$offset; $stmt = $pdo->prepare($query_with_limit); $stmt->execute($params); $data = $stmt->fetchAll(); return [ 'data' => $data, 'total_rows' => $total_rows, 'total_pages' => $total_pages, 'current_page' => $page, 'limit' => $limit ]; } /** * Render pagination controls and limit selector. * * @param array $pagination The result array from paginate_query. * @param array $extra_params Additional GET parameters to preserve. */ function render_pagination_controls($pagination, $extra_params = []) { $page = $pagination['current_page']; $total_pages = $pagination['total_pages']; $limit = $pagination['limit']; // Build query string for limit change $params = array_merge($_GET, $extra_params); unset($params['page']); // Reset page when limit changes // Limit Selector $limits = [20, 50, 100, -1]; echo '
'; echo '
'; echo '
'; // Preserve other GET params foreach ($params as $key => $val) { if ($key !== 'limit') echo ''; } echo 'SHOW:'; echo ''; echo '
'; // Total Count echo 'Total: ' . $pagination['total_rows'] . ' items'; // Optional Total Amount (Sum) if (isset($pagination['total_amount_sum'])) { echo 'Total Sum: ' . format_currency($pagination['total_amount_sum']) . ''; } if ($total_pages > 0) { echo 'Page ' . $page . ' of ' . $total_pages . ''; } echo '
'; // Pagination Links if ($total_pages > 1) { echo ''; } echo '
'; } /** * Auth functions */ function init_session() { if (session_status() === PHP_SESSION_NONE) { session_start(); } } function login_user($username, $password) { $pdo = db(); $stmt = $pdo->prepare("SELECT u.*, g.name as group_name, g.permissions FROM users u LEFT JOIN user_groups g ON u.group_id = g.id WHERE u.username = ? AND u.is_active = 1 LIMIT 1"); $stmt->execute([username]); $user = $stmt->fetch(PDO::FETCH_ASSOC); if ($user && password_verify($password, $user['password'])) { init_session(); unset($user['password']); // Don't store hash in session $_SESSION['user'] = $user; return true; } return false; } function logout_user() { init_session(); unset($_SESSION['user']); session_destroy(); } function get_logged_user() { init_session(); return $_SESSION['user'] ?? null; } function require_login() { if (!get_logged_user()) { header('Location: ' . url('login.php')); exit; } } function has_permission($permission) { $user = get_logged_user(); if (!$user) return false; $userPermissions = $user['permissions'] ?? ''; // Global bypass for super admins if ($userPermissions === 'all') return true; $perms = explode(',', $userPermissions); return in_array($permission, $perms); } function require_permission($permission) { require_login(); if (!has_permission($permission)) { http_response_code(403); echo "Access Denied: You don't have permission to access this page."; exit; } } /** * Get the base path of the application. */ function get_base_path() { static $base_path = null; if ($base_path === null) { $script_dir = dirname($_SERVER['SCRIPT_NAME'] ?? ''); // Replace known subfolders at the end of the path $base_path = preg_replace('/(\/admin|\/api|\/includes)$|(\/admin\/.*|\/api\/.*|\/includes\/.*)$/', '', $script_dir); if ($base_path === DIRECTORY_SEPARATOR || $base_path === '/') { $base_path = ''; } } return $base_path; } /** * Generate a URL relative to the application base. */ function url($path = '') { $path = ltrim($path, '/'); return get_base_path() . '/' . $path; } /** * Get the full base URL including domain and protocol. */ function get_base_url() { $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || ($_SERVER['SERVER_PORT'] ?? '') == 443) ? "https://" : "http://"; $domainName = $_SERVER['HTTP_HOST'] ?? 'localhost'; return $protocol . $domainName . url(); }