Add software tool functionality with device connection and upgrade options
- Implemented the software tool page with user interface for connecting devices. - Added functionality to display connection status and software upgrade options. - Included a help modal with step-by-step instructions for users. - Integrated error handling and user permission checks. - Enhanced user experience with dynamic content updates and visual feedback.
This commit is contained in:
@@ -13,6 +13,7 @@ $pdo = dbConnect($dbname);
|
||||
//NEW ARRAY
|
||||
$criterias = [];
|
||||
$clause = '';
|
||||
$debug = [];
|
||||
|
||||
//Check for $_GET variables and build up clause
|
||||
if(isset($get_content) && $get_content!=''){
|
||||
@@ -26,6 +27,11 @@ if(isset($get_content) && $get_content!=''){
|
||||
}
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
$debug['request_parameters'] = $criterias;
|
||||
$debug['timestamp'] = date('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
// IF SN IS PROVIDED, HANDLE UPGRADE OPTIONS
|
||||
if (isset($criterias['sn']) && $criterias['sn'] != ''){
|
||||
|
||||
@@ -41,9 +47,11 @@ if (isset($criterias['sn']) && $criterias['sn'] != ''){
|
||||
|
||||
//check if current hw_version is send and update the equipment record
|
||||
if(isset($criterias['hw_version']) && $criterias['hw_version'] !=''){
|
||||
// Translate hardware version to standardized format
|
||||
$translated_hw_version = translateDeviceHardwareVersion($criterias['hw_version']);
|
||||
$sql = 'UPDATE equipment SET hw_version = ?, updatedby = ? WHERE serialnumber = ? ';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$criterias['hw_version'],$username,$criterias['sn']]);
|
||||
$stmt->execute([$translated_hw_version,$username,$criterias['sn']]);
|
||||
}
|
||||
|
||||
//GET EQUIPMENT AND PRODUCT DATA BASED ON SERIAL NUMBER
|
||||
@@ -71,8 +79,47 @@ if (isset($criterias['sn']) && $criterias['sn'] != ''){
|
||||
$sw_version_license = $equipment_data['sw_version_license'];
|
||||
$equipment_rowid = $equipment_data['equipment_rowid'];
|
||||
|
||||
//GET ALL DATA: active assignments, version details, and upgrade paths
|
||||
//Filter on active status and hw_version compatibility
|
||||
if (debug) {
|
||||
$debug['equipment_data'] = [
|
||||
'product_rowid' => $product_rowid,
|
||||
'productcode' => $productcode,
|
||||
'current_sw_version_raw' => $current_sw_version,
|
||||
'hw_version' => $hw_version,
|
||||
'sw_version_license' => $sw_version_license
|
||||
];
|
||||
}
|
||||
|
||||
// Normalize software version for comparison (lowercase, trim leading zeros)
|
||||
$current_sw_version = strtolower(ltrim($current_sw_version, '0'));
|
||||
|
||||
// Translate incoming hw_version parameter for comparison if provided
|
||||
$comparison_hw_version = $hw_version;
|
||||
$hw_version_from_request = null;
|
||||
if(isset($criterias['hw_version']) && $criterias['hw_version'] !=''){
|
||||
$hw_version_from_request = $criterias['hw_version'];
|
||||
$comparison_hw_version = translateDeviceHardwareVersion($criterias['hw_version']);
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
$debug['normalized_data'] = [
|
||||
'current_sw_version' => $current_sw_version,
|
||||
'hw_version_from_request' => $hw_version_from_request,
|
||||
'comparison_hw_version' => $comparison_hw_version,
|
||||
'hw_version_valid' => ($comparison_hw_version !== '')
|
||||
];
|
||||
}
|
||||
|
||||
// Check if hardware version is invalid (all zeros)
|
||||
if ($hw_version_from_request && $comparison_hw_version === '') {
|
||||
$messages = ["error" => "Invalid hardware version (000000) - device may not be properly initialized"];
|
||||
if (debug) {
|
||||
$messages['debug'] = $debug;
|
||||
}
|
||||
echo json_encode($messages, JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
//GET ALL ACTIVE SOFTWARE ASSIGNMENTS for this product with matching HW version
|
||||
$sql = 'SELECT
|
||||
psv.rowID as version_id,
|
||||
psv.version,
|
||||
@@ -81,60 +128,157 @@ if (isset($criterias['sn']) && $criterias['sn'] != ''){
|
||||
psv.mandatory,
|
||||
psv.latest,
|
||||
psv.hw_version,
|
||||
psv.file_path,
|
||||
pup.price,
|
||||
pup.currency,
|
||||
pup.from_version_id,
|
||||
from_ver.version as from_version
|
||||
psv.file_path
|
||||
FROM products_software_assignment psa
|
||||
JOIN products_software_versions psv ON psa.software_version_id = psv.rowID
|
||||
LEFT JOIN products_software_upgrade_paths pup ON pup.to_version_id = psv.rowID AND pup.is_active = 1
|
||||
LEFT JOIN products_software_versions from_ver ON pup.from_version_id = from_ver.rowID
|
||||
WHERE psa.product_id = ?
|
||||
AND psa.status = 1
|
||||
AND (psv.hw_version = ? OR psv.hw_version IS NULL OR psv.hw_version = "")
|
||||
AND (? IS NULL OR ? = "" OR psv.version != ?)';
|
||||
AND (psv.hw_version = ? OR psv.hw_version IS NULL OR psv.hw_version = "")';
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$product_rowid, $hw_version, $current_sw_version, $current_sw_version, $current_sw_version]);
|
||||
$stmt->execute([$product_rowid, $comparison_hw_version]);
|
||||
$versions = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
if (debug) {
|
||||
$debug['active_assignments'] = [
|
||||
'count' => count($versions),
|
||||
'versions' => array_map(function($v) {
|
||||
return [
|
||||
'version_id' => $v['version_id'],
|
||||
'version' => $v['version'],
|
||||
'name' => $v['name'],
|
||||
'hw_version' => $v['hw_version'],
|
||||
'latest' => $v['latest']
|
||||
];
|
||||
}, $versions)
|
||||
];
|
||||
}
|
||||
|
||||
if (empty($versions)) {
|
||||
$messages = ["error" => "No active software assignments found for product"];
|
||||
if (debug) {
|
||||
$messages['debug'] = $debug;
|
||||
}
|
||||
} else {
|
||||
// First check if current version has paid upgrade paths FROM it
|
||||
$has_paid_upgrade_from_current = false;
|
||||
if ($current_sw_version) {
|
||||
$sql = 'SELECT COUNT(*) as paid_count
|
||||
FROM products_software_upgrade_paths pup
|
||||
JOIN products_software_versions from_ver ON pup.from_version_id = from_ver.rowID
|
||||
WHERE LOWER(TRIM(LEADING "0" FROM from_ver.version)) = ?
|
||||
AND pup.price > 0
|
||||
AND pup.is_active = 1';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$current_sw_version]);
|
||||
$paid_check = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$has_paid_upgrade_from_current = ($paid_check['paid_count'] > 0);
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
$debug['has_paid_upgrade_from_current'] = $has_paid_upgrade_from_current;
|
||||
$debug['version_decisions'] = [];
|
||||
}
|
||||
|
||||
foreach ($versions as $version) {
|
||||
//Check if this version should be shown:
|
||||
//1. If there's a matching upgrade path from current version, show it
|
||||
//2. If no current version exists, show all
|
||||
//3. If there's no upgrade path but also no paths exist for this version at all, show it (free upgrade)
|
||||
//Normalize version for comparison (lowercase, trim leading zeros)
|
||||
$normalized_version = strtolower(ltrim($version['version'], '0'));
|
||||
|
||||
$is_current_version = ($current_sw_version && $normalized_version == $current_sw_version);
|
||||
|
||||
//All versions with matching HW are potential upgrades
|
||||
$show_version = false;
|
||||
if (!$current_sw_version || $current_sw_version == '') {
|
||||
//No current version - show all
|
||||
$show_version = true;
|
||||
} elseif ($version['from_version'] == $current_sw_version) {
|
||||
//Upgrade path exists from current version
|
||||
$show_version = true;
|
||||
} else {
|
||||
//Check if any upgrade paths exist for this version
|
||||
$sql = 'SELECT COUNT(*) as path_count
|
||||
FROM products_software_upgrade_paths
|
||||
WHERE to_version_id = ? AND is_active = 1';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$version['version_id']]);
|
||||
$path_check = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$final_price = '0.00';
|
||||
$final_currency = '';
|
||||
$is_current = false;
|
||||
$decision_reason = '';
|
||||
|
||||
if ($path_check['path_count'] == 0) {
|
||||
//No paths exist at all - show as free upgrade
|
||||
if (debug) {
|
||||
$version_debug = [
|
||||
'version' => $version['version'],
|
||||
'name' => $version['name'],
|
||||
'normalized_version' => $normalized_version,
|
||||
'is_current_version' => $is_current_version,
|
||||
'latest' => $version['latest']
|
||||
];
|
||||
}
|
||||
|
||||
if (!$current_sw_version || $current_sw_version == '') {
|
||||
//No current version - show all as free upgrades
|
||||
if (!$is_current_version) {
|
||||
$show_version = true;
|
||||
$decision_reason = 'No current version stored - showing as free upgrade';
|
||||
} else {
|
||||
$decision_reason = 'Skipped - is current version but no upgrades scenario';
|
||||
}
|
||||
} else {
|
||||
//Check if this is the current version and should be shown as disabled
|
||||
if ($is_current_version && $has_paid_upgrade_from_current && $version['latest'] == 1) {
|
||||
//Show current version as disabled only if it's the latest AND there's a paid upgrade available
|
||||
$show_version = true;
|
||||
$is_current = true;
|
||||
$final_price = '0.00';
|
||||
$final_currency = '';
|
||||
$decision_reason = 'Showing as CURRENT - is latest version with paid upgrade available';
|
||||
} else if ($is_current_version && !($has_paid_upgrade_from_current && $version['latest'] == 1)) {
|
||||
$decision_reason = 'Skipped - is current version but not (latest + has_paid_upgrade)';
|
||||
} else if (!$is_current_version) {
|
||||
//Check if this version is part of ANY upgrade path system (either FROM or TO)
|
||||
$sql = 'SELECT COUNT(*) as path_count
|
||||
FROM products_software_upgrade_paths
|
||||
WHERE (to_version_id = ? OR from_version_id = ?) AND is_active = 1';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$version['version_id'], $version['version_id']]);
|
||||
$path_check = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (debug) {
|
||||
$version_debug['upgrade_path_count'] = $path_check['path_count'];
|
||||
}
|
||||
|
||||
if ($path_check['path_count'] == 0) {
|
||||
//Not part of any upgrade path system - show as free upgrade
|
||||
$show_version = true;
|
||||
$decision_reason = 'Showing as FREE - no upgrade paths defined for this version';
|
||||
} else {
|
||||
//Part of an upgrade path system
|
||||
//Only show if there's an explicit path FROM current version TO this version
|
||||
$sql = 'SELECT pup.price, pup.currency
|
||||
FROM products_software_upgrade_paths pup
|
||||
JOIN products_software_versions from_ver ON pup.from_version_id = from_ver.rowID
|
||||
WHERE pup.to_version_id = ?
|
||||
AND LOWER(TRIM(LEADING "0" FROM from_ver.version)) = ?
|
||||
AND pup.is_active = 1';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$version['version_id'], $current_sw_version]);
|
||||
$upgrade_path = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($upgrade_path) {
|
||||
//Valid upgrade path found FROM current version
|
||||
$show_version = true;
|
||||
$final_price = $upgrade_path['price'] ?? '0.00';
|
||||
$final_currency = $upgrade_path['currency'] ?? '';
|
||||
$decision_reason = 'Showing - found upgrade path FROM current (' . $current_sw_version . ') with price: ' . $final_price . ' ' . $final_currency;
|
||||
} else {
|
||||
$decision_reason = 'Skipped - has upgrade paths but none FROM current version (' . $current_sw_version . ')';
|
||||
}
|
||||
//If no path from current version exists, don't show (show_version stays false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
$version_debug['decision'] = [
|
||||
'show_version' => $show_version,
|
||||
'is_current' => $is_current,
|
||||
'final_price' => $final_price,
|
||||
'final_currency' => $final_currency,
|
||||
'reason' => $decision_reason
|
||||
];
|
||||
}
|
||||
|
||||
if ($show_version) {
|
||||
//Check if there's a valid license for this upgrade
|
||||
$final_price = $version['price'] ?? '0.00';
|
||||
$final_currency = $version['currency'] ?? '';
|
||||
|
||||
$license_applied = false;
|
||||
if ($final_price > 0 && $sw_version_license) {
|
||||
//Check if the license is valid
|
||||
$sql = 'SELECT status, start_at, expires_at
|
||||
@@ -151,7 +295,17 @@ if (isset($criterias['sn']) && $criterias['sn'] != ''){
|
||||
|
||||
//Check if license is within valid date range
|
||||
if ((!$start_at || $start_at <= $now) && (!$expires_at || $expires_at >= $now)) {
|
||||
$original_price = $final_price;
|
||||
$final_price = '0.00';
|
||||
$license_applied = true;
|
||||
|
||||
if (debug) {
|
||||
$version_debug['license_applied'] = [
|
||||
'license_key' => $sw_version_license,
|
||||
'original_price' => $original_price,
|
||||
'new_price' => $final_price
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -169,9 +323,14 @@ if (isset($criterias['sn']) && $criterias['sn'] != ''){
|
||||
"source" => '',
|
||||
"source_type" => '',
|
||||
"price" => $final_price,
|
||||
"currency" => $final_currency
|
||||
"currency" => $final_currency,
|
||||
"is_current" => $is_current
|
||||
];
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
$debug['version_decisions'][] = $version_debug;
|
||||
}
|
||||
}
|
||||
|
||||
//GENERATE DOWNLOAD TOKENS FOR EACH OPTION
|
||||
@@ -180,13 +339,38 @@ if (isset($criterias['sn']) && $criterias['sn'] != ''){
|
||||
$download_token = create_download_url_token($criterias['sn'], $option['version_id']);
|
||||
|
||||
// Create secure download URL
|
||||
$download_url = 'https://'.$_SERVER['SERVER_NAME'].'/api.php/v2/software_download/token='.$download_token;
|
||||
$download_url = 'https://'.$_SERVER['SERVER_NAME'].'/api.php/v2/software_download?token='.$download_token;
|
||||
|
||||
// Set source as download URL
|
||||
$option['source'] = $download_url;
|
||||
$option['source_type'] = 'token_url';
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
$debug['final_output'] = [
|
||||
'total_versions_shown' => count($output),
|
||||
'versions' => array_map(function($o) {
|
||||
return [
|
||||
'name' => $o['name'],
|
||||
'version' => $o['version'],
|
||||
'price' => $o['price'],
|
||||
'is_current' => $o['is_current']
|
||||
];
|
||||
}, $output)
|
||||
];
|
||||
}
|
||||
|
||||
$messages = $output;
|
||||
|
||||
if (debug && !empty($output)) {
|
||||
// Add debug as separate field in response
|
||||
foreach ($messages as &$msg) {
|
||||
$msg['_debug'] = $debug;
|
||||
break; // Only add to first item
|
||||
}
|
||||
} elseif (debug && empty($output)) {
|
||||
$messages = ['message' => 'No upgrades available', 'debug' => $debug];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user