|.]+/', '_', $filename_without_ext); // Remove any leading/trailing underscores $sanitized_filename = trim($sanitized_filename, '_'); // Ensure the filename is not empty if (empty($sanitized_filename)) { $sanitized_filename = 'unnamed_file'; } // Re-append the extension if it exists if (!empty($extension)) { return $sanitized_filename . '.' . $extension; } return $sanitized_filename; } function upload_error_message($error_code) { switch ($error_code) { case UPLOAD_ERR_INI_SIZE: return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; case UPLOAD_ERR_FORM_SIZE: return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; case UPLOAD_ERR_PARTIAL: return 'The uploaded file was only partially uploaded'; case UPLOAD_ERR_NO_FILE: return 'No file was uploaded'; case UPLOAD_ERR_NO_TMP_DIR: return 'Missing a temporary folder'; case UPLOAD_ERR_CANT_WRITE: return 'Failed to write file to disk.'; case UPLOAD_ERR_EXTENSION: return 'A PHP extension stopped the file upload.'; default: return 'Unknown upload error'; } } function getEffectivePrice(PDO $db, int $productId, ?int $clientId): array { $vatRate = 1.23; $net = null; $gross = null; $priceFound = false; // Priority A: Try to fetch from client_prices if ($clientId) { $stmt = $db->prepare("SELECT price_net, price_gross FROM client_prices WHERE client_id = :client_id AND product_id = :product_id LIMIT 1"); $stmt->execute(['client_id' => $clientId, 'product_id' => $productId]); $priceRow = $stmt->fetch(PDO::FETCH_ASSOC); if ($priceRow) { $net = $priceRow['price_net'] !== null ? (float)$priceRow['price_net'] : null; $gross = $priceRow['price_gross'] !== null ? (float)$priceRow['price_gross'] : null; if ($net !== null || $gross !== null) { $priceFound = true; } } } // Priority B: Fallback to product base prices if no client-specific price was found if (!$priceFound) { $stmt = $db->prepare("SELECT price_net, price_gross FROM products WHERE id = :product_id"); $stmt->execute(['product_id' => $productId]); $priceRow = $stmt->fetch(PDO::FETCH_ASSOC); if ($priceRow) { $net = $priceRow['price_net'] !== null ? (float)$priceRow['price_net'] : null; $gross = $priceRow['price_gross'] !== null ? (float)$priceRow['price_gross'] : null; } } // If we have one price, calculate the other if ($gross !== null && $net === null) { $net = round($gross / $vatRate, 2); } elseif ($net !== null && $gross === null) { $gross = round($net * $vatRate, 2); } // Sanity check: gross must not be less than net. If so, log it and fix it. if ($gross !== null && $net !== null && $gross < $net) { error_log("Price inconsistency for product ID $productId: gross ($gross) is less than net ($net). Recalculating net from gross."); $net = round($gross / $vatRate, 2); } // Final check for nulls before returning if ($net === null || $gross === null) { return ['net' => 0.0, 'gross' => 0.0]; } return ['net' => $net, 'gross' => $gross]; }