pantry-track/static/handlers/receiptHandler.js
2025-04-13 08:27:19 -05:00

508 lines
17 KiB
JavaScript

var pagination_current = 1;
var pagination_end = 10
document.addEventListener('DOMContentLoaded', async function() {
await refreshReceipt()
})
async function refreshReceipt() {
let receipt = await getReceipt(receipt_id)
console.log(receipt)
await replenishFields(receipt)
await replenishLinesTable(receipt.receipt_items)
await replenishFilesCards(receipt.files)
}
async function replenishFields(receipt) {
if (receipt){
document.getElementById('title').innerHTML = receipt.receipt_id
document.getElementById('crumbID').innerHTML = receipt.receipt_id
document.getElementById('receipt_id').innerHTML = receipt.receipt_id
document.getElementById('database_id').value = receipt.id
document.getElementById('date_submitted').value = receipt.date_submitted
document.getElementById('submitted_by').value = receipt.submitted_by
document.getElementById('receipt_status').value = receipt.receipt_status
document.getElementById('vendor_id').value = receipt.vendor.id
document.getElementById('vendor_name').value = receipt.vendor.vendor_name
document.getElementById('vendor_address').value = receipt.vendor.vendor_address
document.getElementById('vendor_phone').value = receipt.vendor.phone_number
}
}
async function checkAPI(line_id, barcode) {
console.log(barcode)
const response = await fetch(`/receipts/checkAPI`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
line_id: line_id,
barcode: barcode
}),
});
data = await response.json()
message_type = "primary"
if(data.error){
message_type = "danger"
}
UIkit.notification({
message: data.message,
status: message_type,
pos: 'top-right',
timeout: 5000
});
await refreshReceipt()
}
async function replenishLinesTable(receipt_items) {
let linesTableBody = document.getElementById("linesTableBody")
linesTableBody.innerHTML = ""
let deniedTableBody = document.getElementById("deniedTableBody")
deniedTableBody.innerHTML = ""
let resolvedTableBody = document.getElementById("resolvedTableBody")
resolvedTableBody.innerHTML = ""
for(let i = 0; i < receipt_items.length; i++){
let tableRow = document.createElement('tr')
let statusCell = document.createElement('td')
statusCell.innerHTML = receipt_items[i].status
let barcodeCell = document.createElement('td')
barcodeCell.innerHTML = receipt_items[i].barcode
let typeCell = document.createElement('td')
let label_color = 'grey'
if(receipt_items[i].type == 'sku'){
label_color = 'green'
}
if(receipt_items[i].type == 'new sku'){
label_color = 'orange'
}
if(receipt_items[i].type == 'api'){
label_color = 'purple'
}
typeCell.innerHTML = `<span style="background-color: ${label_color};" class="uk-label">${receipt_items[i].type}</span>`
let nameCell = document.createElement('td')
nameCell.innerHTML = receipt_items[i].name
let operationsCell = document.createElement('td')
let apiOp = document.createElement('a')
if(receipt_items[i].type === "new sku"){
apiOp.style = "margin-right: 5px;"
apiOp.setAttribute('class', 'uk-button uk-button-small uk-button-default')
apiOp.setAttribute('uk-icon', 'icon: pull')
apiOp.onclick = async function () {
await checkAPI(receipt_items[i].id, receipt_items[i].barcode)
}
}
let editOp = document.createElement('a')
editOp.style = "margin-right: 5px;"
editOp.setAttribute('class', 'uk-button uk-button-small uk-button-default')
editOp.setAttribute('uk-icon', 'icon: pencil')
editOp.onclick = async function () {
await openLineEditModal(receipt_items[i])
}
let resolveOp = document.createElement('a')
resolveOp.style = "margin-right: 5px;"
resolveOp.setAttribute('class', 'uk-button uk-button-small uk-button-default')
resolveOp.setAttribute('uk-icon', 'icon: check')
resolveOp.onclick = async function(){
await resolveLine(receipt_items[i].id)
}
let denyOp = document.createElement('a')
denyOp.style = "margin-right: 5px;"
denyOp.setAttribute('class', 'uk-button uk-button-small uk-button-default')
denyOp.setAttribute('uk-icon', 'icon: close-circle')
denyOp.onclick = async function() {
await denyLine(receipt_items[i].id)
}
let deleteOp = document.createElement('a')
deleteOp.setAttribute('class', 'uk-button uk-button-small uk-button-default')
deleteOp.setAttribute('uk-icon', 'icon: trash')
deleteOp.onclick = async function(){
await deleteLine(receipt_items[i].id)
}
if (receipt_items[i].type === "new sku"){
operationsCell.append(apiOp)
}
operationsCell.append(editOp, resolveOp, denyOp, deleteOp)
operationsCell.classList.add("uk-flex")
operationsCell.classList.add("uk-flex-right")
if(receipt_items[i].status === "Denied" || receipt_items[i].status === "Resolved"){
tableRow.classList.add('uk-disabled')
}
tableRow.append(statusCell, barcodeCell,typeCell, nameCell, operationsCell)
if(receipt_items[i].status === "Denied"){
deniedTableBody.append(tableRow)
} else if(receipt_items[i].status === "Resolved") {
resolvedTableBody.append(tableRow)
} else {
linesTableBody.append(tableRow)
}
}
}
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 idCell = document.createElement('td')
idCell.innerHTML = items[i].id
let barcodeCell = document.createElement('td')
barcodeCell.innerHTML = items[i].barcode
let nameCell = document.createElement('td')
nameCell.innerHTML = items[i].item_name
tableRow.onclick = async function() {
await addSKULine(items[i].id)
}
tableRow.append(idCell, barcodeCell, nameCell)
itemsTableBody.append(tableRow)
}
}
async function replenishFilesCards(files) {
let fileCards = document.getElementById('fileCards')
fileCards.innerHTML = ""
for(let key in files){
let parent_div = document.createElement('div')
let card_div = document.createElement('div')
card_div.setAttribute('class', 'uk-card uk-card-default uk-card-small')
console.log(files[key])
let baseStaticUrl = '/static/files/receipts/previews/';
let imgSrc = `${baseStaticUrl}${files[key].preview_image}`;
let media_div = document.createElement('div')
media_div.setAttribute('class', 'uk-card-media-top')
media_div.innerHTML = `<img data-src="${imgSrc}" width="600" height="400" alt="" uk-img />`;
let body_div = document.createElement('div')
body_div.setAttribute('class', 'uk-card-body')
let file_size = (files[key].file_size / (1024 * 1024)).toFixed(2)
body_div.innerHTML = `
<h3 class="uk-card-title">${key}</h3>
<p>Size: ${file_size} MB</p>
<p>uploaded_by: ${files[key].uploaded_by}</p>
<p>Type: ${files[key].file_type}</p>
<button onclick="viewFile('${key}')" class="uk-button uk-primary">view</button>
`
card_div.append(media_div, body_div)
parent_div.append(card_div)
fileCards.append(parent_div)
}
}
async function viewFile(source) {
let iframeModalBody = document.getElementById('iframeModalBody')
iframeModalBody.innerHTML = ""
document.getElementById('filenameiframemodal').innerHTML = source
let iframe = document.createElement('iframe')
iframe.src = `/receipt/getFile/${source}`
iframe.width = "100%"
iframe.style.height = "100%"
iframeModalBody.append(iframe)
UIkit.modal(document.getElementById("iframeModal")).show();
}
async function openCustomModal() {
console.log("custom")
}
async function openSKUModal() {
pagination_current = 1
let items = await getItems()
await replenishItemsTable(items)
await updateItemsPaginationElement()
UIkit.dropdown(document.getElementById("addLineDropDown")).hide(false);
UIkit.modal(document.getElementById("itemsModal")).show();
}
async function openLineEditModal(line_data) {
console.log(line_data)
document.getElementById('lineName').value = line_data.name
document.getElementById('lineQty').value = line_data.qty
document.getElementById('lineUOM').value = line_data.uom.id
document.getElementById('lineCost').value = line_data.data.cost
document.getElementById('lineExpires').value = line_data.data.expires
if(line_data.type === 'sku'){
document.getElementById('lineUOM').classList.add('uk-disabled')
} else {
document.getElementById('lineUOM').classList.remove('uk-disabled')
}
if(!line_data.data.expires){
document.getElementById('lineExpires').classList.add('uk-disabled')
} else {
document.getElementById('lineExpires').classList.remove('uk-disabled')
}
document.getElementById('saveLineButton').onclick = async function() {
await saveLine(line_data.id)
}
UIkit.modal(document.getElementById("lineEditModal")).show();
}
async function addSKULine(item_id) {
console.log(item_id)
const response = await fetch(`/receipts/addSKULine`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
item_id: item_id,
receipt_id: receipt_id
}),
});
await refreshReceipt()
UIkit.modal(document.getElementById("itemsModal")).hide();
}
async function resolveLine(line_id) {
const response = await fetch(`/receipts/resolveLine`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
line_id: line_id
}),
});
await refreshReceipt()
}
async function resolveReceipt() {
const response = await fetch(`/receipts/resolveReceipt`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
receipt_id: receipt_id
}),
});
await refreshReceipt()
}
async function uploadFile() {
const fileInput = document.querySelector('input[type="file"]');
const formData = new FormData();
formData.append('file', fileInput.files[0]);
await fetch(`/receipt/uploadfile/${receipt_id}`, {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => console.log('File uploaded!', data))
.catch(error => console.error('Error:', error));
}
async function saveLine(line_id){
let name = document.getElementById('lineName').value
let qty = document.getElementById('lineQty').value
let uom = document.getElementById('lineUOM').value
let cost = document.getElementById('lineCost').value
let expires = document.getElementById('lineExpires').value
console.log(expires)
if(expires === ''){
expires = false
}
UIkit.modal(document.getElementById("lineEditModal")).hide();
let payload = {
name: name,
qty: qty,
uom: uom,
data: {
cost: cost,
expires: expires
}
}
const response = await fetch(`/receipts/saveLine`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
line_id: line_id,
payload: payload
}),
});
await refreshReceipt()
}
async function deleteLine(id) {
const response = await fetch(`/receipts/deleteLine`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
line_id: id
}),
});
await refreshReceipt()
}
async function denyLine(id) {
console.log(id)
const response = await fetch(`/receipts/denyLine`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
line_id: id
}),
});
await refreshReceipt()
}
async function getReceipt(id) {
const url = new URL('/receipts/getReceipt', window.location.origin);
url.searchParams.append('id', id);
const response = await fetch(url);
data = await response.json();
let receipt = data.receipt;
return receipt;
}
let items_limit = 50;
async function getItems() {
console.log("getting items")
const url = new URL('/receipts/getItems', window.location.origin);
url.searchParams.append('page', pagination_current);
url.searchParams.append('limit', items_limit);
const response = await fetch(url);
data = await response.json();
pagination_end = data.end
let items = data.items;
return items;
}
async function setPage(page) {
pagination_current = page
let items = await getItems()
await replenishItemsTable(items)
await updateItemsPaginationElement()
}
async function updateItemsPaginationElement() {
let paginationElement = document.getElementById("itemsPage");
paginationElement.innerHTML = "";
// previous
let previousElement = document.createElement('li')
if(pagination_current<=1){
previousElement.innerHTML = `<a><span uk-pagination-previous></span></a>`;
previousElement.classList.add('uk-disabled');
}else {
previousElement.innerHTML = `<a onclick="setPage(${pagination_current-1})"><span uk-pagination-previous></span></a>`;
}
paginationElement.append(previousElement)
//first
let firstElement = document.createElement('li')
if(pagination_current<=1){
firstElement.innerHTML = `<a><strong>1</strong></a>`;
firstElement.classList.add('uk-disabled');
}else {
firstElement.innerHTML = `<a onclick="setPage(1)">1</a>`;
}
paginationElement.append(firstElement)
// ...
if(pagination_current-2>1){
let firstDotElement = document.createElement('li')
firstDotElement.classList.add('uk-disabled')
firstDotElement.innerHTML = `<span>…</span>`;
paginationElement.append(firstDotElement)
}
// last
if(pagination_current-2>0){
let lastElement = document.createElement('li')
lastElement.innerHTML = `<a onclick="setPage(${pagination_current-1})">${pagination_current-1}</a>`
paginationElement.append(lastElement)
}
// current
if(pagination_current!=1 && pagination_current != pagination_end){
let currentElement = document.createElement('li')
currentElement.innerHTML = `<li class="uk-active"><span aria-current="page"><strong>${pagination_current}</strong></span></li>`
paginationElement.append(currentElement)
}
// next
if(pagination_current+2<pagination_end+1){
let nextElement = document.createElement('li')
nextElement.innerHTML = `<a onclick="setPage(${pagination_current+1})">${pagination_current+1}</a>`
paginationElement.append(nextElement)
}
// ...
if(pagination_current+2<=pagination_end){
let secondDotElement = document.createElement('li')
secondDotElement.classList.add('uk-disabled')
secondDotElement.innerHTML = `<span>…</span>`;
paginationElement.append(secondDotElement)
}
//end
let endElement = document.createElement('li')
if(pagination_current>=pagination_end){
endElement.innerHTML = `<a><strong>${pagination_end}</strong></a>`;
endElement.classList.add('uk-disabled');
}else {
endElement.innerHTML = `<a onclick="setPage(${pagination_end})">${pagination_end}</a>`;
}
paginationElement.append(endElement)
//next button
let nextElement = document.createElement('li')
if(pagination_current>=pagination_end){
nextElement.innerHTML = `<a><span uk-pagination-next></span></a>`;
nextElement.classList.add('uk-disabled');
}else {
nextElement.innerHTML = `<a onclick="setPage(${pagination_current+1})"><span uk-pagination-next></span></a>`;
console.log(nextElement.innerHTML)
}
paginationElement.append(nextElement)
}