| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- /*
- Switches to a different strand
- Input:
- name: name of the strand. Must end with "Strand"
- */
- 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();
- }
- /*
- Updates all specified item in the merchant's inventory and updates the page
- If ingredient doesn't exist, add it
- Inputs:
- Array of objects
- id: id of ingredient
- quantityChange: change in quantity (optional)
- name: name of ingredient (only for new ingredient)
- category: category of ingredient (only for new ingredient)
- unit: unit of measurement (only for new ingredient)
- remove: if true, remove ingredient from inventory
- */
- let updateInventory = (ingredients, remove = false)=>{
- for(let i = 0; i < ingredients.length; i++){
- let isNew = true;
- for(let j = 0; j < merchant.inventory.length; j++){
- if(merchant.inventory[j].ingredient._id === ingredients[i].id){
- if(remove){
- merchant.inventory.splice(j, 1);
- }else{
- merchant.inventory[j].quantity += ingredients[i].quantity;
- }
- isNew = false;
- break;
- }
- }
- if(isNew){
- merchant.inventory.push(ingredients[i]);
- }
- }
- homeStrandObj.drawInventoryCheckCard();
- ingredientsStrandObj.populateByProperty("category");
- addIngredientsComp.isPopulated = false;
- closeSidebar();
- }
- /*
- Updates a recipe in the merchants list of recipes
- Can create, edit or remove
- Inputs:
- recipe: object
- _id: id of recipe
- name: name of recipe
- price: price of recipe
- ingredients: list of ingredients
- ingredient: id of ingredient
- quantity: quantity of ingredient
- remove: if true, remove ingredient from inventory
- */
- let updateRecipes = (recipe, remove = false)=>{
- let isNew = true;
- let index = 0;
- for(let i = 0; i < recipe.ingredients.length; i++){
- for(let j = 0; j < merchant.inventory.length; j++){
- if(merchant.inventory[j].ingredient._id === recipe.ingredients[i].ingredient){
- recipe.ingredients[i].ingredient = merchant.inventory[j].ingredient;
- break;
- }
- }
- }
- for(let i = 0; i < merchant.recipes.length; i++){
- if(recipe._id === merchant.recipes[i]._id){
- if(remove){
- merchant.recipes.splice(i, 1);
- }else{
- merchant.recipes[i] = recipe;
- index = i;
- }
- isNew = false;
- break;
- }
- }
- if(isNew){
- merchant.recipes.push(recipe);
- index = merchant.recipes.length - 1;
- }
- recipeBookStrandObj.populateRecipes();
- closeSidebar();
- }
- /*
- Updates an order in the front end
- Can create, edit or remove
- Inputs:
- recipe: object
- _id: id of recipe
- name: name of recipe
- price: price of recipe
- ingredients: list of ingredients
- ingredient: id of ingredient
- quantity: quantity of ingredient
- remove: if true, remove ingredient from inventory
- */
- let updateOrders = (order, remove = false)=>{
- let isNew = true;
- for(let i = 0; i < orders.length; i++){
- if(orders[i]._id === order._id){
- if(remove){
- orders.splice(i, 1);
- }else{
- orders[i] = order;
- }
- isNew = false;
- }
- }
- if(isNew){
- orders.push(order);
- }
- ordersStrandObj.isPopulated = false;
- ordersStrandObj.display();
- }
- //Close any open sidebar
- 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";
- }
- /*
- Open a specific sidebar
- Input:
- sidebar: the outermost element of the sidebar (must contain class sidebar)
- */
- 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)
- Input:
- dateRange: array containing two elements, start and end indices
- id: id of the ingredient to calculate
- Return: (int) Quantity of recipes sold
- */
- 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
- Return:
- 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;
- }
- /*
- Groups all of the merchant's ingredients by their category
- Return:
- Array of objects
- name: Category name
- ingredients: Array of objects
- id: Id of ingredient
- name: Name of ingredient
- quantity: Merchant's quantity of this ingredient
- unit: Measurement unit
- */
- let categorizeIngredients = (ingredients)=>{
- let ingredientsByCategory = [];
- for(let i = 0; i < ingredients.length; i++){
- let categoryExists = false;
- for(let j = 0; j < ingredientsByCategory.length; j++){
- if(ingredients[i].ingredient.category === ingredientsByCategory[j].name){
- ingredientsByCategory[j].ingredients.push({
- id: ingredients[i].ingredient._id,
- name: ingredients[i].ingredient.name,
- quantity: ingredients[i].quantity,
- unit: ingredients[i].ingredient.unit
- });
- categoryExists = true;
- break;
- }
- }
- if(!categoryExists){
- ingredientsByCategory.push({
- name: ingredients[i].ingredient.category,
- ingredients: [{
- id: ingredients[i].ingredient._id,
- name: ingredients[i].ingredient.name,
- quantity: ingredients[i].quantity,
- unit: ingredients[i].ingredient.unit
- }]
- });
- }
- }
- return ingredientsByCategory;
- }
- let unitizeIngredients = (ingredients)=>{
- let ingredientsByUnit = [];
- for(let i = 0; i < ingredients.length; i++){
- let unitExists = false;
- for(let j = 0; j < ingredientsByUnit.length; j++){
- if(ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
- ingredientsByUnit[j].ingredients.push({
- id: ingredients[i].ingredient._id,
- name: ingredients[i].ingredient.name,
- quantity: ingredients[i].quantity,
- unit: ingredients[i].ingredient.unit
- });
- unitExists = true;
- break;
- }
- }
- if(!unitExists){
- ingredientsByUnit.push({
- name: ingredients[i].ingredient.unit,
- ingredients: [{
- id: ingredients[i].ingredient._id,
- name: ingredients[i].ingredient.name,
- quantity: ingredients[i].quantity,
- unit: ingredients[i].ingredient.unit
- }]
- })
- }
- }
- return ingredientsByUnit;
- }
- let categorizeIngredientsFromDB = (ingredients)=>{
- let ingredientsByCategory = [];
- for(let i = 0; i < ingredients.length; i++){
- let categoryExists = false;
- for(let j = 0; j < ingredientsByCategory.length; j++){
- if(ingredients[i].category === ingredientsByCategory[j].name){
- ingredientsByCategory[j].ingredients.push({
- id: ingredients[i]._id,
- name: ingredients[i].name,
- unit: ingredients[i].unit
- });
- categoryExists = true;
- break;
- }
- }
- if(!categoryExists){
- ingredientsByCategory.push({
- name: ingredients[i].category,
- ingredients: [{
- id: ingredients[i]._id,
- name: ingredients[i].name,
- unit: ingredients[i].unit
- }]
- });
- }
- }
- return ingredientsByCategory;
- }
- for(let transaction of transactions){
- transaction.date = new Date(transaction.date);
- }
- homeStrandObj.display();
|