ingredientDetails.js 12 KB

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