| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- let ingredientDetails = {
- ingredient: {},
- dailyUse: 0,
- display: function(ingredient){
- document.getElementById("editIngBtn").onclick = ()=>{controller.openSidebar("editIngredient", ingredient)};
- document.getElementById("ingredientDetailsCategory").innerText = ingredient.ingredient.category;
- document.getElementById("ingredientDetailsName").innerText = ingredient.ingredient.name;
- document.getElementById("ingredientStock").innerText = ingredient.getQuantityDisplay();
- //Calculate and display average daily use
- let quantities = [];
- let now = new Date();
- for(let i = 1; i < 31; i++){
- let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
- let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
- quantities.push(merchant.getSingleIngredientSold(ingredient, startDay, endDay));
- }
- let sum = 0;
- for(let i = 0; i < quantities.length; i++){
- sum += quantities[i];
- }
- let dailyUse = sum / quantities.length;
- const dailyUseDiv = document.getElementById("dailyUse");
- if(ingredient.ingredient.specialUnit === "bottle"){
- dailyUseDiv.innerText = `${dailyUse.toFixed(2)} BOTTLES`;
- }else{
- dailyUseDiv.innerText = `${dailyUse.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
- }
- //Show recipes that this ingredient is a part of
- let ul = document.getElementById("ingredientRecipeList");
- let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
- while(ul.children.length > 0){
- ul.removeChild(ul.firstChild);
- }
- for(let i = 0; i < recipes.length; i++){
- let li = document.createElement("li");
- li.innerText = recipes[i].name;
- li.onclick = ()=>{
- controller.openStrand("recipeBook");
- controller.openSidebar("recipeDetails", recipes[i]);
- }
- ul.appendChild(li);
- }
- },
- remove: function(ingredientsStrand){
- for(let i = 0; i < merchant.recipes.length; i++){
- for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
- if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
- banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
- return;
- }
- }
- }
- let loader = document.getElementById("loaderContainer");
- loader.style.display = "flex";
- fetch(`/ingredients/remove/${this.ingredient.ingredient.id}`, {
- method: "DELETE",
- })
- .then((response) => response.json())
- .then((response)=>{
- if(typeof(response) === "string"){
- banner.createError(response);
- }else{
- merchant.removeIngredient(this.ingredient);
- ingredientsStrand.display();
- banner.createNotification("INGREDIENT REMOVED");
- }
- })
- .catch((err)=>{})
- .finally(()=>{
- loader.style.display = "none";
- });
- }
- }
- module.exports = ingredientDetails;
|