| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- window.homeObj = {
- isPopulated: false,
- recipeTotal: 0,
- revenueTotal: 0,
- dateFrom: "",
- dateTo: "",
- display: function(){
- clearScreen();
- document.querySelector("#homeStrand").style.display = "flex";
-
- if(!this.isPopulated){
- this.populate(data.transactions);
- document.querySelector("#homeFrom").valueAsDate = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
- document.querySelector("#homeTo").valueAsDate = new Date();
- this.isPopulated = true;
- }
- },
- populate: function(transactions){
- console.log(transactions);
- this.recipeTotal = 0;
- this.revenueTotal = 0;
-
- //Create object to store number of recipes sold
- let recipes = [];
- for(let recipe of data.merchant.recipes){
- recipes.push({
- id: recipe._id,
- name: recipe.name,
- quantity: 0,
- ingredients: recipe.ingredients,
- price: recipe.price / 100
- });
- }
- //Create object to store amount of ingredients sold
- let soldIngredients = [];
- for(let item of data.merchant.inventory){
- soldIngredients.push({
- id: item.ingredient._id,
- name: item.ingredient.name,
- quantity: 0,
- quantityRemaining: item.quantity,
- unit: item.ingredient.unit
- });
- }
- //Create object for each merchant ingredient
- let purchaseIngredients = [];
- for(let item of data.merchant.inventory){
- purchaseIngredients.push({
- id: item.ingredient._id,
- name: item.ingredient.name,
- amount: 0,
- unit: item.ingredient.unit
- });
- }
-
- //Populate number of recipes sold
- for(let transaction of transactions){
- for(let recipe of transaction.recipes){
- for(let newRecipe of recipes){
- if(recipe.recipe === newRecipe.id){
- newRecipe.quantity += recipe.quantity;
- this.recipeTotal += recipe.quantity;
- this.revenueTotal += (newRecipe.price * recipe.quantity);
- break;
- }
- }
- }
- }
- //Populate amount of ingredients sold
- for(let recipe of recipes){
- for(let recipeIngredient of recipe.ingredients){
- for(let newIngredient of soldIngredients){
- if(newIngredient.id === recipeIngredient.ingredient){
- newIngredient.quantity += (recipeIngredient.quantity * recipe.quantity);
- break;
- }
- }
- }
- }
- //Populate amount of ingredients purchased
- for(let purchase of data.purchases){
- for(let newPurchaseIngredient of purchase.ingredients){
- for(let newIngredient of purchaseIngredients){
- if(newIngredient.id === newPurchaseIngredient.ingredient){
- newIngredient.amount += newPurchaseIngredient.quantity;
- break;
- }
- }
- }
- }
- //Populate Ingredients table
- let ingredientsBody = document.querySelector("#ingredientsData tbody");
- while(ingredientsBody.children.length > 0){
- ingredientsBody.removeChild(ingredientsBody.firstChild);
- }
-
- for(let ingredient of soldIngredients){
- let row = document.createElement("tr");
- ingredientsBody.appendChild(row);
- row.classList = "clickableRow";
- row.onclick = ()=>{window.ingredientObj.display(ingredient)};
-
- let name = document.createElement("td");
- name.innerText = `${ingredient.name} (${ingredient.unit})`;
- row.appendChild(name);
- let used = document.createElement("td");
- used.innerText = ingredient.quantity;
- row.appendChild(used);
- let remaining = document.createElement("td");
- remaining.innerText = ingredient.quantityRemaining;
- row.appendChild(remaining);
- }
- //Populate recipes table
- let recipesBody = document.querySelector("#recipesData tbody");
- while(recipesBody.children.length > 0){
- recipesBody.removeChild(recipesBody.firstChild);
- }
- for(let recipe of recipes){
- let row = document.createElement("tr");
- recipesBody.appendChild(row);
- let name = document.createElement("td");
- name.innerText = recipe.name;
- row.appendChild(name);
- let quantity = document.createElement("td");
- quantity.innerText = recipe.quantity;
- row.appendChild(quantity);
- let revenue = document.createElement("td");
- revenue.innerText = `$${(recipe.quantity * recipe.price).toFixed(2)}`;
- row.appendChild(revenue);
- }
- //Populate purchases table
- let purchasesBody = document.querySelector("#purchasesData tbody");
- while(purchasesBody.children.length > 0){
- purchasesBody.removeChild(purchasesBody.firstChild);
- }
-
- for(let ingredient of purchaseIngredients){
- let row = document.createElement("tr");
- purchasesBody.appendChild(row);
-
- let name = document.createElement("td");
- name.innerText = `${ingredient.name} (${ingredient.unit})`;
- row.appendChild(name);
- let amount = document.createElement("td");
- amount.innerText = ingredient.amount;
- row.appendChild(amount);
- }
- //Populate totals
- document.querySelector("#revenueTotal").innerText = `$${this.revenueTotal.toFixed(2)}`;
- document.querySelector("#soldTotal").innerText = this.recipeTotal;
- },
- newDates: function(){
- let from = new Date(document.querySelector("#homeFrom").value);
- let to = new Date(document.querySelector("#homeTo").value);
- if(from === "" || to === ""){
- banner.createError("Invalid date");
- return;
- }else{
- from = new Date(from);
- to = new Date(to);
- }
- if(validator.transaction.date(from, to)){
- window.fetchData(from, to, ()=>{
- let startIndex = 0;
- let endIndex = data.transactions.length;
- for(let i = 0; i < data.transactions.length; i++){
- if(from < new Date(data.transactions[i].date)){
- startIndex = i;
- break;
- }
- }
-
- for(let i = 0; i < data.transactions.length; i++){
- if(to < new Date(data.transactions[i].date)){
- endIndex = i;
- break;
- }
- }
-
- this.populate(data.transactions.slice(startIndex, endIndex));
- });
- }
- }
- }
|