| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- let changeStrand = (name)=>{
- closeSidebar();
- for(let strand of document.querySelectorAll(".strand")){
- strand.style.display = "none";
- }
- for(let button of document.querySelectorAll(".menu > button")){
- button.classList = "";
- button.onclick = ()=>{changeStrand(`${button.id.slice(0, button.id.indexOf("Btn"))}Strand`)};
- }
- let activeButton = document.querySelector(`#${name.slice(0, name.indexOf("Strand"))}Btn`);
- activeButton.classList = "active";
- activeButton.onclick = undefined;
- document.querySelector(`#${name}`).style.display = "flex";
- window[`${name}Obj`].display();
- }
- let closeSidebar = ()=>{
- let sidebar = document.querySelector("#sidebarDiv");
- for(let i = 0; i < sidebar.children.length; i++){
- sidebar.children[i].style.display = "none";
- }
- sidebar.classList = "sidebarHide";
- }
- let openSidebar = (sidebar)=>{
- document.querySelector("#sidebarDiv").classList = "sidebar";
- let sideBars = document.querySelector("#sidebarDiv").children;
- for(let i = 0; i < sideBars.length; i++){
- sideBars[i].style.display = "none";
- }
- sidebar.style.display = "flex";
- }
- //Gets the indices of two dates from transactions
- //Inputs
- // from: starting date
- // to: ending date (default to now)
- //Output
- // Array containing starting index and ending index
- //Note: Will return false if it cannot find both necessary dates
- let dateIndices = (from, to = new Date())=>{
- let indices = [];
- for(let i = 0; i < transactions.length; i++){
- if(transactions[i].date > from){
- indices.push(i);
- break;
- }
- }
- for(let i = transactions.length - 1; i >=0; i--){
- if(transactions[i].date < to){
- indices.push(i);
- break;
- }
- }
- if(indices.length < 2){
- return false;
- }
- return indices;
- }
- //Gets the quantity of each ingredient sold between two dates (dateRange)
- //Inputs
- // dateRange: list containing a start date and an end date
- //Output
- // List of objects
- // id: id of specific ingredient
- // quantity: quantity sold of that ingredient
- // name: name of the ingredient
- let ingredientsSold = (dateRange)=>{
- if(!dateRange){
- return false;
- }
-
- let recipes = recipesSold(dateRange);
- let ingredientList = [];
- for(let recipe of recipes){
- for(let merchRecipe of merchant.recipes){
- for(let ingredient of merchRecipe.ingredients){
- let exists = false;
- for(let item of ingredientList){
- if(item.id === ingredient.ingredient._id){
- exists = true;
- item.quantity += ingredient.quantity * recipe.quantity;
- break;
- }
- }
- if(!exists){
- ingredientList.push({
- id: ingredient.ingredient._id,
- quantity: ingredient.quantity * recipe.quantity,
- name: ingredient.ingredient.name,
- unit: ingredient.ingredient.unit
- })
- }
- }
- }
- }
- return ingredientList;
- }
- //Gets the quantity of a single ingredient sold between two dates (dateRange)
- let ingredientSold = (dateRange, id)=>{
- let recipes = recipesSold(dateRange);
- let total = 0;
- let checkRecipes = [];
- let quantities = [];
- for(let merchRecipe of merchant.recipes){
- for(let merchIngredient of merchRecipe.ingredients){
- if(merchIngredient.ingredient._id === id){
- checkRecipes.push(merchRecipe._id);
- quantities.push(merchIngredient.quantity);
- break;
- }
- }
- }
- for(let recipe of recipes){
- for(let i = 0; i < checkRecipes.length; i++){
- if(checkRecipes[i] === recipe.id){
- total += recipe.quantity * quantities[i];
- break;
- }
- }
- }
- return total;
- }
- //Gets the number of recipes sold between two dates (dateRange)
- //Inputs
- // dateRange: array containing a start date and an end date
- //Output
- // List of objects
- // id: id of specific recipe
- // quantity: quantity sold of that recipe
- let recipesSold = (dateRange)=>{
- let recipeList = [];
- for(let i = dateRange[0]; i <= dateRange[1]; i++){
- for(let recipe of transactions[i].recipes){
- let exists = false;
- for(let item of recipeList){
- if(item.id === recipe.recipe){
- exists = true;
- item.quantity += recipe.quantity;
- break;
- }
- }
- if(!exists){
- recipeList.push({
- id: recipe.recipe,
- quantity: recipe.quantity
- })
- }
- }
- }
- return recipeList;
- }
- let categorizeIngredients = ()=>{
- let ingredientsByCategory = [];
- for(let item of merchant.inventory){
- let categoryExists = false;
- for(let category of ingredientsByCategory){
- if(item.ingredient.category === category.name){
- category.ingredients.push({
- id: item.ingredient._id,
- name: item.ingredient.name,
- quantity: item.quantity,
- unit: item.ingredient.unit
- });
- categoryExists = true;
- break;
- }
- }
- if(!categoryExists){
- ingredientsByCategory.push({
- name: item.ingredient.category,
- ingredients: [{
- id: item.ingredient._id,
- name: item.ingredient.name,
- quantity: item.quantity,
- unit: item.ingredient.unit
- }]
- });
- }
- }
- return ingredientsByCategory;
- }
- for(let transaction of transactions){
- transaction.date = new Date(transaction.date);
- }
- homeStrandObj.display();
|