ingredientDetails.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. let ingredientDetails = {
  2. ingredient: {},
  3. dailyUse: 0,
  4. display: function(ingredient){
  5. this.ingredient = ingredient;
  6. document.getElementById("ingredientDetailsCategory").innerText = ingredient.ingredient.category;
  7. let categoryInput = document.getElementById("detailsCategoryInput");
  8. categoryInput.value = "";
  9. categoryInput.placeholder = ingredient.ingredient.category;
  10. document.getElementById("ingredientDetailsName").innerText = ingredient.ingredient.name;
  11. let nameInput = document.getElementById("ingredientDetailsNameIn");
  12. nameInput.value = "";
  13. nameInput.placeholder = ingredient.ingredient.name;
  14. //Display the stock quantity (and the edit input) based on the unit type
  15. let stockInput = document.getElementById("ingredientInput");
  16. let stockDisplay = document.getElementById("ingredientStock");
  17. stockInput.value = "";
  18. if(ingredient.ingredient.unitSize){
  19. let quantity = ingredient.ingredient.convert(ingredient.quantity);
  20. stockDisplay.innerText = `${quantity.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  21. stockInput.placeholder = quantity.toFixed(2);
  22. }else{
  23. stockDisplay.innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  24. stockInput.placeholder = ingredient.ingredient.convert(ingredient.quantity).toFixed(2);
  25. }
  26. let quantities = [];
  27. let now = new Date();
  28. for(let i = 1; i < 31; i++){
  29. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  30. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  31. let indices = controller.transactionIndices(merchant.transactions, startDay, endDay);
  32. if(indices === false){
  33. quantities.push(0);
  34. }else{
  35. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  36. }
  37. }
  38. let sum = 0;
  39. for(let i = 0; i < quantities.length; i++){
  40. sum += quantities[i];
  41. }
  42. let dailyUse = sum / quantities.length;
  43. document.getElementById("dailyUse").innerText = `${ingredient.ingredient.convert(dailyUse).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  44. let ul = document.getElementById("ingredientRecipeList");
  45. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  46. while(ul.children.length > 0){
  47. ul.removeChild(ul.firstChild);
  48. }
  49. for(let i = 0; i < recipes.length; i++){
  50. let li = document.createElement("li");
  51. li.innerText = recipes[i].name;
  52. li.onclick = ()=>{
  53. controller.openStrand("recipeBook");
  54. controller.openSidebar("recipeDetails", recipes[i]);
  55. }
  56. ul.appendChild(li);
  57. }
  58. let ingredientButtons = document.getElementById("ingredientButtons");
  59. let units = [];
  60. if(this.ingredient.ingredient.unitType !== "other"){
  61. units = merchant.units[this.ingredient.ingredient.unitType];
  62. }
  63. while(ingredientButtons.children.length > 0){
  64. ingredientButtons.removeChild(ingredientButtons.firstChild);
  65. }
  66. for(let i = 0; i < units.length; i++){
  67. let button = document.createElement("button");
  68. button.classList.add("unitButton");
  69. button.innerText = units[i].toUpperCase();
  70. button.onclick = ()=>{this.changeUnit(button, units[i])};
  71. ingredientButtons.appendChild(button);
  72. if(units[i] === this.ingredient.ingredient.unit){
  73. button.classList.add("unitActive");
  74. }
  75. }
  76. let add = document.querySelectorAll(".editAdd");
  77. let remove = document.querySelectorAll(".editRemove");
  78. for(let i = 0; i < add.length; i++){
  79. add[i].style.display = "none";
  80. }
  81. for(let i = 0; i < remove.length; i++){
  82. remove[i].style.display = "block";
  83. }
  84. document.getElementById("editSubmitButton").onclick = ()=>{this.editSubmit()};
  85. document.getElementById("editCancelButton").onclick = ()=>{this.display(this.ingredient)};
  86. document.getElementById("editIngBtn").onclick = ()=>{this.edit()};
  87. document.getElementById("removeIngBtn").onclick = ()=>{this.remove()};
  88. },
  89. remove: function(){
  90. for(let i = 0; i < merchant.recipes.length; i++){
  91. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  92. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  93. banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
  94. return;
  95. }
  96. }
  97. }
  98. let loader = document.getElementById("loaderContainer");
  99. loader.style.display = "flex";
  100. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  101. method: "DELETE",
  102. })
  103. .then((response) => response.json())
  104. .then((response)=>{
  105. if(typeof(response) === "string"){
  106. banner.createError(response);
  107. }else{
  108. banner.createNotification("INGREDIENT REMOVED");
  109. merchant.editIngredients([this.ingredient], true);
  110. }
  111. })
  112. .catch((err)=>{})
  113. .finally(()=>{
  114. loader.style.display = "none";
  115. });
  116. },
  117. edit: function(){
  118. let add = document.querySelectorAll(".editAdd");
  119. let remove = document.querySelectorAll(".editRemove");
  120. for(let i = 0; i < add.length; i++){
  121. add[i].style.display = "flex";
  122. }
  123. for(let i = 0; i < remove.length; i++){
  124. remove[i].style.display = "none";
  125. }
  126. },
  127. editSubmit: function(){
  128. //Update the ingredient unit depending on the type of unit
  129. let ingredientButtons = document.querySelectorAll(".unitButton");
  130. for(let i = 0; i < ingredientButtons.length; i++){
  131. if(ingredientButtons[i].classList.contains("unitActive")){
  132. const unit = ingredientButtons[i].innerText.toLowerCase();
  133. this.ingredient.ingredient.unit = unit;
  134. break;
  135. }
  136. }
  137. //Update the ingredient quantity depending on the type of unit
  138. const quantityElem = document.getElementById("ingredientInput");
  139. if(quantityElem.value !== ""){
  140. if(this.ingredient.ingredient.specialUnit === "bottle"){
  141. let quantInMain = controller.convertToMain(
  142. this.ingredient.ingredient.unit,
  143. quantityElem.value * this.ingredient.ingredient.unitSize
  144. );
  145. this.ingredient.quantity = quantInMain / this.ingredient.ingredient.unitSize;
  146. }else{
  147. this.ingredient.quantity = controller.convertToMain(
  148. this.ingredient.ingredient.unit,
  149. Number(quantityElem.value)
  150. );
  151. }
  152. }
  153. const category = document.getElementById("detailsCategoryInput");
  154. this.ingredient.ingredient.category = (category.value === "") ? this.ingredient.ingredient.category : category.value;
  155. const name = document.getElementById("ingredientDetailsNameIn");
  156. this.ingredient.ingredient.name = (name.value === "") ? this.ingredient.ingredient.name : name.value;
  157. let data = {
  158. id: this.ingredient.ingredient.id,
  159. name: this.ingredient.ingredient.name,
  160. quantity: this.ingredient.quantity,
  161. category: this.ingredient.ingredient.category,
  162. defaultUnit: this.ingredient.ingredient.unit
  163. };
  164. let loader = document.getElementById("loaderContainer");
  165. loader.style.display = "flex";
  166. fetch("/ingredients/update", {
  167. method: "PUT",
  168. headers: {
  169. "Content-Type": "application/json;charset=utf-8"
  170. },
  171. body: JSON.stringify(data)
  172. })
  173. .then((response) => response.json())
  174. .then((response)=>{
  175. if(typeof(response) === "string"){
  176. banner.createError(response);
  177. }else{
  178. merchant.editIngredients([this.ingredient]);
  179. this.display(this.ingredient);
  180. banner.createNotification("INGREDIENT UPDATED");
  181. }
  182. })
  183. .catch((err)=>{
  184. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  185. })
  186. .finally(()=>{
  187. loader.style.display = "none";
  188. });
  189. },
  190. changeUnit: function(newActive, unit){
  191. let ingredientButtons = document.querySelectorAll(".unitButton");
  192. for(let i = 0; i < ingredientButtons.length; i++){
  193. ingredientButtons[i].classList.remove("unitActive");
  194. }
  195. newActive.classList.add("unitActive");
  196. },
  197. changeUnitDefault: function(){
  198. let loader = document.getElementById("loaderContainer");
  199. loader.style.display = "flex";
  200. let id = this.ingredient.ingredient.id;
  201. let unit = this.ingredient.ingredient.unit;
  202. fetch(`/merchant/ingredients/update/${id}/${unit}`, {
  203. method: "put",
  204. headers: {
  205. "Content-Type": "application/json;charset=utf-8"
  206. },
  207. })
  208. .then((response)=>{
  209. if(typeof(response) === "string"){
  210. banner.createError(response);
  211. }else{
  212. banner.createNotification("INGREDIENT DEFAULT UNIT UPDATED");
  213. }
  214. })
  215. .catch((err)=>{
  216. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  217. })
  218. .finally(()=>{
  219. loader.style.display = "none";
  220. });
  221. }
  222. }
  223. module.exports = ingredientDetails;