ingredientDetails.js 12 KB

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