ingredientDetails.ejs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <div id="ingredientDetails">
  2. <div class="sidebarIconButtons">
  3. <button onclick="closeSidebar()">
  4. <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  5. <line x1="5" y1="12" x2="19" y2="12"></line>
  6. <polyline points="12 5 19 12 12 19"></polyline>
  7. </svg>
  8. </button>
  9. <button>
  10. <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  11. <path d="M12 20h9"></path>
  12. <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path>
  13. </svg>
  14. </button>
  15. <button onclick="ingredientDetailsComp.remove()">
  16. <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  17. <polyline points="3 6 5 6 21 6"></polyline>
  18. <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
  19. </svg>
  20. </button>
  21. </div>
  22. <p></p>
  23. <h1></h1>
  24. <div class="lineBorder"></div>
  25. <label>Current Stock
  26. <p id="ingredientStock"></p>
  27. </label>
  28. <div class="lineBorder"></div>
  29. <label>Average Daily Use (30 days)
  30. <p id="dailyUse"></p>
  31. </label>
  32. <script>
  33. let ingredientDetailsComp = {
  34. ingredient: {},
  35. display: function(ingredient, category){
  36. this.ingredient = ingredient;
  37. sidebar = document.querySelector("#ingredientDetails");
  38. openSidebar(sidebar);
  39. document.querySelector("#ingredientDetails p").innerText = category.name;
  40. document.querySelector("#ingredientDetails h1").innerText = ingredient.name;
  41. document.querySelector("#ingredientStock").innerText = `${ingredient.quantity} ${ingredient.unit}`;
  42. let quantities = [];
  43. let now = new Date();
  44. for(let i = 1; i < 31; i++){
  45. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  46. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  47. quantities.push(ingredientSold(dateIndices(startDay, endDay), ingredient.id));
  48. }
  49. let sum = 0;
  50. for(let quantity of quantities){
  51. sum += quantity;
  52. }
  53. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.unit}`;
  54. },
  55. remove: function(){
  56. for(let i = 0; i < merchant.recipes.length; i++){
  57. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  58. if(this.ingredient.id === merchant.recipes[i].ingredients[j].ingredient._id){
  59. banner.createError("Must remove ingredient from all recipes before removing");
  60. return;
  61. }
  62. }
  63. }
  64. fetch(`/merchant/ingredients/remove/${this.ingredient.id}`, {
  65. method: "DELETE",
  66. })
  67. .then((response) => response.json())
  68. .then((response)=>{
  69. if(typeof(response) === "string"){
  70. banner.createError(response);
  71. }else{
  72. banner.createNotification("Ingredient removed");
  73. updateInventory([this.ingredient], true);
  74. }
  75. })
  76. .catch((err)=>{});
  77. }
  78. }
  79. </script>
  80. </div>