feat: Add software licenses management page and update payment handling
- Introduced a new licenses management page with functionality to create, update, and view software licenses. - Updated payment return handling in softwaretool.php to check payment status from the database and display appropriate modals for success, pending, and failure states. - Enhanced webhook_mollie.php to log webhook calls, handle payment status updates directly in the database, and generate invoices based on payment status. - Improved CSS styles for better alignment of buttons and modal components. - Added JavaScript for modal interactions and bulk license creation functionality.
This commit is contained in:
@@ -49,6 +49,9 @@ if(isset($get_content) && $get_content!=''){
|
||||
elseif ($v[0] == 'equipmentid') {
|
||||
//build up search
|
||||
$clause .= ' AND e.rowID = :'.$v[0];
|
||||
|
||||
//UPDATE VERSION STATUS
|
||||
$sw_version_latest_update = 1;
|
||||
}
|
||||
elseif ($v[0] == 'servicedate') {
|
||||
//build up service coverage
|
||||
@@ -69,6 +72,7 @@ if(isset($get_content) && $get_content!=''){
|
||||
elseif ($v[0] == 'h_equipmentid') {
|
||||
//build up search
|
||||
$clause .= ' AND h.equipmentid = :'.$v[0];
|
||||
|
||||
}
|
||||
elseif ($v[0] == 'status') {
|
||||
//Update status based on status
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
defined($security_key) or exit;
|
||||
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('display_startup_errors', '1');
|
||||
error_reporting(E_ALL);
|
||||
//------------------------------------------
|
||||
// Products Software Licenses
|
||||
//------------------------------------------
|
||||
@@ -12,7 +14,7 @@ $pdo = dbConnect($dbname);
|
||||
if (empty($partner->soldto) || $partner->soldto == ''){$soldto_search = '%';} else {$soldto_search = '-%';}
|
||||
|
||||
//default whereclause
|
||||
list($whereclause,$condition) = getWhereclauselvl2("software_licenses",$permission,$partner,'get');
|
||||
list($whereclause,$condition) = getWhereclauselvl2("products_software_licenses",$permission,$partner,'get');
|
||||
|
||||
//NEW ARRAY
|
||||
$criterias = [];
|
||||
@@ -52,12 +54,20 @@ if(isset($criterias['totals']) && $criterias['totals'] ==''){
|
||||
$sql = 'SELECT count(*) as count FROM products_software_licenses '.$whereclause.'';
|
||||
}
|
||||
elseif (isset($criterias['list']) && $criterias['list'] =='') {
|
||||
//SQL for list
|
||||
$sql = 'SELECT l.*, u.username, v.name as version_name FROM products_software_licenses l LEFT JOIN users u ON l.user_id = u.id LEFT JOIN products_software_versions v ON l.version_id = v.rowID '.$whereclause.' ORDER BY l.created DESC';
|
||||
//SQL for list
|
||||
$sql = 'SELECT l.*, v.name as version_name, v.version, e.serialnumber as assigned_serial
|
||||
FROM products_software_licenses l
|
||||
LEFT JOIN products_software_versions v ON l.version_id = v.rowID
|
||||
LEFT JOIN equipment e ON l.license_key = e.sw_version_license
|
||||
'.$whereclause.' ORDER BY l.created DESC';
|
||||
}
|
||||
else {
|
||||
//SQL for paged
|
||||
$sql = 'SELECT l.*, u.username, v.name as version_name FROM products_software_licenses l LEFT JOIN users u ON l.user_id = u.id LEFT JOIN products_software_versions v ON l.version_id = v.rowID '.$whereclause.' ORDER BY l.created DESC LIMIT :page,:num_licenses';
|
||||
//SQL for paged
|
||||
$sql = 'SELECT l.*, v.name as version_name, v.version, e.serialnumber as assigned_serial
|
||||
FROM products_software_licenses l
|
||||
LEFT JOIN products_software_versions v ON l.version_id = v.rowID
|
||||
LEFT JOIN equipment e ON l.license_key = e.sw_version_license
|
||||
'.$whereclause.' ORDER BY l.created DESC LIMIT :page,:num_licenses';
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
@@ -245,16 +245,16 @@ if (isset($criterias['sn']) && $criterias['sn'] != ''){
|
||||
//Check if there's a valid license for this upgrade
|
||||
if ($final_price > 0 && $sw_version_license) {
|
||||
//Check if the license is valid
|
||||
$sql = 'SELECT status, start_at, expires_at
|
||||
$sql = 'SELECT status, starts_at, expires_at
|
||||
FROM products_software_licenses
|
||||
WHERE license_key = ? AND equipment_id = ?';
|
||||
WHERE license_key = ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$sw_version_license, $equipment_rowid]);
|
||||
$stmt->execute([$sw_version_license]);
|
||||
$license = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($license && $license['status'] == 1) {
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$start_at = $license['start_at'];
|
||||
$start_at = $license['starts_at'];
|
||||
$expires_at = $license['expires_at'];
|
||||
|
||||
//Check if license is within valid date range
|
||||
|
||||
@@ -281,16 +281,16 @@ if (isset($criterias['sn']) && $criterias['sn'] != ''){
|
||||
$license_applied = false;
|
||||
if ($final_price > 0 && $sw_version_license) {
|
||||
//Check if the license is valid
|
||||
$sql = 'SELECT status, start_at, expires_at
|
||||
$sql = 'SELECT status, starts_at, expires_at
|
||||
FROM products_software_licenses
|
||||
WHERE license_key = ? AND equipment_id = ?';
|
||||
WHERE license_key = ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$sw_version_license, $equipment_rowid]);
|
||||
$stmt->execute([$sw_version_license]);
|
||||
$license = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($license && $license['status'] == 1) {
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$start_at = $license['start_at'];
|
||||
$start_at = $license['starts_at'];
|
||||
$expires_at = $license['expires_at'];
|
||||
|
||||
//Check if license is within valid date range
|
||||
|
||||
@@ -354,6 +354,42 @@ elseif(isset($post_content['dealer_closeby'])){
|
||||
echo json_encode(['error' => "Latitude or longitude not provided."]);
|
||||
}
|
||||
}
|
||||
elseif(isset($post_content['action']) && $post_content['action']=='unsubscribe'){
|
||||
//++++++++++++++++++++++
|
||||
//Process DEALER UNSUBSCRIBE
|
||||
//++++++++++++++++++++++
|
||||
|
||||
// Check if email is provided
|
||||
if (isset($post_content['email']) && !empty($post_content['email'])) {
|
||||
$email = $post_content['email'];
|
||||
|
||||
try {
|
||||
// Update dealer status to 0 (inactive) where email matches
|
||||
$sql = 'UPDATE dealers SET status = 0 WHERE email = ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
if ($stmt->execute([$email])) {
|
||||
// Check if any rows were affected
|
||||
if ($stmt->rowCount() > 0) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['status' => 'success', 'message' => 'Dealer unsubscribed successfully']);
|
||||
} else {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['status' => 'error', 'message' => 'No dealer found with this email']);
|
||||
}
|
||||
} else {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['status' => 'error', 'message' => 'Database update failed']);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['status' => 'error', 'message' => 'Database error occurred']);
|
||||
}
|
||||
} else {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['status' => 'error', 'message' => 'Email not provided']);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//++++++++++++++++++++++
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
<?php
|
||||
defined($security_key) or exit;
|
||||
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('display_startup_errors', '1');
|
||||
error_reporting(E_ALL);
|
||||
//------------------------------------------
|
||||
// Payment Creation (for Software Upgrades)
|
||||
//------------------------------------------
|
||||
// This endpoint creates a Mollie payment and stores transaction data
|
||||
// SECURITY: Price is calculated SERVER-SIDE, never trusted from frontend
|
||||
|
||||
//Connect to DB
|
||||
$pdo = dbConnect($dbname);
|
||||
@@ -13,6 +14,7 @@ $pdo = dbConnect($dbname);
|
||||
//CONTENT FROM API (POST)
|
||||
$post_content = json_decode($input, true);
|
||||
|
||||
|
||||
// Validate required inputs
|
||||
if (empty($post_content['serial_number']) || empty($post_content['version_id'])) {
|
||||
http_response_code(400);
|
||||
@@ -33,6 +35,7 @@ $stmt->execute([$serial_number]);
|
||||
$equipment = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$equipment) {
|
||||
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Device not found with serial number: ' . $serial_number], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
@@ -46,15 +49,15 @@ $hw_version = $equipment['hw_version'] ?? '';
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 2: Get version data from version_id
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$sql = 'SELECT v.rowID as version_id, v.version, v.name, v.description, v.hw_version, p.productcode
|
||||
FROM products_software_versions v
|
||||
JOIN products_software p ON v.product_software_id = p.rowID
|
||||
WHERE v.rowID = ? AND v.is_active = 1';
|
||||
$sql = 'SELECT rowID as version_id, version, name, description, hw_version
|
||||
FROM products_software_versions
|
||||
WHERE rowID = ? AND status = 1';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$version_id]);
|
||||
$version = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$version) {
|
||||
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Software version not found or inactive'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
@@ -93,6 +96,7 @@ if (!$has_upgrade_paths) {
|
||||
$final_currency = $upgrade_path['currency'] ?? 'EUR';
|
||||
} else {
|
||||
// No upgrade path FROM current version
|
||||
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'No valid upgrade path from current version'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
@@ -103,20 +107,20 @@ if (!$has_upgrade_paths) {
|
||||
// STEP 4: Check license validity (lines 280-311 in software_update.php)
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
if ($final_price > 0 && $sw_version_license) {
|
||||
$sql = 'SELECT status, start_at, expires_at
|
||||
$sql = 'SELECT status, starts_at, expires_at
|
||||
FROM products_software_licenses
|
||||
WHERE license_key = ? AND equipment_id = ?';
|
||||
WHERE license_key = ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$sw_version_license, $equipment_id]);
|
||||
$stmt->execute([$sw_version_license]);
|
||||
$license = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($license && $license['status'] == 1) {
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$start_at = $license['start_at'];
|
||||
$starts_at = $license['starts_at'];
|
||||
$expires_at = $license['expires_at'];
|
||||
|
||||
// Check if license is within valid date range
|
||||
if ((!$start_at || $start_at <= $now) && (!$expires_at || $expires_at >= $now)) {
|
||||
if ((!$starts_at || $starts_at <= $now) && (!$expires_at || $expires_at >= $now)) {
|
||||
$final_price = '0.00';
|
||||
}
|
||||
}
|
||||
@@ -126,68 +130,18 @@ if ($final_price > 0 && $sw_version_license) {
|
||||
// STEP 5: Verify price > 0 (free upgrades shouldn't reach payment API)
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
if ($final_price <= 0) {
|
||||
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'This upgrade is free. No payment required.'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 6: DEBUG MODE - Simulate payment without Mollie
|
||||
// STEP 6: DEBUG MODE - Log but continue to real Mollie
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
if (debug) {
|
||||
// Generate fake payment ID
|
||||
$fake_payment_id = 'DEBUG_' . uniqid() . '_' . time();
|
||||
$checkout_url = 'https://'.$_SERVER['SERVER_NAME'].'/softwaretool.php?payment_return=1&payment_id=' . $fake_payment_id;
|
||||
|
||||
// Store transaction in DB
|
||||
$sql = 'INSERT INTO transactions (txn_id, payment_amount, payment_status, payer_email, first_name, last_name,
|
||||
address_street, address_city, address_state, address_zip, address_country, account_id, payment_method, created)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([
|
||||
$fake_payment_id,
|
||||
$final_price,
|
||||
0, // 0 = pending
|
||||
$user_data['email'] ?? '',
|
||||
$user_data['first_name'] ?? '',
|
||||
$user_data['last_name'] ?? '',
|
||||
$user_data['address_street'] ?? '',
|
||||
$user_data['address_city'] ?? '',
|
||||
$user_data['address_state'] ?? '',
|
||||
$user_data['address_zip'] ?? '',
|
||||
$user_data['address_country'] ?? '',
|
||||
$serial_number,
|
||||
0, // payment method
|
||||
date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
// Store transaction item with serial_number in item_options
|
||||
$item_options = json_encode([
|
||||
'serial_number' => $serial_number,
|
||||
'equipment_id' => $equipment_id,
|
||||
'hw_version' => $hw_version
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$sql = 'INSERT INTO transactions_items (txn_id, item_id, item_price, item_quantity, item_options, created)
|
||||
VALUES (?, ?, ?, ?, ?, ?)';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([
|
||||
$fake_payment_id,
|
||||
$version_id,
|
||||
$final_price,
|
||||
1,
|
||||
$item_options,
|
||||
date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
// Return fake checkout URL
|
||||
$messages = json_encode([
|
||||
'checkout_url' => $checkout_url,
|
||||
'payment_id' => $fake_payment_id,
|
||||
'debug_mode' => true
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
echo $messages;
|
||||
exit;
|
||||
debuglog("DEBUG MODE: Creating real Mollie payment for testing");
|
||||
debuglog("DEBUG: Serial Number: $serial_number, Version ID: $version_id, Price: $final_price");
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
@@ -195,66 +149,110 @@ if (debug) {
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
try {
|
||||
// Initialize Mollie
|
||||
require dirname(__FILE__, 3).'/initialize.php';
|
||||
require dirname(__FILE__, 4).'/initialize.php';
|
||||
|
||||
// Format price for Mollie (must be string with 2 decimals)
|
||||
$formatted_price = number_format((float)$final_price, 2, '.', '');
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 7A: Generate transaction ID BEFORE creating Mollie payment
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// Generate unique transaction ID (same as placeorder.php)
|
||||
$txn_id = strtoupper(uniqid('SC') . substr(md5(mt_rand()), 0, 5));
|
||||
|
||||
// Build webhook URL and redirect URL with actual transaction ID
|
||||
$protocol = 'https';
|
||||
$hostname = $_SERVER['SERVER_NAME'];
|
||||
$path = '/';
|
||||
$webhook_url = "{$protocol}://{$hostname}{$path}webhook_mollie.php";
|
||||
$redirect_url = "{$protocol}://{$hostname}{$path}?page=softwaretool&payment_return=1&order_id={$txn_id}";
|
||||
|
||||
if (debug) {
|
||||
debuglog("DEBUG: Transaction ID: {$txn_id}");
|
||||
debuglog("DEBUG: redirectUrl being sent to Mollie: " . $redirect_url);
|
||||
}
|
||||
|
||||
// Create payment with Mollie
|
||||
$payment = $mollie->payments->create([
|
||||
'amount' => [
|
||||
'currency' => $final_currency ?: 'EUR',
|
||||
'value' => $formatted_price
|
||||
'value' => "{$formatted_price}"
|
||||
],
|
||||
'description' => 'Software upgrade to ' . $version['name'] . ' (v' . $version['version'] . ')',
|
||||
'redirectUrl' => 'https://'.$_SERVER['SERVER_NAME'].'/softwaretool.php?payment_return=1&payment_id={id}',
|
||||
'webhookUrl' => 'https://'.$_SERVER['SERVER_NAME'].'/webhook_mollie.php',
|
||||
'description' => "Software upgrade Order #{$txn_id}",
|
||||
'redirectUrl' => "{$redirect_url}",
|
||||
'webhookUrl' => "{$webhook_url}",
|
||||
'metadata' => [
|
||||
'order_id' => $payment->id // Store payment ID in metadata
|
||||
'order_id' => $txn_id,
|
||||
'serial_number' => $serial_number,
|
||||
'version_id' => $version_id,
|
||||
'equipment_id' => $equipment_id
|
||||
]
|
||||
]);
|
||||
|
||||
$mollie_payment_id = $payment->id;
|
||||
$checkout_url = $payment->getCheckoutUrl();
|
||||
|
||||
if (debug) {
|
||||
debuglog("DEBUG: Mollie payment created successfully");
|
||||
debuglog("DEBUG: Payment ID: $mollie_payment_id");
|
||||
debuglog("DEBUG: Redirect URL sent: $redirect_url");
|
||||
debuglog("DEBUG: Redirect URL from Mollie object: " . $payment->redirectUrl);
|
||||
debuglog("DEBUG: Full payment object: " . json_encode($payment));
|
||||
debuglog("DEBUG: Checkout URL: $checkout_url");
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 8: Store transaction in DB
|
||||
// STEP 8: Store transaction in DB using txn_id (order ID)
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// Split name into first/last (simple split on first space)
|
||||
$full_name = $user_data['name'] ?? '';
|
||||
$name_parts = explode(' ', $full_name, 2);
|
||||
$first_name = $name_parts[0] ?? '';
|
||||
$last_name = $name_parts[1] ?? '';
|
||||
|
||||
// BUILD UP PARTNERHIERARCHY FROM USER
|
||||
$partner_product = json_encode(array("salesid"=>$partner->salesid,"soldto"=>$partner->soldto), JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$sql = 'INSERT INTO transactions (txn_id, payment_amount, payment_status, payer_email, first_name, last_name,
|
||||
address_street, address_city, address_state, address_zip, address_country, account_id, payment_method, created)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
||||
address_street, address_city, address_state, address_zip, address_country, account_id, payment_method, accounthierarchy, created)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([
|
||||
$mollie_payment_id,
|
||||
$txn_id, // Use generated transaction ID, not Mollie payment ID
|
||||
$final_price,
|
||||
0, // 0 = pending
|
||||
$user_data['email'] ?? '',
|
||||
$user_data['first_name'] ?? '',
|
||||
$user_data['last_name'] ?? '',
|
||||
$user_data['address_street'] ?? '',
|
||||
$user_data['address_city'] ?? '',
|
||||
$user_data['address_state'] ?? '',
|
||||
$user_data['address_zip'] ?? '',
|
||||
$user_data['address_country'] ?? '',
|
||||
$first_name,
|
||||
$last_name,
|
||||
$user_data['address'] ?? '',
|
||||
$user_data['city'] ?? '',
|
||||
'', // address_state (not collected)
|
||||
$user_data['postal'] ?? '',
|
||||
$user_data['country'] ?? '',
|
||||
$serial_number,
|
||||
0, // payment method
|
||||
$partner_product,
|
||||
date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
// Get the database ID
|
||||
$transaction_id = $pdo->lastInsertId();
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 9: Store transaction item with serial_number in item_options
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$item_options = json_encode([
|
||||
'serial_number' => $serial_number,
|
||||
'equipment_id' => $equipment_id,
|
||||
'hw_version' => $hw_version
|
||||
'hw_version' => $hw_version,
|
||||
'mollie_payment_id' => $mollie_payment_id // Store Mollie payment ID in options
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$sql = 'INSERT INTO transactions_items (txn_id, item_id, item_price, item_quantity, item_options, created)
|
||||
VALUES (?, ?, ?, ?, ?, ?)';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([
|
||||
$mollie_payment_id,
|
||||
$transaction_id, // Use database transaction ID (not txn_id string, not mollie_payment_id)
|
||||
$version_id,
|
||||
$final_price,
|
||||
1,
|
||||
|
||||
@@ -14,12 +14,16 @@ $post_content = json_decode($input,true);
|
||||
if (empty($partner->soldto) || $partner->soldto == ''){$soldto_search = '%';} else {$soldto_search = '-%';}
|
||||
|
||||
//default whereclause
|
||||
list($whereclause,$condition) = getWhereclauselvl2("software_licenses",$permission,$partner,'');
|
||||
list($whereclause,$condition) = getWhereclauselvl2("products_software_licenses",$permission,$partner,'');
|
||||
|
||||
//SET PARAMETERS FOR QUERY
|
||||
$id = $post_content['rowID'] ?? ''; //check for rowID
|
||||
$command = ($id == '')? 'insert' : 'update'; //IF rowID = empty then INSERT
|
||||
if (isset($post_content['delete'])){$command = 'delete';} //change command to delete
|
||||
|
||||
// Check for bulk creation
|
||||
$is_bulk = isset($post_content['bulk']) && $post_content['bulk'] === true;
|
||||
|
||||
$date = date('Y-m-d H:i:s');
|
||||
|
||||
//CREATE EMPTY STRINGS
|
||||
@@ -27,12 +31,90 @@ $clause = '';
|
||||
$clause_insert ='';
|
||||
$input_insert = '';
|
||||
|
||||
//------------------------------------------
|
||||
// BULK LICENSE CREATION
|
||||
//------------------------------------------
|
||||
if ($command == 'insert' && $is_bulk && isAllowed('products_software_licenses',$profile,$permission,'C') === 1){
|
||||
|
||||
$version_id = $post_content['version_id'] ?? '';
|
||||
$serials = $post_content['serials'] ?? [];
|
||||
$transaction_id = $post_content['transaction_id'] ?? '';
|
||||
$license_type = $post_content['license_type'] ?? 0;
|
||||
$status = $post_content['status'] ?? 0;
|
||||
|
||||
if (empty($version_id) || empty($serials) || !is_array($serials)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid parameters for bulk creation']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$accounthierarchy = json_encode(array("salesid"=>$partner->salesid,"soldto"=>$partner->soldto), JSON_UNESCAPED_UNICODE);
|
||||
|
||||
// Prepare statement for bulk insert
|
||||
$sql = 'INSERT INTO products_software_licenses (version_id, license_key, license_type, status, transaction_id, accounthierarchy, created, createdby)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
$created_count = 0;
|
||||
foreach ($serials as $serial) {
|
||||
if (empty($serial)) continue;
|
||||
|
||||
// Generate UUID for license key
|
||||
$license_key = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0x0fff) | 0x4000,
|
||||
mt_rand(0, 0x3fff) | 0x8000,
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
||||
);
|
||||
|
||||
try {
|
||||
$stmt->execute([
|
||||
$version_id,
|
||||
$license_key,
|
||||
$license_type,
|
||||
$status,
|
||||
$transaction_id,
|
||||
$accounthierarchy,
|
||||
$date,
|
||||
$username
|
||||
]);
|
||||
|
||||
// Assign license to equipment if serial number exists
|
||||
$eq_sql = 'UPDATE equipment SET sw_version_license = ? WHERE serialnumber = ? AND accounthierarchy LIKE ?';
|
||||
$eq_stmt = $pdo->prepare($eq_sql);
|
||||
$eq_stmt->execute([$license_key, $serial, '%'.$partner->soldto.'%']);
|
||||
|
||||
$created_count++;
|
||||
} catch (Exception $e) {
|
||||
debuglog("Error creating license for serial $serial: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode(['success' => true, 'created' => $created_count]);
|
||||
exit;
|
||||
}
|
||||
|
||||
//------------------------------------------
|
||||
// SINGLE LICENSE CREATION OR UPDATE
|
||||
//------------------------------------------
|
||||
//ADD STANDARD PARAMETERS TO ARRAY BASED ON INSERT OR UPDATE
|
||||
if ($command == 'update'){
|
||||
$post_content['updated'] = $date;
|
||||
$post_content['updatedby'] = $username;
|
||||
}
|
||||
elseif ($command == 'insert'){
|
||||
// Generate UUID for license key if not provided
|
||||
if (empty($post_content['license_key'])) {
|
||||
$post_content['license_key'] = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0x0fff) | 0x4000,
|
||||
mt_rand(0, 0x3fff) | 0x8000,
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
||||
);
|
||||
}
|
||||
|
||||
$post_content['created'] = $date;
|
||||
$post_content['createdby'] = $username;
|
||||
$post_content['accounthierarchy'] = json_encode(array("salesid"=>$partner->salesid,"soldto"=>$partner->soldto), JSON_UNESCAPED_UNICODE);
|
||||
@@ -42,10 +124,10 @@ else {
|
||||
}
|
||||
|
||||
//CREATE NEW ARRAY AND MAP TO CLAUSE
|
||||
if(isset($post_content) && $post_content!=''){
|
||||
if(isset($post_content) && $post_content!=''){
|
||||
foreach ($post_content as $key => $var){
|
||||
if ($key == 'submit' || $key == 'rowID'){
|
||||
//do nothing
|
||||
if ($key == 'submit' || $key == 'rowID' || $key == 'serial' || $key == 'bulk' || $key == 'serials'){
|
||||
//do nothing - skip these fields
|
||||
}
|
||||
else {
|
||||
$criterias[$key] = $var;
|
||||
@@ -64,27 +146,43 @@ $input_insert = substr($input_insert, 1); //Clean clause - remove first comma
|
||||
|
||||
//QUERY AND VERIFY ALLOWED
|
||||
if ($command == 'update' && isAllowed('products_software_licenses',$profile,$permission,'U') === 1){
|
||||
|
||||
|
||||
$sql = 'UPDATE products_software_licenses SET '.$clause.' WHERE rowID = ? ';
|
||||
$execute_input[] = $id;
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($execute_input);
|
||||
}
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
}
|
||||
elseif ($command == 'insert' && isAllowed('products_software_licenses',$profile,$permission,'C') === 1){
|
||||
|
||||
//INSERT NEW ITEM
|
||||
|
||||
//INSERT NEW ITEM
|
||||
$sql = 'INSERT INTO products_software_licenses ('.$clause_insert.') VALUES ('.$input_insert.')';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($execute_input);
|
||||
|
||||
$new_license_id = $pdo->lastInsertId();
|
||||
$license_key = $post_content['license_key'];
|
||||
|
||||
// Assign license to equipment if serial number provided
|
||||
if (!empty($post_content['serial'])) {
|
||||
$serial = $post_content['serial'];
|
||||
$eq_sql = 'UPDATE equipment SET sw_version_license = ? WHERE serialnumber = ? AND accounthierarchy LIKE ?';
|
||||
$eq_stmt = $pdo->prepare($eq_sql);
|
||||
$eq_stmt->execute([$license_key, $serial, '%'.$partner->soldto.'%']);
|
||||
}
|
||||
|
||||
echo json_encode(['success' => true, 'license_id' => $new_license_id, 'license_key' => $license_key]);
|
||||
}
|
||||
elseif ($command == 'delete' && isAllowed('products_software_licenses',$profile,$permission,'D') === 1){
|
||||
|
||||
|
||||
$stmt = $pdo->prepare('DELETE FROM products_software_licenses WHERE rowID = ? ');
|
||||
$stmt->execute([ $id ]);
|
||||
$stmt->execute([ $id ]);
|
||||
|
||||
//Add deletion to changelog
|
||||
changelog($dbname,'products_software_licenses',$id,'Delete','Delete',$username);
|
||||
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
} else
|
||||
{
|
||||
//do nothing
|
||||
|
||||
@@ -72,10 +72,9 @@ $hw_version = (isset($criterias['hw_version']))? $criterias['hw_version']:'';
|
||||
if ($command == 'update' && isAllowed('products_software_versions',$profile,$permission,'U') === 1){
|
||||
|
||||
//REMOVE LATEST FLAG FROM OTHER WHEN SEND
|
||||
//Max 2 latest flags per hw_version: 1 with price (has upgrade path with price) and 1 without
|
||||
if (isset($criterias['latest']) && $criterias['latest'] == 1){
|
||||
$sql = 'UPDATE products_software_versions SET latest = 0 WHERE hw_version = ? AND rowID != ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$hw_version, $id]);
|
||||
updateSoftwareLatestFlags($pdo, $id, $hw_version);
|
||||
}
|
||||
|
||||
$sql = 'UPDATE products_software_versions SET '.$clause.' WHERE rowID = ? ';
|
||||
@@ -84,18 +83,18 @@ if ($command == 'update' && isAllowed('products_software_versions',$profile,$per
|
||||
$stmt->execute($execute_input);
|
||||
}
|
||||
elseif ($command == 'insert' && isAllowed('products_software_versions',$profile,$permission,'C') === 1){
|
||||
|
||||
//REMOVE LATEST FLAG FROM OTHER IF SET
|
||||
if (isset($criterias['latest']) && $criterias['latest'] == 1){
|
||||
$sql = 'UPDATE products_software_versions SET latest = 0 WHERE hw_version = ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$hw_version]);
|
||||
}
|
||||
|
||||
//INSERT NEW ITEM
|
||||
$sql = 'INSERT INTO products_software_versions ('.$clause_insert.') VALUES ('.$input_insert.')';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($execute_input);
|
||||
$new_id = $pdo->lastInsertId();
|
||||
|
||||
//REMOVE LATEST FLAG FROM OTHER IF SET
|
||||
//Max 2 latest flags per hw_version: 1 with price (has upgrade path with price) and 1 without
|
||||
if (isset($criterias['latest']) && $criterias['latest'] == 1){
|
||||
updateSoftwareLatestFlags($pdo, $new_id, $hw_version);
|
||||
}
|
||||
}
|
||||
elseif ($command == 'delete' && isAllowed('products_software_versions',$profile,$permission,'D') === 1){
|
||||
|
||||
|
||||
Reference in New Issue
Block a user