ingredientDetails.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. let ingredientDetails = {
  2. ingredient: {},
  3. dailyUse: 0,
  4. display: function(ingredient, ingredientsStrand){
  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(ingredientsStrand)};
  98. },
  99. remove: function(ingredientsStrand){
  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. ingredientsStrand.display();
  120. banner.createNotification("INGREDIENT REMOVED");
  121. }
  122. })
  123. .catch((err)=>{})
  124. .finally(()=>{
  125. loader.style.display = "none";
  126. });
  127. },
  128. edit: function(){
  129. let add = document.querySelectorAll(".editAdd");
  130. let remove = document.querySelectorAll(".editRemove");
  131. for(let i = 0; i < add.length; i++){
  132. add[i].style.display = "flex";
  133. }
  134. for(let i = 0; i < remove.length; i++){
  135. remove[i].style.display = "none";
  136. }
  137. if(this.ingredient.ingredient.specialUnit === "bottle"){
  138. const mainDiv = document.getElementById("ingredientDetails");
  139. const displayUnits = document.getElementById("displayUnitLabel");
  140. let label = document.createElement("label");
  141. label.innerText = `BOTTLE SIZE (${this.ingredient.ingredient.unit.toUpperCase()})`;
  142. label.id = "ingredientUnitSizeLabel";
  143. mainDiv.insertBefore(label, displayUnits);
  144. const convQuant = this.ingredient.ingredient.convert(this.ingredient.ingredient.unitSize);
  145. let input = document.createElement("input");
  146. input.type = "number";
  147. input.value = convQuant.toFixed(2);
  148. input.id = "ingredientUnitSizeIn";
  149. input.min = "0";
  150. input.step = "0.01";
  151. label.appendChild(input);
  152. let border = document.createElement("div");
  153. border.classList.add("lineBorder");
  154. border.id="bottleBorder";
  155. mainDiv.insertBefore(border, displayUnits);
  156. }
  157. },
  158. editSubmit: function(){
  159. //Update the ingredient unit depending on the type of unit
  160. let ingredientButtons = document.querySelectorAll(".unitButton");
  161. for(let i = 0; i < ingredientButtons.length; i++){
  162. if(ingredientButtons[i].classList.contains("unitActive")){
  163. const unit = ingredientButtons[i].innerText.toLowerCase();
  164. this.ingredient.ingredient.unit = unit;
  165. break;
  166. }
  167. }
  168. //Update the ingredient quantity depending on the type of unit
  169. const quantityElem = document.getElementById("ingredientInput");
  170. if(quantityElem.value !== ""){
  171. if(this.ingredient.ingredient.specialUnit === "bottle"){
  172. let quantInMain = controller.convertToMain(
  173. this.ingredient.ingredient.unit,
  174. quantityElem.value * this.ingredient.ingredient.unitSize
  175. );
  176. this.ingredient.quantity = quantInMain / this.ingredient.ingredient.unitSize;
  177. }else{
  178. this.ingredient.quantity = controller.convertToMain(
  179. this.ingredient.ingredient.unit,
  180. Number(quantityElem.value)
  181. );
  182. }
  183. }
  184. const category = document.getElementById("detailsCategoryInput");
  185. this.ingredient.ingredient.category = (category.value === "") ? this.ingredient.ingredient.category : category.value;
  186. const name = document.getElementById("ingredientDetailsNameIn");
  187. this.ingredient.ingredient.name = (name.value === "") ? this.ingredient.ingredient.name : name.value;
  188. let data = {
  189. id: this.ingredient.ingredient.id,
  190. name: this.ingredient.ingredient.name,
  191. quantity: this.ingredient.quantity,
  192. category: this.ingredient.ingredient.category,
  193. defaultUnit: this.ingredient.ingredient.unit
  194. };
  195. if(this.ingredient.ingredient.specialUnit === "bottle"){
  196. const unitSize = document.getElementById("ingredientUnitSizeIn").value;
  197. this.ingredient.ingredient.unitSize = controller.convertToMain(this.ingredient.ingredient.unit, unitSize);
  198. data.unitSize = this.ingredient.ingredient.unitSize;
  199. }
  200. let loader = document.getElementById("loaderContainer");
  201. loader.style.display = "flex";
  202. fetch("/ingredients/update", {
  203. method: "PUT",
  204. headers: {
  205. "Content-Type": "application/json;charset=utf-8"
  206. },
  207. body: JSON.stringify(data)
  208. })
  209. .then((response) => response.json())
  210. .then((response)=>{
  211. if(typeof(response) === "string"){
  212. banner.createError(response);
  213. }else{
  214. merchant.updateIngredient(this.ingredient, this.ingredient.quantity);
  215. //ingredient updates
  216. this.display(this.ingredient);
  217. banner.createNotification("INGREDIENT UPDATED");
  218. }
  219. })
  220. .catch((err)=>{
  221. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  222. })
  223. .finally(()=>{
  224. loader.style.display = "none";
  225. });
  226. },
  227. changeUnit: function(newActive, unit){
  228. let ingredientButtons = document.querySelectorAll(".unitButton");
  229. for(let i = 0; i < ingredientButtons.length; i++){
  230. ingredientButtons[i].classList.remove("unitActive");
  231. }
  232. newActive.classList.add("unitActive");
  233. },
  234. changeUnitDefault: function(){
  235. let loader = document.getElementById("loaderContainer");
  236. loader.style.display = "flex";
  237. let id = this.ingredient.ingredient.id;
  238. let unit = this.ingredient.ingredient.unit;
  239. fetch(`/merchant/ingredients/update/${id}/${unit}`, {
  240. method: "put",
  241. headers: {
  242. "Content-Type": "application/json;charset=utf-8"
  243. },
  244. })
  245. .then((response)=>{
  246. if(typeof(response) === "string"){
  247. banner.createError(response);
  248. }else{
  249. banner.createNotification("INGREDIENT DEFAULT UNIT UPDATED");
  250. }
  251. })
  252. .catch((err)=>{
  253. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  254. })
  255. .finally(()=>{
  256. loader.style.display = "none";
  257. });
  258. }
  259. }
  260. module.exports = ingredientDetails;