Files
assetmgt/api/v1/post/application.php
“VeLiTi” 3db13b9ebf feat: Enhance software tool with country selection and tax calculation
- Added a helper function to generate country select options in software tool.
- Updated user info modal and payment modal to use country dropdowns instead of text inputs.
- Implemented tax calculation based on selected country in payment modal.
- Improved software options loading behavior in debug mode.
- Enhanced description formatting in payment modal.
- Added log modal for equipment updates with a link to view logs.
- Introduced a new countries settings file with tax rates for various countries.
- Minor adjustments to various PHP files for better handling of equipment and payment processes.
2026-01-16 16:01:31 +01:00

337 lines
14 KiB
PHP

<?php
defined($security_key) or exit;
///------------------------------------------
// Application related calls
//------------------------------------------
$action = $request[2] ?? '';
//------------------------------------------
// Check for action & start application API
//------------------------------------------
if ($action !=''){
//------------------------------------------
//Connect to DB
//------------------------------------------
$pdo = dbConnect($dbname);
//------------------------------------------
//CONTENT FROM API (POST)
//------------------------------------------
$post_content = json_decode(decode_payload($input),true);
//SoldTo is empty
if (empty($partner->soldto) || $partner->soldto == ''){$soldto_search = '%';} else {$soldto_search = '-%';}
//default whereclause
$whereclause = 'WHERE';
switch ($permission) {
case '4':
$whereclause .= '';
break;
case '3':
$condition = '__salesid___'.$partner->salesid.'___soldto___%';
$whereclause = ' e.accounthierarchy like "'.$condition.'" AND ';
break;
case '2':
$condition = '__salesid___'.$partner->salesid.'___soldto___'.substr($partner->soldto, 0, strpos($partner->soldto, "-")).$soldto_search;
$whereclause .= ' e.accounthierarchy like "'.$condition.'" AND ';
break;
default:
$condition = '__salesid___'.$partner->salesid.'___soldto___'.substr($partner->soldto, 0, strpos($partner->soldto, "-")).$soldto_search.'___shipto___'.substr($partner->shipto, 0, strpos($partner->shipto, "-")).'%___location___'.substr($partner->location, 0, strpos($partner->location, "-")).'%';
$whereclause .= ' e.accounthierarchy like "'.$condition.'" AND ';
break;
}
//------------------------------------------
// Actions
//------------------------------------------
switch ($action) {
case 'unscribe':
// -----------------------------------------------------------
// Unscribe from mailinglist -> set communication status to 0
// -----------------------------------------------------------
if (isset($post_content['email']) && $post_content['email'] !=''){
$sql = 'UPDATE communication SET status = 0 WHERE email = ?';
$stmt = $pdo->prepare($sql);
$stmt->execute([$post_content['email']]);
if($stmt->execute()){
$messages = $unscribe_msg1;
} else{
$messages = $unscribe_msg_error;
}
} else{
$messages = $unscribe_msg_error;
}
//Encrypt results
$messages = generate_payload($messages);
//Send results
echo $messages;
break;
case 'register':
// --------------------------------------------
// User registration
// --------------------------------------------
$firmware_account_send = 0; //Default value -> no mail send
$communication_check = 0; //Check communication record
$message_box = [];
$timestamp = date("Y-m-d H:i:s");
// --------------------------------------------
// Check if multiple serialnumbers are provided
// --------------------------------------------
// Normalize input to always be an array
$serial_numbers = is_array($post_content['sn']) ? $post_content['sn'] : [$post_content['sn']];
foreach ($serial_numbers as $sn) {
// Get equipment ID based on serial number
$sql = 'SELECT rowID, warranty_date, order_send_date from equipment where serialnumber = ?';
$stmt = $pdo->prepare($sql);
$stmt->execute([$sn]);
$rowID = $stmt->fetch();
if (!$rowID['rowID']) {
// Serial number not recognized
$message_box[] = $sn . ' - ' . $register_message_1;
continue;
}
// Check if under warranty
$warranty_types = [$type9, $type10, $type11, $type12];
$warranty_condition = 'equipmentid="' . $rowID['rowID'] . '" && (type="' . implode('" || type="', $warranty_types) . '")';
$warranty = getrowID($dbname, 'rowID', 'equipment_history', $warranty_condition);
if ($warranty) {
// Already under contract
$message_box[] = $sn . ' - ' . $register_message_2;
$communication_check = 1;
continue;
}
//define warranty_end_date
$order_send_date = $rowID['order_send_date'] ?? $rowID['warranty_date'];
// Check if order_send_date is available
if (empty($order_send_date)) {
// No valid date found - skip this serial number
$message_box[] = $sn . ' - ' . $register_message_1; // or create a specific message for missing date
continue;
}
// Calculate warranty end date based on eligibility window
$current_date = new DateTime();
$order_date = new DateTime($order_send_date);
$months_diff = $current_date->diff($order_date)->m + ($current_date->diff($order_date)->y * 12);
if ($months_diff <= WARRANTY_ELIGIBILITY_WINDOW) {
// Within eligibility window - apply extended warranty
$warranty_end_date = (clone $order_date)->modify('+' . WARRANTY_EXTENDED_MONTH . ' months')->format('Y-m-d');
} else {
// Outside eligibility window - apply standard warranty
$warranty_end_date = (clone $order_date)->modify('+' . WARRANTY_MONTHS . ' months')->format('Y-m-d');
}
// Not under warranty - process registration
$firmware_account_send = 1;
//Create history description
$history_description = [
"start_date"=>$timestamp,
"end_date"=> $warranty_end_date,
"organization"=>strip_tags(trim($post_content['organization'])),
"phone"=>strip_tags(trim($post_content['phone'])),
"city"=>strip_tags(trim($post_content['city'])),
"country"=>strip_tags(trim($post_content['country'])),
"email_consent"=>strip_tags(trim($post_content['email_consent'])),
"terms_consent"=>strip_tags(trim($post_content['terms_consent']))
];
$description = json_encode($history_description, JSON_UNESCAPED_UNICODE);
// Create history entry
$sql = 'INSERT INTO equipment_history (equipmentid, type, description, created, createdby, updatedby) VALUES (?,?,?,?,?,?)';
$stmt = $pdo->prepare($sql);
$stmt->execute([
$rowID['rowID'],
$type9,
$description,
$timestamp,
$post_content['email'],
$post_content['email']
]);
// Get partner details of equipment
$partner_equipment = getrowID($dbname, 'accounthierarchy', 'equipment', 'rowID="' . $rowID['rowID'] . '"');
$partner_equipment = json_decode($partner_equipment['accounthierarchy']);
// Setup partner hierarchy
$partnerhierarchy = [
"salesid" => $partner_equipment->salesid,
"soldto" => $partner_equipment->soldto
];
// Setup variables for partner
$partnername = $post_content['organization'];
$salesID = json_encode($partnerhierarchy, JSON_UNESCAPED_UNICODE);
$createdby = 'system';
// Helper function to get or create partner
$getOrCreatePartner = function($partnertype) use ($dbname, $partnername, $salesID, $createdby, $pdo) {
$partner = getrowID($dbname, 'partnerID', 'partner', 'partnername = "' . $partnername . '" && partnertype="' . $partnertype . '"');
if ($partner) {
return $partner['partnerID'] . '-' . $partnername;
}
// Partner does not exist - create
$sql = 'INSERT INTO partner (partnertype, partnername, salesID, createdby, status) VALUES (?,?,?,?,?)';
$stmt = $pdo->prepare($sql);
$stmt->execute([$partnertype, $partnername, $salesID, $createdby, '1']);
$partner_rowid = $pdo->lastInsertId();
return $partner_rowid . '-' . $partnername;
};
// Handle shipto
$partnerhierarchy['shipto'] = empty($partner_equipment->shipto)
? $getOrCreatePartner($partnertype3)
: $partner_equipment->shipto;
// Handle location
$partnerhierarchy['location'] = empty($partner_equipment->location)
? $getOrCreatePartner($partnertype4)
: $partner_equipment->location;
$partnerhierarchy_json = json_encode($partnerhierarchy, JSON_UNESCAPED_UNICODE);
// Update equipment record
$sql = 'UPDATE equipment SET status = ?, warranty_date = ?, accounthierarchy = ?, updatedby = ? WHERE rowID = ?';
$stmt = $pdo->prepare($sql);
$stmt->execute(['4', $warranty_end_date, $partnerhierarchy_json, $username, $rowID['rowID']]);
// Add warranty to changelog
$warranty_user = $post_content['email'] ?? 'system';
changelog($dbname, 'equipment', $rowID['rowID'], 'Warranty', $warranty_end_date, $warranty_user);
// Serial number recognized
$message_box[] = $sn . ' - ' . $register_message_3;
$communication_check = 1;
}
// --------------------------------------------
// Send generic account to user for software updates
// --------------------------------------------
if ($firmware_account_send == 1){
include_once './assets/mail/email_template_software.php';
send_mail($post_content['email'],$subject,$message,'','');
}
// ----------------------------------------------
// Create communication user when not exist
// ----------------------------------------------
if ($communication_check == 1 && isset($shipto) && $shipto !=''){
//Check if communication record exist
$rowID = getrowID($dbname,'rowID','communication','partnerID ="'.$shipto.'" and email = "'.$post_content['email'].'"');
if ($rowID){
//communication record exist
}
else
{
//communication record does not exist ->create
$sql = 'INSERT INTO communication (status,partnerID,email,type_1,type_2,type_3,createdby,salesID,coms_type) VALUES (?,?,?,?,?,?,?,?,?)';
$stmt = $pdo->prepare($sql);
$stmt->execute(['1',$shipto,$post_content['email'],'1','1','1',$createdby,$salesID,'1']);
}
}
//Encrypt results
$messages = generate_payload($message_box);
//Send results
echo $messages;
// --------------------------------------------
// END User registration
// --------------------------------------------
break;
case 'firmwareCommunication':
if (isset($post_content['hw_version']) && $post_content['hw_version'] != ''){
include './settings/systemfirmware.php';
$target = $post_content['target'] ?? '0';
//FILTER VARIABLES FOR SQL
$filter1 = 'soldto":"';
$filter2 = '","shipto';
$filter3 = 'shipto":"';
$filter4 = '","location';
//ADD additional createria to whereclause (Firmware and Active)
$whereclause .= " e.hw_version= ? AND c.type_1='1' AND c.status='1' AND e.status != 5 AND (e.sw_version != '$FirmwarenameR06' OR e.sw_version != '$FirmwarenameR06A' OR e.sw_version != '$FirmwarenameR07A' OR e.sw_version != '$FirmwarenameR07B' OR e.sw_version != '$FirmwarenameR07' OR e.sw_version != '$FirmwarenameR08')";
//get target
switch ($target) {
case '0': // Both
$onclause ="SUBSTRING_INDEX(SUBSTRING_INDEX(e.accounthierarchy, '$filter1', -1),'$filter2',1) = c.partnerID or SUBSTRING_INDEX(SUBSTRING_INDEX(e.accounthierarchy, '$filter3', -1),'$filter4',1) = c.partnerID";
break;
case '1': // SoldTO only
$onclause ="SUBSTRING_INDEX(SUBSTRING_INDEX(e.accounthierarchy, '$filter1', -1),'$filter2',1) = c.partnerID";
break;
case '2': // ShipTO only
$onclause =" SUBSTRING_INDEX(SUBSTRING_INDEX(e.accounthierarchy, '$filter3', -1),'$filter4',1) = c.partnerID";
break;
}
//CHECK IF WHERE CLAUSE CONTAINS WHERE
//GET THE FULL LIST OF COMMUNCATION RECORDS FOR FIRMWARE MESSAGE
$sql = "SELECT e.sw_version, c.email from equipment e join communication c on $onclause $whereclause group by c.email";
$stmt = $pdo->prepare($sql);
$stmt->execute([$post_content['hw_version']]);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($results) {
//IF RESULTS ARE RETURNED
foreach ($results as $result) {
//LOOP OVER ALL RESULTS AND SET SEND_INDICATOR to 1
$sql = "UPDATE communication SET send_indicator = 1 WHERE email = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$result['email']]);
}
//------------------------------------------
//Encrypt results
//------------------------------------------
$messages = generate_payload('200');
//------------------------------------------
//Send results
//------------------------------------------
echo $messages;
}
else {
http_response_code(200);
}
}
else {
http_response_code(400);
}
break;
}
}