| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- window.transactionsStrandObj = {
- isPopulated: false,
- display: function(){
- if(!this.isPopulated){
- let transactionsList = document.getElementById("transactionsList");
- let checkboxes = document.getElementById("transFilCheckboxes");
- let template = document.getElementById("transaction").content.children[0];
- let now = new Date();
- let monthAgo = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
- document.getElementById("transFilDate1").valueAsDate = monthAgo;
- document.getElementById("transFilDate2").valueAsDate = now;
- while(checkboxes.children.length > 0){
- checkboxes.removeChild(checkboxes.firstChild);
- }
- for(let i = 0; i < merchant.recipes.length; i++){
- let label = document.createElement("label");
- label.innerText = merchant.recipes[i].name;
- checkboxes.appendChild(label);
- let checkbox = document.createElement("input");
- checkbox.type = "checkbox";
- checkbox.recipe = merchant.recipes[i];
- label.appendChild(checkbox);
- }
- while(transactionsList.children.length > 0){
- transactionsList.removeChild(transactionsList.firstChild);
- }
- let i = 0
- while(i < merchant.transactions.length && i < 100){
- let transactionDiv = template.cloneNode(true);
- let transaction = merchant.transactions[i];
- transactionDiv.onclick = ()=>{transactionDetailsComp.display(transaction)};
- transactionsList.appendChild(transactionDiv);
- let totalRecipes = 0;
- let totalPrice = 0;
- for(let j = 0; j < merchant.transactions[i].recipes.length; j++){
- totalRecipes += merchant.transactions[i].recipes[j].quantity;
- totalPrice += merchant.transactions[i].recipes[j].recipe.price * merchant.transactions[i].recipes[j].quantity;
- }
- transactionDiv.children[0].innerText = `${merchant.transactions[i].date.toLocaleDateString()} ${merchant.transactions[i].date.toLocaleTimeString()}`;
- transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
- transactionDiv.children[2].innerText = `$${(totalPrice / 100).toFixed(2)}`;
- i++;
- }
- this.isPopulated = true;
- }
- },
- submitFilter: function(){
- event.preventDefault();
- let data = {
- startDate: document.getElementById("transFilDate1").valueAsDate,
- endDate: document.getElementById("transFilDate2").valueAsDate,
- recipes: []
- }
- if(data.startDate >= data.endDate){
- banner.createError("START DATE CANNOT BE AFTER END DATE");
- return;
- }
- let recipeChoices = document.getElementById("transFilCheckboxes");
- for(let i = 0; i < recipeChoices.children.length; i++){
- if(recipeChoices.children[i].children[0].checked){
- data.recipes.push(recipeChoices.children[i].children[0].recipe.id);
- }
- }
- let loader = document.getElementById("loaderContainer");
- loader.style.display = "flex";
- fetch("/transaction", {
- method: "POST",
- headers: {
- "Content-Type": "application/json;charset=utf-8"
- },
- body: JSON.stringify(data)
- })
- .then((response) => response.json())
- .then((response)=>{
- if(typeof(response) === "string"){
- banner.createError(response);
- }else{
- let transactionList = document.getElementById("transactionsList");
- let template = document.getElementById("transaction").content.children[0];
- while(transactionList.children.length > 0){
- transactionList.removeChild(transactionList.firstChild);
- }
- for(let i = 0; i < response.length; i++){
- let transactionDiv = template.cloneNode(true);
- let recipeCount = 0;
- let cost = 0;
- let transaction = new Transaction(
- response[i]._id,
- response[i].date,
- response[i].recipes,
- merchant
- );
- for(let j = 0; j < transaction.recipes.length; j++){
- recipeCount += transaction.recipes[j].quantity;
- cost += transaction.recipes[j].quantity * transaction.recipes[j].recipe.price;
- }
- transactionDiv.children[0].innerText = `${transaction.date.toLocaleDateString()} ${transaction.date.toLocaleTimeString()}`;
- transactionDiv.children[1].innerText = `${recipeCount} recipes sold`;
- transactionDiv.children[2].innerText = `$${(cost / 100).toFixed(2)}`;
- transactionList.appendChild(transactionDiv);
- }
- }
- })
- .catch((err)=>{
- banner.createError("UNABLE TO DISPLAY THE TRANSACTIONS");
- })
- .finally(()=>{
- loader.style.display = "none";
- });
- }
- }
|