ingredientDetails.js 10 KB

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