function updateTotal() { } function editTransaction(obj) { transactionEditorID.value = obj[0]; transactionEditorDate.value = obj[2]; transactionEditorQuantity.value = obj[3]; transactionEditorProductID.value = obj[4]; try { fetch(URL + "/api/v1/read/?t=product").then(resp => resp.json()).then(json => { transactionEditorProductID.innerHTML = ""; const p_id = obj[4]; const selected_product = json.find(e => e[0] == p_id); transactionEditorProductID.innerHTML += ` `; for (var i = 0; i < json.length; ++i) { if (json[i][0] == selected_product[0]) continue; transactionEditorProductID.innerHTML += ` `; } }); } catch (e) { console.error(e); } next('transactionEditor'); } function deleteTransaction(obj) { const t_id = obj[0]; try { const payload = JSON.stringify({ t: "transaction", t_id: t_id }); if (confirm(`Are you sure you want to delete the transaction with ID: ${t_id}`)) { fetch(URL + "/api/v1/delete/", { method: "POST", headers: { "Content-Type": "application/json" }, body: payload }).then(resp => { if (resp.status != 204) { const msg = `Error deleting transaction, status: ${resp.status}`; alert(msg); throw msg; } else { alert(`Succesfully deleted transaction with ID: ${t_id}`); initTable(allTransactionsTable, 'transaction', 'editTransaction', 'deleteTransaction'); } }); } } catch(e) { console.error(e); } } function editTransactionHandler(event) { event.preventDefault(); console.log(event.target); const type = event.target[1].value; const date = event.target[2].value; const quantity = event.target[3].value; const p_id = event.target[4].value; const t_id = event.target[6].value; try { if (confirm(`Do you want to edit the transaction with ID: ${t_id}?`)) { const payload = JSON.stringify({ t: "transaction", t_id: t_id, type: type, date: date, quantity: quantity, p_id: p_id }) fetch(URL + "/api/v1/update/", { method: "POST", headers: { "Content-Type": "application/json" }, body: payload }).then(resp => { if (resp.status != 201) { const msg = `Error updating transaction, status: ${resp.status}`; alert(msg); throw msg; } return resp.json(); }).then(json => { alert(`Succesfully updated transaction with ID: ${json.t_id}`); restart(); }); } } catch (e) { console.error(e); } } async function fillTransactionCreator() { const now = new Date(Date.now()); const yyyy = now.getFullYear(); const mm = (now.getMonth() < 10 ? "0" : "") + (now.getMonth() + 1); const dd = (now.getDay() < 10 ? "0" : "") + now.getDay(); const HH = (now.getHours() < 10 ? "0" : "") + now.getHours(); const MM = (now.getMinutes() < 10 ? "0" : "") + now.getMinutes(); const date = `${yyyy}-${mm}-${dd}T${HH}:${MM}`; transactionCreatorDate.value = date; try { await fetch(URL + "/api/v1/read/?t=product").then(response => response.json()).then(json => { createTransactionProductId.innerHTML = ""; for (var i = 0; i < json.length; i++) { const obj = json[i]; const id = obj[0]; const cents = obj[1]; const description = obj[2]; const cost = (cents / 100.00).toFixed(2); createTransactionProductId.innerHTML += ` `; } }); fetch(URL + "/api/v1/read/?t=member").then(resp => resp.json()).then(json => { initiator.innerHTML = ""; for (var i = 0; i < json.length; ++i) { const obj = json[i]; const m_id = obj[0]; const name = obj[1]; const second_name = obj[2]; const last_name = obj[3]; const second_last_name = obj[4]; initiator.innerHTML += ` ` } }); fetch(URL + "/api/v1/read/?t=board_member").then(resp => resp.json()).then(json => { logger.innerHTML = ""; for (var i = 0; i < json.length; ++i) { const obj = json[i]; const m_id = obj[0]; const position = obj[1]; logger.innerHTML += ` ` } }); } catch (e) { console.error(e.message); } } function transactionCreatorFormHandler(event) { event.preventDefault(); try { const type = event.target[1].value; const date = event.target[2].value; const quantity = event.target[3].value; const p_id = event.target[4].value; const desc = event.target[4].selectedOptions[0].innerText; if (confirm(`Do you want to create a transaction: Type: ${type} Date: ${date} Quantity: ${quantity} Product: ${desc} `)) { fetch(URL + "/api/v1/create/", { method: "POST", body: JSON.stringify({ t: "transaction", type: type, date: date, quantity: quantity, p_id: p_id }) }).then(response => { if (response.status == 201) { alert("Transaction succesfully created."); restart(); } else { alert("Error ocurred during transaction creation") console.error(response.statusText); } }); } } catch (error) { console.error(error.message); } } function fillTransactionTable() { try { fetch(URL + "/api/v1/read/?t=transaction").then(resp => resp.json()).then(t_json => { fetch(URL + "/api/v1/read/?t=product").then(resp => resp.json()).then(p_json => { const products = {}; for (var i = 0; i < p_json.length; ++i) { const obj = p_json[i]; products[obj[0]] = { cents: obj[1], desc: obj[2] }; } transactionResultsTable.innerHTML = `