| 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 i = 0; i < recipes.length; i++){
- for(let j = 0; j < merchant.recipes.length; j++){
- for(let k = 0; k < merchant.recipes[j].ingredients.length; k++){
- let exists = false;
- for(let l = 0; l < ingredientList.length; l++){
- if(ingredientList[l].id === merchant.recipes[j].ingredients[k].ingredient._id){
- exists = true;
- ingredientList[l].quantity += merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity;
- break;
- }
- }
- if(!exists){
- ingredientList.push({
- id: merchant.recipes[j].ingredients[k].ingredient._id,
- quantity: merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity,
- name: merchant.recipes[j].ingredients[k].ingredient.name,
- unit: merchant.recipes[j].ingredients[k].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 i = 0; i < merchant.recipes.length; i++){
- for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
- if(merchant.recipes[i].ingredients[j].ingredient._id === id){
- checkRecipes.push(merchant.recipes[i]._id);
- quantities.push(merchant.recipes[i].ingredients[j].quantity);
- break;
- }
- }
- }
- for(let i = 0; i < recipes.length; i++){
- for(let i = 0; i < checkRecipes.length; i++){
- if(checkRecipes[i] === recipes[i].id){
- total += recipes[i].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 j = 0; j < transactions[i].recipes.length; j++){
- let exists = false;
- for(let k = 0; k < recipeList.length; k++){
- if(recipeList[k].id === transactions[i].recipes[j].recipe){
- exists = true;
- recipeList[k].quantity += transactions[i].recipes[j].quantity;
- break;
- }
- }
- if(!exists){
- recipeList.push({
- id: transactions[i].recipes[j].recipe,
- quantity: transactions[i].recipes[j].quantity
- })
- }
- }
- }
- return recipeList;
- }
- let categorizeIngredients = ()=>{
- let ingredientsByCategory = [];
- for(let i = 0; i < merchant.inventory.length; i++){
- let categoryExists = false;
- for(let j = 0; j < ingredientsByCategory.length; j++){
- if(merchant.inventory[i].ingredient.category === ingredientsByCategory[j].name){
- ingredientsByCategory[j].ingredients.push({
- id: merchant.inventory[i].ingredient._id,
- name: merchant.inventory[i].ingredient.name,
- quantity: merchant.inventory[i].quantity,
- unit: merchant.inventory[i].ingredient.unit
- });
- categoryExists = true;
- break;
- }
- }
- if(!categoryExists){
- ingredientsByCategory.push({
- name: merchant.inventory[i].ingredient.category,
- ingredients: [{
- id: merchant.inventory[i].ingredient._id,
- name: merchant.inventory[i].ingredient.name,
- quantity: merchant.inventory[i].quantity,
- unit: merchant.inventory[i].ingredient.unit
- }]
- });
- }
- }
- return ingredientsByCategory;
- }
- for(let transaction of transactions){
- transaction.date = new Date(transaction.date);
- }
- homeStrandObj.display();
|