Files
assetmgt/equipments_mass_update.php

327 lines
13 KiB
PHP

<?php
defined(page_security_key) or exit;
if (debug && debug_id == $_SESSION['id']){
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
}
include_once './assets/functions.php';
include_once './settings/settings_redirector.php';
$page = 'equipments_mass_update';
//Check if allowed
if (isAllowed($page,$_SESSION['profile'],$_SESSION['permission'],'R') === 0){
header('location: index.php');
exit;
}
//PAGE Security
$update_allowed = isAllowed($page ,$_SESSION['profile'],$_SESSION['permission'],'U');
$delete_allowed = isAllowed($page ,$_SESSION['profile'],$_SESSION['permission'],'D');
$create_allowed = isAllowed($page ,$_SESSION['profile'],$_SESSION['permission'],'C');
//GET Details from URL
$GET_VALUES = urlGETdetails($_GET) ?? '';
$output_excel_display ='';
$total_summary = '';
if ($update_allowed === 1){
if (isset($_POST['excel_data'])) {
//GET ALL POST DATA
$excel_data = explode("\n",$_POST['excel_data']) ?? '';
$trimmedArray = array_map('trim', $excel_data);
$excel_data_1 = array_filter($trimmedArray, 'strlen');
//Create clean array
$output_excel = [];
//Totals variables
$total_rowID = 0;
$total_not_found = 0;
$output_excel_display .= '<table>
<thead style="text-align: left;">
<tr>
<th>'.$equipment_label2.'</th>
<th>'.$product_code.'</th>
<th>'.$product_name.'</th>
<th>'.$product_status.'</th>
</tr>
</thead>
<tbody>';
foreach ($excel_data_1 as $key => $val){
//GET ROW ID
$api_url = '/v1/application/serialnumber='.$val.'/get_rowID';
$responses = ioServer($api_url,'');
//Decode Payload
if (!empty($responses)){$responses = decode_payload($responses);}else{$responses = null;}
//IF rowID is found add to array
if (isset ($responses->rowID)){
$output_excel[$val]['rowID'] = $responses->rowID;
$output_excel[$val]['serialnumber'] = $val;
$output_excel[$val]['productcode'] = $responses->productcode;
$output_excel[$val]['productname'] = ${$responses->productname} ?? $responses->productname;
$output_excel[$val]['order_ref'] = $_POST['order_ref'];
$output_excel[$val]['status'] = $_POST['status'];
$output_excel[$val]['salesid'] = $_POST['salesid'];
$output_excel[$val]['soldto'] = $_POST['soldto'];
if (!empty($_POST['shipto']) || $_POST['shipto'] !=''){
$output_excel[$val]['shipto'] = $_POST['shipto'];
}
if (!empty($_POST['location']) || $_POST['location'] !=''){
$output_excel[$val]['location'] = $_POST['location'];
}
//Count total_rowID
$total_rowID++;
}
else
{
$output_excel_display .= '<tr><td>'.$val.'</td><td></td><td></td><td>'.$mass_update_not_found.'</td></tr>';
//Count total_not_found
$total_not_found++;
}
}
// Update all items of array
foreach ($output_excel as $data_to_update){
$data = json_encode($data_to_update, JSON_UNESCAPED_UNICODE);
//Secure data
$payload = generate_payload($data);
//API call
$responses = ioServer('/v1/equipments', $payload);
if ($responses === 'NOK'){
}
else {
$output_excel_display .= '<tr><td>'.$data_to_update['serialnumber'].'</td><td>'.$data_to_update['productcode'].'</td><td>'.(${$data_to_update['productname']} ?? $data_to_update['productname']).'</td><td>'.$mass_update_correct.'</td></tr>';
}
}
$output_excel_display .= '</tbody></table>';
//Totals
$total_input = $total_rowID + $total_not_found;
$total_summary = '('.$total_rowID.$general_page_of.$total_input.')';
//CHECK if shipto exists if not empty. Use shipto when soldto not available
$shipto = (isset($_POST['shipto']) ? $_POST['shipto'] : '');
$customer = (isset($_POST['soldto']) ? $_POST['soldto'] : $shipto);
$order_header = '<table style="margin-bottom: 15px;width:50%;">
<thead>
<tr><th style="text-align: left;">'.$tab1.'</th></td>
<tr><th></th></td>
</thead>
<tbody>
<tr><td>'.$mass_update_order_ref.'</td><td>'.($_POST['order_ref'] ?? '').'</td></tr>
<tr><td>'.$mass_update_order_total.'</td><td>'.$total_rowID.'</td></tr>
<tr><td>'.$mass_update_customer.'</td><td>'.$customer.'</td></tr>
</tbody>
</table>';
}
}
// Create success modal if update was completed
$success_modal = '';
if ($update_allowed === 1 && isset($_POST['excel_data']) && $output_excel_display != ''){
$success_modal = '
<div id="successModal" class="modal" style="display: flex; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 1000; align-items: center; justify-content: center;">
<div class="modal-content" style="background: white; border-radius: 12px; max-width: 500px; margin: 20px; box-shadow: 0 10px 40px rgba(0,0,0,0.3); position: relative;">
<span class="close" onclick="closeSuccessModal()" style="position: absolute; top: 15px; right: 20px; font-size: 28px; font-weight: bold; color: #999; cursor: pointer;">&times;</span>
<div style="text-align: center; padding: 40px 30px;">
<i class="fa-solid fa-check-circle" style="font-size: 64px; color: #28a745; margin-bottom: 20px;"></i>
<h2 style="color: #155724; margin-bottom: 15px;">Update Completed!</h2>
<p style="margin-bottom: 10px; color: #333;">Successfully updated '.$total_rowID.' items</p>
<p style="font-size: 12px; color: #666; margin-bottom: 25px;">Order: '.htmlspecialchars($_POST['order_ref']).'</p>
<div style="display: flex; gap: 10px; justify-content: center;">
<button onclick="printDiv(\'excel_table\')" class="btn alt" style="padding: 12px 30px;"><i class="fa-solid fa-print"></i></button>
<button onclick="closeSuccessModal()" class="btn" style="padding: 12px 30px;">✓</button>
</div>
</div>
</div>
</div>';
}
// Handle success messages
if (isset($_GET['success_msg'])) {
if ($_GET['success_msg'] == 1) {
$success_msg = $message_eq_1;
}
if ($_GET['success_msg'] == 2) {
$success_msg = $message_eq_2;
}
if ($_GET['success_msg'] == 3) {
$success_msg = $message_eq_3;
}
}
template_header('Mass update', 'equipment_mass_update', 'view');
$view = '
<div class="content-title responsive-flex-wrap responsive-pad-bot-3">
<h2 class="responsive-width-100">'.$mass_update_h2.'</h2>
<a href="index.php?page=equipments" class="btn alt mar-right-2">←</a>
';
if ($update_allowed === 1){
$print_btn_class = ($output_excel_display != '') ? 'btn' : 'btn alt';
$view .= '<button class="'.$print_btn_class.' mar-right-2" onclick="printDiv(\'excel_table\')"><i class="fa-solid fa-print"></i></button>';
$view .= '<input type="submit" form="mass_update_form" id="mass_update_submit" value="💾+" onclick="return confirm(\'".$mass_update_confirm_message."\')" class="btn">';
}
$view .= '</div>';
if (isset($success_msg)){
$view .= ' <div class="msg success">
<i class="fas fa-check-circle"></i>
<p>'.$success_msg.'</p>
<i class="fas fa-times"></i>
</div>';
}
$view .= '<form action="" method="post" id="mass_update_form">
<div class="content-block-wrapper">
';
//BUID UP DROPDOWNS
$partner = json_decode($_SESSION['partnerhierarchy'],true);
$salesid_dropdown = listPartner('salesid',$_SESSION['permission'],$partner['salesid'],'yes');
$soldto_dropdown = listPartner('soldto',$_SESSION['permission'],'','yes');
$shipto_dropdown = listPartner('shipto',$_SESSION['permission'],'','');
$location_dropdown = listPartner('location',$_SESSION['permission'],'','');
$view .='<div class="content-block">
<div class="block-header">
<i class="fa-solid fa-user fa-sm"></i>
</div>
<div class="form responsive-width-100" style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px;">';
// SHOW SALESID and SOLDTO ONLY TO ADMIN
if ($_SESSION['permission'] == 3 || $_SESSION['permission'] == 4){
$view .='<div>
<label for="salesid">'.$general_salesid.'</label>
'.$salesid_dropdown.'
</div>
<div>
<label for="soldto">'.$general_soldto.'</label>
'.$soldto_dropdown.'
</div>';
}
$view .=' <div>
<label for="shipto">'.$general_shipto.'</label>
'.$shipto_dropdown.'
</div>
<div>
<label for="location">'.$general_location.'</label>
'.$location_dropdown.'
</div>
<div>
<label for="order_ref">'.$equipment_label11.'</label>
<input id="order_ref" type="text" name="order_ref" required>
</div>
<div>
<label for="status">'.$equipment_label3.'</label>
<select id="status" name="status" required>';
if ($_SESSION['permission'] == 3 || $_SESSION['permission'] == 4){
$view .= '<option value="0">'.$status0_text .'</option>
<option value="1">'.$status1_text .'</option>
<option value="2">'.$status2_text .'</option>';
}
$view .= '
<option value="3" selected>'.$status3_text .'</option>
<option value="4">'.$status4_text .'</option>
<option value="5">'.$status5_text .'</option>
</select>
</div>
</div>
</div>
<div class="content-block">
<div class="block-header">
<i class="fa-solid fa-circle-info"></i>
</div>
<div class="form responsive-width-100" style="margin-top: 20px;">
<label for="excel_data">'.$paste_excel_h3.' <span id="count" style="font-weight: normal; color: #666;"></span></label>
<textarea id="excel_data" name="excel_data" onkeydown="countLines()" style="width:100%;height:200px; font-family: monospace;" placeholder="'.$paste_excel_1.'&#10;'.$paste_excel_2.'"></textarea>
<input type="hidden" name="section" value="">
</div>
</div>';
$view .= '</form>';
$view .= '</div></div>';
if ($update_allowed === 1){
$view .= '<div class="tabs">
<a href="#"><i class="fa-solid fa-bars fa-sm"></i>'.$tab3.' '.$total_summary.'</a>
</div>
<div class="content-block tab-content" style="display: none;">
<div class="table order-table" id="excel_table">
'.($order_header ?? '' ).'
'.$output_excel_display.'
</div>
</div>
';
}
//OUTPUT
echo $view;
// Output success modal if exists
echo $success_modal;
echo '
<script>
function closeSuccessModal() {
document.getElementById("successModal").style.display = "none";
}
// Ensure log section starts closed
document.addEventListener("DOMContentLoaded", function() {
const tabs = document.querySelectorAll(".tabs a");
const tabContents = document.querySelectorAll(".tab-content");
tabs.forEach(tab => tab.classList.remove("active"));
tabContents.forEach(content => {
content.classList.remove("active");
content.style.display = "none";
});
});
function countLines(){
let button = document.getElementById("mass_update_submit");
var text = document.getElementById("excel_data").value.trim();
var lines = text.split(/\r|\r\n|\n/);
var count = lines.length;
document.getElementById("count").innerHTML = lines.length;
function checkIfDuplicateExists(arr) {
return new Set(arr).size !== arr.length
}
if (checkIfDuplicateExists(lines) == true){
var lines = [...new Set(lines)];
var count = lines.length;
document.getElementById("count").innerHTML = lines.length;
button.disabled = true;
button.style.background="#e3e4e6";
} else {
button.disabled = false;
button.style.background="#4a79b4";
}
}
</script>
';
template_footer();
?>