hdwpos/store/view/view-store.js

279 lines
14 KiB
JavaScript

"use strict";
// Class definition
var KTStoreView = function () {
// Shared variables
var storeId
var ipAddress
var datatableItem;
var tableItem = document.getElementById('kt_table_item');
var datatableItemOption
var tableItemOptions = document.getElementById('kt_table_item_options');
var datatableItemPrices;
var tableItemPrices = document.getElementById('kt_table_item_prices');
var datatableItemStock;
var tableItemStock = document.getElementById('kt_table_item_stock');
// Init store view page
var initStoreView = () => {
// call StoreDetails API
fetch('http://localhost:8080/hdpos/api/stores/getStoreDetails?dbHost='+ipAddress+'&storeId='+storeId)
.then(response => response.json())
.then(data => {
document.getElementById("hb_storeTitle").innerText = data.store.id_structure + " - " + data.store.nom;
document.getElementById("hb_storeIpAddress").innerText = ipAddress;
document.getElementById("hb_storePhoneNumber").innerText = data.store.telephone;
// Display backOffice transaction informations
var statusTransaction = document.getElementById("hb_backOfficeTransactionOk");
debugger;
if (data.transaction.backOfficeTransactionOk==true) {
statusTransaction.classList.add("bg-success");
document.getElementById("hb_backOfficeBusinessDate").classList.add("text-gray-800");
} else {
statusTransaction.classList.add("bg-danger");
document.getElementById("hb_backOfficeBusinessDate").classList.add("text-danger");
}
document.getElementById("hb_backOfficeTransactions").innerText = data.transaction.backOfficeTransactions;
document.getElementById("hb_minBackOfficeTransactionDate").innerText = data.transaction.minBackOfficeTransactionDate;
document.getElementById("hb_maxBackOfficeTransactionDate").innerText = data.transaction.maxBackOfficeTransactionDate;
document.getElementById("hb_backOfficeBusinessDate").innerText = data.transaction.backOfficeBusinessDate;
// Display replication informations
var statusReplication = document.getElementById("hb_pendingReplicationOk");
if (data.replication.pendingReplicationOk) {
statusReplication.classList.add("bg-success");
} else {
statusReplication.classList.add("bg-danger");
}
document.getElementById("hb_pendingReplications").innerText = data.replication.pendingReplications;
document.getElementById("hb_minPendingReplicationDate").innerText = data.replication.minPendingReplicationDate;
document.getElementById("hb_maxPendingReplicationDate").innerText = data.replication.maxPendingReplicationDate;
})
.catch(error => {
console.error('Error retrieving store details:', error);
});
}
// Submit form handler
const handleItemSubmit = () => {
// Define variables
let validator;
// Get elements
const form = document.getElementById('kt_store_item_form');
const submitButton = document.getElementById('kt_store_item_submit');
// Init form validation rules. For more info check the FormValidation plugin's official documentation:https://formvalidation.io/
validator = FormValidation.formValidation(
form,
{
fields: {
'itemId': {
validators: {
notEmpty: {
message: 'Item ID is required'
}
}
}
},
plugins: {
trigger: new FormValidation.plugins.Trigger(),
bootstrap: new FormValidation.plugins.Bootstrap5({
rowSelector: '.fv-row',
eleInvalidClass: '',
eleValidClass: ''
})
}
}
);
// Handle submit item search button
submitButton.addEventListener('click', e => {
e.preventDefault();
// Validate form before submit
if (validator) {
validator.validate().then(function (status) {
if (status == 'Valid') {
submitButton.setAttribute('data-kt-indicator', 'on');
// Disable submit button while loading
submitButton.disabled = true;
var itemId = document.querySelector('input[name="itemId"]').value;
// call StoreDetails API
fetch('http://localhost:8080/hdpos/api/items/getItemDetails?dbHost='+ipAddress+'&itemId='+itemId)
.then(response => response.json())
.then(data => {
if ( $.fn.dataTable.isDataTable('#kt_table_item') ) {
$('#kt_table_item').DataTable().destroy();
}
datatableItem = $(tableItem).DataTable({
"info": true,
"bStateSave": true, // cookie
'order': [],
"pageLength": 10,
"lengthChange": false,
columns: [
{ data: 'itemId', name: "ITEM_ID" },
{ data: 'itemLevelCode', name: "LEVEL" },
{ data: 'parentItemId', name: "PARENT" },
{ data: 'itemTypeCode', name: "TYPE" },
{ data: null, // Using null to create a custom column
render: function(data, type, full, meta) {
return full.createDate + '<br>' + full.createUserId;
}
},
{ data: null, // Using null to create a custom column
render: function(data, type, full, meta) {
return full.updateDate + '<br>' + full.updateUserId;
}
}
],
"data": data.items
});
if ( $.fn.dataTable.isDataTable('#kt_table_item_options') ) {
$('#kt_table_item_options').DataTable().destroy();
}
datatableItemOption = $(tableItemOptions).DataTable({
"info": true,
"bStateSave": true, // cookie
'order': [],
"pageLength": 10,
"lengthChange": false,
columns: [
{ data: 'itemId', name: "ITEM_ID" },
{ data: null,
render: function(data, type, full, meta) {
return full.levelCode + ':' + full.levelValue;
}
},
{ data: 'itemAvailabilityCode', name: "VENDABLE" },
{ data: 'taxGroupId', name: "TAXE" },
{ data: 'vendor', name: "VENDOR" },
{ data: 'seasonCode', name: "SEASON" },
{ data: null, // Using null to create a custom column
render: function(data, type, full, meta) {
return full.createDate + '<br>' + full.createUserId;
}
},
{ data: null, // Using null to create a custom column
render: function(data, type, full, meta) {
return full.updateDate + '<br>' + full.updateUserId;
}
}
],
"data": data.itemOptions
});
if ( $.fn.dataTable.isDataTable('#kt_table_item_prices') ) {
$('#kt_table_item_prices').DataTable().destroy();
}
datatableItemPrices = $(tableItemPrices).DataTable({
"info": true,
"bStateSave": true, // cookie
'order': [],
"pageLength": 10,
"lengthChange": false,
columns: [
{ data: 'itemId', name: "ITEM_ID" },
{ data: null,
render: function(data, type, full, meta) {
return full.levelCode + ':' + full.levelValue;
}
},
{ data: 'itmPricePropertyCode', name: "TYPE" },
{ data: null, // Using null to create a custom column
render: function(data, type, full, meta) {
return full.effectiveDate + '<br>' + full.expirationDate;
}
},
{ data: 'price', name: "PRICE" },
{ data: 'externalId', name: "EXTERNAL_ID" },
{ data: null, // Using null to create a custom column
render: function(data, type, full, meta) {
return full.createDate + '<br>' + full.createUserId;
}
},
{ data: null, // Using null to create a custom column
render: function(data, type, full, meta) {
return full.updateDate + '<br>' + full.updateUserId;
}
}
],
"data": data.itemPrices
});
if ( $.fn.dataTable.isDataTable('#kt_table_item_stock') ) {
$('#kt_table_item_stock').DataTable().destroy();
}
datatableItemStock = $(tableItemStock).DataTable({
"info": true,
"bStateSave": true, // cookie
'order': [],
"pageLength": 10,
"lengthChange": false,
columns: [
{ data: 'itemId', name: "ITEM_ID" },
{ data: 'invLocationId', name: "LOCATION" },
{ data: 'bucketId', name: "BUCKET" },
{ data: 'itemId', name: "ITEM_ID" },
{ data: 'unitCount', name: "STOCK" },
{ data: null, // Using null to create a custom column
render: function(data, type, full, meta) {
return full.createDate + '<br>' + full.createUserId;
}
},
{ data: null, // Using null to create a custom column
render: function(data, type, full, meta) {
return full.updateDate + '<br>' + full.updateUserId;
}
}
],
"data": data.itemStock
});
// Enable submit button after loading
submitButton.removeAttribute('data-kt-indicator');
submitButton.disabled = false;
})
.catch(error => {
console.error('Error retrieving item details:', error);
// Enable submit button after loading
submitButton.removeAttribute('data-kt-indicator');
submitButton.disabled = false;
});
}
});
}
})
}
return {
// Public functions
init: function () {
// Parse the URL parameter
storeId = KTUtil.getURLParam("storeId");
ipAddress = KTUtil.getURLParam("ip");
// Handle forms
initStoreView();
handleItemSubmit();
}
};
}();
// On document ready
KTUtil.onDOMContentLoaded(function () {
KTStoreView.init();
});