var pagination_current = 1; var search_string = ''; var defaqult_limit = 2; var pagination_end = 1; var item; async function changeSite(site){ console.log(site) const response = await fetch(`/changeSite`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ site: site, }), }); data = await response.json(); transaction_status = "success" if (data.error){ transaction_status = "danger" } UIkit.notification({ message: data.message, status: transaction_status, pos: 'top-right', timeout: 5000 }); location.reload(true) } async function replenishItemsTable(items) { let itemsTableBody = document.getElementById("itemsTableBody") itemsTableBody.innerHTML = "" for(let i = 0; i < items.length; i++){ let tableRow = document.createElement('tr') let nameCell = document.createElement('td') nameCell.innerHTML = items[i].item_name let opCell = document.createElement('td') let selectButton = document.createElement('button') selectButton.setAttribute('class', 'uk-button uk-button-small uk-button-primary') selectButton.innerHTML = "Select" selectButton.onclick = async function(){ console.log('clicked') await selectItem(items[i].item_uuid) } opCell.append(selectButton) tableRow.append(nameCell, opCell) itemsTableBody.append(tableRow) } } async function populateForm() { if (item){ document.getElementById('database_uuid').value = item.item_uuid document.getElementById('name').value = item.item_name document.getElementById('transaction_cost').value = parseFloat(item.item_cost) await selectLocation( item.primary_zone.zone_uuid, item.primary_location.location_uuid, item.primary_zone.zone_name, item.primary_location.location_name ) document.getElementById('QOH').value = item.item_quantity_on_hand document.getElementById('UOM').value = item.unit_fullname await replenishItemLocationsTable(item.item_locations) } } async function selectItem(item_uuid) { console.log(item_uuid) UIkit.modal(document.getElementById("itemsModal")).hide(); item = await getItem(item_uuid) console.log(item) await populateForm() } var transaction_zone_uuid = "" var transaction_item_location_uuid = "" async function selectLocation(zone_uuid, location_uuid, zone_name, location_name) { document.getElementById('zone').value = zone_name document.getElementById('location').value = location_name transaction_zone_uuid = zone_uuid transaction_item_location_uuid = location_uuid } async function openItemsModal(elementID){ UIkit.modal(document.getElementById("itemsModal")).show(); pagination_current = 1 search_string = '' let items = await getItems() await replenishItemsTable(items) await updatePaginationElement(elementID) setFormButtonsEnabled(true) } async function setFormButtonsEnabled(state) { let item_location_button = document.getElementById("itemLocations") if(state){ item_location_button.classList.remove("uk-disabled") } else { item_location_button.classList.add("uk-disabled") } } async function setTransactionTypeAdjustments() { let trans_type = document.getElementById('trans_type').value if(trans_type=="Adjust Out"){ document.getElementById('transaction_cost').classList.add('uk-disabled') } if(trans_type=="Adjust In"){ document.getElementById('transaction_cost').classList.remove('uk-disabled') } } async function replenishItemLocationsTable(locations) { let itemLocationTableBody = document.getElementById('itemLocationTableBody') itemLocationTableBody.innerHTML = "" for(let i = 0; i < locations.length; i++){ console.log(locations[i]) let tableRow = document.createElement('tr') let loca = locations[i].location_shortname.split('@') let zoneCell = document.createElement('td') zoneCell.innerHTML = loca[0] let locationCell = document.createElement('td') locationCell.innerHTML = loca[1] let qohCell = document.createElement('td') qohCell.innerHTML = parseFloat(locations[i].item_quantity_on_hand) tableRow.append(zoneCell, locationCell, qohCell) tableRow.onclick = async function(){ await selectLocation( locations[i].zone_uuid, locations[i].location_uuid, loca[0], loca[1] ) } itemLocationTableBody.append(tableRow) } } let locations_limit = 10; async function getItemLocations() { console.log("getting Locations") const url = new URL('/items/getItemLocations', window.location.origin); url.searchParams.append('page', pagination_current); url.searchParams.append('limit', locations_limit); url.searchParams.append('id', item.id); const response = await fetch(url); data = await response.json(); pagination_end = data.end let locations = data.locations; console.log(locations) return locations; } let items_limit = 50; async function getItems() { console.log("getting items") const url = new URL('/items/api/getModalItems', window.location.origin); url.searchParams.append('page', pagination_current); url.searchParams.append('limit', items_limit); url.searchParams.append('search_string', search_string) const response = await fetch(url); data = await response.json(); pagination_end = data.end let items = data.items; return items; } async function getItem(item_uuid) { console.log(`selected item: ${item_uuid}`) const url = new URL('/items/api/getTransactionItem', window.location.origin); url.searchParams.append('item_uuid', item_uuid); const response = await fetch(url); data = await response.json(); item = data.item; return item; } async function validateTransaction() { let database_uuid = document.getElementById("database_uuid") let transaction_type = document.getElementById("trans_type") let transaction_zone = document.getElementById("zone") let transaction_location = document.getElementById("location") let transaction_quantity = document.getElementById("transaction_quantity") let transaction_cost = document.getElementById("transaction_cost") let error_count = 0 if(database_uuid.value === ""){ error_count = error_count + 1 database_uuid.classList.add("uk-form-danger") } else { database_uuid.classList.remove("uk-form-danger") } if(transaction_type.value === "0"){ error_count = error_count + 1 transaction_type.classList.add("uk-form-danger") } else { transaction_type.classList.remove("uk-form-danger") } if (transaction_zone.value === ""){ error_count = error_count + 1 transaction_zone.classList.add("uk-form-danger") } else { transaction_zone.classList.remove("uk-form-danger") } if (transaction_location.value === ""){ error_count = error_count + 1 transaction_location.classList.add("uk-form-danger") } else { transaction_location.classList.remove("uk-form-danger") } let transaction_quantity_int = parseFloat(transaction_quantity.value) if (transaction_quantity_int === 0.00 || transaction_quantity_int < 0.00){ error_count = error_count + 1 transaction_quantity.classList.add("uk-form-danger") } else { transaction_quantity.classList.remove("uk-form-danger") } let transaction_cost_int = parseFloat(transaction_cost.value) if (transaction_cost_int == 0.00 && transaction_type.value == "Adjust In"){ error_count = error_count + 1 transaction_cost.classList.add("uk-form-danger") } else { transaction_cost.classList.remove("uk-form-danger") } if(error_count > 0){ return false } return true } async function submitTransaction() { let validated = await validateTransaction() if (validated){ let cost = parseFloat(document.getElementById('transaction_cost').value.replace(/[^0-9.-]+/g, "")); const response = await fetch(`/items/postTransaction`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ item_uuid: item.item_uuid, item_name: item.item_name, transaction_type: document.getElementById('trans_type').value, transaction_quantity: parseFloat(document.getElementById('transaction_quantity').value), transaction_description: document.getElementById('transaction_description').value, transaction_cost: cost, transaction_vendor: null, trans_action_expires: null, location_uuid: transaction_item_location_uuid }), }); data = await response.json(); transaction_status = "success" if (data.error){ transaction_status = "danger" } UIkit.notification({ message: data.message, status: transaction_status, pos: 'top-right', timeout: 5000 }); item = await getItem(item.item_uuid) await populateForm() document.getElementById('transaction_quantity').value = '0.00' } else { UIkit.notification({ message: 'Please verify your transaction receipt.', status: 'warning', pos: 'top-right', timeout: 5000 }) } } async function searchTable(event, logis, elementID) { if(event.key==='Enter' && logis==='items'){ search_string = event.srcElement.value let items = await getItems() await replenishItemsTable(items) } await updatePaginationElement(elementID) } async function setPage(pageNumber, elementID){ pagination_current = pageNumber; if(elementID=="itemsPage"){ let items = await getItems() await replenishItemsTable(items) } await updatePaginationElement(elementID) } async function updatePaginationElement(elementID) { let paginationElement = document.getElementById(elementID); paginationElement.innerHTML = ""; // previous let previousElement = document.createElement('li') if(pagination_current<=1){ previousElement.innerHTML = ``; previousElement.classList.add('uk-disabled'); }else { previousElement.innerHTML = ``; } paginationElement.append(previousElement) //first let firstElement = document.createElement('li') if(pagination_current<=1){ firstElement.innerHTML = `1`; firstElement.classList.add('uk-disabled'); }else { firstElement.innerHTML = `1`; } paginationElement.append(firstElement) // ... if(pagination_current-2>1){ let firstDotElement = document.createElement('li') firstDotElement.classList.add('uk-disabled') firstDotElement.innerHTML = `…`; paginationElement.append(firstDotElement) } // last if(pagination_current-2>0){ let lastElement = document.createElement('li') lastElement.innerHTML = `${pagination_current-1}` paginationElement.append(lastElement) } // current if(pagination_current!=1 && pagination_current != pagination_end){ let currentElement = document.createElement('li') currentElement.innerHTML = `