ingredientDetails.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. module.exports = {
  2. ingredient: {},
  3. dailyUse: 0,
  4. display: function(ingredient){
  5. this.ingredient = ingredient;
  6. document.getElementById("editIngBtn").onclick = ()=>{this.edit()};
  7. document.getElementById("removeIngBtn").onclick = ()=>{this.remove(merchant)};
  8. document.querySelector("#ingredientDetails p").innerText = ingredient.ingredient.category;
  9. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  10. let ingredientStock = document.getElementById("ingredientStock");
  11. ingredientStock.innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  12. ingredientStock.style.display = "block";
  13. let ingredientInput = document.getElementById("ingredientInput");
  14. ingredientInput.value = ingredient.ingredient.convert(ingredient.quantity).toFixed(2);
  15. ingredientInput.style.display = "none";
  16. let quantities = [];
  17. let now = new Date();
  18. for(let i = 1; i < 31; i++){
  19. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  20. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  21. let indices = merchant.transactionIndices(startDay, endDay);
  22. if(indices === false){
  23. quantities.push(0);
  24. }else{
  25. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  26. }
  27. }
  28. let sum = 0;
  29. for(let i = 0; i < quantities.length; i++){
  30. sum += quantities[i];
  31. }
  32. this.dailyUse = sum / quantities.length;
  33. document.getElementById("dailyUse").innerText = `${ingredient.ingredient.convert(this.dailyUse).toFixed(2)} ${ingredient.ingredient.unit}`;
  34. let ul = document.getElementById("ingredientRecipeList");
  35. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  36. while(ul.children.length > 0){
  37. ul.removeChild(ul.firstChild);
  38. }
  39. for(let i = 0; i < recipes.length; i++){
  40. let li = document.createElement("li");
  41. li.innerText = recipes[i].name;
  42. li.onclick = ()=>{
  43. controller.openStrand("recipeBook");
  44. controller.openSidebar("recipeDetails", recipes[i]);
  45. }
  46. ul.appendChild(li);
  47. }
  48. let ingredientButtons = document.getElementById("ingredientButtons");
  49. let units = [];
  50. let unitLabel = document.getElementById("displayUnitLabel");
  51. let defaultButton = document.getElementById("defaultUnit");
  52. if(this.ingredient.ingredient.unitType !== "other"){
  53. units = merchant.units[this.ingredient.ingredient.unitType];
  54. unitLabel.style.display = "block";
  55. defaultButton.style.display = "block";
  56. }else{
  57. unitLabel.style.display = "none";
  58. defaultButton.style.display = "none";
  59. }
  60. while(ingredientButtons.children.length > 0){
  61. ingredientButtons.removeChild(ingredientButtons.firstChild);
  62. }
  63. for(let i = 0; i < units.length; i++){
  64. let button = document.createElement("button");
  65. button.classList.add("unitButton");
  66. button.innerText = units[i].toUpperCase();
  67. button.onclick = ()=>{this.changeUnit(button, units[i])};
  68. ingredientButtons.appendChild(button);
  69. if(units[i] === this.ingredient.ingredient.unit){
  70. button.classList.add("unitActive");
  71. }
  72. }
  73. document.getElementById("defaultUnit").onclick = ()=>{this.changeUnitDefault()};
  74. document.getElementById("editSubmitButton").onclick = ()=>{this.editSubmit()};
  75. },
  76. remove: function(merchant){
  77. for(let i = 0; i < merchant.recipes.length; i++){
  78. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  79. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  80. banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
  81. return;
  82. }
  83. }
  84. }
  85. let loader = document.getElementById("loaderContainer");
  86. loader.style.display = "flex";
  87. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  88. method: "DELETE",
  89. })
  90. .then((response) => response.json())
  91. .then((response)=>{
  92. if(typeof(response) === "string"){
  93. banner.createError(response);
  94. }else{
  95. banner.createNotification("INGREDIENT REMOVED");
  96. merchant.editIngredients([this.ingredient], true);
  97. }
  98. })
  99. .catch((err)=>{})
  100. .finally(()=>{
  101. loader.style.display = "none";
  102. });
  103. },
  104. edit: function(){
  105. document.getElementById("ingredientStock").style.display = "none";
  106. document.getElementById("ingredientInput").style.display = "block";
  107. document.getElementById("editSubmitButton").style.display = "block";
  108. },
  109. editSubmit: function(){
  110. this.ingredient.quantity = controller.convertToMain(
  111. this.ingredient.ingredient.unit,
  112. Number(document.getElementById("ingredientInput").value)
  113. );
  114. let data = [{
  115. id: this.ingredient.ingredient.id,
  116. quantity: controller.convertToMain(this.ingredient.ingredient.unit, this.ingredient.quantity)
  117. }];
  118. let loader = document.getElementById("loaderContainer");
  119. loader.style.display = "flex";
  120. fetch("/merchant/ingredients/update", {
  121. method: "PUT",
  122. headers: {
  123. "Content-Type": "application/json;charset=utf-8"
  124. },
  125. body: JSON.stringify(data)
  126. })
  127. .then((response) => response.json())
  128. .then((response)=>{
  129. if(typeof(response) === "string"){
  130. banner.createError(response);
  131. }else{
  132. merchant.editIngredients([this.ingredient]);
  133. banner.createNotification("INGREDIENT UPDATED");
  134. }
  135. })
  136. .catch((err)=>{
  137. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  138. })
  139. .finally(()=>{
  140. loader.style.display = "none";
  141. });
  142. },
  143. changeUnit: function(newActive, unit){
  144. this.ingredient.ingredient.unit = unit;
  145. let ingredientButtons = document.querySelectorAll(".unitButton");
  146. for(let i = 0; i < ingredientButtons.length; i++){
  147. ingredientButtons[i].classList.remove("unitActive");
  148. }
  149. newActive.classList.add("unitActive");
  150. controller.updateData("unit");
  151. document.getElementById("ingredientStock").innerText = `${this.ingredient.ingredient.convert(this.ingredient.quantity).toFixed(2)} ${this.ingredient.ingredient.unit.toUpperCase()}`;
  152. document.getElementById("dailyUse").innerText = `${this.ingredient.ingredient.convert(this.dailyUse).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  153. },
  154. changeUnitDefault: function(){
  155. let loader = document.getElementById("loaderContainer");
  156. loader.style.display = "flex";
  157. let id = this.ingredient.ingredient.id;
  158. let unit = this.ingredient.ingredient.unit;
  159. fetch(`/merchant/ingredients/update/${id}/${unit}`, {
  160. method: "put",
  161. headers: {
  162. "Content-Type": "application/json;charset=utf-8"
  163. },
  164. })
  165. .then((response)=>{
  166. if(typeof(response) === "string"){
  167. banner.createError(response);
  168. }else{
  169. banner.createNotification("INGREDIENT DEFAULT UNIT UPDATED");
  170. }
  171. })
  172. .catch((err)=>{
  173. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  174. })
  175. .finally(()=>{
  176. loader.style.display = "none";
  177. });
  178. }
  179. }