Firstly, I need to validate the serial numbers put in by users to make sure it is correct with what is already in database. However, I noticed even if the wrong serial number is inputed, the page reloads (shows the error message) and still fills in the wrong serial number in the field cf_954. So whether it is wrong or right, the field still gets populated.
Secondly, i noticed that the alert script only shows in mobile not on desktop browser.
<?php
/***
This code was rewritten by Peter Iteka
The Original code is placed in Root dir as OLD CODES
*****/
// Enable error reporting for debugging purposes
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
function Jobs_DeliverBinDelivered_field_Workflow($entity) {
global $current_user, $adb, $log;
// Prevent any output before the JSON response
if (ob_get_length()) ob_end_clean(); // Ensure any prior output buffer is cleaned.
// Include necessary vtiger utility files
include_once 'include/utils/CommonUtils.php';
include_once 'include/database/PearDatabase.php';
// Extract the entity ID
$id = explode("x", $entity->get('id'));
if (count($id) < 2) {
error_log("Invalid entity ID format.");
return;
}
$potid = $id[1];
// Ensure required fields are set
if (!isset($entity->data['cf_accounts_id']) || !isset($entity->data['cf_954'])) {
error_log("Required fields are missing in the entity data.");
return;
}
// Retrieve field values
$acc_id = explode("x", $entity->data['cf_accounts_id']);
if (count($acc_id) < 2) {
error_log("Invalid cf_accounts_id format.");
return;
}
$accounts_id = $acc_id[1];
$serialnumber = $entity->data['cf_954'];
// Define an array of valid product IDs
$valid_products = [
6570, 6569, 6571, 6574, 6575, 6576, 6577, 6578, 6580, 6588,
6589, 6591, 6593, 6598, 6603, 6605, 6610, 6611, 6628, 6634,
6637, 7065, 7479, 11047, 11061, 11196, 12518, 20067, 20210,
30015, 51241, 60124, 60546, 63629, 72512, 72513, 72596, 77932,
77944, 78159, 79504, 80215, 107739, 132729, 144886, 148546,
149185, 152960, 156049, 156354, 160736, 160737
];
// Prepare and execute the SQL query to find assets with the given serial number and a valid product
$sql = "SELECT * FROM `vtiger_assets` WHERE `serialnumber` = ? AND `product` IN (" . implode(',', $valid_products) . ")";
$result = $adb->pquery($sql, array($serialnumber));
if ($result === false) {
error_log("Database query failed: " . $adb->database->ErrorMsg());
return;
}
$row_find_count = $adb->num_rows($result);
function phpAlert($msg) {
echo '<script type="text/javascript">
alert("' . $msg . '");
window.location.reload();
</script>';
}
// If no assets are found, reject the serial number and return a JSON error message
if ($row_find_count == 0) {
// Log error for internal purposes
error_log("Invalid serial number: $serialnumber or product mismatch. Contact the office for assistance.");
// Show an alert to the user
phpAlert("Invalid serial number or Bin number mismatch. Input the correct one or call the office. Thank you.");
// Ensure that the response is sent and the connection is closed
die();
flush();
exit(); // Prevent further execution
}
// If assets are found, proceed with updating them
$ACC_ID = array();
for ($i = 0; $i < $row_find_count; $i++) {
$ACC_ID[] = $adb->query_result($result, $i, 'assetsid');
}
// Update each found asset record with the new account ID
foreach ($ACC_ID as $row_id) {
$recordModel = Vtiger_Record_Model::getInstanceById($row_id, 'Assets');
if ($recordModel) {
$recordModel->set('account', $accounts_id);
$recordModel->set('mode', 'edit');
$recordModel->save();
} else {
error_log("Failed to load record model for assetsid: " . $row_id);
}
}
}
?>
Used this function to stop the code from running
if ($row_find_count == 0) {
// Log error for internal purposes
error_log("Invalid serial number: $serialnumber or product mismatch. Contact the office for assistance.");
// Show an alert to the user
phpAlert("Invalid serial number or Bin number mismatch. Input the correct one or call the office. Thank you.");
// Ensure that the response is sent and the connection is closed
die();
flush();
exit(); // Prevent further execution
}