AECC database project.
at master 238 lines 6.6 kB view raw
1function updateTotal() { 2 3} 4function editTransaction(obj) { 5 transactionEditorID.value = obj[0]; 6 transactionEditorDate.value = obj[2]; 7 transactionEditorQuantity.value = obj[3]; 8 transactionEditorProductID.value = obj[4]; 9 try { 10 fetch(URL + "/api/v1/read/?t=product").then(resp => resp.json()).then(json => { 11 transactionEditorProductID.innerHTML = ""; 12 const p_id = obj[4]; 13 const selected_product = json.find(e => e[0] == p_id); 14 transactionEditorProductID.innerHTML += ` 15 <option value="${p_id}">${selected_product[2]}</option> 16 `; 17 for (var i = 0; i < json.length; ++i) { 18 if (json[i][0] == selected_product[0]) continue; 19 transactionEditorProductID.innerHTML += ` 20 <option value="${json[i][0]}">${json[i][2]}</option> 21 `; 22 } 23 }); 24 } catch (e) { 25 console.error(e); 26 } 27 next('transactionEditor'); 28} 29 30function deleteTransaction(obj) { 31 const t_id = obj[0]; 32 try { 33 const payload = JSON.stringify({ 34 t: "transaction", 35 t_id: t_id 36 }); 37 if (confirm(`Are you sure you want to delete the transaction with ID: ${t_id}`)) { 38 fetch(URL + "/api/v1/delete/", { 39 method: "POST", 40 headers: { "Content-Type": "application/json" }, 41 body: payload 42 }).then(resp => { 43 if (resp.status != 204) { 44 const msg = `Error deleting transaction, status: ${resp.status}`; 45 alert(msg); 46 throw msg; 47 } else { 48 alert(`Succesfully deleted transaction with ID: ${t_id}`); 49 initTable(allTransactionsTable, 'transaction', 'editTransaction', 'deleteTransaction'); 50 } 51 }); 52 } 53 } catch(e) { 54 console.error(e); 55 } 56} 57 58function editTransactionHandler(event) { 59 event.preventDefault(); 60 console.log(event.target); 61 const type = event.target[1].value; 62 const date = event.target[2].value; 63 const quantity = event.target[3].value; 64 const p_id = event.target[4].value; 65 const t_id = event.target[6].value; 66 try { 67 if (confirm(`Do you want to edit the transaction with ID: ${t_id}?`)) { 68 const payload = JSON.stringify({ 69 t: "transaction", 70 t_id: t_id, 71 type: type, 72 date: date, 73 quantity: quantity, 74 p_id: p_id 75 }) 76 fetch(URL + "/api/v1/update/", { 77 method: "POST", 78 headers: { 79 "Content-Type": "application/json" 80 }, 81 body: payload 82 }).then(resp => { 83 if (resp.status != 201) { 84 const msg = `Error updating transaction, status: ${resp.status}`; 85 alert(msg); 86 throw msg; 87 } 88 return resp.json(); 89 }).then(json => { 90 alert(`Succesfully updated transaction with ID: ${json.t_id}`); 91 restart(); 92 }); 93 } 94 } catch (e) { 95 console.error(e); 96 } 97} 98 99async function fillTransactionCreator() { 100 const now = new Date(Date.now()); 101 const yyyy = now.getFullYear(); 102 const mm = (now.getMonth() < 10 ? "0" : "") + (now.getMonth() + 1); 103 const dd = (now.getDay() < 10 ? "0" : "") + now.getDay(); 104 const HH = (now.getHours() < 10 ? "0" : "") + now.getHours(); 105 const MM = (now.getMinutes() < 10 ? "0" : "") + now.getMinutes(); 106 const date = `${yyyy}-${mm}-${dd}T${HH}:${MM}`; 107 transactionCreatorDate.value = date; 108 try { 109 await fetch(URL + "/api/v1/read/?t=product").then(response => response.json()).then(json => { 110 createTransactionProductId.innerHTML = ""; 111 for (var i = 0; i < json.length; i++) { 112 const obj = json[i]; 113 const id = obj[0]; 114 const cents = obj[1]; 115 const description = obj[2]; 116 const cost = (cents / 100.00).toFixed(2); 117 createTransactionProductId.innerHTML += ` 118 <option value="${id}">${description} ($${cost})</option> 119 `; 120 } 121 }); 122 fetch(URL + "/api/v1/read/?t=member").then(resp => resp.json()).then(json => { 123 initiator.innerHTML = ""; 124 for (var i = 0; i < json.length; ++i) { 125 const obj = json[i]; 126 const m_id = obj[0]; 127 const name = obj[1]; 128 const second_name = obj[2]; 129 const last_name = obj[3]; 130 const second_last_name = obj[4]; 131 initiator.innerHTML += ` 132 <option value="${m_id}">${name} ${second_name} ${last_name} ${second_last_name}</option> 133 ` 134 } 135 }); 136 fetch(URL + "/api/v1/read/?t=board_member").then(resp => resp.json()).then(json => { 137 logger.innerHTML = ""; 138 for (var i = 0; i < json.length; ++i) { 139 const obj = json[i]; 140 const m_id = obj[0]; 141 const position = obj[1]; 142 logger.innerHTML += ` 143 <option value="${m_id}">${position}</option> 144 ` 145 } 146 }); 147 } catch (e) { 148 console.error(e.message); 149 } 150} 151 152function transactionCreatorFormHandler(event) { 153 event.preventDefault(); 154 try { 155 const type = event.target[1].value; 156 const date = event.target[2].value; 157 const quantity = event.target[3].value; 158 const p_id = event.target[4].value; 159 const desc = event.target[4].selectedOptions[0].innerText; 160 if (confirm(`Do you want to create a transaction: 161 Type: ${type} 162 Date: ${date} 163 Quantity: ${quantity} 164 Product: ${desc} 165 `)) { 166 fetch(URL + "/api/v1/create/", { 167 method: "POST", 168 body: JSON.stringify({ 169 t: "transaction", 170 type: type, 171 date: date, 172 quantity: quantity, 173 p_id: p_id 174 }) 175 }).then(response => { 176 if (response.status == 201) { 177 alert("Transaction succesfully created."); 178 restart(); 179 } else { 180 alert("Error ocurred during transaction creation") 181 console.error(response.statusText); 182 } 183 }); 184 } 185 } catch (error) { 186 console.error(error.message); 187 } 188} 189 190function fillTransactionTable() { 191 try { 192 fetch(URL + "/api/v1/read/?t=transaction").then(resp => resp.json()).then(t_json => { 193 fetch(URL + "/api/v1/read/?t=product").then(resp => resp.json()).then(p_json => { 194 const products = {}; 195 for (var i = 0; i < p_json.length; ++i) { 196 const obj = p_json[i]; 197 products[obj[0]] = { 198 cents: obj[1], 199 desc: obj[2] 200 }; 201 } 202 203 transactionResultsTable.innerHTML = ` 204 <tr> 205 <th>ID</th> 206 <th>Type</th> 207 <th>Date</th> 208 <th>Quantity</th> 209 <th style="width:80ch;">Description</th> 210 <th style="width:10ch;">Total ($USD)</th> 211 <th style="width:10ch;">Per product ($USD)</th> 212 </tr> 213 `; 214 215 for (var i = 0; i < t_json.length; i++) { 216 const obj = t_json[i]; 217 const quantity = obj[3]; 218 const product = products[obj[4]]; 219 const cost = (product.cents / 100.00).toFixed(2); 220 const total = (1.0 * cost * quantity).toFixed(2); 221 transactionResultsTable.innerHTML += ` 222 <tr> 223 <td>${obj[0]}</td> 224 <td>${obj[1]}</td> 225 <td>${obj[2]}</td> 226 <td>${quantity}</td> 227 <td>${product.desc}</td> 228 <td>$${total}</td> 229 <td>$${cost}</td> 230 </tr> 231 `; 232 } 233 }); 234 }); 235 } catch (error) { 236 console.error(error.message); 237 } 238}